Sub topic 8
Time Zones and Offsets: ZoneId and ZoneOffset
The types you have met so far, such as LocalDateTime, have no idea where in the world they are. "9:00 on July 1" happens at a different moment in London than in Tokyo. To pin a time to a real moment, Java needs to know the place. Two classes describe that place: ZoneId and ZoneOffset.
What Is an Offset?
An offset is a fixed difference from UTC. UTC is the world's reference time. For example, +02:00 means "two hours ahead of UTC." An offset never changes.
ZoneOffset is the Java class for an offset. ZoneOffset.UTC is the offset of zero.
What Is a Time Zone?
A time zone is a region that follows the same clock rules, such as Europe/Paris. Its offset can change during the year, mostly because of daylight saving time. Paris uses +01:00 in winter and +02:00 in summer.
ZoneId is the Java class for a time zone. Each zone has a name in the form Region/City. These names come from the IANA time zone database, which is a public list of the world's clock rules that ships with the JDK.
A Worked Example
This program creates a zone and some offsets, then asks the zone which offset it uses on two different dates.
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class ZonesAndOffsets {
public static void main(String[] args) {
ZoneId paris = ZoneId.of("Europe/Paris");
ZoneOffset plusTwo = ZoneOffset.ofHours(2);
ZoneOffset india = ZoneOffset.ofHoursMinutes(5, 30);
System.out.println(paris); // Europe/Paris
System.out.println(plusTwo); // +02:00
System.out.println(india); // +05:30
System.out.println(ZoneOffset.UTC); // Z
Instant winter = Instant.parse("2024-01-15T12:00:00Z");
Instant summer = Instant.parse("2024-07-15T12:00:00Z");
System.out.println(paris.getRules().getOffset(winter)); // +01:00
System.out.println(paris.getRules().getOffset(summer)); // +02:00
System.out.println(ZoneId.systemDefault()); // depends on your computer
}
}Some notes on the code:
ZoneId.oftakes a zone name. If you misspell it, Java throws aZoneRulesException.getRules()returns the zone's rule set.getOffset(instant)tells you which offset applies at that moment.ZoneId.systemDefault()reads the zone your computer is set to, so its output differs between machines.- UTC prints as
Z, which is short for "Zulu," the military name for UTC.
Choosing Between Them
| Use | When |
|---|---|
| `ZoneId` such as `Europe/Paris` | You care about a real place, such as meetings, alarms, or opening hours |
| `ZoneOffset` such as `+02:00` | You only need a fixed difference from UTC, for example when storing a timestamp |
The next chapter uses these classes to build a ZonedDateTime and convert times between zones.
Exercise
Why does ZoneId.of("Europe/Paris") give +01:00 for one date and +02:00 for another, while ZoneOffset.ofHours(1) always gives +01:00?
Show answer
A ZoneId holds rules that change the offset during the year, such as daylight saving time. A ZoneOffset is one fixed difference from UTC and never changes.
Exercise
Write one line of Java that creates a ZoneOffset for nine hours ahead of UTC. What does it print?
Show answer
ZoneOffset tokyoOffset = ZoneOffset.ofHours(9); printing it shows +09:00.