JAVA8使用LocalDate系列

275 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第5天,点击查看活动详情

常用时间类

在JDK8中,新增了日期和时间处理类,常用的类有

  • LocalDate 日期处理类,精确到天
  • LocalDateTime 时间处理类,精确到纳秒
  • DateTimeFormatter 时间格式化类
  • ZoneId 时区设置类
  • LocalTime 时间类
  • ZonedDateTime 带时区的日期和时间类

时间类使用

@Test
public void testLocalDate() {
    LocalDate localDate = LocalDate.now();
    System.out.println("当前日期:" + localDate);
    System.out.println("当前日期:" + localDate.getYear() + "-" + localDate.getMonthValue() + "-" + localDate.getDayOfMonth());


    LocalDateTime localDateTime = LocalDateTime.now();
    System.out.println("当前日期时间" + localDateTime);
    System.out.println("当前日期时间获取不同:" + localDateTime.getYear() + "-" + localDateTime.getMonthValue() + "-" + localDateTime.getDayOfMonth() + " " + localDateTime.getHour() + ":" + localDateTime.getMinute() + ":" + localDateTime.getSecond());

    LocalTime localTime = LocalTime.now();
    System.out.println("当前时间:" + localTime);
    System.out.println("当前时间:" + localTime.getHour() + ":" + localTime.getMinute() + ":" + localTime.getSecond());

    // 有时区的当前精确时间
    ZonedDateTime nowZone = LocalDateTime.now().atZone(ZoneId.systemDefault());
    System.out.println("当前精确时间(有时区):" + nowZone);
    System.out.println("当前精确时间(有时区):" + nowZone.getYear() + "-" + nowZone.getMonthValue() + "-" + nowZone.getDayOfMonth() + " " + nowZone.getHour() + ":" + nowZone.getMinute() + ":" + nowZone.getSecond());

}

输出

当前日期:2022-03-22
当前日期:2022-3-22
当前日期时间2022-03-22T11:27:03.292
当前日期时间获取不同:2022-3-22 11:27:3
当前时间:11:27:03.292
当前时间:11:27:3
当前精确时间(有时区):2022-03-22T11:27:03.292+08:00[Asia/Shanghai]
当前精确时间(有时区):2022-3-22 11:27:3

创建时间

@Test
public void testLocalDate() {

    LocalDateTime ofTime = LocalDateTime.of(2022, 03, 22, 8, 8, 8);
    System.out.println("当前精确时间:" + ofTime);

    LocalDate localDateCreate = LocalDate.of(2022, 03, 22);
    System.out.println("当前日期:" + localDateCreate);

    LocalTime localTimeCreate = LocalTime.of(12, 01, 01);
    System.out.println("当天时间:" + localTimeCreate);
}

输出:

当前精确时间:2022-03-22T08:08:08
当前日期:2022-03-22
当天时间:12:01:01

时间转换

@Test
public void testLocalDate() {
    LocalDateTime parseTime = LocalDateTime.parse("2022-03-22T22:22:22.222");
    System.out.println("字符串时间转换:" + parseTime);

    LocalDate formatted = LocalDate.parse("20220322", DateTimeFormatter.BASIC_ISO_DATE);
    System.out.println("字符串时间yyyyMMdd转换格式yyyy-MM-dd:" + formatted);

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
    LocalDate formatted2 = LocalDate.parse("2022/03/22", formatter);
    System.out.println("字符串时间yyyy/MM/dd转换格式yyyy-MM-dd:" + formatted2);

    // Date -> LocalDateTime
    Date date = new Date();
    ZoneId zoneId = ZoneId.systemDefault();
    System.out.println("Date -> LocalDateTime:" + LocalDateTime.ofInstant(date.toInstant(), zoneId));

    // LocalDateTime -> Date
    LocalDateTime localDateTime = LocalDateTime.now();
    Date toDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    System.out.println("LocalDateTime -> Date:" + toDate);

    // 当前时间转时间戳
    long epochMilli = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
    System.out.println("当前时间转时间戳:" + epochMilli);
    // 时间戳转换成时间
    LocalDateTime epochMilliTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
    System.out.println("时间戳转换成时间:" + epochMilliTime);
}

输出:

字符串时间转换:2022-03-22T22:22:22.222
字符串时间yyyyMMdd转换格式yyyy-MM-dd:2022-03-22
字符串时间yyyy/MM/dd转换格式yyyy-MM-dd:2022-03-22
Date -> LocalDateTime:2022-03-22T11:42:15.949
LocalDateTime -> Date:Tue Mar 22 11:42:16 CST 2022
当前时间转时间戳:1647920536193
时间戳转换成时间:2022-03-22T11:42:16.193

时间格式化

@Test
public void testLocalDate() {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间:" + now);
    System.out.println("格式化 ISO_LOCAL_DATE_TIME:" + now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    System.out.println("格式化 ISO_LOCAL_DATE:" + now.format(DateTimeFormatter.ISO_LOCAL_DATE));
    System.out.println("格式化 ISO_LOCAL_TIME:" + now.format(DateTimeFormatter.ISO_LOCAL_TIME));
    System.out.println("格式化 指定格式:" + now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")));
}

输出:

当前时间:2022-03-22T11:45:23.532
格式化 ISO_LOCAL_DATE_TIME:2022-03-22T11:45:23.532
格式化 ISO_LOCAL_DATE:2022-03-22
格式化 ISO_LOCAL_TIME:11:45:23.532
格式化 指定格式:2022-03-22 11:45:23

时间对比和加减

@Test
public void testLocalDate() {
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime yesterday = now.minusDays(1);
    System.out.println("现在时间: " + now + " 昨天时间:" + yesterday);
    System.out.println("现在在昨天之后:" + now.isAfter(yesterday) + " 现在在昨天之前:" + now.isBefore(yesterday));

    // 时间差
    long day = yesterday.until(now, ChronoUnit.DAYS);
    long month = yesterday.until(now, ChronoUnit.MONTHS);
    long hours = yesterday.until(now, ChronoUnit.HOURS);
    long minutes = yesterday.until(now, ChronoUnit.MINUTES);
    System.out.println("相差月份" + month);
    System.out.println("相差天数" + day);
    System.out.println("相差小时" + hours);
    System.out.println("相差分钟" + minutes);

    // 距离国庆还有多少天
    LocalDate holiday = LocalDate.of(2022, 10, 1);
    LocalDate nowDate = LocalDate.now();
    System.out.println("距离国庆还有:" + nowDate.until(holiday, ChronoUnit.DAYS) + "天");
}

输出:

现在时间: 2022-03-22T11:51:39.032 昨天时间:2022-03-21T11:51:39.032
现在在昨天之后:true 现在在昨天之前:false
相差月份0
相差天数1
相差小时24
相差分钟1440
距离国庆还有:193