The Instant class represents a specific moment in GMT time zone.
Instant, Period, Duration, TemporalUnit |
There are a few ways of getting an Instance:
- now(): Instant instant = Instant.now();
- ofEpochSecond(): Instant instant = Instant.ofEpochSecond(1518619048);
- with ZonedDateTime: Instant instant = ZonedDateTime.of(2015, 1, 1, 1, 1, 1, "US/Eastern").toInstant();
- from another Instant: Instant instant = Instant.now().plus(1, ChronoUnit.HOURS);
The Period and Duration classes represents a span of time. Period is for a day or more of time, Duration is for smaller units of time.
There are five ways to create a Period:
- Period p = Period.ofYears(1);
- Period p = Period.ofMonths(1);
- Period p = Period.ofWeeks(1);
- Period p = Period.ofDays(1);
- Period p = Period.of(1, 1, 1);
There are many ways to create a Duration:
- Duration d = Duration.ofDays(1);
- Duration d = Duration.ofHours(1);
- Duration d = Duration.ofMinutes(1);
- Duration d = Duration.ofSeconds(1);
- Duration d = Duration.ofMillis(1);
- Duration d = Duration.ofNanos(1);
- Duration d = Duration.of(1, ChronoUnit.DAYS);
- Duration d = Duration.of(1, ChronoUnit.HOURS);
- Duration d = Duration.of(1, ChronoUnit.MINUTES);
- Duration d = Duration.of(1, ChronoUnit.SECONDS);
- Duration d = Duration.of(1, ChronoUnit.MILLIS);
- Duration d = Duration.of(1, ChronoUnit.NANOS);
- Duration d = Duration.of(1, ChronoUnit.HALF_DAYS);
- Duration d = ChronoUnit.HOURS.between(LocalTime.of(1, 10), LocalTime.of(2, 1));
- Duration d = ChronoUnit.MINUTES.between(LocalTime.of(1, 10), LocalTime.of(2, 1));
Notice if you call the Period and Duration's of() method in a chain, only the last call count. For example,
Duration.ofDays(1).ofHours(1).ofMinutes(1) created a 1 minute duration.
Also notice toString of Period start with P, toString of Duration start with PT.
OCPJP>cat DatesInstantsTest.java
import java.time.*;
import java.time.temporal.ChronoUnit;
public class DatesInstantsTest {
public static void main(String... args) {
Instant start = Instant.now();
long epo = start.getEpochSecond();
ZonedDateTime st = ZonedDateTime.ofInstant(start, ZoneId.of("US/Eastern"));
ZonedDateTime zdt = ZonedDateTime.of(st.getYear(), st.getMonthValue(), st.getDayOfMonth()
, st.getHour(), st.getMinute(), st.getSecond(), st.getNano(), st.getZone());
Instant end = zdt.plus(1, ChronoUnit.SECONDS).toInstant();
System.out.println(isExpired(start, end, Duration.ofHours(1)));
System.out.println("====================");
start = Instant.ofEpochSecond(epo);
boolean r = isExpired(LocalDateTime.ofInstant(start, ZoneId.of("US/Eastern"))
, LocalDateTime.now(), Period.of(0, 0, 0));
System.out.println(r);
}
public static boolean isExpired(Instant start, Instant end, Duration ttl) {
System.out.println(start);
System.out.println(end);
Duration d = Duration.between(start, end);
System.out.println(d);
d = d.minusNanos(ttl.toNanos());
System.out.println(d);
return end.isAfter(start.plus(ttl));
}
public static boolean isExpired(LocalDateTime start, LocalDateTime end, Period ttl) {
System.out.println(start);
System.out.println(end);
Duration d = Duration.between(end, start);
System.out.println(d);
Period p = Period.between(end.toLocalDate(), start.toLocalDate());
System.out.println(p);
return start.isBefore(end.minus(ttl));
}
}
OCPJP>
OCPJP>
OCPJP>javac DatesInstantsTest.java
OCPJP>java DatesInstantsTest
2018-02-14T17:18:44.222Z
2018-02-14T17:18:45.222Z
PT1S
PT-59M-59S
false
====================
2018-02-14T12:18:44
2018-02-14T12:18:44.298
PT-0.298S
P0D
true
OCPJP>
The above example is intentionally verbose to use more Duration, Period, Instant and ChronoUnit APIs and Constants.