Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

ZoneId.of and ZoneId.getRules: time-zone rules

Look up the UTC offsets in Warsaw and New York at the same instant in 2025, and see why a fixed offset cannot replace a regional time zone.

Version: Java SE 21

Syntax

java.time.ZoneId
  public static ZoneId of(String zoneId)
  public static ZoneId of(String zoneId, java.util.Map<String, String> aliasMap)
  public abstract java.time.zone.ZoneRules getRules()

The static ZoneId.of(String) factory in java.time validates an ID and returns a ZoneId. Europe/Warsaw and America/New_York identify regional zones; +01:00 gives a ZoneOffset, a fixed offset from UTC. Two zones having the same offset at one instant does not mean they have the same rules.

The ZoneId.of(String, Map<String, String>) overload lets you supply your own mapping from aliases to zone IDs. Prefer region IDs to ambiguous abbreviations. An invalid format throws DateTimeException; a well-formed but unknown region ID throws ZoneRulesException, a subtype of DateTimeException.

The instance method ZoneId.getRules() returns an immutable java.time.zone.ZoneRules. getOffset(Instant) gives the one offset in effect at that instant. For a LocalDateTime, use getValidOffsets: it returns zero offsets in a gap when clocks jump forward, one outside a transition, and usually two in the repeated interval when clocks go back. In theory, more than two valid offsets are possible if clocks are set back twice in quick succession. In a gap or overlap, getOffset(LocalDateTime) gives the offset before the transition, which is not valid for the given local time in a gap.

Regional rules come from a ZoneRulesProvider, with IANA TZDB as the default data source. Historical results depend on the runtime's data. A provider that supports dynamic updates may return different rules on a later call to getRules(). If an ID is deserialized and its rules are unavailable, getRules() throws ZoneRulesException.

Examples

import java.time.Instant;
import java.time.ZoneId;
import java.time.zone.ZoneRules;

public class Main {
    public static void main(String[] args) {
        ZoneRules warsaw = ZoneId.of("Europe/Warsaw").getRules();
        ZoneRules newYork = ZoneId.of("America/New_York").getRules();
        ZoneRules fixed = ZoneId.of("+01:00").getRules();

        System.out.println("UTC instant | Europe/Warsaw | America/New_York | fixed +01:00");
        for (String timestamp : new String[] {
                "2025-03-01T12:00:00Z", "2025-03-15T12:00:00Z", "2025-04-01T12:00:00Z"
        }) {
            Instant instant = Instant.parse(timestamp);
            System.out.println(instant + " | " + warsaw.getOffset(instant)
                    + " | " + newYork.getOffset(instant) + " | " + fixed.getOffset(instant));
        }
    }
}

Result

UTC instant | Europe/Warsaw | America/New_York | fixed +01:00
2025-03-01T12:00:00Z | +01:00 | -05:00 | +01:00
2025-03-15T12:00:00Z | +01:00 | -04:00 | +01:00
2025-04-01T12:00:00Z | +02:00 | -04:00 | +01:00

Each row compares the same UTC instant. On March 15, 2025, New York is already on daylight saving time but Warsaw is not. By April 1 Warsaw has also changed offset; the fixed `+01:00` stays unchanged.

Related lessons

  • Java · Lesson 60 · Advanced · Version: 2

Sources