Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/time/ZoneId.java
38829 views
/*1* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos32*33* All rights reserved.34*35* Redistribution and use in source and binary forms, with or without36* modification, are permitted provided that the following conditions are met:37*38* * Redistributions of source code must retain the above copyright notice,39* this list of conditions and the following disclaimer.40*41* * Redistributions in binary form must reproduce the above copyright notice,42* this list of conditions and the following disclaimer in the documentation43* and/or other materials provided with the distribution.44*45* * Neither the name of JSR-310 nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS50* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT51* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR52* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR53* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,54* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,55* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR56* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF57* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING58* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS59* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.60*/61package java.time;6263import java.io.DataOutput;64import java.io.IOException;65import java.io.InvalidObjectException;66import java.io.ObjectInputStream;67import java.io.Serializable;68import java.time.format.DateTimeFormatterBuilder;69import java.time.format.TextStyle;70import java.time.temporal.TemporalAccessor;71import java.time.temporal.TemporalField;72import java.time.temporal.TemporalQueries;73import java.time.temporal.TemporalQuery;74import java.time.temporal.UnsupportedTemporalTypeException;75import java.time.zone.ZoneRules;76import java.time.zone.ZoneRulesException;77import java.time.zone.ZoneRulesProvider;78import java.util.Collections;79import java.util.HashMap;80import java.util.Locale;81import java.util.Map;82import java.util.Objects;83import java.util.Set;84import java.util.TimeZone;8586/**87* A time-zone ID, such as {@code Europe/Paris}.88* <p>89* A {@code ZoneId} is used to identify the rules used to convert between90* an {@link Instant} and a {@link LocalDateTime}.91* There are two distinct types of ID:92* <ul>93* <li>Fixed offsets - a fully resolved offset from UTC/Greenwich, that uses94* the same offset for all local date-times95* <li>Geographical regions - an area where a specific set of rules for finding96* the offset from UTC/Greenwich apply97* </ul>98* Most fixed offsets are represented by {@link ZoneOffset}.99* Calling {@link #normalized()} on any {@code ZoneId} will ensure that a100* fixed offset ID will be represented as a {@code ZoneOffset}.101* <p>102* The actual rules, describing when and how the offset changes, are defined by {@link ZoneRules}.103* This class is simply an ID used to obtain the underlying rules.104* This approach is taken because rules are defined by governments and change105* frequently, whereas the ID is stable.106* <p>107* The distinction has other effects. Serializing the {@code ZoneId} will only send108* the ID, whereas serializing the rules sends the entire data set.109* Similarly, a comparison of two IDs only examines the ID, whereas110* a comparison of two rules examines the entire data set.111*112* <h3>Time-zone IDs</h3>113* The ID is unique within the system.114* There are three types of ID.115* <p>116* The simplest type of ID is that from {@code ZoneOffset}.117* This consists of 'Z' and IDs starting with '+' or '-'.118* <p>119* The next type of ID are offset-style IDs with some form of prefix,120* such as 'GMT+2' or 'UTC+01:00'.121* The recognised prefixes are 'UTC', 'GMT' and 'UT'.122* The offset is the suffix and will be normalized during creation.123* These IDs can be normalized to a {@code ZoneOffset} using {@code normalized()}.124* <p>125* The third type of ID are region-based IDs. A region-based ID must be of126* two or more characters, and not start with 'UTC', 'GMT', 'UT' '+' or '-'.127* Region-based IDs are defined by configuration, see {@link ZoneRulesProvider}.128* The configuration focuses on providing the lookup from the ID to the129* underlying {@code ZoneRules}.130* <p>131* Time-zone rules are defined by governments and change frequently.132* There are a number of organizations, known here as groups, that monitor133* time-zone changes and collate them.134* The default group is the IANA Time Zone Database (TZDB).135* Other organizations include IATA (the airline industry body) and Microsoft.136* <p>137* Each group defines its own format for the region ID it provides.138* The TZDB group defines IDs such as 'Europe/London' or 'America/New_York'.139* TZDB IDs take precedence over other groups.140* <p>141* It is strongly recommended that the group name is included in all IDs supplied by142* groups other than TZDB to avoid conflicts. For example, IATA airline time-zone143* region IDs are typically the same as the three letter airport code.144* However, the airport of Utrecht has the code 'UTC', which is obviously a conflict.145* The recommended format for region IDs from groups other than TZDB is 'group~region'.146* Thus if IATA data were defined, Utrecht airport would be 'IATA~UTC'.147*148* <h3>Serialization</h3>149* This class can be serialized and stores the string zone ID in the external form.150* The {@code ZoneOffset} subclass uses a dedicated format that only stores the151* offset from UTC/Greenwich.152* <p>153* A {@code ZoneId} can be deserialized in a Java Runtime where the ID is unknown.154* For example, if a server-side Java Runtime has been updated with a new zone ID, but155* the client-side Java Runtime has not been updated. In this case, the {@code ZoneId}156* object will exist, and can be queried using {@code getId}, {@code equals},157* {@code hashCode}, {@code toString}, {@code getDisplayName} and {@code normalized}.158* However, any call to {@code getRules} will fail with {@code ZoneRulesException}.159* This approach is designed to allow a {@link ZonedDateTime} to be loaded and160* queried, but not modified, on a Java Runtime with incomplete time-zone information.161*162* <p>163* This is a <a href="{@docRoot}/java/lang/doc-files/ValueBased.html">value-based</a>164* class; use of identity-sensitive operations (including reference equality165* ({@code ==}), identity hash code, or synchronization) on instances of166* {@code ZoneId} may have unpredictable results and should be avoided.167* The {@code equals} method should be used for comparisons.168*169* @implSpec170* This abstract class has two implementations, both of which are immutable and thread-safe.171* One implementation models region-based IDs, the other is {@code ZoneOffset} modelling172* offset-based IDs. This difference is visible in serialization.173*174* @since 1.8175*/176public abstract class ZoneId implements Serializable {177178/**179* A map of zone overrides to enable the short time-zone names to be used.180* <p>181* Use of short zone IDs has been deprecated in {@code java.util.TimeZone}.182* This map allows the IDs to continue to be used via the183* {@link #of(String, Map)} factory method.184* <p>185* This map contains a mapping of the IDs that is in line with TZDB 2005r and186* later, where 'EST', 'MST' and 'HST' map to IDs which do not include daylight187* savings.188* <p>189* This maps as follows:190* <ul>191* <li>EST - -05:00</li>192* <li>HST - -10:00</li>193* <li>MST - -07:00</li>194* <li>ACT - Australia/Darwin</li>195* <li>AET - Australia/Sydney</li>196* <li>AGT - America/Argentina/Buenos_Aires</li>197* <li>ART - Africa/Cairo</li>198* <li>AST - America/Anchorage</li>199* <li>BET - America/Sao_Paulo</li>200* <li>BST - Asia/Dhaka</li>201* <li>CAT - Africa/Harare</li>202* <li>CNT - America/St_Johns</li>203* <li>CST - America/Chicago</li>204* <li>CTT - Asia/Shanghai</li>205* <li>EAT - Africa/Addis_Ababa</li>206* <li>ECT - Europe/Paris</li>207* <li>IET - America/Indiana/Indianapolis</li>208* <li>IST - Asia/Kolkata</li>209* <li>JST - Asia/Tokyo</li>210* <li>MIT - Pacific/Apia</li>211* <li>NET - Asia/Yerevan</li>212* <li>NST - Pacific/Auckland</li>213* <li>PLT - Asia/Karachi</li>214* <li>PNT - America/Phoenix</li>215* <li>PRT - America/Puerto_Rico</li>216* <li>PST - America/Los_Angeles</li>217* <li>SST - Pacific/Guadalcanal</li>218* <li>VST - Asia/Ho_Chi_Minh</li>219* </ul>220* The map is unmodifiable.221*/222public static final Map<String, String> SHORT_IDS;223static {224Map<String, String> map = new HashMap<>(64);225map.put("ACT", "Australia/Darwin");226map.put("AET", "Australia/Sydney");227map.put("AGT", "America/Argentina/Buenos_Aires");228map.put("ART", "Africa/Cairo");229map.put("AST", "America/Anchorage");230map.put("BET", "America/Sao_Paulo");231map.put("BST", "Asia/Dhaka");232map.put("CAT", "Africa/Harare");233map.put("CNT", "America/St_Johns");234map.put("CST", "America/Chicago");235map.put("CTT", "Asia/Shanghai");236map.put("EAT", "Africa/Addis_Ababa");237map.put("ECT", "Europe/Paris");238map.put("IET", "America/Indiana/Indianapolis");239map.put("IST", "Asia/Kolkata");240map.put("JST", "Asia/Tokyo");241map.put("MIT", "Pacific/Apia");242map.put("NET", "Asia/Yerevan");243map.put("NST", "Pacific/Auckland");244map.put("PLT", "Asia/Karachi");245map.put("PNT", "America/Phoenix");246map.put("PRT", "America/Puerto_Rico");247map.put("PST", "America/Los_Angeles");248map.put("SST", "Pacific/Guadalcanal");249map.put("VST", "Asia/Ho_Chi_Minh");250map.put("EST", "-05:00");251map.put("MST", "-07:00");252map.put("HST", "-10:00");253SHORT_IDS = Collections.unmodifiableMap(map);254}255/**256* Serialization version.257*/258private static final long serialVersionUID = 8352817235686L;259260//-----------------------------------------------------------------------261/**262* Gets the system default time-zone.263* <p>264* This queries {@link TimeZone#getDefault()} to find the default time-zone265* and converts it to a {@code ZoneId}. If the system default time-zone is changed,266* then the result of this method will also change.267*268* @return the zone ID, not null269* @throws DateTimeException if the converted zone ID has an invalid format270* @throws ZoneRulesException if the converted zone region ID cannot be found271*/272public static ZoneId systemDefault() {273return TimeZone.getDefault().toZoneId();274}275276/**277* Gets the set of available zone IDs.278* <p>279* This set includes the string form of all available region-based IDs.280* Offset-based zone IDs are not included in the returned set.281* The ID can be passed to {@link #of(String)} to create a {@code ZoneId}.282* <p>283* The set of zone IDs can increase over time, although in a typical application284* the set of IDs is fixed. Each call to this method is thread-safe.285*286* @return a modifiable copy of the set of zone IDs, not null287*/288public static Set<String> getAvailableZoneIds() {289return ZoneRulesProvider.getAvailableZoneIds();290}291292//-----------------------------------------------------------------------293/**294* Obtains an instance of {@code ZoneId} using its ID using a map295* of aliases to supplement the standard zone IDs.296* <p>297* Many users of time-zones use short abbreviations, such as PST for298* 'Pacific Standard Time' and PDT for 'Pacific Daylight Time'.299* These abbreviations are not unique, and so cannot be used as IDs.300* This method allows a map of string to time-zone to be setup and reused301* within an application.302*303* @param zoneId the time-zone ID, not null304* @param aliasMap a map of alias zone IDs (typically abbreviations) to real zone IDs, not null305* @return the zone ID, not null306* @throws DateTimeException if the zone ID has an invalid format307* @throws ZoneRulesException if the zone ID is a region ID that cannot be found308*/309public static ZoneId of(String zoneId, Map<String, String> aliasMap) {310Objects.requireNonNull(zoneId, "zoneId");311Objects.requireNonNull(aliasMap, "aliasMap");312String id = aliasMap.get(zoneId);313id = (id != null ? id : zoneId);314return of(id);315}316317/**318* Obtains an instance of {@code ZoneId} from an ID ensuring that the319* ID is valid and available for use.320* <p>321* This method parses the ID producing a {@code ZoneId} or {@code ZoneOffset}.322* A {@code ZoneOffset} is returned if the ID is 'Z', or starts with '+' or '-'.323* The result will always be a valid ID for which {@link ZoneRules} can be obtained.324* <p>325* Parsing matches the zone ID step by step as follows.326* <ul>327* <li>If the zone ID equals 'Z', the result is {@code ZoneOffset.UTC}.328* <li>If the zone ID consists of a single letter, the zone ID is invalid329* and {@code DateTimeException} is thrown.330* <li>If the zone ID starts with '+' or '-', the ID is parsed as a331* {@code ZoneOffset} using {@link ZoneOffset#of(String)}.332* <li>If the zone ID equals 'GMT', 'UTC' or 'UT' then the result is a {@code ZoneId}333* with the same ID and rules equivalent to {@code ZoneOffset.UTC}.334* <li>If the zone ID starts with 'UTC+', 'UTC-', 'GMT+', 'GMT-', 'UT+' or 'UT-'335* then the ID is a prefixed offset-based ID. The ID is split in two, with336* a two or three letter prefix and a suffix starting with the sign.337* The suffix is parsed as a {@link ZoneOffset#of(String) ZoneOffset}.338* The result will be a {@code ZoneId} with the specified UTC/GMT/UT prefix339* and the normalized offset ID as per {@link ZoneOffset#getId()}.340* The rules of the returned {@code ZoneId} will be equivalent to the341* parsed {@code ZoneOffset}.342* <li>All other IDs are parsed as region-based zone IDs. Region IDs must343* match the regular expression <code>[A-Za-z][A-Za-z0-9~/._+-]+</code>344* otherwise a {@code DateTimeException} is thrown. If the zone ID is not345* in the configured set of IDs, {@code ZoneRulesException} is thrown.346* The detailed format of the region ID depends on the group supplying the data.347* The default set of data is supplied by the IANA Time Zone Database (TZDB).348* This has region IDs of the form '{area}/{city}', such as 'Europe/Paris' or 'America/New_York'.349* This is compatible with most IDs from {@link java.util.TimeZone}.350* </ul>351*352* @param zoneId the time-zone ID, not null353* @return the zone ID, not null354* @throws DateTimeException if the zone ID has an invalid format355* @throws ZoneRulesException if the zone ID is a region ID that cannot be found356*/357public static ZoneId of(String zoneId) {358return of(zoneId, true);359}360361/**362* Obtains an instance of {@code ZoneId} wrapping an offset.363* <p>364* If the prefix is "GMT", "UTC", or "UT" a {@code ZoneId}365* with the prefix and the non-zero offset is returned.366* If the prefix is empty {@code ""} the {@code ZoneOffset} is returned.367*368* @param prefix the time-zone ID, not null369* @param offset the offset, not null370* @return the zone ID, not null371* @throws IllegalArgumentException if the prefix is not one of372* "GMT", "UTC", or "UT", or ""373*/374public static ZoneId ofOffset(String prefix, ZoneOffset offset) {375Objects.requireNonNull(prefix, "prefix");376Objects.requireNonNull(offset, "offset");377if (prefix.length() == 0) {378return offset;379}380381if (!prefix.equals("GMT") && !prefix.equals("UTC") && !prefix.equals("UT")) {382throw new IllegalArgumentException("prefix should be GMT, UTC or UT, is: " + prefix);383}384385if (offset.getTotalSeconds() != 0) {386prefix = prefix.concat(offset.getId());387}388return new ZoneRegion(prefix, offset.getRules());389}390391/**392* Parses the ID, taking a flag to indicate whether {@code ZoneRulesException}393* should be thrown or not, used in deserialization.394*395* @param zoneId the time-zone ID, not null396* @param checkAvailable whether to check if the zone ID is available397* @return the zone ID, not null398* @throws DateTimeException if the ID format is invalid399* @throws ZoneRulesException if checking availability and the ID cannot be found400*/401static ZoneId of(String zoneId, boolean checkAvailable) {402Objects.requireNonNull(zoneId, "zoneId");403if (zoneId.length() <= 1 || zoneId.startsWith("+") || zoneId.startsWith("-")) {404return ZoneOffset.of(zoneId);405} else if (zoneId.startsWith("UTC") || zoneId.startsWith("GMT")) {406return ofWithPrefix(zoneId, 3, checkAvailable);407} else if (zoneId.startsWith("UT")) {408return ofWithPrefix(zoneId, 2, checkAvailable);409}410return ZoneRegion.ofId(zoneId, checkAvailable);411}412413/**414* Parse once a prefix is established.415*416* @param zoneId the time-zone ID, not null417* @param prefixLength the length of the prefix, 2 or 3418* @return the zone ID, not null419* @throws DateTimeException if the zone ID has an invalid format420*/421private static ZoneId ofWithPrefix(String zoneId, int prefixLength, boolean checkAvailable) {422String prefix = zoneId.substring(0, prefixLength);423if (zoneId.length() == prefixLength) {424return ofOffset(prefix, ZoneOffset.UTC);425}426if (zoneId.charAt(prefixLength) != '+' && zoneId.charAt(prefixLength) != '-') {427return ZoneRegion.ofId(zoneId, checkAvailable); // drop through to ZoneRulesProvider428}429try {430ZoneOffset offset = ZoneOffset.of(zoneId.substring(prefixLength));431if (offset == ZoneOffset.UTC) {432return ofOffset(prefix, offset);433}434return ofOffset(prefix, offset);435} catch (DateTimeException ex) {436throw new DateTimeException("Invalid ID for offset-based ZoneId: " + zoneId, ex);437}438}439440//-----------------------------------------------------------------------441/**442* Obtains an instance of {@code ZoneId} from a temporal object.443* <p>444* This obtains a zone based on the specified temporal.445* A {@code TemporalAccessor} represents an arbitrary set of date and time information,446* which this factory converts to an instance of {@code ZoneId}.447* <p>448* A {@code TemporalAccessor} represents some form of date and time information.449* This factory converts the arbitrary temporal object to an instance of {@code ZoneId}.450* <p>451* The conversion will try to obtain the zone in a way that favours region-based452* zones over offset-based zones using {@link TemporalQueries#zone()}.453* <p>454* This method matches the signature of the functional interface {@link TemporalQuery}455* allowing it to be used as a query via method reference, {@code ZoneId::from}.456*457* @param temporal the temporal object to convert, not null458* @return the zone ID, not null459* @throws DateTimeException if unable to convert to a {@code ZoneId}460*/461public static ZoneId from(TemporalAccessor temporal) {462ZoneId obj = temporal.query(TemporalQueries.zone());463if (obj == null) {464throw new DateTimeException("Unable to obtain ZoneId from TemporalAccessor: " +465temporal + " of type " + temporal.getClass().getName());466}467return obj;468}469470//-----------------------------------------------------------------------471/**472* Constructor only accessible within the package.473*/474ZoneId() {475if (getClass() != ZoneOffset.class && getClass() != ZoneRegion.class) {476throw new AssertionError("Invalid subclass");477}478}479480//-----------------------------------------------------------------------481/**482* Gets the unique time-zone ID.483* <p>484* This ID uniquely defines this object.485* The format of an offset based ID is defined by {@link ZoneOffset#getId()}.486*487* @return the time-zone unique ID, not null488*/489public abstract String getId();490491//-----------------------------------------------------------------------492/**493* Gets the textual representation of the zone, such as 'British Time' or494* '+02:00'.495* <p>496* This returns the textual name used to identify the time-zone ID,497* suitable for presentation to the user.498* The parameters control the style of the returned text and the locale.499* <p>500* If no textual mapping is found then the {@link #getId() full ID} is returned.501*502* @param style the length of the text required, not null503* @param locale the locale to use, not null504* @return the text value of the zone, not null505*/506public String getDisplayName(TextStyle style, Locale locale) {507return new DateTimeFormatterBuilder().appendZoneText(style).toFormatter(locale).format(toTemporal());508}509510/**511* Converts this zone to a {@code TemporalAccessor}.512* <p>513* A {@code ZoneId} can be fully represented as a {@code TemporalAccessor}.514* However, the interface is not implemented by this class as most of the515* methods on the interface have no meaning to {@code ZoneId}.516* <p>517* The returned temporal has no supported fields, with the query method518* supporting the return of the zone using {@link TemporalQueries#zoneId()}.519*520* @return a temporal equivalent to this zone, not null521*/522private TemporalAccessor toTemporal() {523return new TemporalAccessor() {524@Override525public boolean isSupported(TemporalField field) {526return false;527}528@Override529public long getLong(TemporalField field) {530throw new UnsupportedTemporalTypeException("Unsupported field: " + field);531}532@SuppressWarnings("unchecked")533@Override534public <R> R query(TemporalQuery<R> query) {535if (query == TemporalQueries.zoneId()) {536return (R) ZoneId.this;537}538return TemporalAccessor.super.query(query);539}540};541}542543//-----------------------------------------------------------------------544/**545* Gets the time-zone rules for this ID allowing calculations to be performed.546* <p>547* The rules provide the functionality associated with a time-zone,548* such as finding the offset for a given instant or local date-time.549* <p>550* A time-zone can be invalid if it is deserialized in a Java Runtime which551* does not have the same rules loaded as the Java Runtime that stored it.552* In this case, calling this method will throw a {@code ZoneRulesException}.553* <p>554* The rules are supplied by {@link ZoneRulesProvider}. An advanced provider may555* support dynamic updates to the rules without restarting the Java Runtime.556* If so, then the result of this method may change over time.557* Each individual call will be still remain thread-safe.558* <p>559* {@link ZoneOffset} will always return a set of rules where the offset never changes.560*561* @return the rules, not null562* @throws ZoneRulesException if no rules are available for this ID563*/564public abstract ZoneRules getRules();565566/**567* Normalizes the time-zone ID, returning a {@code ZoneOffset} where possible.568* <p>569* The returns a normalized {@code ZoneId} that can be used in place of this ID.570* The result will have {@code ZoneRules} equivalent to those returned by this object,571* however the ID returned by {@code getId()} may be different.572* <p>573* The normalization checks if the rules of this {@code ZoneId} have a fixed offset.574* If they do, then the {@code ZoneOffset} equal to that offset is returned.575* Otherwise {@code this} is returned.576*577* @return the time-zone unique ID, not null578*/579public ZoneId normalized() {580try {581ZoneRules rules = getRules();582if (rules.isFixedOffset()) {583return rules.getOffset(Instant.EPOCH);584}585} catch (ZoneRulesException ex) {586// invalid ZoneRegion is not important to this method587}588return this;589}590591//-----------------------------------------------------------------------592/**593* Checks if this time-zone ID is equal to another time-zone ID.594* <p>595* The comparison is based on the ID.596*597* @param obj the object to check, null returns false598* @return true if this is equal to the other time-zone ID599*/600@Override601public boolean equals(Object obj) {602if (this == obj) {603return true;604}605if (obj instanceof ZoneId) {606ZoneId other = (ZoneId) obj;607return getId().equals(other.getId());608}609return false;610}611612/**613* A hash code for this time-zone ID.614*615* @return a suitable hash code616*/617@Override618public int hashCode() {619return getId().hashCode();620}621622//-----------------------------------------------------------------------623/**624* Defend against malicious streams.625*626* @param s the stream to read627* @throws InvalidObjectException always628*/629private void readObject(ObjectInputStream s) throws InvalidObjectException {630throw new InvalidObjectException("Deserialization via serialization delegate");631}632633/**634* Outputs this zone as a {@code String}, using the ID.635*636* @return a string representation of this time-zone ID, not null637*/638@Override639public String toString() {640return getId();641}642643//-----------------------------------------------------------------------644/**645* Writes the object using a646* <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.647* @serialData648* <pre>649* out.writeByte(7); // identifies a ZoneId (not ZoneOffset)650* out.writeUTF(getId());651* </pre>652* <p>653* When read back in, the {@code ZoneId} will be created as though using654* {@link #of(String)}, but without any exception in the case where the655* ID has a valid format, but is not in the known set of region-based IDs.656*657* @return the instance of {@code Ser}, not null658*/659// this is here for serialization Javadoc660private Object writeReplace() {661return new Ser(Ser.ZONE_REGION_TYPE, this);662}663664abstract void write(DataOutput out) throws IOException;665666}667668669