新的日期和时间类

  • LocalDate
  • LocalTime
  • LocalDateTime

都是不可变的对象 三者使用方式一样

1
2
获取实例 用静态方法
LocalDateTime.now()
  • Instant 时间戳 默认获取UTC 时区
1
Instant.now.atOffset(ZoneOffset.ofHours(8))
  • Duration 计算两个时间之间的间隔
  • Period 计算两个日期之间的间隔
1
2
3
4
5
Duration.between(ins1,ins2)
Duration.between(lt1,lt2)

Period.between(ins1,ins2)
Period.between(lt1,lt2)
  • TeporalAdjuster 时间校正器 接口
1
2
3
4
5
ldt.withDayOfMonth(10)  指定月份
//自定义
ldt.with(()->{

})
  • DateTimeFormatter 格式化
1
2
3
4
5
6
DateTimeFormatter dtf = DateTimeFormatter ISO_DATE_TIME;
LocalDateTime ldt = LocalDateTime.now();
ldt.format(dtf)

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd ");
ldt.parse()
  • ZonedDate ZonedTime ZonedDateTime
1
2
ZoneId.getAvailable.ZoneIds();
LocalDateTime.now(ZoneId.of("Europe/Tallinn"))