public abstract classZoneIdextendsObjectimplementsSerializable
Europe/Paris. AZoneId is used to identify the rules used to convert between anInstant and aLocalDateTime. There are two distinct types of ID:
ZoneOffset. Callingnormalized() on anyZoneId will ensure that a fixed offset ID will be represented as aZoneOffset. The actual rules, describing when and how the offset changes, are defined byZoneRules. 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 theZoneId 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.
The simplest type of ID is that fromZoneOffset. 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 aZoneOffset usingnormalized().
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, seeZoneRulesProvider. The configuration focuses on providing the lookup from the ID to the underlyingZoneRules.
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'.
ZoneOffset subclass uses a dedicated format that only stores the offset from UTC/Greenwich. AZoneId 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, theZoneId object will exist, and can be queried usinggetId,equals,hashCode,toString,getDisplayName andnormalized. However, any call togetRules will fail withZoneRulesException. This approach is designed to allow aZonedDateTime to be loaded and queried, but not modified, on a Java Runtime with incomplete time-zone information.
This is avalue-based class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances ofZoneId may have unpredictable results and should be avoided. Theequals method should be used for comparisons.
ZoneOffset modelling offset-based IDs. This difference is visible in serialization.| Modifier and Type | Field | Description |
|---|---|---|
staticMap<String,String> | SHORT_IDS | A map of zone overrides to enable the short time-zone names to be used. |
| Modifier and Type | Method | Description |
|---|---|---|
boolean | equals(Object obj) | Checks if this time-zone ID is equal to another time-zone ID. |
staticZoneId | from(TemporalAccessor temporal) | Obtains an instance of ZoneId from a temporal object. |
staticSet<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'. |
abstractString | getId() | Gets the unique time-zone ID. |
abstractZoneRules | 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. |
staticZoneId | of(String zoneId) | Obtains an instance of ZoneId from an ID ensuring that the ID is valid and available for use. |
staticZoneId | 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. |
staticZoneId | ofOffset(String prefix,ZoneOffset offset) | Obtains an instance of ZoneId wrapping an offset. |
staticZoneId | systemDefault() | Gets the system default time-zone. |
String | toString() | Outputs this zone as a String, using the ID. |
public static final Map<String,String> SHORT_IDS
Use of short zone IDs has been deprecated injava.util.TimeZone. This map allows the IDs to continue to be used via theof(String, Map) factory method.
This map contains a mapping of the IDs that is in line with TZDB 2005r and later, where 'EST', 'MST' and 'HST' map to IDs which do not include daylight savings.
This maps as follows:
public static ZoneId systemDefault()
This queriesTimeZone.getDefault() to find the default time-zone and converts it to aZoneId. If the system default time-zone is changed, then the result of this method will also change.
DateTimeException - if the converted zone ID has an invalid formatZoneRulesException - if the converted zone region ID cannot be foundpublic static Set<String> getAvailableZoneIds()
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 toof(String) to create aZoneId.
The set of zone IDs can increase over time, although in a typical application the set of IDs is fixed. Each call to this method is thread-safe.
public static ZoneId of(String zoneId,Map<String,String> aliasMap)
ZoneId using its ID using a map of aliases to supplement the standard zone IDs.Many users of time-zones use short abbreviations, such as PST for 'Pacific Standard Time' and PDT for 'Pacific Daylight Time'. These abbreviations are not unique, and so cannot be used as IDs. This method allows a map of string to time-zone to be setup and reused within an application.
zoneId - the time-zone ID, not nullaliasMap - a map of alias zone IDs (typically abbreviations) to real zone IDs, not nullDateTimeException - if the zone ID has an invalid formatZoneRulesException - if the zone ID is a region ID that cannot be foundpublic static ZoneId of(String zoneId)
ZoneId from an ID ensuring that the ID is valid and available for use. This method parses the ID producing aZoneId orZoneOffset. AZoneOffset is returned if the ID is 'Z', or starts with '+' or '-'. The result will always be a valid ID for whichZoneRules can be obtained.
Parsing matches the zone ID step by step as follows.
ZoneOffset.UTC.DateTimeException is thrown.ZoneOffset usingZoneOffset.of(String).ZoneId with the same ID and rules equivalent toZoneOffset.UTC.ZoneOffset. The result will be aZoneId with the specified UTC/GMT/UT prefix and the normalized offset ID as perZoneOffset.getId(). The rules of the returnedZoneId will be equivalent to the parsedZoneOffset.[A-Za-z][A-Za-z0-9~/._+-]+ otherwise aDateTimeException is thrown. If the zone ID is not in the configured set of IDs,ZoneRulesException is thrown. The detailed format of the region ID depends on the group supplying the data. The default set of data is supplied by the IANA Time Zone Database (TZDB). This has region IDs of the form '{area}/{city}', such as 'Europe/Paris' or 'America/New_York'. This is compatible with most IDs fromTimeZone.zoneId - the time-zone ID, not nullDateTimeException - if the zone ID has an invalid formatZoneRulesException - if the zone ID is a region ID that cannot be foundpublic static ZoneId ofOffset(String prefix,ZoneOffset offset)
ZoneId wrapping an offset. If the prefix is "GMT", "UTC", or "UT" aZoneId with the prefix and the non-zero offset is returned. If the prefix is empty"" theZoneOffset is returned.
prefix - the time-zone ID, not nulloffset - the offset, not nullIllegalArgumentException - if the prefix is not one of "GMT", "UTC", or "UT", or ""public static ZoneId from(TemporalAccessor temporal)
ZoneId from a temporal object. This obtains a zone based on the specified temporal. ATemporalAccessor represents an arbitrary set of date and time information, which this factory converts to an instance ofZoneId.
ATemporalAccessor represents some form of date and time information. This factory converts the arbitrary temporal object to an instance ofZoneId.
The conversion will try to obtain the zone in a way that favours region-based zones over offset-based zones usingTemporalQueries.zone().
This method matches the signature of the functional interfaceTemporalQuery allowing it to be used as a query via method reference,ZoneId::from.
temporal - the temporal object to convert, not nullDateTimeException - if unable to convert to aZoneIdpublic abstract String getId()
This ID uniquely defines this object. The format of an offset based ID is defined byZoneOffset.getId().
public String getDisplayName(TextStyle style,Locale locale)
This returns the textual name used to identify the time-zone ID, suitable for presentation to the user. The parameters control the style of the returned text and the locale.
If no textual mapping is found then thefull ID is returned.
style - the length of the text required, not nulllocale - the locale to use, not nullpublic abstract ZoneRules getRules()
The rules provide the functionality associated with a time-zone, such as finding the offset for a given instant or local date-time.
A time-zone can be invalid if it is deserialized in a Java Runtime which does not have the same rules loaded as the Java Runtime that stored it. In this case, calling this method will throw aZoneRulesException.
The rules are supplied byZoneRulesProvider. An advanced provider may support dynamic updates to the rules without restarting the Java Runtime. If so, then the result of this method may change over time. Each individual call will be still remain thread-safe.
ZoneOffset will always return a set of rules where the offset never changes.
ZoneRulesException - if no rules are available for this IDpublic ZoneId normalized()
ZoneOffset where possible. The returns a normalizedZoneId that can be used in place of this ID. The result will haveZoneRules equivalent to those returned by this object, however the ID returned bygetId() may be different.
The normalization checks if the rules of thisZoneId have a fixed offset. If they do, then theZoneOffset equal to that offset is returned. Otherwisethis is returned.
public boolean equals(Object obj)
The comparison is based on the ID.
equals in class Objectobj - the object to check, null returns falseObject.hashCode(),HashMappublic int hashCode()
hashCode in class ObjectObject.equals(java.lang.Object),System.identityHashCode(java.lang.Object)