Sub topic 9
ZonedDateTime and Converting Between Zones
A LocalDateTime holds a date and a clock time, such as "July 1, 9:00". It does not know where in the world that time applies. 9:00 in New York and 9:00 in Tokyo are different moments, 13 hours apart.
A ZonedDateTime is a date and time together with a time zone. A time zone is a named region, such as America/New_York, whose clocks follow the same rules. Because it carries its zone, a ZonedDateTime points to one exact moment on the timeline.
Creating a ZonedDateTime
The usual way to make one is to start with a LocalDateTime and attach a zone using atZone. A ZoneId is the object that names a time zone, and you create it with ZoneId.of.
The example below schedules a meeting in New York and then finds the matching time in Tokyo.
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Zones {
public static void main(String[] args) {
LocalDateTime meeting = LocalDateTime.of(2024, 7, 1, 9, 0);
ZonedDateTime newYork = meeting.atZone(ZoneId.of("America/New_York"));
ZonedDateTime tokyo = newYork.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
System.out.println(newYork); // 2024-07-01T09:00-04:00[America/New_York]
System.out.println(tokyo); // 2024-07-01T22:00+09:00[Asia/Tokyo]
System.out.println(newYork.toInstant()); // 2024-07-01T13:00:00Z
}
}The printed value has three parts:
2024-07-01T09:00is the local date and time.-04:00is the offset. The offset is how far that zone's clock is from UTC on this date.[America/New_York]is the zone ID.
Converting to another zone
The withZoneSameInstant method keeps the moment the same and changes the zone. Java works out the new clock time for you. At 9:00 in New York, clocks in Tokyo show 22:00. The toInstant() call shows that both values describe the same moment: 13:00 UTC.
Same instant versus same local time
A second method, withZoneSameLocal, does something different. It keeps the clock time and swaps the zone, so the result is a different moment.
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class SameLocal {
public static void main(String[] args) {
ZonedDateTime ny = ZonedDateTime.of(2024, 7, 1, 9, 0, 0, 0, ZoneId.of("America/New_York"));
ZoneId london = ZoneId.of("Europe/London");
System.out.println(ny.withZoneSameInstant(london)); // 2024-07-01T14:00+01:00[Europe/London]
System.out.println(ny.withZoneSameLocal(london)); // 2024-07-01T09:00+01:00[Europe/London]
}
}| Method | Keeps | Changes | Use it when |
|---|---|---|---|
| `withZoneSameInstant` | The moment | The clock time | You want the matching time somewhere else |
| `withZoneSameLocal` | The clock time | The moment | You entered the wrong zone and want to fix it |
Exercise
A call starts at 2024-07-01T16:00 in Asia/Tokyo. Write the line that finds the start time in America/New_York, and say what it prints.
Show answer
tokyo.withZoneSameInstant(ZoneId.of("America/NewYork")) prints 2024-07-01T03:00-04:00[America/NewYork]. Tokyo is 13 hours ahead of New York in July, so 16:00 in Tokyo is 03:00 the same day in New York.
Exercise
You call withZoneSameLocal instead of withZoneSameInstant to convert 9:00 New York to London. What time does the result show, and is it the same moment as the original?
Show answer
It shows 09:00 in London. It is not the same moment. 09:00 in London is five hours earlier than 09:00 in New York.