Loading [MathJax]/jax/input/TeX/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LocalDateTime日期格式化和指定日期的时分秒

LocalDateTime日期格式化和指定日期的时分秒

作者头像
oktokeep
发布于 2024-10-09 02:55:35
发布于 2024-10-09 02:55:35
35100
代码可运行
举报
文章被收录于专栏:第三方工具第三方工具
运行总次数:0
代码可运行

LocalDateTime日期格式化和指定日期的时分秒

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.example.core.mydemo.date;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * now=2023-06-30
 * after=2023-07-04
 * afterTime=2023-07-04T00:00:01
 * dateFormat=06/30-07/01
 */
public class LocalDateTest {
    public static final String YYYYMMDDHHMMSSS_PATTERN = "yyyyMMddHHmmss";
    public static final String DEFAULT_PATTERN_NEW_SHORT = "MM/dd";

    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        LocalDate after = now.plusDays(4);
        LocalDateTime afterTime = after.atTime(0,0,1);

        System.out.println("now=" + now);
        System.out.println("after=" + after);
        System.out.println("afterTime=" + afterTime);

        String startTime = "20230630163000";
        String endTime = "20230701163000";
        LocalDateTime startLdt = parseStringToDateTime(startTime,YYYYMMDDHHMMSSS_PATTERN);
        LocalDateTime endLdt = parseStringToDateTime(endTime,YYYYMMDDHHMMSSS_PATTERN);
        String rentStr = formatDateTime(startLdt,DEFAULT_PATTERN_NEW_SHORT);
        String revertStr = formatDateTime(endLdt,DEFAULT_PATTERN_NEW_SHORT);
        String rentViewFormat = rentStr + "-" + revertStr;
        System.out.println("dateFormat=" + rentViewFormat);

    }


    public static LocalDateTime parseStringToDateTime(String time, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(time, df);
    }

    public static String formatDateTime(LocalDateTime dateTime, String pattern) {
        if (dateTime == null) {
            return null;
        }
        if (pattern == null || pattern.isEmpty()) {
            pattern = YYYYMMDDHHMMSSS_PATTERN;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return dateTime.format(formatter);
    }


}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-06-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
LocalDateTime 全面详解与实用案例
LocalDateTime 无时区信息,转换时需要配合 ZonedDateTime 或 OffsetDateTime。
用户3672714
2025/08/13
970
JAVA日期安全格式化之SimpleDateFormat和jodaTime,DateTimeFormatter
SimpleDateFormat线程不安全的日期格式化库 SimpleDateFormat是JAVA提供的一个日期转换类。 package com.rumenz.task; import java.text.SimpleDateFormat; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; impor
开源日记
2021/01/28
6900
一文带你入坑JDK8的新日期时间类 LocalDate、LocalTime、LocalDateTime
参考 https://blog.csdn.net/duan196_118/article/details/111597682 https://blog.csdn.net/qq_24754061/article/details/95500209 https://xijia.blog.csdn.net/article/details/106007147
时间静止不是简史
2023/02/23
5.3K0
一文带你入坑JDK8的新日期时间类 LocalDate、LocalTime、LocalDateTime
Java日期时间处理:DateTimeFormatter、LocalDate与LocalDateTime、Duration实践
在现代 Java 开发中,精确高效处理日期和时间是构建高质量应用关键。本文探讨 Java 8 新型日期时间 API,包括 DateTimeFormatter 格式化与解析、LocalDate 和 LocalDateTime 操作及 Duration 时间间隔计算。通过代码示例与场景分析,助读者掌握并应用于实际项目。
Yeats_Liao
2025/01/02
2.8K0
Java日期时间处理:DateTimeFormatter、LocalDate与LocalDateTime、Duration实践
Java1.8新时间api - LocalDateTime
新时间对象: 1.LocalDate表示年月日 2.LocalTime表示时分秒 3.LocalDateTime表示年月日时分秒 4.Instant:表示时刻,不直接对应年月日信息,需要通过时区转换 5.ZonedDateTime: 表示特定时区的日期和时间 6.ZoneId/ZoneOffset:表示时区 1、获取对象的方法 //通过静态方法now()获取当前时间 LocalDate now1 = LocalDate.now(); LocalTime now2 = LocalTim
Java深度编程
2020/12/22
3.4K0
【Java 基础篇】Java 日期类详解
日期和时间在软件开发中是非常常见且重要的概念。Java 提供了一套强大的日期和时间 API,用于处理日期、时间、时区等相关操作。本文将详细介绍 Java 日期类的概念、用法和常见操作,并提供一些示例代码。
繁依Fanyi
2023/10/12
4650
还在用SimpleDateFormat?Java8都发布N年了,转LocalDateTime吧
Java8发布,已有数年之久,但是发现很多人都还是坚持着用SimpleDateFormat和Date进行时间操作。SimpleDateFormat这个类不是线程安全的,在使用的时候稍不注意,就会产生致命的问题。Date这个类,是可以重新设置时间的,这对于一些类内部的属性来说,是非常不安全的。
Happyjava
2024/02/02
1870
还在用SimpleDateFormat?Java8都发布N年了,转LocalDateTime吧
LocalDateTime反序列化,LocalDateTime格式化
使用mybatis-plus的时候出现了LocalDateTime类(jdk8 中新出现的类 那么我在反序列化的时候出了问题。 我在springboot 2.1.3 中使用以下类结局问题) 用到了下面这个类解决了所有的问题。包括前端传值过来也能直接接受字符串 百度上面也猜了许许多多的坑 最终换了一个spring boot的版本 然后用以下的代码配置完美解决了问题 import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml
云扬四海
2019/06/05
2.2K0
Java 8: LocalDate、LocalTime 、LocalDateTime 处理日期时间
原文链接:https://blog.csdn.net/weixin_39800144/article/details/78460643、https://blog.csdn.net/ty0903csdn/article/details/84927507
微风-- 轻许--
2019/09/18
3.7K0
还在用SimpleDateFormat?Java8都发布N年了,转LocalDateTime吧
Java8发布,已有数年之久,但是发现很多人都还是坚持着用SimpleDateFormat和Date进行时间操作。SimpleDateFormat这个类不是线程安全的,在使用的时候稍不注意,就会产生致命的问题。Date这个类,是可以重新设置时间的,这对于一些类内部的属性来说,是非常不安全的。
Happyjava
2019/07/16
1K0
还在用SimpleDateFormat?Java8都发布N年了,转LocalDateTime吧
LocalDateTime、Date时间工具类
参考:Date、LocalTime、LocalDate、LocalDate-时间操作工具类_Hatsune_Miku_的博客-CSDN博客
CBeann
2023/12/25
4110
JDK 8 中的LocalDate不符合正常逻辑的解析报错: Unable to obtain LocalDateTime from TemporalAccessor: {Year=2019, ...
I am simply trying to convert a date string into a DateTime object in Java 8. Upon running the following lines:
一个会写诗的程序员
2019/06/25
8.8K0
DateTimeFormatter日期格式化
使用旧的Date对象时,我们用SimpleDateFormat进行格式化显示。使用新的LocalDateTime或ZonedLocalDateTime时,我们要进行格式化显示,就要使用DateTimeFormatter。
六月的雨在Tencent
2024/03/28
3310
String、Date和Timestamp的互转
关于String和Date的互转,在java8后会有不同。因为java8添加java.time包及子包,其中主要API是关于日期、时间、时刻和时间段及它们之间的转换和打印输出,比较重要一点的是java.time中LocalDate、LocalTime、LocalDateTime都是线程安全的。有兴趣可以查看官网的描述:Package java.time和Package java.time.format。
mingmingcome
2021/11/29
9730
Java中日期处理的一些坑
记录下最近在用java处理日期格式的时候遇到的一些坑,虽然是挺简单的一些点,但是如果不了解清楚在使用的时候还是会走很多弯路的。
mythsman
2022/11/14
5430
时间格式化转换及时间比较compareTo,Controller层接收参数格式化,从数据源头解决时间格式错误数据对接口的影响
时间格式化转换及时间比较compareTo,Controller层接收参数格式化,从数据源头解决时间格式错误数据对接口的影响
oktokeep
2024/10/09
2650
日期格式化与解析:如何使用DateTimeFormatter处理不同格式的日期与时间?
本文将详细讲解DateTimeFormatter的功能,并通过丰富的示例演示如何高效地格式化和解析日期与时间,包括自定义格式的应用。
猫头虎
2024/12/24
2.3K0
JSR310新日期API(三)-日期时间格式化与解析
前一篇文章已经比较详细地介绍了JSR-310中新增的常用的日期时间类,在实际应用中,我们也十分关注这些日期时间类的格式化操作,更加通俗来说就是字符串和日期时间类的相互转换问题。下面先回顾一下Java旧有的日期时间类和字符串之间的转换方案,然后重点分析JSR-310中新增的常用的日期时间类和字符串之间的转换方案。
Throwable
2020/06/23
1.7K0
FastJson - 序列化LocalDateTime初探
Json的序列化方式有很多种,常见的有FastJson、Gson、Jackson,下面将对FastJson的LocalDateTime序列化使用及源码进行分析讲解。
夹胡碰
2021/06/11
6.5K0
FastJson - 序列化LocalDateTime初探
06-Java8新特性 新时间日期API
Instant : 时间戳(以Unix 元年: 1970年1月1日 00:00:00 到某个时间之间的毫秒值)
彼岸舞
2021/12/14
7200
推荐阅读
相关推荐
LocalDateTime 全面详解与实用案例
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验