Sub topic 9
ZonedDateTime and Converting Between Zones
A LocalDateTime says "9:00 on July 1" but does not say where. A meeting at 9:00 in New York and a meeting at 9:00 in Tokyo happen at different moments. To pin a date and time to one exact moment, you need a time zone. In this chapter you will join a date-time to a zone, read its parts, and convert it to other zones.
What ZonedDateTime Is
A ZonedDateTime is a date, a time, and a time zone held together in one object. A time zone is a named region, such as America/New_York, that follows one set of clock rules. You met these names as ZoneId values in the last chapter.
A ZonedDateTime holds three things:
- A local date-time: the date and time a wall clock shows in that place.
- A zone: the
ZoneId, such asEurope/London. - An offset: how far that place's clock is ahead of or behind UTC at that moment, such as
+01:00. UTC is the world's reference time.
Java works out the offset for you from the zone's rules. In summer, New York's offset is -04:00. In winter it is -05:00. You only supply the zone name.
Like every java.time type, ZonedDateTime is immutable. Each change gives you a new object, and you must keep the result.
Creating a ZonedDateTime
You can create a ZonedDateTime in several ways. The program below shows the common ones.
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class CreateZoned {
public static void main(String[] args) {
ZoneId paris = ZoneId.of("Europe/Paris");
// 1. From year, month, day, hour, minute, second, nanosecond, zone
ZonedDateTime a = ZonedDateTime.of(2024, 7, 1, 9, 0, 0, 0, paris);
// 2. From a LocalDateTime plus a zone
ZonedDateTime b = LocalDateTime.of(2024, 7, 1, 9, 0).atZone(paris);
// 3. From a LocalDate, a LocalTime, and a zone
ZonedDateTime c = ZonedDateTime.of(LocalDate.of(2024, 7, 1), LocalTime.of(9, 0), paris);
// 4. By parsing text in the standard format
ZonedDateTime d = ZonedDateTime.parse("2024-07-01T09:00+02:00[Europe/Paris]");
System.out.println(a); // 2024-07-01T09:00+02:00[Europe/Paris]
System.out.println(a.equals(b) && b.equals(c) && c.equals(d)); // true
// 5. The current moment in a chosen zone (output changes every run)
System.out.println(ZonedDateTime.now(paris));
}
}Look at the printed form: 2024-07-01T09:00+02:00[Europe/Paris]. It shows the local date-time, then the offset, then the zone name in square brackets. This is the standard ISO-8601 format, with the zone name added on the end.
Reading the Parts
You can read each part with a getter method. A getter is a method that returns a value stored in the object.
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ReadParts {
public static void main(String[] args) {
ZonedDateTime t = ZonedDateTime.of(2024, 7, 1, 9, 0, 0, 0, ZoneId.of("America/New_York"));
System.out.println(t.getYear()); // 2024
System.out.println(t.getMonth()); // JULY
System.out.println(t.getHour()); // 9
System.out.println(t.getZone()); // America/New_York
System.out.println(t.getOffset()); // -04:00
System.out.println(t.toLocalDate()); // 2024-07-01
System.out.println(t.toLocalDateTime()); // 2024-07-01T09:00
System.out.println(t.toInstant()); // 2024-07-01T13:00:00Z
}
}The toInstant() call gives the exact point on the timeline, written in UTC. New York is 4 hours behind UTC in July, so 09:00 there is 13:00 UTC.
Converting Between Zones
This is the main job of ZonedDateTime. You have two methods that look alike, and they do very different things.
withZoneSameInstant(zone)keeps the same moment and changes the clock reading. Use it to answer "what time is it there right now?"withZoneSameLocal(zone)keeps the same clock reading and changes the moment. Use it when you mean "9:00 wherever I am."
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"));
ZonedDateTime london = newYork.withZoneSameLocal(ZoneId.of("Europe/London"));
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
System.out.println(london); // 2024-07-01T09:00+01:00[Europe/London]
}
}The Tokyo value shows 22:00, but it is the same moment as 09:00 in New York. The London value shows 09:00, but it is a different moment, five hours earlier.
| Method | Clock reading | Moment on timeline | Typical use |
|---|---|---|---|
| `withZoneSameInstant` | Changes | Stays the same | Show a meeting time to people in other zones |
| `withZoneSameLocal` | Stays the same | Changes | Move an alarm "at 7:00" to a new home zone |
A Worked Example: Flight Arrival Time
A flight leaves New York at 18:30 on July 1 and takes 7 hours 10 minutes. What does the clock in London show when it lands?
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Flight {
public static void main(String[] args) {
ZoneId ny = ZoneId.of("America/New_York");
ZoneId london = ZoneId.of("Europe/London");
ZonedDateTime departure = ZonedDateTime.of(2024, 7, 1, 18, 30, 0, 0, ny);
ZonedDateTime arrivalNy = departure.plusHours(7).plusMinutes(10);
ZonedDateTime arrivalLondon = arrivalNy.withZoneSameInstant(london);
System.out.println(departure); // 2024-07-01T18:30-04:00[America/New_York]
System.out.println(arrivalNy); // 2024-07-02T01:40-04:00[America/New_York]
System.out.println(arrivalLondon); // 2024-07-02T06:40+01:00[Europe/London]
}
}The steps are: start in the departure zone, add the flight time, then convert to the arrival zone. The plane lands at 06:40 London time on July 2.
Comparing ZonedDateTime Values
Be careful when you compare two values. The equals method checks the date-time, the offset, and the zone. The isEqual method checks only the moment on the timeline.
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Compare {
public static void main(String[] args) {
ZonedDateTime ny = ZonedDateTime.of(2024, 7, 1, 9, 0, 0, 0, ZoneId.of("America/New_York"));
ZonedDateTime tokyo = ny.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
System.out.println(ny.equals(tokyo)); // false: different zones
System.out.println(ny.isEqual(tokyo)); // true: same moment
System.out.println(ny.isBefore(tokyo)); // false
System.out.println(ny.isAfter(tokyo)); // false
}
}Use isEqual, isBefore, and isAfter when you care about time order. Use equals only when the zone must match too.
Edge Cases to Know
- Offsets change during the year. The same zone can print
-05:00in January and-04:00in July. Never hard-code an offset when you mean a place. Use the zone name. - Adding days and adding hours can differ.
plusDays(1)keeps the same clock time on the next day.plusHours(24)adds exactly 24 hours. On a day when clocks change, these two results differ. The chapter on daylight saving time covers this in detail. - Zone names must be valid.
ZoneId.of("New York")throws aZoneRulesException(a missing zone name) or aDateTimeException(a badly formed one). Use theRegion/Cityform. - Dates can change when you convert. In the Tokyo example, the date stayed July 1. Convert 20:00 in New York and Tokyo shows the next day. Always read the date again after converting.
Exercise
A webinar starts at 2024-07-01T09:00 in Asia/Tokyo. Write the code that prints the start time in America/Los_Angeles, and give the printed result.
Show answer
ZonedDateTime tokyo = ZonedDateTime.of(2024, 7, 1, 9, 0, 0, 0, ZoneId.of("Asia/Tokyo")); System.out.println(tokyo.withZoneSameInstant(ZoneId.of("America/LosAngeles"))); It prints 2024-06-30T17:00-07:00[America/LosAngeles]. Tokyo is UTC+9, so the moment is 00:00 UTC on July 1. Los Angeles is UTC-7 in summer, which gives 17:00 on June 30.
Exercise
You have ZonedDateTime a at 09:00 in Europe/Paris and b = a.withZoneSameInstant(ZoneId.of("UTC")). What do a.equals(b) and a.isEqual(b) return, and why?
Show answer
a.equals(b) returns false because the zones and offsets differ. a.isEqual(b) returns true because both refer to the same moment on the timeline.
Exercise
A user in New York sets a daily reminder for 08:00. They move to Berlin and want it to stay at 08:00 on their new local clock. Which method should you call, withZoneSameInstant or withZoneSameLocal?
Show answer
withZoneSameLocal(ZoneId.of("Europe/Berlin")). It keeps the clock reading of 08:00 and moves the reminder to Berlin's zone. withZoneSameInstant would set the reminder to 14:00 Berlin time.