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

Posted 一只黄焖鸡

tags:

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

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类

以上是关于计算两个时间之间相差几天月年的主要内容,如果未能解决你的问题,请参考以下文章

Java计算两个日期时间相差几天,几小时,几分钟等

计算两个日期之间相差几天(Date类型与String类型互相转换)

js 怎么求两个日期相差几个月零几天?

java怎样计算两个日期之间的秒数

java中计算两个日期之间差的天数

用java怎么计算两个日期之间年数,季度数,月数