Temporal Arithmetic with Zoned Date-Time

Temporal arithmetic with zoned date-times is analogous to temporal arithmetic with date-times (p. 1040). However, it is important to note the differences that are due to time zones and daylight savings (p. 1082).

Example 17.8 illustrates calculating flight times. The code at (1) creates a flight departure time for a flight to London from New York at 8:30 pm on July 4, 2021. The flight time is 7 hours and 30 minutes. The code at (2) calculates the local time of arrival at London, first by calling the withZoneSameInstant() method to find the local time in London at the time the flight departs from New York, and then adding the flight time to obtain the local arrival time in London.

In Example 17.8, the code at (3) calculates the flight duration by calling the Duration.between() method with the departure and arrival times as arguments. The code at (4) calls the until() method to calculate the flight time in minutes from the departure time to the arrival time.

Finally, the code at (5) and (6) calculates the local time at the departure airport at the time the flight arrives in London in two different ways. At (5), the plusMinutes() method adds the flight time to the departure time. At (6), the withZoneSameInstant() method converts the arrival time to its equivalent in the departure time zone.

Example 17.8 Flight Time Information

Click here to view code image

import java.time.DateTimeException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

public class FlightTimeInfo {
  public static void main(String[] args) {
    try {
      // Departure from New York at 8:30pm on July 4, 2021.                  (1)
      LocalDateTime departure = LocalDateTime.of(2021, Month.JULY, 4, 20, 30);
      ZoneId departureZone = ZoneId.of(“America/New_York”);
      ZonedDateTime departureZDT = ZonedDateTime.of(departure, departureZone);
      // Flight time is 7 hours and 30 minutes.
      // Calculate local arrival time at London:                             (2)
      ZoneId arrivalZone = ZoneId.of(“Europe/London”);
      ZonedDateTime arrivalZDT
          = departureZDT.withZoneSameInstant(arrivalZone)
                        .plusMinutes(7*60 + 30);
      System.out.printf(“DEPARTURE:  %s%n”, departureZDT);
      System.out.printf(“ARRIVAL:    %s%n”, arrivalZDT);
      // Flight time as a Duration:                                          (3)
      Duration flightduration = Duration.between(departureZDT, arrivalZDT);
      System.out.println(“Flight duration:     ” + flightduration);
      // Flight time in minutes:                                             (4)
      long flightTime = departureZDT.until(arrivalZDT, ChronoUnit.MINUTES);
      System.out.println(“Flight time (mins.): ” + flightTime);
      System.out.printf(                                                  // (5)
          “Time at departure airport on arrival: %s%n”,
          departureZDT.plusMinutes(7*60 + 30));
      System.out.printf(                                                  // (6)
          “Time at departure airport on arrival: %s%n”,
          arrivalZDT.withZoneSameInstant(departureZone));
    } catch (DateTimeException e) {
      e.printStackTrace();
    }
  }
}

Output from the program:

Click here to view code image

DEPARTURE:  2021-07-04T20:30-04:00[America/New_York]
ARRIVAL:    2021-07-05T09:00+01:00[Europe/London]
Flight duration:     PT7H30M
Flight time (mins.): 450
Time at departure airport on arrival: 2021-07-05T04:00-04:00[America/New_York]
Time at departure airport on arrival: 2021-07-05T04:00-04:00[America/New_York]

Selected methods from the ZonedDateTime class for temporal arithmetic are presented below.

Click here to view code image

ZonedDateTime plus
UNIT
(long amount)
ZonedDateTime minus
UNIT
(long amount)

Return a copy of this zoned date-time with the specified amount either added or subtracted, respectively, where the unit is designated by the suffix UNIT, which can be Nanos, Seconds, Minutes, Hours, Days, Weeks, Months, or Years.

With the time units (Nanos, Seconds, Minutes, Hours), the operation is on the instant timeline, where a Duration of the specified amount is added or subtracted.

With the date units (Days, Weeks, Months, Years), the operation is on the local timeline, adding and subtracting the amount from the date-time corresponding to this zoned date-time.

Click here to view code image

ZonedDateTime plus(long amountToAdd, TemporalUnit unit)
ZonedDateTime minus(long amountToSubtract, TemporalUnit unit)
boolean isSupported(TemporalUnit unit)

Return a copy of this zoned date-time with the specified amount added or subtracted, respectively, according to the TemporalUnit specified. The methods support all ChronoUnit enum constants, except FORVER (p. 1044).

Time units operate on the instant timeline, but date units operate on the local timeline, when performing these operations.

The isSupported() method checks if the specified TemporalUnit is supported by this zoned date-time (p. 1044).

Click here to view code image

ZonedDateTime plus(TemporalAmount amountToAdd)
ZonedDateTime minus(TemporalAmount amountToSubtract)

Return a copy of this zoned date-time with the specified temporal amount added or subtracted, respectively. The classes Period (p. 1057) and Duration (p. 1064) implement the TemporalAmount interface.

If the amount is a Duration, the time units operate on the instant timeline. However, if the amount is a Period, the date units operate on the local timeline.

Click here to view code image

long until(Temporal endExclusive, TemporalUnit unit)

Calculates the amount of time between this zoned date-time and the specified temporal object in terms of the specified TemporalUnit (p. 1044). The end point is excluded. An exception is thrown if the specified temporal object cannot be converted to a zoned date-time.

This method supports all ChronoUnit enum constants, except FORVER (p. 1044).

Date units operate on the local timeline, but time units operate on the instant timeline when performing this operation.

Leave a Reply

Your email address will not be published. Required fields are marked *