当前位置:网站首页>Calculate the number of days between two times (supports cross month and cross year)

Calculate the number of days between two times (supports cross month and cross year)

2022-06-13 05:30:00 Nagging program dog

Calculate the number of days between two times ( Support cross month 、 straddle old and new years ):

    /** * JDK 8 Calculate the number of days between two times , Support cross month 、 straddle old and new years  * @param startTimeStamp  Start timestamp  * @param endTimeStamp  End timestamp  * @return  Days apart , If the start time is greater than the end time , *  Returns the corresponding complex number  */
    public long diffDays(long startTimeStamp,long endTimeStamp){
    
        LocalDate startTime = Instant.ofEpochMilli(startTimeStamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();
        LocalDate endTime = Instant.ofEpochMilli(endTimeStamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();
        return ChronoUnit.DAYS.between(startTime, endTime);
    }
原网站

版权声明
本文为[Nagging program dog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130526193454.html