Java日期时间API系列17—–Jdk8中java.time包中的新的日期时间API类,java日期计算4,2个日期对比,获取相差年月日部分属性和相差总的天时分秒毫秒纳秒等

通过Java日期时间API系列9—–Jdk8中java.time包中的新的日期时间API类的Period和Duration的区别 ,可以看出java8设计非常好,新增了Period和Duration类,专用于对比2个时间场景:

Period,可以获取2个时间相差的年月日的属性。

Duration,可以获取2个时间相差总的天时分秒毫秒纳秒。

 

 

下面应用:

    /**
     * 获取2个日期的相差年月天的年数部分
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenYears(LocalDateTime startInclusive, LocalDateTime endExclusive){
        Objects.requireNonNull(startInclusive, "startInclusive");
        Objects.requireNonNull(endExclusive, "endExclusive");
        return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
                DateTimeConverterUtil.toLocalDate(endExclusive)).getYears();
    }
    
    /**
     * 获取2个日期的相差年月天的年数部分
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenYears(Date startInclusive, Date endExclusive){
        Objects.requireNonNull(startInclusive, "startInclusive");
        Objects.requireNonNull(endExclusive, "endExclusive");
        return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
                DateTimeConverterUtil.toLocalDate(endExclusive)).getYears();
    }
    
    /**
     * 获取2个日期的相差年月天的年数部分
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenYears(LocalDate startInclusive, LocalDate endExclusive){
        return Period.between(startInclusive, endExclusive).getYears();
    }
    
    /**
     * 获取2个日期的相差年月天的月数部分
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenMonths(LocalDateTime startInclusive, LocalDateTime endExclusive){
        Objects.requireNonNull(startInclusive, "startInclusive");
        Objects.requireNonNull(endExclusive, "endExclusive");
        return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
                DateTimeConverterUtil.toLocalDate(endExclusive)).getMonths();
    }
    
    /**
     * 获取2个日期的相差年月天的月数部分
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenMonths(Date startInclusive, Date endExclusive){
        Objects.requireNonNull(startInclusive, "startInclusive");
        Objects.requireNonNull(endExclusive, "endExclusive");
        return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
                DateTimeConverterUtil.toLocalDate(endExclusive)).getMonths();
    }
    
    /**
     * 获取2个日期的相差年月天的月数部分
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenMonths(LocalDate startInclusive, LocalDate endExclusive){
        return Period.between(startInclusive, endExclusive).getMonths();
    }
    
    /**
     * 获取2个日期的相差年月天的天数部分
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenDays(LocalDateTime startInclusive, LocalDateTime endExclusive){
        Objects.requireNonNull(startInclusive, "startInclusive");
        Objects.requireNonNull(endExclusive, "endExclusive");
        return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
                DateTimeConverterUtil.toLocalDate(endExclusive)).getDays();
    }
    
    /**
     * 获取2个日期的相差年月天的天数部分
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenDays(Date startInclusive, Date endExclusive){
        Objects.requireNonNull(startInclusive, "startInclusive");
        Objects.requireNonNull(endExclusive, "endExclusive");
        return Period.between(DateTimeConverterUtil.toLocalDate(startInclusive),
                DateTimeConverterUtil.toLocalDate(endExclusive)).getDays();
    }
    
    /**
     * 获取2个日期的相差年月天的天数部分
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenDays(LocalDate startInclusive, LocalDate endExclusive){
        return Period.between(startInclusive, endExclusive).getDays();
    }
    
    /**
     * 获取2个日期的相差总天数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalDays(LocalDateTime startInclusive, LocalDateTime endExclusive){
        return Duration.between(startInclusive, endExclusive).toDays();
    }
    
    /**
     * 获取2个日期的相差总天数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalDays(Date startInclusive, Date endExclusive){
        Objects.requireNonNull(startInclusive, "startInclusive");
        Objects.requireNonNull(endExclusive, "endExclusive");
        return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toDays();
    }
    
    /**
     * 获取2个日期的相差总小时数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalHours(LocalDateTime startInclusive, LocalDateTime endExclusive){
        return Duration.between(startInclusive, endExclusive).toHours();
    }
    
    /**
     * 获取2个日期的相差总小时数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalHours(LocalTime startInclusive, LocalTime endExclusive){
        return Duration.between(startInclusive, endExclusive).toHours();
    }
    
    /**
     * 获取2个日期的相差总小时数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalHours(Date startInclusive, Date endExclusive){
        Objects.requireNonNull(startInclusive, "startInclusive");
        Objects.requireNonNull(endExclusive, "endExclusive");
        return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toHours();
    }
    
    /**
     * 获取2个日期的相差总分钟数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalMinutes(LocalDateTime startInclusive, LocalDateTime endExclusive){
        return Duration.between(startInclusive, endExclusive).toMinutes();
    }
    
    /**
     * 获取2个日期的相差总分钟数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalMinutes(LocalTime startInclusive, LocalTime endExclusive){
        return Duration.between(startInclusive, endExclusive).toMinutes();
    }
    
    /**
     * 获取2个日期的相差总分钟数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalMinutes(Date startInclusive, Date endExclusive){
        Objects.requireNonNull(startInclusive, "startInclusive");
        Objects.requireNonNull(endExclusive, "endExclusive");
        return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toMinutes();
    }
    
    /**
     * 获取2个日期的相差总秒数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalSeconds(LocalDateTime startInclusive, LocalDateTime endExclusive){
        return Duration.between(startInclusive, endExclusive).getSeconds();
    }
    
    /**
     * 获取2个日期的相差总秒数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalSeconds(LocalTime startInclusive, LocalTime endExclusive){
        return Duration.between(startInclusive, endExclusive).getSeconds();
    }
    
    /**
     * 获取2个日期的相差总秒数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalSeconds(Date startInclusive, Date endExclusive){
        Objects.requireNonNull(startInclusive, "startInclusive");
        Objects.requireNonNull(endExclusive, "endExclusive");
        return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).getSeconds();
    }
    
    /**
     * 获取2个日期的相差总毫秒数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalMillis(LocalDateTime startInclusive, LocalDateTime endExclusive){
        return Duration.between(startInclusive, endExclusive).toMillis();
    }
    
    /**
     * 获取2个日期的相差总毫秒数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalMillis(LocalTime startInclusive, LocalTime endExclusive){
        return Duration.between(startInclusive, endExclusive).toMillis();
    }
    
    /**
     * 获取2个日期的相差总毫秒数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalMillis(Date startInclusive, Date endExclusive){
        Objects.requireNonNull(startInclusive, "startInclusive");
        Objects.requireNonNull(endExclusive, "endExclusive");
        return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toMillis();
    }    
    
    /**
     * 获取2个日期的相差总纳秒数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalNanos(LocalDateTime startInclusive, LocalDateTime endExclusive){
        return Duration.between(startInclusive, endExclusive).toNanos();
    }
    
    /**
     * 获取2个日期的相差总纳秒数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalNanos(LocalTime startInclusive, LocalTime endExclusive){
        return Duration.between(startInclusive, endExclusive).toNanos();
    }
    
    /**
     * 获取2个日期的相差总纳秒数
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long betweenTotalNanos(Date startInclusive, Date endExclusive){
        Objects.requireNonNull(startInclusive, "startInclusive");
        Objects.requireNonNull(endExclusive, "endExclusive");
        return durationBetween(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endExclusive)).toNanos();
    }

 

测试代码

    /**
     * 使用Period比较2个LocalDate
     */
    @Test
    public void dateCalculatorPeriodBetweenTest(){
        LocalDate localDate = LocalDate.now();
        LocalDate localDate2 = LocalDate.of(2021, 3, 7);
        System.out.println(localDate);
        System.out.println(localDate2);
        
        System.out.println(DateTimeCalculatorUtil.betweenYears(localDate, localDate2));
        System.out.println(DateTimeCalculatorUtil.betweenMonths(localDate, localDate2));
        System.out.println(DateTimeCalculatorUtil.betweenDays(localDate, localDate2));
    }
    
    /**
     * 使用Period比较2个Date
     */
    @Test
    public void dateCalculatorPeriodBetweenTest2(){
        Date date = new Date();
        LocalDate localDate2 = LocalDate.of(2021, 3, 7);
        Date date2 = DateTimeConverterUtil.toDate(localDate2);
        System.out.println(date);
        System.out.println(date2);
        
        System.out.println(DateTimeCalculatorUtil.betweenYears(date, date2));
        System.out.println(DateTimeCalculatorUtil.betweenMonths(date, date2));
        System.out.println(DateTimeCalculatorUtil.betweenDays(date, date2));
    }
    
    /**
     * 使用Duration比较2个LocalDateTime
     */
    @Test
    public void dateCalculatorDurationBetweenTest(){
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime localDateTime2 = LocalDateTime.of(2021, 3, 7, 22, 10, 10);
        System.out.println(localDateTime);
        System.out.println(localDateTime2);
        
        System.out.println(DateTimeCalculatorUtil.betweenTotalDays(localDateTime, localDateTime2));
        System.out.println(DateTimeCalculatorUtil.betweenTotalHours(localDateTime, localDateTime2));
        System.out.println(DateTimeCalculatorUtil.betweenTotalMinutes(localDateTime, localDateTime2));
        System.out.println(DateTimeCalculatorUtil.betweenTotalSeconds(localDateTime, localDateTime2));
        System.out.println(DateTimeCalculatorUtil.betweenTotalMillis(localDateTime, localDateTime2));
        System.out.println(DateTimeCalculatorUtil.betweenTotalNanos(localDateTime, localDateTime2));
    }
    
    /**
     * 使用Duration比较2个Date
     */
    @Test
    public void dateCalculatorDurationBetweenTest2(){
        Date date = new Date();
        LocalDate localDate2 = LocalDate.of(2021, 3, 7);
        Date date2 = DateTimeConverterUtil.toDate(localDate2);
        System.out.println(date);
        System.out.println(date2);
        
        System.out.println(DateTimeCalculatorUtil.betweenTotalDays(date, date2));
        System.out.println(DateTimeCalculatorUtil.betweenTotalHours(date, date2));
        System.out.println(DateTimeCalculatorUtil.betweenTotalMinutes(date, date2));
        System.out.println(DateTimeCalculatorUtil.betweenTotalSeconds(date, date2));
        System.out.println(DateTimeCalculatorUtil.betweenTotalMillis(date, date2));
        System.out.println(DateTimeCalculatorUtil.betweenTotalNanos(date, date2));
    }

 

测试结果:

2020-02-06
2021-03-07
1
1
1



Thu Feb 06 22:09:38 CST 2020
Sun Mar 07 00:00:00 CST 2021
1
1
1


2020-02-06T22:09:48.247
2021-03-07T22:10:10
395
9480
568800
34128021
34128021753
34128021753000000


Thu Feb 06 22:09:58 CST 2020
Sun Mar 07 00:00:00 CST 2021
394
9457
567470
34048201
34048201995
34048201995000000

 

 

源代码地址:https://github.com/xkzhangsan/xk-time