搜索
简帛阁>技术文章>Java日期时间API系列17-----Jdk8中java.time包中的新的日期时间API类,java日期计算4,2个日期对比,获取相差年月日部分属性和相差总的天时分秒毫秒纳秒等

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

通过Java日期时间API系列9Jdk8javatime中的日期时间APIPeriodDuration区别,可以看出java8设计非常好,新增了PeriodDuration,专用于
通过Java日期时间API系列8Jdk8javatime中的日期时间APILocalDate源码分析,可以看出java8设计非常好,实现接口Temporal,TemporalAdjuste
通过Java日期时间API系列8Jdk8javatime中的日期时间APILocalDate源码分析,可以看出java8设计非常好,实现接口Temporal,TemporalAdjuste
通过Java日期时间API系列8Jdk8javatime中的日期时间APILocalDate源码分析,可以看出java8设计非常好,实现接口Temporal,TemporalAdjuste
通过Java日期时间API系列8Jdk8javatime中的日期时间APILocalDate源码分析可以看出,java8中的方法特别丰富,一些常用计算如星期计算,闰年计算等等。下面是
通过Java日期时间API系列1Jdk7及以前日期时间得知,Java8以前除了javasqlTimestamp扩充,其他最大只精确到毫秒Java8time所有相关都支持。下面是
时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在毫秒数,是所有时间基础,其他时间可以通过时间戳转换得到。Java本来已经有相关获取时间方法,Java8后增加In
1不变性日期/时间API,所有的都是不可变,这对多线程环境有好处。比如:LocalDateTime2关注点分离API将人可读日期时间机器时间(unixtimestamp)明确分离,
时候,往往需要统计某个时间区间销量问题,这就需要准确起始时间获取准确开始时间00:00:00,获取准确结束时间23:59:59。下面增加了一一些方法,获取当天起始时间,昨天起始时间,当前月第
因为Jdk7及以前日期时间不方便使用问题线程安全问题问题,2005年,StephenColebourne创建了JodaTime库,作为替代日期时间API。Stephen向JCP提交了一