Java : ZoneId with Examples

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


Summary

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

Class diagram

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

final var dateTime = ZonedDateTime.of(
        LocalDate.of(2022, 11, 10),
        LocalTime.of(0, 0),
        zoneId);

System.out.println(dateTime); // 2022-11-10T00:00-08:00[America/Los_Angeles]

Fields

static final Map<String,String> SHORT_IDS

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

//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
ZoneId.SHORT_IDS.entrySet().stream()
        .sorted(Map.Entry.comparingByKey())
        .forEach(System.out::println);

Methods

boolean equals (Object obj)

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

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

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

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

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

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

System.out.println(zoneId1.equals(zoneId2)); // false
System.out.println(zoneId1.equals(zoneId3)); // false
System.out.println(zoneId1.equals(zoneId3)); // false

static ZoneId from (TemporalAccessor temporal)

Obtains an instance of ZoneId from a temporal object.

final var temporal = ZonedDateTime.of(2021, 4, 7, 12, 30, 45, 0, ZoneId.systemDefault());
System.out.println(temporal); // 2021-04-07T12:30:45-07:00[America/Los_Angeles]

final var zoneId = ZoneId.from(temporal);
System.out.println(zoneId); // America/Los_Angeles
final var temporal = LocalDateTime.of(2021, 4, 7, 12, 30);
System.out.println(temporal); // 2021-04-07T12:30

//ZoneId.from(temporal); // DateTimeException: Unable to obtain ZoneId from TemporalAccessor

static Set<String> getAvailableZoneIds ()

Gets the set of available zone IDs.

//Africa/Abidjan
//Africa/Accra
//Africa/Addis_Ababa
//Africa/Algiers
//...
//America/Los_Angeles
//...
//Asia/Tokyo
//...
//GMT
//...
//UTC
//...
ZoneId.getAvailableZoneIds().stream().sorted().forEach(System.out::println);

String getDisplayName (TextStyle style, Locale locale)

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

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

System.out.println(zoneId.getDisplayName(TextStyle.FULL, Locale.ENGLISH)); // Pacific Time
System.out.println(zoneId.getDisplayName(TextStyle.SHORT, Locale.ENGLISH)); // PT
System.out.println(zoneId.getDisplayName(TextStyle.NARROW, Locale.ENGLISH)); // America/Los_Angeles

abstract String getId ()

Gets the unique time-zone ID.

System.out.println(ZoneId.of("America/Los_Angeles").getId()); // America/Los_Angeles
System.out.println(ZoneId.ofOffset("GMT", ZoneOffset.ofHours(12)).getId()); // GMT+12:00

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("Europe/Paris").getRules();
System.out.println(rules); // ZoneRules[currentStandardOffset=+01:00]
final var rules = ZoneId.ofOffset("GMT", ZoneOffset.ofHours(12)).getRules();
System.out.println(rules); // ZoneRules[currentStandardOffset=+12:00]

int hashCode ()

A hash code for this time-zone ID.

System.out.println(ZoneId.of("America/Los_Angeles").hashCode()); // -1536188513
System.out.println(ZoneId.ofOffset("GMT", ZoneOffset.ofHours(12)).hashCode()); // 90912348

ZoneId normalized ()

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

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

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("+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("UT")); // UT
System.out.println(ZoneId.of("GMT")); // GMT
System.out.println(ZoneId.of("UTC+09:00")); // UTC+09:00

//ZoneId.of("A"); // DateTimeException: Invalid ID for ZoneOffset, invalid format: A
//ZoneId.of("PST"); // ZoneRulesException: Unknown time-zone ID: PST

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.

System.out.println(ZoneId.of("America/Los_Angeles")); // America/Los_Angeles
//ZoneId.of("PST"); // Unknown time-zone ID: PST

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

static ZoneId ofOffset (String prefix, ZoneOffset offset)

Obtains an instance of ZoneId wrapping an offset.

System.out.println(ZoneId.ofOffset("UTC", ZoneOffset.ofHours(4))); // UTC+04:00
System.out.println(ZoneId.ofOffset("GMT", ZoneOffset.ofHours(-3))); // GMT-03:00
System.out.println(ZoneId.ofOffset("UT", ZoneOffset.of("+09:30:15"))); // UT+09:30:15

static ZoneId systemDefault ()

Gets the system default time-zone.

System.out.println(ZoneId.systemDefault()); // 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