Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

ZoneId — a time-zone identifier

ZoneId identifies a time zone, while ZoneRules describes its UTC-offset rules. Learn ZoneId.of, ZoneId.getRules, and the difference between a region and a fixed offset.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

ZoneId zone = ZoneId.of("+02:00");
ZoneRules rules = zone.getRules();

java.time.ZoneId identifies a time zone; it does not store a date or time. ZoneId.of(String) creates a region identifier, such as Europe/Paris, or a fixed-offset identifier, such as +02:00. A region is not the same as a fixed offset.

ZoneId.getRules() returns ZoneRules. Its getOffset(Instant) method supplies the offset for a particular instant, while isFixedOffset() checks whether that offset never changes. Regional rules may include clock changes and depend on the data available in the runtime.

An Instant has one applicable offset. When converting a LocalDateTime, however, an offset transition can create a gap or an overlap in local times. getValidOffsets(LocalDateTime) helps identify these cases; do not assume that a local time always identifies one instant.

An invalid format passed to ZoneId.of causes DateTimeException, while an unknown region causes ZoneRulesException. ZoneId.getRules can also throw ZoneRulesException if rules are unavailable. The example uses a fixed offset and a specified instant so that its output does not depend on the clock or regional data updates.

Examples

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

public class Main {
    public static void main(String[] args) {
        ZoneId zone = ZoneId.of("+02:00");
        ZoneRules rules = zone.getRules();
        Instant instant = Instant.parse("2024-01-15T12:00:00Z");

        System.out.println(zone.getId());
        System.out.println(rules.isFixedOffset());
        System.out.println(rules.getOffset(instant));
    }
}

Result

+02:00
true
+02:00

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources