ZoneId (Java SE 25 & JDK 25)
Class ZoneId java.lang.Object java.time.ZoneId All Implemented Interfaces: Serializable Direct Known Subclasses: ZoneOffset public abstract sealed class ZoneId extends Object implements Serializable permits ZoneOffset (not exhaustive) A time-zone ID, such as Europe/Paris . A ZoneId is used to identify the rules used to convert between an Instant and a LocalDateTime . There are two distinct types of ID: Fixed offsets - a fully resolved offset from UTC/Greenwich, that uses the same offset for all local date-times Geographical regions - an area where a specific set of rules for finding the offset from UTC/Greenwich apply Most fixed offsets are represented by ZoneOffset .
Calling normalized() on any ZoneId will ensure that a fixed offset ID will be represented as a ZoneOffset . The actual rules, describing when and how the offset changes, are defined by ZoneRules . This class is simply an ID used to obtain the underlying rules. This approach is taken because rules are defined by governments and change frequently, whereas the ID is stable. The distinction has other effects. Serializing the ZoneId will only send the ID, whereas serializing the rules sends the entire data set.
Similarly, a comparison of two IDs only examines the ID, whereas a comparison of two rules examines the entire data set. Time-zone IDs The ID is unique within the system. There are three types of ID. The simplest type of ID is that from ZoneOffset . This consists of 'Z' and IDs starting with '+' or '-'. The next type of ID are offset-style IDs with some form of prefix, such as 'GMT+2' or 'UTC+01:00'. The recognised prefixes are 'UTC', 'GMT' and 'UT'.
The offset is the suffix and will be normalized during creation. These IDs can be normalized to a ZoneOffset using normalized() . The third type of ID are region-based IDs. A region-based ID must be of two or more characters, and not start with 'UTC', 'GMT', 'UT' '+' or '-'. Region-based IDs are defined by configuration, see ZoneRulesProvider . The configuration focuses on providing the lookup from the ID to the underlying ZoneRules .
Time-zone rules are defined by governments and change frequently. There are a number of organizations, known here as groups, that monitor time-zone changes and collate them. The default group is the IANA Time Zone Database (TZDB). Other organizations include IATA (the airline industry body) and Microsoft. Each group defines its own format for the region ID it provides. The TZDB group defines IDs such as 'Europe/London' or 'America/New_York'.
TZDB IDs take precedence over other groups. It is strongly recommended that the group name is included in all IDs supplied by groups other than TZDB to avoid conflicts. For example, IATA airline time-zone region IDs are typically the same as the three letter airport code. However, the airport of Utrecht has the code 'UTC', which is obviously a conflict. The recommended format for region IDs from groups other than TZDB is 'group~region'.
Thus if IATA data were defined, Utrecht airport would be 'IATA~UTC'. Serialization This class can be serialized and stores the string zone ID in the external form. The ZoneOffset subclass uses a dedicated format that only stores the offset from UTC/Greenwich. A ZoneId can be deserialized in a Java Runtime where the ID is unknown. For example, if a server-side Java Runtime has been updated with a new zone ID, but the client-side Java Runtime has not been updated.
In this case, the ZoneId object will exist, and can be queried using getId , equals , hashCode , toString , getDisplayName and normalized . However, any call to getRules will fail with ZoneRulesException . This approach is designed to allow a ZonedDateTime to be loaded and queried, but not modified, on a Java Runtime with incomplete time-zone information. This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur.
For example, in a future release, synchronization may fail. The equals method should be used for comparisons. Implementation Requirements: This abstract sealed class permits two implementations, both of which are immutable and thread-safe. One implementation models region-based IDs, the other is ZoneOffset modelling offset-based IDs. This difference is visible in serialization. Sealed Class Hierarchy Graph: Since: 1.8 See Also: Serialized Form Field Summary Fields Modifier and Type Field Description static final Map < String , String > SHORT_IDS A map of zone overrides to enable the short time-zone names to be used.
Method Summary Modifier and Type Method Description boolean equals ( Object obj) Checks if this time-zone ID is equal to another time-zone ID. static ZoneId from ( TemporalAccessor temporal) Obtains an instance of ZoneId from a temporal object. static Set < String > getAvailableZoneIds () Gets the set of available zone IDs. String getDisplayName ( TextStyle style, Locale locale) Gets the textual representation of the zone, such as 'British Time' or '+02:00'. abstract String getId () Gets the unique time-zone ID. abstract ZoneRules getRules () Gets the time-zone rules for this ID allowing calculations to be performed. int hashCode () A hash code for this time-zone ID.
ZoneId normalized () Normalizes the time-zone ID, returning a ZoneOffset where possible. static ZoneId of ( String zoneId) Obtains an instance of ZoneId from an ID ensuring that the ID is valid and available for use. 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. static ZoneId ofOffset ( String prefix, ZoneOffset offset) Obtains an instance of ZoneId wrapping an offset. static ZoneId systemDefault () Gets the system default time-zone.
String toString () Outputs this zone as a String , using the ID. Methods declared in class Object clone , finalize , getClass , notify , notifyAll , wait , wait , wait Field Details SHORT_IDS public static final Map < String , String > SHORT_IDS A map of zone overrides to enable the short time-zone names to be used. Use of short zone IDs has been deprecated in java.util.TimeZone . This map allows the IDs to continue to be used via the of(String, Map) factory method.
This map contains a mapping of the IDs that is in line with TZDB 2024b and later, where 'EST', 'MST' and 'HST' map to IDs which do not include daylight savings since 1970. This mapping may change in update releases in support of new versions of TZDB. This maps as follows: 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 - America/Panama HST - Pacific/Honolulu IET - America/Indiana/Indianapolis IST - Asia/Kolkata JST - Asia/Tokyo MIT - Pacific/Apia MST - America/Phoenix 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 The map is unmodifiable.
Method Details systemDefault public static ZoneId systemDefault () Gets the system default time-zone. This queries TimeZone.getDefault() to find the default time-zone and converts it to a ZoneId . If the system default time-zone is changed, then the result of this method will also change. Returns: the zone ID, not null Throws: DateTimeException - if the converted zone ID has an invalid format ZoneRulesException - if the converted zone region ID cannot be found getAvailableZoneIds public static Set < String > getAvailableZoneIds () Gets the set of available zone IDs.
This set includes the string form of all available region-based IDs. Offset-based zone IDs are not included in the returned set. The ID can be passed to of(String) to create a Zon
The rest of this page is on the live site.