计算两个时间类型LocalDate合着LocalDateTime之间相差天数

Posted Joker-0927

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算两个时间类型LocalDate合着LocalDateTime之间相差天数相关的知识,希望对你有一定的参考价值。


LocalDate startDate = LocalDate.of(2020, Month.JANUARY, 1);
LocalDate endDate = LocalDate.of(2021, Month.JANUARY, 1);
long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
System.out.println("Days between: " + daysBetween);

 

计算两个时间之间相差几天月年

ChronoUnit类
//获取当前时间(2021-06-22)
        LocalDate today = LocalDate.now();
        //将String转LocalDateTime
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate collectTimeDate = LocalDate.parse("2021-05-01",df);
        //判断相差多少天
        log.info("ChronoUnit====相差{}年,相差{}月,相差{}天",
                ChronoUnit.YEARS.between(collectTimeDate,today),
                ChronoUnit.MONTHS.between(collectTimeDate,today),
                ChronoUnit.DAYS.between(collectTimeDate,today));

注意:若调用获取时、分、秒、半天方法皆报错

结果:

//获取当前时间(2021-06-22 17:27:03)
        LocalDateTime today = LocalDateTime.now();
        //将String转LocalDateTime
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime collectTimeDate = LocalDateTime.parse("2021-05-01 16:40:23",df);
        //判断相差多少天
        log.info("ChronoUnit====相差{}年,相差{}月,相差{}天",
                ChronoUnit.YEARS.between(collectTimeDate,today),
                ChronoUnit.MONTHS.between(collectTimeDate,today),
                ChronoUnit.DAYS.between(collectTimeDate,today));
        
        log.info("ChronoUnit====相差{}小时,相差{}分钟,相差{}秒",
                ChronoUnit.HOURS.between(collectTimeDate,today),
                ChronoUnit.MINUTES.between(collectTimeDate,today),
                ChronoUnit.SECONDS.between(collectTimeDate,today));
        
        log.info("ChronoUnit====相差{}半天",ChronoUnit.HALF_DAYS.between(collectTimeDate,today));

结果:

Period类

//获取当前时间(2021-06-22)
        LocalDate todayDate = LocalDate.now();
        //将String转LocalDateTime
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate lastDate = LocalDate.parse("2021-05-01",dtf);
        //判断相差多少天
        log.info("Period====相差{}年,相差{}月,相差{}天",
                Period.between(lastDate,todayDate).getYears(),
                Period.between(lastDate,todayDate).getMonths(),
                Period.between(lastDate,todayDate).getDays());

注意:Period.between方法只接收LocalDate类型的传参

结果:

从结果中可看到,两个时间本是相差52天,但此处只显示相差21天;因为此处只用两者的日相减,不管年份和月份

Duration类

//获取当前时间(2021-06-22 17:27:03)
        LocalDateTime currentDate = LocalDateTime.now();
        //将String转LocalDateTime
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime date = LocalDateTime.parse("2021-05-01 16:40:23",formatter);

        log.info("Duration====相差{}天,相差{}小时,相差{}分钟",
                Duration.between(date,currentDate).toDays(),
                Duration.between(date,currentDate).toHours(),
                Duration.between(date,currentDate).toMinutes());

结果:

以下这种会报错:

//获取当前时间(2021-06-22)
        LocalDate currentDate = LocalDate.now();
        //将String转LocalDate
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse("2021-05-01",formatter);
        //判断相差多少天
        log.info("Duration====相差{}天", Duration.between(date,currentDate).toDays());

结果:

综上,推荐使用ChronoUnit类

以上是关于计算两个时间类型LocalDate合着LocalDateTime之间相差天数的主要内容,如果未能解决你的问题,请参考以下文章

Java 8 使用

计算两个时间之间相差几天月年

localdate求时间差

JDK8时间工具类

Gson 没有正确序列化 LocalDate

如何比较 LocalDate 和 Calendar 类的对象?