public abstract class ZoneId
extends java.lang.Object
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:
Most fixed offsets are represented by 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.
The code supports loading a ZoneId
on a JVM which does not have available rules
for that ID. This allows the date-time object, such as ZonedDateTime
,
to still be queried.
An ID is parsed as an offset ID if it starts with 'UTC', 'GMT', '+' or '-', or is a single letter. For example, 'Z', '+02:00', '-05:00', 'UTC+05' and 'GMT-6' are all valid offset IDs. Note that some IDs, such as 'D' or '+ABC' meet the criteria, but are invalid.
All other IDs are considered to be region IDs.
Region IDs are defined by configuration, which can be thought of as a Map
from region ID to ZoneRules
, see ZoneRulesProvider
.
Time-zones 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 region ID. 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'.
Modifier and Type | Field and Description |
---|---|
static java.util.Map<java.lang.String,java.lang.String> |
OLD_IDS_POST_2005
A map of zone overrides to enable the older US time-zone names to be used.
|
static java.util.Map<java.lang.String,java.lang.String> |
OLD_IDS_PRE_2005
A map of zone overrides to enable the older US time-zone names to be used.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.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. |
abstract java.lang.String |
getId()
Gets the unique time-zone ID.
|
abstract ZoneRules |
getRules()
Gets the time-zone rules for this ID allowing calculations to be performed.
|
java.lang.String |
getText(TextStyle style,
java.util.Locale locale)
Gets the textual representation of the zone, such as 'British Time' or
'+02:00'.
|
int |
hashCode()
A hash code for this time-zone ID.
|
static ZoneId |
of(java.lang.String zoneId)
Obtains an instance of
ZoneId from an ID ensuring that the
ID is valid and available for use. |
static ZoneId |
of(java.lang.String zoneId,
java.util.Map<java.lang.String,java.lang.String> aliasMap)
Obtains an instance of
ZoneId using its ID using a map
of aliases to supplement the standard zone IDs. |
static ZoneId |
systemDefault()
Gets the system default time-zone.
|
java.lang.String |
toString()
Outputs this zone as a
String , using the ID. |
public static final java.util.Map<java.lang.String,java.lang.String> OLD_IDS_PRE_2005
This maps as follows:
The map is unmodifiable.
public static final java.util.Map<java.lang.String,java.lang.String> OLD_IDS_POST_2005
This maps as follows:
The map is unmodifiable.
public static ZoneId systemDefault()
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.
DateTimeException
- if the converted zone ID has an invalid formatZoneRulesException
- if the converted zone region ID cannot be foundpublic static ZoneId of(java.lang.String zoneId, java.util.Map<java.lang.String,java.lang.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 region ID cannot be foundpublic static ZoneId of(java.lang.String zoneId)
ZoneId
from an ID ensuring that the
ID is valid and available for use.
This method parses the ID, applies any appropriate normalization, and validates it against the known set of IDs for which rules are available.
An ID is parsed as though it is an offset ID if it starts with 'UTC', 'GMT', '+'
or '-', or if it has less then two letters.
The offset of zero
may be represented in multiple ways,
including 'Z', 'UTC', 'GMT', 'UTC0' 'GMT0', '+00:00', '-00:00' and 'UTC+00:00'.
Eight forms of ID are recognized, where '{offset}' means to parse using ZoneOffset.of(String)
:
{offset}
- a ZoneOffset
ID, such as 'Z' or '+02:00'
UTC
- alternate form of a ZoneOffset
ID equal to 'Z'
UTC0
- alternate form of a ZoneOffset
ID equal to 'Z'
UTC{offset}
- alternate form of a ZoneOffset
ID equal to '{offset}'
GMT
- alternate form of a ZoneOffset
ID equal to 'Z'
GMT0
- alternate form of a ZoneOffset
ID equal to 'Z'
GMT{offset}
- alternate form of a ZoneOffset
ID equal to '{offset}'r
{regionID}
- full region ID, loaded from configuration
Region IDs must match the regular expression [A-Za-z][A-Za-z0-9~/._+-]+
.
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 from TimeZone
.
zoneId
- the time-zone ID, not nullDateTimeException
- if the zone ID has an invalid formatZoneRulesException
- if the zone region ID cannot be foundpublic static ZoneId from(TemporalAccessor temporal)
ZoneId
from a temporal object.
A TemporalAccessor
represents some form of date and time information.
This factory converts the arbitrary temporal object to an instance of ZoneId
.
The conversion will try to obtain the zone in a way that favours region-based
zones over offset-based zones using TemporalAccessor.Query.ZONE
.
temporal
- the temporal object to convert, not nullDateTimeException
- if unable to convert to a ZoneId
public abstract java.lang.String getId()
This ID uniquely defines this object.
The format of an offset based ID is defined by ZoneOffset.getId()
.
public 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 JVM which does not have the same rules loaded as the JVM that stored it. In this case, calling this method will throw an exception.
The rules are supplied by ZoneRulesProvider
. An advanced provider may
support dynamic updates to the rules without restarting the JVM.
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.
DateTimeException
- if no rules are available for this IDpublic java.lang.String getText(TextStyle style, java.util.Locale locale)
This returns a textual description for the time-zone ID.
If no textual mapping is found then the full ID
is returned.
style
- the length of the text required, not nulllocale
- the locale to use, not nullpublic boolean equals(java.lang.Object obj)
The comparison is based on the ID.
equals
in class java.lang.Object
obj
- the object to check, null returns falseObject.hashCode()
,
HashMap
public int hashCode()
hashCode
in class java.lang.Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public java.lang.String toString()
String
, using the ID.toString
in class java.lang.Object