In addition to the most common approach to Period and Duration objects, you can expand your knowledge in another way of working with time in Java.
Java Advanced Libraries 8. ChronoUnit for differences.
ChronoUnit is a great way to determine how far away are two time values. Temporal includes LocalDate, LocalTime, and so on.
LocalTime one = LocalTime.of(5,15); LocalTime two = LocalTime.of(6,30); LocalDate date = LocalDate.of(2019, 1, 29); System.out.println(ChronoUnit.HOURS.between(one, two)); //1 System.out.println(ChronoUnit.MINUTES.between(one, two)); //75 System.out.println(ChronoUnit.MINUTES.between(one, date)); //DateTimeException
The first example shows that between truncated rather than rounds.
The second shows how easy it is to count different units.
And the last example reminds us that we should not be confused with dates and times in Java :)
Alexander Jan 29 '19 at 19:31 2019-01-29 19:31
source share