Starting with JDK 8

Starting with JDK 8, the recommended way to represent date-time information is via Temporal (for example, DateTime, DateLocalTime, ZonedDateTime, and so on).

Let's assume the following two LocalDate objects, January 1, 2018, and March 1, 2019:

LocalDate ld1 = LocalDate.of(2018, 1, 1);
LocalDate ld2 = LocalDate.of(2019, 3, 1);

The simplest way to compute the difference between these two Temporal objects is via the ChronoUnit class. Beside representing the standard set of date periods units, ChronoUnit comes with several handy methods, including between(Temporal t1Inclusive, Temporal t2Exclusive). As its name suggests, the between() method calculates the amount of time between two Temporal objects. Let's see it at work to compute the difference between ld1 and ld2 in days, months, and years:

// 424
long betweenInDays = Math.abs(ChronoUnit.DAYS.between(ld1, ld2));

// 14
long betweenInMonths = Math.abs(ChronoUnit.MONTHS.between(ld1, ld2));

// 1
long betweenInYears = Math.abs(ChronoUnit.YEARS.between(ld1, ld2));

Alternatively, every Temporal exposes a method named until(). Actually, LocalDate has two, one that returns Period as a difference between two dates and another one that returns long as a difference between two dates in the specified time unit. Using the one that returns  Period looks like this:

Period period = ld1.until(ld2);

// Difference as Period: 1y2m0d
System.out.println("Difference as Period: " + period.getYears() + "y"
+ period.getMonths() + "m" + period.getDays() + "d");

Using the one that allows us to specify the time unit looks like this:

// 424
long untilInDays = Math.abs(ld1.until(ld2, ChronoUnit.DAYS));

// 14
long untilInMonths = Math.abs(ld1.until(ld2, ChronoUnit.MONTHS));

// 1
long untilInYears = Math.abs(ld1.until(ld2, ChronoUnit.YEARS));

The ChronoUnit.convert() method is also useful in the case of LocalDateTime. Let's consider the following two LocalDateTime objects—January 1, 2018 22:15:15, and March 1, 2019 23:15:15:

LocalDateTime ldt1 = LocalDateTime.of(2018, 1, 1, 22, 15, 15);
LocalDateTime ldt2 = LocalDateTime.of(2018, 1, 1, 23, 15, 15);

Now, let's see the difference between ldt1 and ldt2, when expressed in minutes:

// 60
long betweenInMinutesWithoutZone
= Math.abs(ChronoUnit.MINUTES.between(ldt1, ldt2));

And, the difference when expressed in hours via the LocalDateTime.until() method:

// 1
long untilInMinutesWithoutZone
= Math.abs(ldt1.until(ldt2, ChronoUnit.HOURS));

But, a really awesome thing about ChronoUnit.between() and until() is the fact that they work with ZonedDateTime. For example, let's consider ldt1 in the Europe/Bucharest time zone and in the Australia/Perth time zone, plus one hour:

ZonedDateTime zdt1 = ldt1.atZone(ZoneId.of("Europe/Bucharest"));
ZonedDateTime zdt2 = zdt1.withZoneSameInstant(
ZoneId.of("Australia/Perth")).plusHours(1);

Now, let's use ChronoUnit.between() to express the difference between zdt1 and zdt2 in minutes, and ZonedDateTime.until() to express the difference between zdt1 and zdt2 in hours:

// 60
long betweenInMinutesWithZone
= Math.abs(ChronoUnit.MINUTES.between(zdt1, zdt2));

// 1
long untilInHoursWithZone
= Math.abs(zdt1.until(zdt2, ChronoUnit.HOURS));

Finally, let's repeat this technique, but for two independent ZonedDateTime objects; one obtained for ldt1 and one for ldt2:

ZonedDateTime zdt1 = ldt1.atZone(ZoneId.of("Europe/Bucharest"));
ZonedDateTime zdt2 = ldt2.atZone(ZoneId.of("Australia/Perth"));

// 300
long betweenInMinutesWithZone
= Math.abs(ChronoUnit.MINUTES.between(zdt1, zdt2));

// 5
long untilInHoursWithZone
= Math.abs(zdt1.until(zdt2, ChronoUnit.HOURS));
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset