Java : ZoneId with Examples
ZoneId (Java SE 18 & JDK 18) with Examples.
You will find code examples on most ZoneId methods.
Summary
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
//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)
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)
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 ()
//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)
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 ()
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 ()
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 ()
System.out.println(ZoneId.of("America/Los_Angeles").hashCode()); // -1536188513
System.out.println(ZoneId.ofOffset("GMT", ZoneOffset.ofHours(12)).hashCode()); // 90912348
ZoneId normalized ()
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)
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)
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)
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 ()
System.out.println(ZoneId.systemDefault()); // America/Los_Angeles
String toString ()
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
- Get the current time
- Basics of Date and Time
- Don't use the legacy Date and Calendar classes, use new APIs instead
- API Examples
- Calendar
- ChronoLocalDate
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneOffset