Home | History | Annotate | Download | only in time
      1 /*
      2  * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
      3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      4  *
      5  * This code is free software; you can redistribute it and/or modify it
      6  * under the terms of the GNU General Public License version 2 only, as
      7  * published by the Free Software Foundation.  Oracle designates this
      8  * particular file as subject to the "Classpath" exception as provided
      9  * by Oracle in the LICENSE file that accompanied this code.
     10  *
     11  * This code is distributed in the hope that it will be useful, but WITHOUT
     12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     14  * version 2 for more details (a copy is included in the LICENSE file that
     15  * accompanied this code).
     16  *
     17  * You should have received a copy of the GNU General Public License version
     18  * 2 along with this work; if not, write to the Free Software Foundation,
     19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     20  *
     21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     22  * or visit www.oracle.com if you need additional information or have any
     23  * questions.
     24  */
     25 
     26 /*
     27  * This file is available under and governed by the GNU General Public
     28  * License version 2 only, as published by the Free Software Foundation.
     29  * However, the following notice accompanied the original version of this
     30  * file:
     31  *
     32  * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
     33  *
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions are met:
     38  *
     39  *  * Redistributions of source code must retain the above copyright notice,
     40  *    this list of conditions and the following disclaimer.
     41  *
     42  *  * Redistributions in binary form must reproduce the above copyright notice,
     43  *    this list of conditions and the following disclaimer in the documentation
     44  *    and/or other materials provided with the distribution.
     45  *
     46  *  * Neither the name of JSR-310 nor the names of its contributors
     47  *    may be used to endorse or promote products derived from this software
     48  *    without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     61  */
     62 package java.time;
     63 
     64 import java.io.DataOutput;
     65 import java.io.IOException;
     66 import java.io.InvalidObjectException;
     67 import java.io.ObjectInputStream;
     68 import java.io.Serializable;
     69 import java.time.format.DateTimeFormatterBuilder;
     70 import java.time.format.TextStyle;
     71 import java.time.temporal.TemporalAccessor;
     72 import java.time.temporal.TemporalField;
     73 import java.time.temporal.TemporalQueries;
     74 import java.time.temporal.TemporalQuery;
     75 import java.time.temporal.UnsupportedTemporalTypeException;
     76 import java.time.zone.ZoneRules;
     77 import java.time.zone.ZoneRulesException;
     78 import java.time.zone.ZoneRulesProvider;
     79 import java.util.Collections;
     80 import java.util.HashMap;
     81 import java.util.Locale;
     82 import java.util.Map;
     83 import java.util.Objects;
     84 import java.util.Set;
     85 import java.util.TimeZone;
     86 
     87 // Android-changed: removed ValueBased paragraph.
     88 // Android-changed: removed {@link ZoneRulesProvider}.
     89 /**
     90  * A time-zone ID, such as {@code Europe/Paris}.
     91  * <p>
     92  * A {@code ZoneId} is used to identify the rules used to convert between
     93  * an {@link Instant} and a {@link LocalDateTime}.
     94  * There are two distinct types of ID:
     95  * <ul>
     96  * <li>Fixed offsets - a fully resolved offset from UTC/Greenwich, that uses
     97  *  the same offset for all local date-times
     98  * <li>Geographical regions - an area where a specific set of rules for finding
     99  *  the offset from UTC/Greenwich apply
    100  * </ul>
    101  * Most fixed offsets are represented by {@link ZoneOffset}.
    102  * Calling {@link #normalized()} on any {@code ZoneId} will ensure that a
    103  * fixed offset ID will be represented as a {@code ZoneOffset}.
    104  * <p>
    105  * The actual rules, describing when and how the offset changes, are defined by {@link ZoneRules}.
    106  * This class is simply an ID used to obtain the underlying rules.
    107  * This approach is taken because rules are defined by governments and change
    108  * frequently, whereas the ID is stable.
    109  * <p>
    110  * The distinction has other effects. Serializing the {@code ZoneId} will only send
    111  * the ID, whereas serializing the rules sends the entire data set.
    112  * Similarly, a comparison of two IDs only examines the ID, whereas
    113  * a comparison of two rules examines the entire data set.
    114  *
    115  * <h3>Time-zone IDs</h3>
    116  * The ID is unique within the system.
    117  * There are three types of ID.
    118  * <p>
    119  * The simplest type of ID is that from {@code ZoneOffset}.
    120  * This consists of 'Z' and IDs starting with '+' or '-'.
    121  * <p>
    122  * The next type of ID are offset-style IDs with some form of prefix,
    123  * such as 'GMT+2' or 'UTC+01:00'.
    124  * The recognised prefixes are 'UTC', 'GMT' and 'UT'.
    125  * The offset is the suffix and will be normalized during creation.
    126  * These IDs can be normalized to a {@code ZoneOffset} using {@code normalized()}.
    127  * <p>
    128  * The third type of ID are region-based IDs. A region-based ID must be of
    129  * two or more characters, and not start with 'UTC', 'GMT', 'UT' '+' or '-'.
    130  * Region-based IDs are defined by configuration.
    131  * The configuration focuses on providing the lookup from the ID to the
    132  * underlying {@code ZoneRules}.
    133  * <p>
    134  * Time-zone rules are defined by governments and change frequently.
    135  * There are a number of organizations, known here as groups, that monitor
    136  * time-zone changes and collate them.
    137  * The default group is the IANA Time Zone Database (TZDB).
    138  * Other organizations include IATA (the airline industry body) and Microsoft.
    139  * <p>
    140  * Each group defines its own format for the region ID it provides.
    141  * The TZDB group defines IDs such as 'Europe/London' or 'America/New_York'.
    142  * TZDB IDs take precedence over other groups.
    143  * <p>
    144  * It is strongly recommended that the group name is included in all IDs supplied by
    145  * groups other than TZDB to avoid conflicts. For example, IATA airline time-zone
    146  * region IDs are typically the same as the three letter airport code.
    147  * However, the airport of Utrecht has the code 'UTC', which is obviously a conflict.
    148  * The recommended format for region IDs from groups other than TZDB is 'group~region'.
    149  * Thus if IATA data were defined, Utrecht airport would be 'IATA~UTC'.
    150  *
    151  * <h3>Serialization</h3>
    152  * This class can be serialized and stores the string zone ID in the external form.
    153  * The {@code ZoneOffset} subclass uses a dedicated format that only stores the
    154  * offset from UTC/Greenwich.
    155  * <p>
    156  * A {@code ZoneId} can be deserialized in a Java Runtime where the ID is unknown.
    157  * For example, if a server-side Java Runtime has been updated with a new zone ID, but
    158  * the client-side Java Runtime has not been updated. In this case, the {@code ZoneId}
    159  * object will exist, and can be queried using {@code getId}, {@code equals},
    160  * {@code hashCode}, {@code toString}, {@code getDisplayName} and {@code normalized}.
    161  * However, any call to {@code getRules} will fail with {@code ZoneRulesException}.
    162  * This approach is designed to allow a {@link ZonedDateTime} to be loaded and
    163  * queried, but not modified, on a Java Runtime with incomplete time-zone information.
    164  *
    165  * @implSpec
    166  * This abstract class has two implementations, both of which are immutable and thread-safe.
    167  * One implementation models region-based IDs, the other is {@code ZoneOffset} modelling
    168  * offset-based IDs. This difference is visible in serialization.
    169  *
    170  * @since 1.8
    171  */
    172 public abstract class ZoneId implements Serializable {
    173 
    174     /**
    175      * A map of zone overrides to enable the short time-zone names to be used.
    176      * <p>
    177      * Use of short zone IDs has been deprecated in {@code java.util.TimeZone}.
    178      * This map allows the IDs to continue to be used via the
    179      * {@link #of(String, Map)} factory method.
    180      * <p>
    181      * This map contains a mapping of the IDs that is in line with TZDB 2005r and
    182      * later, where 'EST', 'MST' and 'HST' map to IDs which do not include daylight
    183      * savings.
    184      * <p>
    185      * This maps as follows:
    186      * <ul>
    187      * <li>EST - -05:00</li>
    188      * <li>HST - -10:00</li>
    189      * <li>MST - -07:00</li>
    190      * <li>ACT - Australia/Darwin</li>
    191      * <li>AET - Australia/Sydney</li>
    192      * <li>AGT - America/Argentina/Buenos_Aires</li>
    193      * <li>ART - Africa/Cairo</li>
    194      * <li>AST - America/Anchorage</li>
    195      * <li>BET - America/Sao_Paulo</li>
    196      * <li>BST - Asia/Dhaka</li>
    197      * <li>CAT - Africa/Harare</li>
    198      * <li>CNT - America/St_Johns</li>
    199      * <li>CST - America/Chicago</li>
    200      * <li>CTT - Asia/Shanghai</li>
    201      * <li>EAT - Africa/Addis_Ababa</li>
    202      * <li>ECT - Europe/Paris</li>
    203      * <li>IET - America/Indiana/Indianapolis</li>
    204      * <li>IST - Asia/Kolkata</li>
    205      * <li>JST - Asia/Tokyo</li>
    206      * <li>MIT - Pacific/Apia</li>
    207      * <li>NET - Asia/Yerevan</li>
    208      * <li>NST - Pacific/Auckland</li>
    209      * <li>PLT - Asia/Karachi</li>
    210      * <li>PNT - America/Phoenix</li>
    211      * <li>PRT - America/Puerto_Rico</li>
    212      * <li>PST - America/Los_Angeles</li>
    213      * <li>SST - Pacific/Guadalcanal</li>
    214      * <li>VST - Asia/Ho_Chi_Minh</li>
    215      * </ul>
    216      * The map is unmodifiable.
    217      */
    218     public static final Map<String, String> SHORT_IDS;
    219     static {
    220         Map<String, String> map = new HashMap<>(64);
    221         map.put("ACT", "Australia/Darwin");
    222         map.put("AET", "Australia/Sydney");
    223         map.put("AGT", "America/Argentina/Buenos_Aires");
    224         map.put("ART", "Africa/Cairo");
    225         map.put("AST", "America/Anchorage");
    226         map.put("BET", "America/Sao_Paulo");
    227         map.put("BST", "Asia/Dhaka");
    228         map.put("CAT", "Africa/Harare");
    229         map.put("CNT", "America/St_Johns");
    230         map.put("CST", "America/Chicago");
    231         map.put("CTT", "Asia/Shanghai");
    232         map.put("EAT", "Africa/Addis_Ababa");
    233         map.put("ECT", "Europe/Paris");
    234         map.put("IET", "America/Indiana/Indianapolis");
    235         map.put("IST", "Asia/Kolkata");
    236         map.put("JST", "Asia/Tokyo");
    237         map.put("MIT", "Pacific/Apia");
    238         map.put("NET", "Asia/Yerevan");
    239         map.put("NST", "Pacific/Auckland");
    240         map.put("PLT", "Asia/Karachi");
    241         map.put("PNT", "America/Phoenix");
    242         map.put("PRT", "America/Puerto_Rico");
    243         map.put("PST", "America/Los_Angeles");
    244         map.put("SST", "Pacific/Guadalcanal");
    245         map.put("VST", "Asia/Ho_Chi_Minh");
    246         map.put("EST", "-05:00");
    247         map.put("MST", "-07:00");
    248         map.put("HST", "-10:00");
    249         SHORT_IDS = Collections.unmodifiableMap(map);
    250     }
    251     /**
    252      * Serialization version.
    253      */
    254     private static final long serialVersionUID = 8352817235686L;
    255 
    256     //-----------------------------------------------------------------------
    257     /**
    258      * Gets the system default time-zone.
    259      * <p>
    260      * This queries {@link TimeZone#getDefault()} to find the default time-zone
    261      * and converts it to a {@code ZoneId}. If the system default time-zone is changed,
    262      * then the result of this method will also change.
    263      *
    264      * @return the zone ID, not null
    265      * @throws DateTimeException if the converted zone ID has an invalid format
    266      * @throws ZoneRulesException if the converted zone region ID cannot be found
    267      */
    268     public static ZoneId systemDefault() {
    269         return TimeZone.getDefault().toZoneId();
    270     }
    271 
    272     /**
    273      * Gets the set of available zone IDs.
    274      * <p>
    275      * This set includes the string form of all available region-based IDs.
    276      * Offset-based zone IDs are not included in the returned set.
    277      * The ID can be passed to {@link #of(String)} to create a {@code ZoneId}.
    278      * <p>
    279      * The set of zone IDs can increase over time, although in a typical application
    280      * the set of IDs is fixed. Each call to this method is thread-safe.
    281      *
    282      * @return a modifiable copy of the set of zone IDs, not null
    283      */
    284     public static Set<String> getAvailableZoneIds() {
    285         return ZoneRulesProvider.getAvailableZoneIds();
    286     }
    287 
    288     //-----------------------------------------------------------------------
    289     /**
    290      * Obtains an instance of {@code ZoneId} using its ID using a map
    291      * of aliases to supplement the standard zone IDs.
    292      * <p>
    293      * Many users of time-zones use short abbreviations, such as PST for
    294      * 'Pacific Standard Time' and PDT for 'Pacific Daylight Time'.
    295      * These abbreviations are not unique, and so cannot be used as IDs.
    296      * This method allows a map of string to time-zone to be setup and reused
    297      * within an application.
    298      *
    299      * @param zoneId  the time-zone ID, not null
    300      * @param aliasMap  a map of alias zone IDs (typically abbreviations) to real zone IDs, not null
    301      * @return the zone ID, not null
    302      * @throws DateTimeException if the zone ID has an invalid format
    303      * @throws ZoneRulesException if the zone ID is a region ID that cannot be found
    304      */
    305     public static ZoneId of(String zoneId, Map<String, String> aliasMap) {
    306         Objects.requireNonNull(zoneId, "zoneId");
    307         Objects.requireNonNull(aliasMap, "aliasMap");
    308         String id = aliasMap.get(zoneId);
    309         id = (id != null ? id : zoneId);
    310         return of(id);
    311     }
    312 
    313     /**
    314      * Obtains an instance of {@code ZoneId} from an ID ensuring that the
    315      * ID is valid and available for use.
    316      * <p>
    317      * This method parses the ID producing a {@code ZoneId} or {@code ZoneOffset}.
    318      * A {@code ZoneOffset} is returned if the ID is 'Z', or starts with '+' or '-'.
    319      * The result will always be a valid ID for which {@link ZoneRules} can be obtained.
    320      * <p>
    321      * Parsing matches the zone ID step by step as follows.
    322      * <ul>
    323      * <li>If the zone ID equals 'Z', the result is {@code ZoneOffset.UTC}.
    324      * <li>If the zone ID consists of a single letter, the zone ID is invalid
    325      *  and {@code DateTimeException} is thrown.
    326      * <li>If the zone ID starts with '+' or '-', the ID is parsed as a
    327      *  {@code ZoneOffset} using {@link ZoneOffset#of(String)}.
    328      * <li>If the zone ID equals 'GMT', 'UTC' or 'UT' then the result is a {@code ZoneId}
    329      *  with the same ID and rules equivalent to {@code ZoneOffset.UTC}.
    330      * <li>If the zone ID starts with 'UTC+', 'UTC-', 'GMT+', 'GMT-', 'UT+' or 'UT-'
    331      *  then the ID is a prefixed offset-based ID. The ID is split in two, with
    332      *  a two or three letter prefix and a suffix starting with the sign.
    333      *  The suffix is parsed as a {@link ZoneOffset#of(String) ZoneOffset}.
    334      *  The result will be a {@code ZoneId} with the specified UTC/GMT/UT prefix
    335      *  and the normalized offset ID as per {@link ZoneOffset#getId()}.
    336      *  The rules of the returned {@code ZoneId} will be equivalent to the
    337      *  parsed {@code ZoneOffset}.
    338      * <li>All other IDs are parsed as region-based zone IDs. Region IDs must
    339      *  match the regular expression <code>[A-Za-z][A-Za-z0-9~/._+-]+</code>
    340      *  otherwise a {@code DateTimeException} is thrown. If the zone ID is not
    341      *  in the configured set of IDs, {@code ZoneRulesException} is thrown.
    342      *  The detailed format of the region ID depends on the group supplying the data.
    343      *  The default set of data is supplied by the IANA Time Zone Database (TZDB).
    344      *  This has region IDs of the form '{area}/{city}', such as 'Europe/Paris' or 'America/New_York'.
    345      *  This is compatible with most IDs from {@link java.util.TimeZone}.
    346      * </ul>
    347      *
    348      * @param zoneId  the time-zone ID, not null
    349      * @return the zone ID, not null
    350      * @throws DateTimeException if the zone ID has an invalid format
    351      * @throws ZoneRulesException if the zone ID is a region ID that cannot be found
    352      */
    353     public static ZoneId of(String zoneId) {
    354         return of(zoneId, true);
    355     }
    356 
    357     /**
    358      * Obtains an instance of {@code ZoneId} wrapping an offset.
    359      * <p>
    360      * If the prefix is "GMT", "UTC", or "UT" a {@code ZoneId}
    361      * with the prefix and the non-zero offset is returned.
    362      * If the prefix is empty {@code ""} the {@code ZoneOffset} is returned.
    363      *
    364      * @param prefix  the time-zone ID, not null
    365      * @param offset  the offset, not null
    366      * @return the zone ID, not null
    367      * @throws IllegalArgumentException if the prefix is not one of
    368      *     "GMT", "UTC", or "UT", or ""
    369      */
    370     public static ZoneId ofOffset(String prefix, ZoneOffset offset) {
    371         Objects.requireNonNull(prefix, "prefix");
    372         Objects.requireNonNull(offset, "offset");
    373         if (prefix.length() == 0) {
    374             return offset;
    375         }
    376 
    377         if (!prefix.equals("GMT") && !prefix.equals("UTC") && !prefix.equals("UT")) {
    378              throw new IllegalArgumentException("prefix should be GMT, UTC or UT, is: " + prefix);
    379         }
    380 
    381         if (offset.getTotalSeconds() != 0) {
    382             prefix = prefix.concat(offset.getId());
    383         }
    384         return new ZoneRegion(prefix, offset.getRules());
    385     }
    386 
    387     /**
    388      * Parses the ID, taking a flag to indicate whether {@code ZoneRulesException}
    389      * should be thrown or not, used in deserialization.
    390      *
    391      * @param zoneId  the time-zone ID, not null
    392      * @param checkAvailable  whether to check if the zone ID is available
    393      * @return the zone ID, not null
    394      * @throws DateTimeException if the ID format is invalid
    395      * @throws ZoneRulesException if checking availability and the ID cannot be found
    396      */
    397     static ZoneId of(String zoneId, boolean checkAvailable) {
    398         Objects.requireNonNull(zoneId, "zoneId");
    399         if (zoneId.length() <= 1 || zoneId.startsWith("+") || zoneId.startsWith("-")) {
    400             return ZoneOffset.of(zoneId);
    401         } else if (zoneId.startsWith("UTC") || zoneId.startsWith("GMT")) {
    402             return ofWithPrefix(zoneId, 3, checkAvailable);
    403         } else if (zoneId.startsWith("UT")) {
    404             return ofWithPrefix(zoneId, 2, checkAvailable);
    405         }
    406         return ZoneRegion.ofId(zoneId, checkAvailable);
    407     }
    408 
    409     /**
    410      * Parse once a prefix is established.
    411      *
    412      * @param zoneId  the time-zone ID, not null
    413      * @param prefixLength  the length of the prefix, 2 or 3
    414      * @return the zone ID, not null
    415      * @throws DateTimeException if the zone ID has an invalid format
    416      */
    417     private static ZoneId ofWithPrefix(String zoneId, int prefixLength, boolean checkAvailable) {
    418         String prefix = zoneId.substring(0, prefixLength);
    419         if (zoneId.length() == prefixLength) {
    420             return ofOffset(prefix, ZoneOffset.UTC);
    421         }
    422         if (zoneId.charAt(prefixLength) != '+' && zoneId.charAt(prefixLength) != '-') {
    423             return ZoneRegion.ofId(zoneId, checkAvailable);  // drop through to ZoneRulesProvider
    424         }
    425         try {
    426             ZoneOffset offset = ZoneOffset.of(zoneId.substring(prefixLength));
    427             if (offset == ZoneOffset.UTC) {
    428                 return ofOffset(prefix, offset);
    429             }
    430             return ofOffset(prefix, offset);
    431         } catch (DateTimeException ex) {
    432             throw new DateTimeException("Invalid ID for offset-based ZoneId: " + zoneId, ex);
    433         }
    434     }
    435 
    436     //-----------------------------------------------------------------------
    437     /**
    438      * Obtains an instance of {@code ZoneId} from a temporal object.
    439      * <p>
    440      * This obtains a zone based on the specified temporal.
    441      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
    442      * which this factory converts to an instance of {@code ZoneId}.
    443      * <p>
    444      * A {@code TemporalAccessor} represents some form of date and time information.
    445      * This factory converts the arbitrary temporal object to an instance of {@code ZoneId}.
    446      * <p>
    447      * The conversion will try to obtain the zone in a way that favours region-based
    448      * zones over offset-based zones using {@link TemporalQueries#zone()}.
    449      * <p>
    450      * This method matches the signature of the functional interface {@link TemporalQuery}
    451      * allowing it to be used as a query via method reference, {@code ZoneId::from}.
    452      *
    453      * @param temporal  the temporal object to convert, not null
    454      * @return the zone ID, not null
    455      * @throws DateTimeException if unable to convert to a {@code ZoneId}
    456      */
    457     public static ZoneId from(TemporalAccessor temporal) {
    458         ZoneId obj = temporal.query(TemporalQueries.zone());
    459         if (obj == null) {
    460             throw new DateTimeException("Unable to obtain ZoneId from TemporalAccessor: " +
    461                     temporal + " of type " + temporal.getClass().getName());
    462         }
    463         return obj;
    464     }
    465 
    466     //-----------------------------------------------------------------------
    467     /**
    468      * Constructor only accessible within the package.
    469      */
    470     ZoneId() {
    471         if (getClass() != ZoneOffset.class && getClass() != ZoneRegion.class) {
    472             throw new AssertionError("Invalid subclass");
    473         }
    474     }
    475 
    476     //-----------------------------------------------------------------------
    477     /**
    478      * Gets the unique time-zone ID.
    479      * <p>
    480      * This ID uniquely defines this object.
    481      * The format of an offset based ID is defined by {@link ZoneOffset#getId()}.
    482      *
    483      * @return the time-zone unique ID, not null
    484      */
    485     public abstract String getId();
    486 
    487     //-----------------------------------------------------------------------
    488     /**
    489      * Gets the textual representation of the zone, such as 'British Time' or
    490      * '+02:00'.
    491      * <p>
    492      * This returns the textual name used to identify the time-zone ID,
    493      * suitable for presentation to the user.
    494      * The parameters control the style of the returned text and the locale.
    495      * <p>
    496      * If no textual mapping is found then the {@link #getId() full ID} is returned.
    497      *
    498      * @param style  the length of the text required, not null
    499      * @param locale  the locale to use, not null
    500      * @return the text value of the zone, not null
    501      */
    502     public String getDisplayName(TextStyle style, Locale locale) {
    503         return new DateTimeFormatterBuilder().appendZoneText(style).toFormatter(locale).format(toTemporal());
    504     }
    505 
    506     /**
    507      * Converts this zone to a {@code TemporalAccessor}.
    508      * <p>
    509      * A {@code ZoneId} can be fully represented as a {@code TemporalAccessor}.
    510      * However, the interface is not implemented by this class as most of the
    511      * methods on the interface have no meaning to {@code ZoneId}.
    512      * <p>
    513      * The returned temporal has no supported fields, with the query method
    514      * supporting the return of the zone using {@link TemporalQueries#zoneId()}.
    515      *
    516      * @return a temporal equivalent to this zone, not null
    517      */
    518     private TemporalAccessor toTemporal() {
    519         return new TemporalAccessor() {
    520             @Override
    521             public boolean isSupported(TemporalField field) {
    522                 return false;
    523             }
    524             @Override
    525             public long getLong(TemporalField field) {
    526                 throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    527             }
    528             @SuppressWarnings("unchecked")
    529             @Override
    530             public <R> R query(TemporalQuery<R> query) {
    531                 if (query == TemporalQueries.zoneId()) {
    532                     return (R) ZoneId.this;
    533                 }
    534                 return TemporalAccessor.super.query(query);
    535             }
    536         };
    537     }
    538 
    539     //-----------------------------------------------------------------------
    540     // Android-removed: ZoneRulesProvider related paragraph
    541     /**
    542      * Gets the time-zone rules for this ID allowing calculations to be performed.
    543      * <p>
    544      * The rules provide the functionality associated with a time-zone,
    545      * such as finding the offset for a given instant or local date-time.
    546      * <p>
    547      * A time-zone can be invalid if it is deserialized in a Java Runtime which
    548      * does not have the same rules loaded as the Java Runtime that stored it.
    549      * In this case, calling this method will throw a {@code ZoneRulesException}.
    550      * <p>
    551      * {@link ZoneOffset} will always return a set of rules where the offset never changes.
    552      *
    553      * @return the rules, not null
    554      * @throws ZoneRulesException if no rules are available for this ID
    555      */
    556     public abstract ZoneRules getRules();
    557 
    558     /**
    559      * Normalizes the time-zone ID, returning a {@code ZoneOffset} where possible.
    560      * <p>
    561      * The returns a normalized {@code ZoneId} that can be used in place of this ID.
    562      * The result will have {@code ZoneRules} equivalent to those returned by this object,
    563      * however the ID returned by {@code getId()} may be different.
    564      * <p>
    565      * The normalization checks if the rules of this {@code ZoneId} have a fixed offset.
    566      * If they do, then the {@code ZoneOffset} equal to that offset is returned.
    567      * Otherwise {@code this} is returned.
    568      *
    569      * @return the time-zone unique ID, not null
    570      */
    571     public ZoneId normalized() {
    572         try {
    573             ZoneRules rules = getRules();
    574             if (rules.isFixedOffset()) {
    575                 return rules.getOffset(Instant.EPOCH);
    576             }
    577         } catch (ZoneRulesException ex) {
    578             // invalid ZoneRegion is not important to this method
    579         }
    580         return this;
    581     }
    582 
    583     //-----------------------------------------------------------------------
    584     /**
    585      * Checks if this time-zone ID is equal to another time-zone ID.
    586      * <p>
    587      * The comparison is based on the ID.
    588      *
    589      * @param obj  the object to check, null returns false
    590      * @return true if this is equal to the other time-zone ID
    591      */
    592     @Override
    593     public boolean equals(Object obj) {
    594         if (this == obj) {
    595            return true;
    596         }
    597         if (obj instanceof ZoneId) {
    598             ZoneId other = (ZoneId) obj;
    599             return getId().equals(other.getId());
    600         }
    601         return false;
    602     }
    603 
    604     /**
    605      * A hash code for this time-zone ID.
    606      *
    607      * @return a suitable hash code
    608      */
    609     @Override
    610     public int hashCode() {
    611         return getId().hashCode();
    612     }
    613 
    614     //-----------------------------------------------------------------------
    615     /**
    616      * Defend against malicious streams.
    617      *
    618      * @param s the stream to read
    619      * @throws InvalidObjectException always
    620      */
    621     private void readObject(ObjectInputStream s) throws InvalidObjectException {
    622         throw new InvalidObjectException("Deserialization via serialization delegate");
    623     }
    624 
    625     /**
    626      * Outputs this zone as a {@code String}, using the ID.
    627      *
    628      * @return a string representation of this time-zone ID, not null
    629      */
    630     @Override
    631     public String toString() {
    632         return getId();
    633     }
    634 
    635     //-----------------------------------------------------------------------
    636     /**
    637      * Writes the object using a
    638      * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
    639      * @serialData
    640      * <pre>
    641      *  out.writeByte(7);  // identifies a ZoneId (not ZoneOffset)
    642      *  out.writeUTF(getId());
    643      * </pre>
    644      * <p>
    645      * When read back in, the {@code ZoneId} will be created as though using
    646      * {@link #of(String)}, but without any exception in the case where the
    647      * ID has a valid format, but is not in the known set of region-based IDs.
    648      *
    649      * @return the instance of {@code Ser}, not null
    650      */
    651     // this is here for serialization Javadoc
    652     private Object writeReplace() {
    653         return new Ser(Ser.ZONE_REGION_TYPE, this);
    654     }
    655 
    656     abstract void write(DataOutput out) throws IOException;
    657 
    658 }
    659