Calculates the amount of time until the specified temporal object in terms of the specified unit.
package com.logicbig.example.year;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
public class UntilExample {
public static void main(String... args) {
Year y = Year.of(2011);
System.out.println(y);
until(y, LocalDate.now());
until(y, LocalDateTime.now());
until(y, ZonedDateTime.now());
until(y, OffsetDateTime.now());
}
private static void until(Year y, Temporal temporal) {
System.out.printf("%s = %s%n", temporal.getClass().getSimpleName(), temporal);
for (ChronoUnit unit : ChronoUnit.values()) {
if (y.isSupported(unit)) {
long l = y.until(temporal, unit);
System.out.printf("%10s > %s%n", unit, l);
}
}
}
}
Output
2011
LocalDate = 2017-05-01
Years > 6
Decades > 0
Centuries > 0
Millennia > 0
Eras > 0
LocalDateTime = 2017-05-01T15:52:40.931
Years > 6
Decades > 0
Centuries > 0
Millennia > 0
Eras > 0
ZonedDateTime = 2017-05-01T15:52:40.932-05:00[America/Chicago]
Years > 6
Decades > 0
Centuries > 0
Millennia > 0
Eras > 0
OffsetDateTime = 2017-05-01T15:52:40.932-05:00
Years > 6
Decades > 0
Centuries > 0
Millennia > 0
Eras > 0