Converting Instants – Date and Time

Converting Instants

Each of the classes LocalTime, LocalDate, LocalDateTime, and ZonedDateTime provides the ofInstant() method to obtain a temporal object from an Instant. The code below shows how instants can be converted to other temporal objects for a given time zone. For date/time represented by this particular instant, the offset for the time zone “America/New_York” is -4 hours from UTC.

Click here to view code image

Instant instant = Instant.parse(“2021-04-28T03:15:00Z”);
ZoneId zid = ZoneId.of(“America/New_York”);
LocalTime lt = LocalTime.ofInstant(instant, zid);           // 10:18:30
LocalDate ld = LocalDate.ofInstant(instant, zid);           // 2021-04-27
LocalDateTime ldt = LocalDateTime.ofInstant(instant, zid);  // 2021-04-27T23:15
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, zid);
    // 2021-04-27T23:15-04:00[America/New_York]

Click here to view code image

static
TemporalType
 ofInstant(Instant instant, ZoneId zone)

Creates a TemporalType object from the given Instant and ZoneId (p. 1072), where TemporalType can be LocalTime, LocalDate, LocalDateTime, or ZonedDateTime.

17.5 Working with Periods

For representing an amount of time, the Date and Time API provides the two classes Period and Duration. We will concentrate on the Period class in this section and discuss the Duration class in ยง17.6, p. 1064.

The Period class essentially represents a date-based amount of time in terms of years, months, and days, whereas, the Duration class represents a time-based amount of time in terms of seconds and nanoseconds.

The date-based Period class can be used with the LocalDate class, and not surprisingly, the time-based Duration class can be used with the LocalTime class. Of course, the LocalDateTime class can use both temporal amount classes.

The Period and Duration classes are in the same package (java.time) as the temporal classes, and the repertoire of methods they provide should look familiar, as they share many of the method prefixes with the temporal classes (Table 17.2, p. 1026).

The mantra of immutable and thread-safe objects also applies to both the Period and the Duration classes.

Comparing Periods for Equality – Date and Time

Comparing Periods for Equality

The Period class overrides the equals() and hashCode() methods of the Object class, but the class does not implement the Comparable<E> interface. The value of each date unit is compared individually, and must have the same value to be considered equal. A period of 1 year and 14 months is not equal to a period of 2 years and 2 months, or to a period of 26 months. For this reason, Period objects do not implement the Comparable<E> interface.

Click here to view code image

Period px = Period.of(1, 14, 0);
Period py = Period.of(2, 2, 0);
Period pz = Period.ofMonths(26);
System.out.println(px.equals(py));           // false
System.out.println(px.equals(pz));           // false
System.out.println(px.equals(Period.ZERO));  // false

boolean equals(Object obj)

Determines whether this period is equal to another period, meaning that each corresponding date unit has the same value.

int hashCode()

Returns a hash code for this period.

Creating Modified Copies of Periods

The Period class provides with methods to set a new value for each date unit individually, while the values of the other date units remain unchanged. Note that each method call returns a new Period object, and chaining method calls work as expected.

Click here to view code image

Period p5 = Period.of(2, 1, 30) // P2Y1M30D
    .withYears(3)               // P3Y1M30D, sets the number of years
    .withMonths(16)             // P3Y16M30D, sets the number of months
    .withDays(1);               // P3Y16M1D, sets the number of days

Period withYears(int years)
Period withMonths(int months)
Period withDays(int days)

Return a copy of this period where a specific date unit is set to the value of the argument. The values of the other date units are not affected.