计算两个日期天数差

 

 

 

public class Test {

    public static void main(String[] args) {

        LocalDate ld = LocalDate.of(2018, 6, 6);
        LocalDate ld2 = LocalDate.now();
        Period period = Period.between(ld, ld2);
        
        
        /**
         * 仅仅是日的差值     例子  2018,6,6  2019,12,6          都是6号   结果0
         *                  2018,6,1  2019,12,6   结果5
         */
        System.out.println(period.getDays());
        
        
        
        //计算两个日期的相差多少天    例子 2018,6,6  2019,12,6  结果 548
        System.out.println(ld2.toEpochDay() - ld.toEpochDay());
        
        //结果    相差31天
        System.out.println(getDifferenceDayCount("2019-08-02", "2019-09-02"));
        //结果    相差4天
        System.out.println(getDifferenceDayCount("2019-09-06", "2019-09-02"));
        
    }

    // 字符串转日期类 计算天数差
    public static int getDifferenceDayCount(String startDateStr, String endDateStr) {

        LocalDate startDate = LocalDate.parse(startDateStr);
        LocalDate endDate = LocalDate.parse(endDateStr);
        // 取正数
        return Math.abs((int) (endDate.toEpochDay() - startDate.toEpochDay()));

    }

}

 

posted @ 2019-12-06 16:05  wf.zhang  阅读(436)  评论(0编辑  收藏  举报