Java : ZoneId with Examples

ZoneId (Java SE 21 & JDK 21) with Examples.
You will find code examples on most ZoneId methods.


Summary

A time-zone ID, such as Europe/Paris.

Class diagram

final var locale = Locale.getDefault();
System.out.println(locale.toLanguageTag()); // en-US

final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles

final var dateTime = ZonedDateTime.of(
        LocalDate.of(2100, 1, 2),
        LocalTime.of(0, 0),
        zone);

System.out.println(dateTime); // 2100-01-02T00:00-08:00[America/Los_Angeles]
final var zone1 = ZoneId.of("America/Los_Angeles");
System.out.println(zone1); // America/Los_Angeles

final var zone2 = ZoneId.of("PST", ZoneId.SHORT_IDS);
System.out.println(zone2); // America/Los_Angeles

final var zone3 = ZoneId.of("Asia/Tokyo");
System.out.println(zone3); // Asia/Tokyo

Fields

static final Map<String,String> SHORT_IDS

A map of zone overrides to enable the short time-zone names to be used.

System.out.println("-- short ids --");
ZoneId.SHORT_IDS.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(
        entry -> {
            System.out.println(entry);
        }
);

// Result
// ↓
//-- short ids --
//ACT=Australia/Darwin
//AET=Australia/Sydney
//AGT=America/Argentina/Buenos_Aires
//ART=Africa/Cairo
//AST=America/Anchorage
//BET=America/Sao_Paulo
//BST=Asia/Dhaka
//CAT=Africa/Harare
//CNT=America/St_Johns
//CST=America/Chicago
//CTT=Asia/Shanghai
//EAT=Africa/Addis_Ababa
//ECT=Europe/Paris
//EST=-05:00
//HST=-10:00
//IET=America/Indiana/Indianapolis
//IST=Asia/Kolkata
//JST=Asia/Tokyo
//MIT=Pacific/Apia
//MST=-07:00
//NET=Asia/Yerevan
//NST=Pacific/Auckland
//PLT=Asia/Karachi
//PNT=America/Phoenix
//PRT=America/Puerto_Rico
//PST=America/Los_Angeles
//SST=Pacific/Guadalcanal
//VST=Asia/Ho_Chi_Minh

Methods

boolean equals (Object obj)

Checks if this time-zone ID is equal to another time-zone ID.

final var zone1 = ZoneId.of("America/Los_Angeles");
System.out.println(zone1); // America/Los_Angeles

final var zone2 = ZoneId.of("PST", ZoneId.SHORT_IDS);
System.out.println(zone2); // America/Los_Angeles

System.out.println(zone1.equals(zone2)); // true
final var zone1 = ZoneId.ofOffset("GMT", ZoneOffset.ofHours(12));
System.out.println(zone1); // GMT+12:00

final var zone2 = ZoneId.ofOffset("GMT", ZoneOffset.ofHours(3));
System.out.println(zone2); // GMT+03:00

final var zone3 = ZoneId.of("GMT");
System.out.println(zone3); // GMT

System.out.println(zone1.equals(zone2)); // false
System.out.println(zone1.equals(zone3)); // false
System.out.println(zone1.equals(zone3)); // false

static ZoneId from (TemporalAccessor temporal)

Obtains an instance of ZoneId from a temporal object.

final var zone = ZoneId.of("Etc/GMT+1");
System.out.println(zone); // Etc/GMT+1

final var temporal = ZonedDateTime.of(2100, 4, 7, 12, 30, 45, 0, zone);
System.out.println(temporal); // 2100-04-07T12:30:45-01:00[Etc/GMT+1]

System.out.println(ZoneId.from(temporal)); // Etc/GMT+1

static Set<String> getAvailableZoneIds ()

Gets the set of available zone IDs.

System.out.println("-- available ids --");
ZoneId.getAvailableZoneIds().stream().sorted().forEach(id -> {
    System.out.println(id);
});

// Result
// ↓
//-- available ids --
//Africa/Abidjan
//Africa/Accra
//Africa/Addis_Ababa
//Africa/Algiers
//Africa/Asmara
//Africa/Asmera
//Africa/Bamako
// .
// .
// .

String getDisplayName (TextStyle style, Locale locale)

Gets the textual representation of the zone, such as 'British Time' or '+02:00'.

final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles

final var ret1 = zone.getDisplayName(TextStyle.FULL, Locale.ENGLISH);
System.out.println(ret1); // Pacific Time

final var ret2 = zone.getDisplayName(TextStyle.SHORT, Locale.ENGLISH);
System.out.println(ret2); // PT

final var ret3 = zone.getDisplayName(TextStyle.NARROW, Locale.ENGLISH);
System.out.println(ret3); // America/Los_Angeles

abstract String getId ()

Gets the unique time-zone ID.

final var zone1 = ZoneId.of("Etc/GMT+1");
System.out.println(zone1.getId()); // Etc/GMT+1

final var zone2 = ZoneId.of("PST", ZoneId.SHORT_IDS);
System.out.println(zone2.getId()); // America/Los_Angeles

abstract ZoneRules getRules ()

Gets the time-zone rules for this ID allowing calculations to be performed.

final var rules = ZoneId.of("America/Los_Angeles").getRules();
System.out.println(rules); // ZoneRules[currentStandardOffset=-08:00]
final var rules = ZoneId.of("Asia/Tokyo").getRules();
System.out.println(rules); // ZoneRules[currentStandardOffset=+09:00]

int hashCode ()

A hash code for this time-zone ID.

final var zone1 = ZoneId.of("Etc/GMT+1");
System.out.println(zone1.hashCode()); // -775703111

final var zone2 = ZoneId.of("PST", ZoneId.SHORT_IDS);
System.out.println(zone2.hashCode()); // -1536188513

ZoneId normalized ()

Normalizes the time-zone ID, returning a ZoneOffset where possible.

final var zone = ZoneId.ofOffset("GMT", ZoneOffset.ofHours(12));
System.out.println(zone); // GMT+12:00
System.out.println(zone.normalized()); // +12:00
final var zone = ZoneId.of("America/Los_Angeles");
System.out.println(zone); // America/Los_Angeles
System.out.println(zone.normalized()); // America/Los_Angeles

static ZoneId of (String zoneId)

Obtains an instance of ZoneId from an ID ensuring that the ID is valid and available for use.

System.out.println(ZoneId.of("America/Los_Angeles")); // America/Los_Angeles
System.out.println(ZoneId.of("Asia/Tokyo")); // Asia/Tokyo
System.out.println(ZoneId.of("+02:00")); // +02:00
System.out.println(ZoneId.of("-12:00")); // -12:00
System.out.println(ZoneId.of("Z")); // Z
System.out.println(ZoneId.of("UTC")); // UTC
System.out.println(ZoneId.of("GMT")); // GMT
System.out.println(ZoneId.of("UTC+09:00")); // UTC+09:00

static ZoneId of (String zoneId, Map<String,String> aliasMap)

Obtains an instance of ZoneId using its ID using a map of aliases to supplement the standard zone IDs.

final var zone1 = ZoneId.of("PST", ZoneId.SHORT_IDS);
System.out.println(zone1); // America/Los_Angeles

final var zone2 = ZoneId.of("JST", ZoneId.SHORT_IDS);
System.out.println(zone2); // Asia/Tokyo
try {
    final var zone = ZoneId.of("JST");
} catch (ZoneRulesException e) {
    System.out.println("ZoneRulesException! : " + e.getMessage());
}

// Result
// ↓
//ZoneRulesException! : Unknown time-zone ID: JST

static ZoneId ofOffset (String prefix, ZoneOffset offset)

Obtains an instance of ZoneId wrapping an offset.

final var zone1 = ZoneId.ofOffset("UTC", ZoneOffset.ofHours(4));
System.out.println(zone1); // UTC+04:00

final var zone2 = ZoneId.ofOffset("GMT", ZoneOffset.ofHours(-3));
System.out.println(zone2); // GMT-03:00

final var zone3 = ZoneId.ofOffset("UT", ZoneOffset.of("+09:30:15"));
System.out.println(zone3); // UT+09:30:15

static ZoneId systemDefault ()

Gets the system default time-zone.

final var locale = Locale.getDefault();
System.out.println(locale.toLanguageTag()); // en-US

final var zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles

String toString ()

Outputs this zone as a String, using the ID.

final var str1 = ZoneId.of("America/Los_Angeles").toString();
System.out.println(str1); // America/Los_Angeles

final var str2 = ZoneId.ofOffset("GMT", ZoneOffset.ofHours(12)).toString();
System.out.println(str2); // GMT+12:00

Related posts

To top of page