新日期API

Instant & Duration

  • Instant 用来处理绝对时间(即时间戳)相关的操作
  • Duration 线程安全的主要用来计算日期的差值
@Test
public void test1() throws InterruptedException {
    Instant now = Instant.now();
    // 时间戳
    System.out.println("当前时间戳(秒级):" + now.getEpochSecond());
    System.out.println("当前时间戳(毫秒级):" + now.toEpochMilli());
    // 计算操作时间
    Instant start = Instant.now();
    System.out.println("开始时间戳(秒级):" + start.getEpochSecond());

    Thread.sleep(3000);

    Instant end = Instant.now();
    System.out.println("结束时间戳(秒级):" + end.getEpochSecond());
    Duration between = Duration.between(start, end);
    System.out.println("操作时间:" + between.getSeconds());
}

输出:
当前时间戳(秒级):1620304081
当前时间戳(毫秒级):1620304081141
开始时间戳(秒级):1620304081
结束时间戳(秒级):1620304084
操作时间:3

ZonedDateTime & LocalDateTime

  • ZonedDateTime 带时区的日历时间
  • LocalDateTime 不带时区的日历时间

DateTimeFormatter 日期格式化

TemporalAdjusters 特定日期API

@Test
public void Test2() {
    Instant now = Instant.now();
    // 时间戳转换为日历时间(UTC时区)
    ZonedDateTime dtUTC = ZonedDateTime.ofInstant(now, ZoneId.of("UTC"));
    ZonedDateTime dtsh = ZonedDateTime.ofInstant(now, ZoneId.systemDefault());
    System.out.println(" 时间戳转换为日历时间(UTC时区)" + dtUTC);
    System.out.println(" 时间戳转换为日历时间(默认时区)" + dtsh);
    System.out.println("日历时间转换为时间戳(默认时区)" + ZonedDateTime.now().toInstant().getEpochSecond());
    // System.out.println("时区列表:" + ZoneRulesProvider.getAvailableZoneIds());

    // 日期转换
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String dateStr = df.format(dtsh);
    System.out.println("日期格式化为字符串:" + dateStr);
    DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssxx");
    String dateStr1 = df1.format(dtsh);
    System.out.println("字符串转换为日期:" + ZonedDateTime.parse(dateStr1,df1));

    System.out.println("下个周五:" + dtsh.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)));
    System.out.println("本月第一天:" + dtsh.with(TemporalAdjusters.firstDayOfMonth()));
}
输出:
时间戳转换为日历时间(UTC时区)2021-05-06T12:58:34.927Z[UTC]
 时间戳转换为日历时间(默认时区)2021-05-06T20:58:34.927+08:00[Asia/Shanghai]
日历时间转换为时间戳(默认时区)1620305914
日期格式化为字符串:2021-05-06 20:58:34
字符串转换为日期:2021-05-06T20:58:34+08:00
下个周五:2021-05-07T20:58:34.927+08:00[Asia/Shanghai]
本月第一天:2021-05-01T20:58:34.927+08:00[Asia/Shanghai]