src/share/classes/java/time/ZoneId.java

Print this page




  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.Serializable;
  67 import java.time.format.DateTimeFormatterBuilder;
  68 import java.time.format.TextStyle;
  69 import java.time.temporal.Queries;
  70 import java.time.temporal.TemporalAccessor;
  71 import java.time.temporal.TemporalField;
  72 import java.time.temporal.TemporalQuery;
  73 import java.time.zone.ZoneRules;

  74 import java.time.zone.ZoneRulesProvider;
  75 import java.util.Collections;
  76 import java.util.HashMap;
  77 import java.util.Locale;
  78 import java.util.Map;
  79 import java.util.Objects;
  80 import java.util.TimeZone;
  81 
  82 /**
  83  * A time-zone ID, such as {@code Europe/Paris}.
  84  * <p>
  85  * A {@code ZoneId} is used to identify the rules used to convert between
  86  * an {@link Instant} and a {@link LocalDateTime}.
  87  * There are two distinct types of ID:
  88  * <p><ul>
  89  * <li>Fixed offsets - a fully resolved offset from UTC/Greenwich, that uses
  90  *  the same offset for all local date-times
  91  * <li>Geographical regions - an area where a specific set of rules for finding
  92  *  the offset from UTC/Greenwich apply
  93  * </ul><p>
  94  * Most fixed offsets are represented by {@link ZoneOffset}.
  95  * <p>
  96  * The actual rules, describing when and how the offset changes, are defined by {@link ZoneRules}.
  97  * This class is simply an ID used to obtain the underlying rules.
  98  * This approach is taken because rules are defined by governments and change
  99  * frequently, whereas the ID is stable.
 100  * <p>
 101  * The distinction has other effects. Serializing the {@code ZoneId} will only send
 102  * the ID, whereas serializing the rules sends the entire data set.
 103  * Similarly, a comparison of two IDs only examines the ID, whereas
 104  * a comparison of two rules examines the entire data set.
 105  * <p>
 106  * The code supports loading a {@code ZoneId} on a JVM which does not have available rules
 107  * for that ID. This allows the date-time object, such as {@link ZonedDateTime},
 108  * to still be queried.
 109  *
 110  * <h3>Time-zone IDs</h3>
 111  * The ID is unique within the system.
 112  * The formats for offset and region IDs differ.
 113  * <p>
 114  * An ID is parsed as an offset ID if it starts with 'UTC', 'GMT', '+' or '-', or
 115  * is a single letter.
 116  * For example, 'Z', '+02:00', '-05:00', 'UTC+05' and 'GMT-6' are all valid offset IDs.
 117  * Note that some IDs, such as 'D' or '+ABC' meet the criteria, but are invalid.

 118  * <p>
 119  * All other IDs are considered to be region IDs.
 120  * <p>
 121  * Region IDs are defined by configuration, which can be thought of as a {@code Map}
 122  * from region ID to {@code ZoneRules}, see {@link ZoneRulesProvider}.
 123  * <p>
 124  * Time-zones are defined by governments and change frequently. There are a number of
 125  * organizations, known here as groups, that monitor time-zone changes and collate them.
 126  * The default group is the IANA Time Zone Database (TZDB).
 127  * Other organizations include IATA (the airline industry body) and Microsoft.
 128  * <p>
 129  * Each group defines its own format for region ID.
 130  * The TZDB group defines IDs such as 'Europe/London' or 'America/New_York'.
 131  * TZDB IDs take precedence over other groups.
 132  * <p>
 133  * It is strongly recommended that the group name is included in all Ids supplied by
 134  * groups other than TZDB to avoid conflicts. For example, IATA airline time-zone
 135  * region IDs are typically the same as the three letter airport code.
 136  * However, the airport of Utrecht has the code 'UTC', which is obviously a conflict.
 137  * The recommended format for region IDs from groups other than TZDB is 'group~region'.
 138  * Thus if IATA data were defined, Utrecht airport would be 'IATA~UTC'.
 139  *
 140  * <h3>Specification for implementors</h3>
 141  * This abstract class has two implementations, both of which are immutable and thread-safe.
 142  * One implementation models region-based IDs, the other is {@code ZoneOffset} modelling
 143  * offset-based IDs.
 144  *
 145  * @since 1.8
 146  */
 147 public abstract class ZoneId implements Serializable {
 148 
 149     /**
 150      * A map of zone overrides to enable the older US time-zone names to be used.
 151      * <p>
 152      * This maps as follows:
 153      * <p><ul>
 154      * <li>EST - America/Indianapolis</li>
 155      * <li>MST - America/Phoenix</li>
 156      * <li>HST - Pacific/Honolulu</li>
 157      * <li>ACT - Australia/Darwin</li>
 158      * <li>AET - Australia/Sydney</li>
 159      * <li>AGT - America/Argentina/Buenos_Aires</li>
 160      * <li>ART - Africa/Cairo</li>
 161      * <li>AST - America/Anchorage</li>
 162      * <li>BET - America/Sao_Paulo</li>
 163      * <li>BST - Asia/Dhaka</li>
 164      * <li>CAT - Africa/Harare</li>
 165      * <li>CNT - America/St_Johns</li>
 166      * <li>CST - America/Chicago</li>
 167      * <li>CTT - Asia/Shanghai</li>
 168      * <li>EAT - Africa/Addis_Ababa</li>
 169      * <li>ECT - Europe/Paris</li>
 170      * <li>IET - America/Indiana/Indianapolis</li>
 171      * <li>IST - Asia/Kolkata</li>
 172      * <li>JST - Asia/Tokyo</li>
 173      * <li>MIT - Pacific/Apia</li>
 174      * <li>NET - Asia/Yerevan</li>
 175      * <li>NST - Pacific/Auckland</li>


 231         base.put("BST", "Asia/Dhaka");
 232         base.put("CAT", "Africa/Harare");
 233         base.put("CNT", "America/St_Johns");
 234         base.put("CST", "America/Chicago");
 235         base.put("CTT", "Asia/Shanghai");
 236         base.put("EAT", "Africa/Addis_Ababa");
 237         base.put("ECT", "Europe/Paris");
 238         base.put("IET", "America/Indiana/Indianapolis");
 239         base.put("IST", "Asia/Kolkata");
 240         base.put("JST", "Asia/Tokyo");
 241         base.put("MIT", "Pacific/Apia");
 242         base.put("NET", "Asia/Yerevan");
 243         base.put("NST", "Pacific/Auckland");
 244         base.put("PLT", "Asia/Karachi");
 245         base.put("PNT", "America/Phoenix");
 246         base.put("PRT", "America/Puerto_Rico");
 247         base.put("PST", "America/Los_Angeles");
 248         base.put("SST", "Pacific/Guadalcanal");
 249         base.put("VST", "Asia/Ho_Chi_Minh");
 250         Map<String, String> pre = new HashMap<>(base);
 251         pre.put("EST", "America/Indianapolis");
 252         pre.put("MST", "America/Phoenix");
 253         pre.put("HST", "Pacific/Honolulu");
 254         OLD_IDS_PRE_2005 = Collections.unmodifiableMap(pre);
 255         Map<String, String> post = new HashMap<>(base);
 256         post.put("EST", "-05:00");
 257         post.put("MST", "-07:00");
 258         post.put("HST", "-10:00");
 259         OLD_IDS_POST_2005 = Collections.unmodifiableMap(post);
 260     }
 261     /**
 262      * Serialization version.
 263      */
 264     private static final long serialVersionUID = 8352817235686L;
 265 
 266     //-----------------------------------------------------------------------
 267     /**
 268      * Gets the system default time-zone.
 269      * <p>
 270      * This queries {@link TimeZone#getDefault()} to find the default time-zone
 271      * and converts it to a {@code ZoneId}. If the system default time-zone is changed,
 272      * then the result of this method will also change.
 273      *
 274      * @return the zone ID, not null
 275      * @throws DateTimeException if the converted zone ID has an invalid format
 276      * @throws java.time.zone.ZoneRulesException if the converted zone region ID cannot be found
 277      */
 278     public static ZoneId systemDefault() {
 279         return ZoneId.of(TimeZone.getDefault().getID(), OLD_IDS_POST_2005);
 280     }
 281 
 282     //-----------------------------------------------------------------------
 283     /**
 284      * Obtains an instance of {@code ZoneId} using its ID using a map
 285      * of aliases to supplement the standard zone IDs.
 286      * <p>
 287      * Many users of time-zones use short abbreviations, such as PST for
 288      * 'Pacific Standard Time' and PDT for 'Pacific Daylight Time'.
 289      * These abbreviations are not unique, and so cannot be used as IDs.
 290      * This method allows a map of string to time-zone to be setup and reused
 291      * within an application.
 292      *
 293      * @param zoneId  the time-zone ID, not null
 294      * @param aliasMap  a map of alias zone IDs (typically abbreviations) to real zone IDs, not null
 295      * @return the zone ID, not null
 296      * @throws DateTimeException if the zone ID has an invalid format
 297      * @throws java.time.zone.ZoneRulesException if the zone region ID cannot be found
 298      */
 299     public static ZoneId of(String zoneId, Map<String, String> aliasMap) {
 300         Objects.requireNonNull(zoneId, "zoneId");
 301         Objects.requireNonNull(aliasMap, "aliasMap");
 302         String id = aliasMap.get(zoneId);
 303         id = (id != null ? id : zoneId);
 304         return of(id);
 305     }
 306 
 307     /**
 308      * Obtains an instance of {@code ZoneId} from an ID ensuring that the
 309      * ID is valid and available for use.
 310      * <p>
 311      * This method parses the ID, applies any appropriate normalization, and validates it
 312      * against the known set of IDs for which rules are available.
 313      * <p>
 314      * An ID is parsed as though it is an offset ID if it starts with 'UTC', 'GMT', '+'
 315      * or '-', or if it has less then two letters.
 316      * The offset of {@link ZoneOffset#UTC zero} may be represented in multiple ways,
 317      * including 'Z', 'UTC', 'GMT', 'UTC0' 'GMT0', '+00:00', '-00:00' and 'UTC+00:00'.
 318      * <p>
 319      * Eight forms of ID are recognized, where '{offset}' means to parse using {@link ZoneOffset#of(String)}:
 320      * <p><ul>
 321      * <li><code>{offset}</code> - a {@link ZoneOffset} ID, such as 'Z' or '+02:00'
 322      * <li><code>UTC</code> - alternate form of a {@code ZoneOffset} ID equal to 'Z'
 323      * <li><code>UTC0</code> - alternate form of a {@code ZoneOffset} ID equal to 'Z'
 324      * <li><code>UTC{offset}</code> - alternate form of a {@code ZoneOffset} ID equal to '{offset}'
 325      * <li><code>GMT</code> - alternate form of a {@code ZoneOffset} ID equal to 'Z'
 326      * <li><code>GMT0</code> - alternate form of a {@code ZoneOffset} ID equal to 'Z'
 327      * <li><code>GMT{offset}</code> - alternate form of a {@code ZoneOffset} ID equal to '{offset}'r
 328      * <li><code>{regionID}</code> - full region ID, loaded from configuration
 329      * </ul><p>


 330      * Region IDs must match the regular expression <code>[A-Za-z][A-Za-z0-9~/._+-]+</code>.
 331      * <p>
 332      * The detailed format of the region ID depends on the group supplying the data.
 333      * The default set of data is supplied by the IANA Time Zone Database (TZDB)
 334      * This has region IDs of the form '{area}/{city}', such as 'Europe/Paris' or 'America/New_York'.
 335      * This is compatible with most IDs from {@link java.util.TimeZone}.
 336      *
 337      * @param zoneId  the time-zone ID, not null
 338      * @return the zone ID, not null
 339      * @throws DateTimeException if the zone ID has an invalid format
 340      * @throws java.time.zone.ZoneRulesException if the zone region ID cannot be found
 341      */
 342     public static ZoneId of(String zoneId) {
 343         Objects.requireNonNull(zoneId, "zoneId");
 344         if (zoneId.length() <= 1 || zoneId.startsWith("+") || zoneId.startsWith("-")) {
 345             return ZoneOffset.of(zoneId);
 346         } else if (zoneId.startsWith("UTC") || zoneId.startsWith("GMT")) {
 347             if (zoneId.length() == 3 || (zoneId.length() == 4 && zoneId.charAt(3) == '0')) {


















 348                 return ZoneOffset.UTC;
 349             }
 350             return ZoneOffset.of(zoneId.substring(3));




 351         }
 352         return ZoneRegion.ofId(zoneId, true);

 353     }
 354 
 355     //-----------------------------------------------------------------------
 356     /**
 357      * Obtains an instance of {@code ZoneId} from a temporal object.
 358      * <p>




 359      * A {@code TemporalAccessor} represents some form of date and time information.
 360      * This factory converts the arbitrary temporal object to an instance of {@code ZoneId}.
 361      * <p>
 362      * The conversion will try to obtain the zone in a way that favours region-based
 363      * zones over offset-based zones using {@link Queries#zone()}.
 364      * <p>
 365      * This method matches the signature of the functional interface {@link TemporalQuery}
 366      * allowing it to be used in queries via method reference, {@code ZoneId::from}.
 367      *
 368      * @param temporal  the temporal object to convert, not null
 369      * @return the zone ID, not null
 370      * @throws DateTimeException if unable to convert to a {@code ZoneId}
 371      */
 372     public static ZoneId from(TemporalAccessor temporal) {
 373         ZoneId obj = temporal.query(Queries.zone());
 374         if (obj == null) {
 375             throw new DateTimeException("Unable to obtain ZoneId from TemporalAccessor: " + temporal.getClass());
 376         }
 377         return obj;
 378     }


 400 
 401     //-----------------------------------------------------------------------
 402     /**
 403      * Gets the time-zone rules for this ID allowing calculations to be performed.
 404      * <p>
 405      * The rules provide the functionality associated with a time-zone,
 406      * such as finding the offset for a given instant or local date-time.
 407      * <p>
 408      * A time-zone can be invalid if it is deserialized in a JVM which does not
 409      * have the same rules loaded as the JVM that stored it. In this case, calling
 410      * this method will throw an exception.
 411      * <p>
 412      * The rules are supplied by {@link ZoneRulesProvider}. An advanced provider may
 413      * support dynamic updates to the rules without restarting the JVM.
 414      * If so, then the result of this method may change over time.
 415      * Each individual call will be still remain thread-safe.
 416      * <p>
 417      * {@link ZoneOffset} will always return a set of rules where the offset never changes.
 418      *
 419      * @return the rules, not null
 420      * @throws DateTimeException if no rules are available for this ID
 421      */
 422     public abstract ZoneRules getRules();
 423 
 424     //-----------------------------------------------------------------------
 425     /**
 426      * Gets the textual representation of the zone, such as 'British Time' or
 427      * '+02:00'.
 428      * <p>
 429      * This returns a textual description for the time-zone ID.


 430      * <p>
 431      * If no textual mapping is found then the {@link #getId() full ID} is returned.
 432      *
 433      * @param style  the length of the text required, not null
 434      * @param locale  the locale to use, not null
 435      * @return the text value of the zone, not null
 436      */
 437     public String getText(TextStyle style, Locale locale) {
 438         return new DateTimeFormatterBuilder().appendZoneText(style).toFormatter(locale).print(new TemporalAccessor() {
 439             @Override
 440             public boolean isSupported(TemporalField field) {
 441                 return false;
 442             }
 443             @Override
 444             public long getLong(TemporalField field) {
 445                 throw new DateTimeException("Unsupported field: " + field);
 446             }
 447             @SuppressWarnings("unchecked")
 448             @Override
 449             public <R> R query(TemporalQuery<R> query) {
 450                 if (query == Queries.zoneId()) {
 451                     return (R) ZoneId.this;
 452                 }
 453                 return TemporalAccessor.super.query(query);
 454             }
 455         });
 456     }
 457 
 458     //-----------------------------------------------------------------------




  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.Serializable;
  67 import java.time.format.DateTimeFormatterBuilder;
  68 import java.time.format.TextStyle;
  69 import java.time.temporal.Queries;
  70 import java.time.temporal.TemporalAccessor;
  71 import java.time.temporal.TemporalField;
  72 import java.time.temporal.TemporalQuery;
  73 import java.time.zone.ZoneRules;
  74 import java.time.zone.ZoneRulesException;
  75 import java.time.zone.ZoneRulesProvider;
  76 import java.util.Collections;
  77 import java.util.HashMap;
  78 import java.util.Locale;
  79 import java.util.Map;
  80 import java.util.Objects;
  81 import java.util.TimeZone;
  82 
  83 /**
  84  * A time-zone ID, such as {@code Europe/Paris}.
  85  * <p>
  86  * A {@code ZoneId} is used to identify the rules used to convert between
  87  * an {@link Instant} and a {@link LocalDateTime}.
  88  * There are two distinct types of ID:
  89  * <p><ul>
  90  * <li>Fixed offsets - a fully resolved offset from UTC/Greenwich, that uses
  91  *  the same offset for all local date-times
  92  * <li>Geographical regions - an area where a specific set of rules for finding
  93  *  the offset from UTC/Greenwich apply
  94  * </ul><p>
  95  * Most fixed offsets are represented by {@link ZoneOffset}.
  96  * <p>
  97  * The actual rules, describing when and how the offset changes, are defined by {@link ZoneRules}.
  98  * This class is simply an ID used to obtain the underlying rules.
  99  * This approach is taken because rules are defined by governments and change
 100  * frequently, whereas the ID is stable.
 101  * <p>
 102  * The distinction has other effects. Serializing the {@code ZoneId} will only send
 103  * the ID, whereas serializing the rules sends the entire data set.
 104  * Similarly, a comparison of two IDs only examines the ID, whereas
 105  * a comparison of two rules examines the entire data set.
 106  * <p>
 107  * The code supports loading a {@code ZoneId} on a JVM which does not have available rules
 108  * for that ID. This allows the date-time object, such as {@link ZonedDateTime},
 109  * to still be queried.
 110  *
 111  * <h3>Time-zone IDs</h3>
 112  * The ID is unique within the system.
 113  * The formats for offset and region IDs differ.
 114  * <p>
 115  * An ID is parsed as an offset ID if it starts with 'UTC', 'GMT', 'UT' '+' or '-', or
 116  * is a single letter. For example, 'Z', '+02:00', '-05:00', 'UTC+05', 'GMT-6' and
 117  * 'UT+01:00' are all valid offset IDs.
 118  * Note that some IDs, such as 'D' or '+ABC' meet the criteria to be parsed as offset IDs,
 119  * but have an invalid offset.
 120  * <p>
 121  * All other IDs are considered to be region IDs.
 122  * <p>
 123  * Region IDs are defined by configuration, which can be thought of as a {@code Map}
 124  * from region ID to {@code ZoneRules}, see {@link ZoneRulesProvider}.
 125  * <p>
 126  * Time-zones are defined by governments and change frequently. There are a number of
 127  * organizations, known here as groups, that monitor time-zone changes and collate them.
 128  * The default group is the IANA Time Zone Database (TZDB).
 129  * Other organizations include IATA (the airline industry body) and Microsoft.
 130  * <p>
 131  * Each group defines its own format for the region ID it provides.
 132  * The TZDB group defines IDs such as 'Europe/London' or 'America/New_York'.
 133  * TZDB IDs take precedence over other groups.
 134  * <p>
 135  * It is strongly recommended that the group name is included in all IDs supplied by
 136  * groups other than TZDB to avoid conflicts. For example, IATA airline time-zone
 137  * region IDs are typically the same as the three letter airport code.
 138  * However, the airport of Utrecht has the code 'UTC', which is obviously a conflict.
 139  * The recommended format for region IDs from groups other than TZDB is 'group~region'.
 140  * Thus if IATA data were defined, Utrecht airport would be 'IATA~UTC'.
 141  *
 142  * <h3>Specification for implementors</h3>
 143  * This abstract class has two implementations, both of which are immutable and thread-safe.
 144  * One implementation models region-based IDs, the other is {@code ZoneOffset} modelling
 145  * offset-based IDs. This difference is visible in serialization.
 146  *
 147  * @since 1.8
 148  */
 149 public abstract class ZoneId implements Serializable {
 150 
 151     /**
 152      * A map of zone overrides to enable the older US time-zone names to be used.
 153      * <p>
 154      * This maps as follows:
 155      * <p><ul>
 156      * <li>EST - America/New_York</li>
 157      * <li>MST - America/Denver</li>
 158      * <li>HST - Pacific/Honolulu</li>
 159      * <li>ACT - Australia/Darwin</li>
 160      * <li>AET - Australia/Sydney</li>
 161      * <li>AGT - America/Argentina/Buenos_Aires</li>
 162      * <li>ART - Africa/Cairo</li>
 163      * <li>AST - America/Anchorage</li>
 164      * <li>BET - America/Sao_Paulo</li>
 165      * <li>BST - Asia/Dhaka</li>
 166      * <li>CAT - Africa/Harare</li>
 167      * <li>CNT - America/St_Johns</li>
 168      * <li>CST - America/Chicago</li>
 169      * <li>CTT - Asia/Shanghai</li>
 170      * <li>EAT - Africa/Addis_Ababa</li>
 171      * <li>ECT - Europe/Paris</li>
 172      * <li>IET - America/Indiana/Indianapolis</li>
 173      * <li>IST - Asia/Kolkata</li>
 174      * <li>JST - Asia/Tokyo</li>
 175      * <li>MIT - Pacific/Apia</li>
 176      * <li>NET - Asia/Yerevan</li>
 177      * <li>NST - Pacific/Auckland</li>


 233         base.put("BST", "Asia/Dhaka");
 234         base.put("CAT", "Africa/Harare");
 235         base.put("CNT", "America/St_Johns");
 236         base.put("CST", "America/Chicago");
 237         base.put("CTT", "Asia/Shanghai");
 238         base.put("EAT", "Africa/Addis_Ababa");
 239         base.put("ECT", "Europe/Paris");
 240         base.put("IET", "America/Indiana/Indianapolis");
 241         base.put("IST", "Asia/Kolkata");
 242         base.put("JST", "Asia/Tokyo");
 243         base.put("MIT", "Pacific/Apia");
 244         base.put("NET", "Asia/Yerevan");
 245         base.put("NST", "Pacific/Auckland");
 246         base.put("PLT", "Asia/Karachi");
 247         base.put("PNT", "America/Phoenix");
 248         base.put("PRT", "America/Puerto_Rico");
 249         base.put("PST", "America/Los_Angeles");
 250         base.put("SST", "Pacific/Guadalcanal");
 251         base.put("VST", "Asia/Ho_Chi_Minh");
 252         Map<String, String> pre = new HashMap<>(base);
 253         pre.put("EST", "America/New_York");
 254         pre.put("MST", "America/Denver");
 255         pre.put("HST", "Pacific/Honolulu");
 256         OLD_IDS_PRE_2005 = Collections.unmodifiableMap(pre);
 257         Map<String, String> post = new HashMap<>(base);
 258         post.put("EST", "-05:00");
 259         post.put("MST", "-07:00");
 260         post.put("HST", "-10:00");
 261         OLD_IDS_POST_2005 = Collections.unmodifiableMap(post);
 262     }
 263     /**
 264      * Serialization version.
 265      */
 266     private static final long serialVersionUID = 8352817235686L;
 267 
 268     //-----------------------------------------------------------------------
 269     /**
 270      * Gets the system default time-zone.
 271      * <p>
 272      * This queries {@link TimeZone#getDefault()} to find the default time-zone
 273      * and converts it to a {@code ZoneId}. If the system default time-zone is changed,
 274      * then the result of this method will also change.
 275      *
 276      * @return the zone ID, not null
 277      * @throws DateTimeException if the converted zone ID has an invalid format
 278      * @throws ZoneRulesException if the converted zone region ID cannot be found
 279      */
 280     public static ZoneId systemDefault() {
 281         return ZoneId.of(TimeZone.getDefault().getID(), OLD_IDS_POST_2005);
 282     }
 283 
 284     //-----------------------------------------------------------------------
 285     /**
 286      * Obtains an instance of {@code ZoneId} using its ID using a map
 287      * of aliases to supplement the standard zone IDs.
 288      * <p>
 289      * Many users of time-zones use short abbreviations, such as PST for
 290      * 'Pacific Standard Time' and PDT for 'Pacific Daylight Time'.
 291      * These abbreviations are not unique, and so cannot be used as IDs.
 292      * This method allows a map of string to time-zone to be setup and reused
 293      * within an application.
 294      *
 295      * @param zoneId  the time-zone ID, not null
 296      * @param aliasMap  a map of alias zone IDs (typically abbreviations) to real zone IDs, not null
 297      * @return the zone ID, not null
 298      * @throws DateTimeException if the zone ID has an invalid format
 299      * @throws ZoneRulesException if the zone ID is a region ID that cannot be found
 300      */
 301     public static ZoneId of(String zoneId, Map<String, String> aliasMap) {
 302         Objects.requireNonNull(zoneId, "zoneId");
 303         Objects.requireNonNull(aliasMap, "aliasMap");
 304         String id = aliasMap.get(zoneId);
 305         id = (id != null ? id : zoneId);
 306         return of(id);
 307     }
 308 
 309     /**
 310      * Obtains an instance of {@code ZoneId} from an ID ensuring that the
 311      * ID is valid and available for use.
 312      * <p>
 313      * This method parses the ID, applies any appropriate normalization, and validates it
 314      * against the known set of IDs for which rules are available.
 315      * <p>
 316      * An ID is parsed as though it is an offset ID if it starts with 'UTC', 'GMT', 'UT', '+'
 317      * or '-', or if it has less then two letters.
 318      * The offset of {@linkplain ZoneOffset#UTC zero} may be represented in multiple ways,
 319      * including 'Z', 'UTC', 'GMT', 'UT', 'UTC0', 'GMT0', 'UT0', '+00:00', '-00:00' and 'UTC+00:00'.
 320      * <p>
 321      * Six forms of ID are recognized:
 322      * <p><ul>
 323      * <li><code>Z</code> - an offset of zero, which is {@code ZoneOffset.UTC}
 324      * <li><code>{offset}</code> - a {@code ZoneOffset} ID, such as '+02:00'
 325      * <li><code>{utcPrefix}</code> - a {@code ZoneOffset} ID equal to 'Z'
 326      * <li><code>{utcPrefix}0</code> - a {@code ZoneOffset} ID equal to 'Z'
 327      * <li><code>{utcPrefix}{offset}</code> - a {@code ZoneOffset} ID equal to '{offset}'


 328      * <li><code>{regionID}</code> - full region ID, loaded from configuration
 329      * </ul><p>
 330      * The {offset} is a valid format for {@link ZoneOffset#of(String)}, excluding 'Z'.
 331      * The {utcPrefix} is 'UTC', 'GMT' or 'UT'.
 332      * Region IDs must match the regular expression <code>[A-Za-z][A-Za-z0-9~/._+-]+</code>.
 333      * <p>
 334      * The detailed format of the region ID depends on the group supplying the data.
 335      * The default set of data is supplied by the IANA Time Zone Database (TZDB)
 336      * This has region IDs of the form '{area}/{city}', such as 'Europe/Paris' or 'America/New_York'.
 337      * This is compatible with most IDs from {@link java.util.TimeZone}.
 338      *
 339      * @param zoneId  the time-zone ID, not null
 340      * @return the zone ID, not null
 341      * @throws DateTimeException if the zone ID has an invalid format
 342      * @throws ZoneRulesException if the zone ID is a region ID that cannot be found
 343      */
 344     public static ZoneId of(String zoneId) {
 345         Objects.requireNonNull(zoneId, "zoneId");
 346         if (zoneId.length() <= 1 || zoneId.startsWith("+") || zoneId.startsWith("-")) {
 347             return ZoneOffset.of(zoneId);
 348         } else if (zoneId.startsWith("UTC") || zoneId.startsWith("GMT")) {
 349             return ofWithPrefix(zoneId, 3);
 350         } else if (zoneId.startsWith("UT")) {
 351             return ofWithPrefix(zoneId, 2);
 352         }
 353         return ZoneRegion.ofId(zoneId, true);
 354     }
 355 
 356     /**
 357      * Parse once a prefix is established.
 358      *
 359      * @param zoneId  the time-zone ID, not null
 360      * @param prefixLength  the length of the prefix, 2 or 3
 361      * @return the zone ID, not null
 362      * @return the zone ID, not null
 363      * @throws DateTimeException if the zone ID has an invalid format
 364      */
 365     private static ZoneId ofWithPrefix(String zoneId, int prefixLength) {
 366         if (zoneId.length() == prefixLength ||
 367                 (zoneId.length() == prefixLength + 1 && zoneId.charAt(prefixLength) == '0')) {
 368             return ZoneOffset.UTC;
 369         }
 370         if (zoneId.charAt(prefixLength) == '+' || zoneId.charAt(prefixLength) == '-') {
 371             try {
 372                 return ZoneOffset.of(zoneId.substring(prefixLength));
 373             } catch (DateTimeException ex) {
 374                 throw new DateTimeException("Invalid ID for offset-based ZoneId: " + zoneId, ex);
 375             }
 376         }
 377         throw new DateTimeException("Invalid ID for offset-based ZoneId: " + zoneId);
 378     }
 379 
 380     //-----------------------------------------------------------------------
 381     /**
 382      * Obtains an instance of {@code ZoneId} from a temporal object.
 383      * <p>
 384      * This obtains a zone based on the specified temporal.
 385      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 386      * which this factory converts to an instance of {@code ZoneId}.
 387      * <p>
 388      * A {@code TemporalAccessor} represents some form of date and time information.
 389      * This factory converts the arbitrary temporal object to an instance of {@code ZoneId}.
 390      * <p>
 391      * The conversion will try to obtain the zone in a way that favours region-based
 392      * zones over offset-based zones using {@link Queries#zone()}.
 393      * <p>
 394      * This method matches the signature of the functional interface {@link TemporalQuery}
 395      * allowing it to be used in queries via method reference, {@code ZoneId::from}.
 396      *
 397      * @param temporal  the temporal object to convert, not null
 398      * @return the zone ID, not null
 399      * @throws DateTimeException if unable to convert to a {@code ZoneId}
 400      */
 401     public static ZoneId from(TemporalAccessor temporal) {
 402         ZoneId obj = temporal.query(Queries.zone());
 403         if (obj == null) {
 404             throw new DateTimeException("Unable to obtain ZoneId from TemporalAccessor: " + temporal.getClass());
 405         }
 406         return obj;
 407     }


 429 
 430     //-----------------------------------------------------------------------
 431     /**
 432      * Gets the time-zone rules for this ID allowing calculations to be performed.
 433      * <p>
 434      * The rules provide the functionality associated with a time-zone,
 435      * such as finding the offset for a given instant or local date-time.
 436      * <p>
 437      * A time-zone can be invalid if it is deserialized in a JVM which does not
 438      * have the same rules loaded as the JVM that stored it. In this case, calling
 439      * this method will throw an exception.
 440      * <p>
 441      * The rules are supplied by {@link ZoneRulesProvider}. An advanced provider may
 442      * support dynamic updates to the rules without restarting the JVM.
 443      * If so, then the result of this method may change over time.
 444      * Each individual call will be still remain thread-safe.
 445      * <p>
 446      * {@link ZoneOffset} will always return a set of rules where the offset never changes.
 447      *
 448      * @return the rules, not null
 449      * @throws ZoneRulesException if no rules are available for this ID
 450      */
 451     public abstract ZoneRules getRules();
 452 
 453     //-----------------------------------------------------------------------
 454     /**
 455      * Gets the textual representation of the zone, such as 'British Time' or
 456      * '+02:00'.
 457      * <p>
 458      * This returns the textual name used to identify the time-zone ID,
 459      * suitable for presentation to the user.
 460      * The parameters control the style of the returned text and the locale.
 461      * <p>
 462      * If no textual mapping is found then the {@link #getId() full ID} is returned.
 463      *
 464      * @param style  the length of the text required, not null
 465      * @param locale  the locale to use, not null
 466      * @return the text value of the zone, not null
 467      */
 468     public String getDisplayName(TextStyle style, Locale locale) {
 469         return new DateTimeFormatterBuilder().appendZoneText(style).toFormatter(locale).format(new TemporalAccessor() {
 470             @Override
 471             public boolean isSupported(TemporalField field) {
 472                 return false;
 473             }
 474             @Override
 475             public long getLong(TemporalField field) {
 476                 throw new DateTimeException("Unsupported field: " + field);
 477             }
 478             @SuppressWarnings("unchecked")
 479             @Override
 480             public <R> R query(TemporalQuery<R> query) {
 481                 if (query == Queries.zoneId()) {
 482                     return (R) ZoneId.this;
 483                 }
 484                 return TemporalAccessor.super.query(query);
 485             }
 486         });
 487     }
 488 
 489     //-----------------------------------------------------------------------