public abstract class ZoneId extends Object implements Serializable
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', 'UT' '+' or '-', or is a single letter. For example, 'Z', '+02:00', '-05:00', 'UTC+05', 'GMT-6' and 'UT+01:00' are all valid offset IDs. Note that some IDs, such as 'D' or '+ABC' meet the criteria to be parsed as offset IDs, but have an invalid offset.
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 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
modelling
offset-based IDs. This difference is visible in serialization.Modifier and Type | Field and Description |
---|---|
static Map<String,String> |
OLD_IDS_POST_2005
A map of zone overrides to enable the older US time-zone names to be used.
|
static Map<String,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(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. |
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.
|
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 |
systemDefault()
Gets the system default time-zone.
|
String |
toString()
Outputs this zone as a
String , using the ID. |
public static final Map<String,String> OLD_IDS_PRE_2005
This maps as follows:
The map is unmodifiable.
public static final Map<String,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(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, 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', 'UT', '+' or '-', or if it has less then two letters. The offset of zero may be represented in multiple ways, including 'Z', 'UTC', 'GMT', 'UT', 'UTC0', 'GMT0', 'UT0', '+00:00', '-00:00' and 'UTC+00:00'.
Six forms of ID are recognized:
Z
- an offset of zero, which is ZoneOffset.UTC
{offset}
- a ZoneOffset
ID, such as '+02:00'
{utcPrefix}
- a ZoneOffset
ID equal to 'Z'
{utcPrefix}0
- a ZoneOffset
ID equal to 'Z'
{utcPrefix}{offset}
- a ZoneOffset
ID equal to '{offset}'
{regionID}
- full region ID, loaded from configuration
The {offset} is a valid format for ZoneOffset.of(String)
, excluding 'Z'.
The {utcPrefix} is 'UTC', 'GMT' or 'UT'.
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 ID is a region ID that cannot be foundpublic static ZoneId from(TemporalAccessor temporal)
ZoneId
from a temporal object.
This obtains a zone based on the specified temporal.
A TemporalAccessor
represents an arbitrary set of date and time information,
which this factory converts to an instance of ZoneId
.
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 Queries.zone()
.
This method matches the signature of the functional interface TemporalQuery
allowing it to be used in queries via method reference, ZoneId::from
.
temporal
- the temporal object to convert, not nullDateTimeException
- if unable to convert to a ZoneId
public abstract 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.
ZoneRulesException
- if no rules are available for this IDpublic 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 the full ID
is returned.
style
- the length of the text required, not nulllocale
- the locale to use, not nullpublic boolean equals(Object obj)
The comparison is based on the ID.
equals
in class Object
obj
- the object to check, null returns falseObject.hashCode()
,
HashMap
public int hashCode()
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2013, Oracle and/or its affiliates. All rights reserved.
DRAFT internal-0