Site Search:

Define and create and manage date-based and time-based events using Instant, Period, Duration, and TemporalUnit

Back>

The Instant class represents a specific moment in GMT time zone.
Instant, Period, Duration, TemporalUnit
Instant, Period, Duration, TemporalUnit


There are a few ways of getting an Instance:

  1. now(): Instant instant = Instant.now();
  2. ofEpochSecond(): Instant instant = Instant.ofEpochSecond(1518619048);
  3. with ZonedDateTime: Instant instant = ZonedDateTime.of(2015, 1, 1, 1, 1, 1, "US/Eastern").toInstant();
  4. 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:
  1. Period p = Period.ofYears(1);
  2. Period p = Period.ofMonths(1);
  3. Period p = Period.ofWeeks(1);
  4. Period p = Period.ofDays(1);
  5. Period p = Period.of(1, 1, 1);
There are many ways to create a Duration:
  1. Duration d = Duration.ofDays(1);
  2. Duration d = Duration.ofHours(1);
  3. Duration d = Duration.ofMinutes(1);
  4. Duration d = Duration.ofSeconds(1);
  5. Duration d = Duration.ofMillis(1);
  6. Duration d = Duration.ofNanos(1);
  7. Duration d = Duration.of(1, ChronoUnit.DAYS);
  8. Duration d = Duration.of(1, ChronoUnit.HOURS);
  9. Duration d = Duration.of(1, ChronoUnit.MINUTES);
  10. Duration d = Duration.of(1, ChronoUnit.SECONDS);
  11. Duration d = Duration.of(1, ChronoUnit.MILLIS);
  12. Duration d = Duration.of(1, ChronoUnit.NANOS);
  13. Duration d = Duration.of(1, ChronoUnit.HALF_DAYS);
  14. Duration d = ChronoUnit.HOURS.between(LocalTime.of(1, 10), LocalTime.of(2, 1));
  15. 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.