https://www.cnblogs.com/chy18883701161/p/10854547.html
https://blog.youkuaiyun.com/saywhat_sayhello/article/details/80361486
大全:https://blog.youkuaiyun.com/qq_40733968/article/details/106860610
一 相关的类
处理日期、时间的类:
Date、Calendar、LocalDate三个类可用于处理日期时间。其中LocalDate最新,应使用这个。相关的类还有:LocalTime、LocalDateTime。
处理日期、时间格式的类:
DateFormat、SimpleDateFormat、DateTimeFormatter。其中DateTimeFormatter最新,功能最完善,应使用这个。
补充:根据上面大全教程,还有以下相关类
时间戳与时区:Instant、ZoneId
日期差与时间差:Period、Duration
二 功能与实现代码
2.1 获取当前日期时间、创建指定日期时间
// 获取当前日期、时间、日期时间对象
LocalDate ld=LocalDate.now();
LocalTime lt = LocalTime.now();
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ld);
System.out.println(lt);
System.out.println(ldt);
// 创建指定日期时间对象
LocalDate ld2 = LocalDate.of(2000,10,1);
LocalTime lt2 = LocalTime.of(12,20,10);
LocalDateTime ldt2 = LocalDateTime.of(2000,10,1,12,20,10);
LocalDateTime ldt3 = LocalDateTime.of(ld2,lt2);
LocalDate ld3 = ldt4.toLocalDate();
LocalTime lt4 = ldt3.toLocalTime();
注意:
LocalDateTime直接输出时,日期和时间中间有个字母"T"分割;从源码可知,这是在toString中特意加上的。
2.2 创建时间日期格式、格式化(String)
格式化日期、时间、日期时间对象为字符串:可使用内置格式和自定义格式;注意格式化之后返回的是String
// 创建格式对象
// 内置中等长度的日期、时间、日期时间格式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT,FormatStyle.SHORT);
// 自定义日期、时间、日期时间格式
DateTimeFormatter dateTimeFormatter4 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter dateTimeFormatter5 = DateTimeFormatter.ofPattern("HH:mm:ss");
DateTimeFormatter dateTimeFormatter6 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 格式化为字符串
// 内置格式
String str1 = dateTimeFormatter.format(ld);
String str2 = dateTimeFormatter2.format(lt);
String str3 = dateTimeFormatter3.format(ldt);
// 自定义格式
String str4 = dateTimeFormatter4.format(ld);
String str5 = dateTimeFormatter5.format(lt);
String str6 = dateTimeFormatter6.format(ldt);
2.3 时间日期转为字符串、字符串转时间日期
时间日期转字符串就是上面说的格式化。
// 时间转字符串
String str = dateTimeFormatter5.format(lt);
System.out.println(str);
// 字符串转时间
LocalTime lt2 = LocalTime.parse(str,DateTimeFormatter.ofPattern("HH:mm:ss"));
System.out.println(lt2);
2.4 日期时间转时间戳、时间戳转日期时间
Instant类,其实不是一个时间戳类,而是一个有转为时间戳方法的日期时间类,LocalDateTime可以转为Instant类,然后在再转为时间戳。
时间戳与时区相关,Instant获取的是格林尼治时间(0时区),要通过时区类为Instant时间设置偏移。
2.4.1 Instant转为指定时区时间
Instant时间转为目标时区时间,有两种方法:
- 设置时区偏移:时区偏移类ZoneOffset
- 直接设置时区Id:时区Id类ZoneId
// 0时区时间
Instant ins = Instant.now();
// 转为指定时区偏移后的时间:ZoneOffset.ofHours返回ZoneOffset对象
OffsetDateTime ins1 = ins.atOffset(ZoneOffset.ofHours(8));
// 转为指定时区的时间:ZoneId.systemDefault返回ZoneId对象,表示系统默认时区Id
ZonedDateTime ins2 = ins.atZone(ZoneId.systemDefault());
// 直接设置时区Id也行:ZoneId.of返回ZoneId对象
ZonedDateTime ins2 = ins.atZone(ZoneId.of("Asia/Shanghai"));
三个结果如下:可见差8小时,第二个带时间偏移,最后两个还带时区Id
2020-11-02T12:42:38.955Z
2020-11-02T20:42:38.955+08:00
2020-11-02T20:42:38.955+08:00[Asia/Shanghai]
2020-11-02T20:42:38.955+08:00[Asia/Shanghai]
2.4.2 Instant转为时间戳
// 获取时间戳:秒,1604322074
Long timeStamp = ins.getEpochSecond();
// 获取时间戳:毫秒,1604322074636
Long timeStamp1 = ins.toEpochMilli();
2.4.3 LocalDateTime转为Instant
// toInstant必须要参数ZoneOffset
Instant ins4 = ldt.toInstant(ZoneOffset.of("+8"));
2.5 日期时间比较、求和、求差
https://blog.youkuaiyun.com/zuihongyan518/article/details/83867265
时间比大小、时间差比大小
在这里插入代码片
时间差、时间差判断
Duration表示时间差,单位可为年、月、日、时、分、秒、毫秒,且返回类型为Long,可直接进行数值比较。
在这里插入代码片