Creating Modified Copies of Zoned Date-Time

Individual fields of a zoned date-time can be set to a new value in a copy of a zoned date-time as illustrated by the following code.

Click here to view code image

ZonedDateTime theZDT = concertZDT0         // 1973-01-14T00:10-10:00[US/Hawaii]
    .withYear(1977)                        // 1977-01-14T00:10-10:00[US/Hawaii]
    .with(ChronoField.MONTH_OF_YEAR, 8)    // 1977-08-14T00:10-10:00[US/Hawaii]
    .withDayOfMonth(16)                    // 1977-08-16T00:10-10:00[US/Hawaii]
    .with(ChronoField.HOUR_OF_DAY, 9)      // 1977-08-16T09:10-10:00[US/Hawaii]
    .with(ChronoField.MINUTE_OF_HOUR, 30); // 1977-08-16T09:30-10:00[US/Hawaii]

The withField() methods behave analogous to the with() method with a corresponding field argument, taking into consideration the time gap and the time overlap that occur when daylight saving time starts and ends (p. 1082).

The withZoneSameLocal() method can be used to change the time zone, while retaining the date-time. The code at (1) below changes the time zone from US/Hawaii to US/Central, while retaining the date-time. Note that the two zoned date-times, theZDT and zdtSameLocal, do not represent the same instant according to UTC/ Greenwich, as shown by the output from the print statements.

Click here to view code image

ZoneId cTZ = ZoneId.of(“US/Central”);
ZonedDateTime zdtSameLocal = theZDT.withZoneSameLocal(cTZ);            // (1)
System.out.printf(“%23s %25s%n”, “ZonedDateTime”, “Instant”);
System.out.printf(“%-35s %s%n”, theZDT, theZDT.toInstant());
System.out.printf(“%-35s %s%n”, zdtSameLocal, zdtSameLocal.toInstant());

Output from the print statements:

Click here to view code image

          ZonedDateTime                   Instant
1977-08-16T09:30-10:00[US/Hawaii]   1977-08-16T19:30:00Z
1977-08-16T09:30-05:00[US/Central]  1977-08-16T14:30:00Z

The local time 09:30 in the US/Hawaii time zone is 10 hours (offset -10:00) behind UTC/Greenwich, whereas the same local time in the US/Central time zone (offset -05:00) is 5 hours behind UTC/Greenwich on the date 1977-08-16, resulting in different instants at UTC/Greenwich for the same local time.

The withZoneSameInstant() method can be used to change the time zone and adjust the date-time to the new time zone. We see in the code below at (2) that the adjusted local time 14:30 in the US/Central time zone is 5 hours behind UTC/Greenwich, resulting in both zoned date-times representing the same instant at UTC/Greenwich.

Click here to view code image

ZonedDateTime zdtSameInstant = theZDT.withZoneSameInstant(cTZ);        // (2)
System.out.printf(“%23s %25s%n”, “ZonedDateTime”, “Instant”);
System.out.printf(“%-35s %s%n”, theZDT, theZDT.toInstant());
System.out.printf(“%-35s %s%n”, zdtSameInstant, zdtSameInstant.toInstant());

Output from the print statements:

Click here to view code image

          ZonedDateTime                   Instant
1977-08-16T09:30-10:00[US/Hawaii]   1977-08-16T19:30:00Z
1977-08-16T14:30-05:00[US/Central]  1977-08-16T19:30:00Z

Click here to view code image

ZonedDateTime with
FIELD
(int amount)

Returns a copy of this zoned date-time with the field designated by the suffix FIELD set to the specified value, where the suffix FIELD can be DayOfMonth, DayOfYear, MonthValue, Nano, Second, Minute, Hour, or Year.

Note that these methods take into consideration the time gap and the time overlap that can occur due to daylight savings (p. 1082).

Click here to view code image

ZonedDateTime with(TemporalField field, long newValue)

Returns a copy of this zoned date-time with the specified TemporalField set to the specified value. All constants of the ChronoField enum type (p. 1046) can be used to specify a particular field.

Click here to view code image

ZonedDateTime withZoneSameLocal(ZoneId zone)
ZonedDateTime withZoneSameInstant(ZoneId zone)

The first method returns a copy of this zoned date-time with the specified time zone, but normally retaining the date-time.

The second method returns a copy of this zoned date-time with the specified time zone, but retaining the instant; that is, it results in the date-time being adjusted according to the specified time zone.

Leave a Reply

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