使用LocalDateTime获取两个时间点相隔几天几小时几分几秒


    public static void main(String[] args) {

        LocalDateTime fromDateTime = LocalDateTime.of(2018, 9, 11, 7, 45, 55);
        LocalDateTime toDateTime = LocalDateTime.of(2019, 9, 10, 7, 40, 45);
        LocalDateTime tempDateTime = LocalDateTime.from( fromDateTime );

        long days = tempDateTime.until( toDateTime, ChronoUnit.DAYS);
        tempDateTime = tempDateTime.plusDays( days );


        long hours = tempDateTime.until( toDateTime, ChronoUnit.HOURS);
        tempDateTime = tempDateTime.plusHours( hours );

        long minutes = tempDateTime.until( toDateTime, ChronoUnit.MINUTES);
        tempDateTime = tempDateTime.plusMinutes( minutes );

        long seconds = tempDateTime.until( toDateTime, ChronoUnit.SECONDS);

        System.out.println(
                days + " 天 " +
                hours + " 小时 " +
                minutes + " 分 " +
                seconds + " 秒.");
    }
输出结果为:363 天 23 小时 54 分 50 秒.(注:输出结果可能为负数)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM