Java 8: Difference between two LocalDateTime in multiple units

后端 未结 10 1684
后悔当初
后悔当初 2020-11-22 11:36

I am trying to calculate the difference between two LocalDateTime.

The output needs to be of the format y years m months d days h hours m minutes

10条回答
  •  情歌与酒
    2020-11-22 11:45

    I found the best way to do this is with ChronoUnit.

    long minutes = ChronoUnit.MINUTES.between(fromDate, toDate);
    long hours = ChronoUnit.HOURS.between(fromDate, toDate);
    

    Additional documentation is here: https://docs.oracle.com/javase/tutorial/datetime/iso/period.html

提交回复
热议问题