TimeZone.java revision 5641
5641N/A * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved 0N/A * (C) Copyright IBM Corp. 1996 - All Rights Reserved 0N/A * The original version of this source code and documentation is copyrighted 0N/A * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These 0N/A * materials are provided under terms of a License Agreement between Taligent 0N/A * and Sun. This technology is protected by multiple US and International 0N/A * patents. This notice and attribution to Taligent may not be removed. 0N/A * Taligent is a registered trademark of Taligent, Inc. 0N/A * <code>TimeZone</code> represents a time zone offset, and also figures out daylight 0N/A * Typically, you get a <code>TimeZone</code> using <code>getDefault</code> 0N/A * which creates a <code>TimeZone</code> based on the time zone where the program 0N/A * is running. For example, for a program running in Japan, <code>getDefault</code> 0N/A * creates a <code>TimeZone</code> object based on Japanese Standard Time. 0N/A * You can also get a <code>TimeZone</code> using <code>getTimeZone</code> 0N/A * along with a time zone ID. For instance, the time zone ID for the 0N/A * U.S. Pacific Time <code>TimeZone</code> object with: 0N/A * </pre></blockquote> 0N/A * You can use the <code>getAvailableIDs</code> method to iterate through 0N/A * all the supported time zone IDs. You can then choose a 0N/A * supported ID to get a <code>TimeZone</code>. 0N/A * If the time zone you want is not represented by one of the 0N/A * supported IDs, then a custom time zone ID can be specified to 0N/A * produce a TimeZone. The syntax of a custom time zone ID is: 0N/A * <a name="CustomID"><i>CustomID:</i></a> 0N/A * <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i> 0N/A * <code>GMT</code> <i>Sign</i> <i>Hours</i> <i>Minutes</i> 0N/A * <code>GMT</code> <i>Sign</i> <i>Hours</i> 0N/A * <i>Sign:</i> one of 0N/A * <i>Digit</i> <i>Digit</i> 0N/A * <i>Digit</i> <i>Digit</i> 0N/A * <i>Digit:</i> one of 0N/A * <code>0 1 2 3 4 5 6 7 8 9</code> 0N/A * </pre></blockquote> 0N/A * <i>Hours</i> must be between 0 to 23 and <i>Minutes</i> must be 0N/A * between 00 to 59. For example, "GMT+10" and "GMT+0010" mean ten 0N/A * hours and ten minutes ahead of GMT, respectively. 0N/A * The format is locale independent and digits must be taken from the 0N/A * Basic Latin block of the Unicode standard. No daylight saving time 0N/A * transition schedule can be specified with a custom time zone ID. If 0N/A * the specified string doesn't match the syntax, <code>"GMT"</code> 0N/A * When creating a <code>TimeZone</code>, the specified custom time 0N/A * zone ID is normalized in the following syntax: 0N/A * <a name="NormalizedCustomID"><i>NormalizedCustomID:</i></a> 0N/A * <code>GMT</code> <i>Sign</i> <i>TwoDigitHours</i> <code>:</code> <i>Minutes</i> 0N/A * <i>Sign:</i> one of 0N/A * <i>TwoDigitHours:</i> 0N/A * <i>Digit</i> <i>Digit</i> 0N/A * <i>Digit</i> <i>Digit</i> 0N/A * <i>Digit:</i> one of 0N/A * <code>0 1 2 3 4 5 6 7 8 9</code> 0N/A * </pre></blockquote> 0N/A * For example, TimeZone.getTimeZone("GMT-8").getID() returns "GMT-08:00". 0N/A * <h4>Three-letter time zone IDs</h4> 0N/A * For compatibility with JDK 1.1.x, some other three-letter time zone IDs 0N/A * (such as "PST", "CTT", "AST") are also supported. However, <strong>their 0N/A * use is deprecated</strong> because the same abbreviation is often used 0N/A * for multiple time zones (for example, "CST" could be U.S. "Central Standard 0N/A * Time" and "China Standard Time"), and the Java platform can then only 0N/A * recognize one of them. 0N/A * @see GregorianCalendar 0N/A * @see SimpleTimeZone 0N/A * @author Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu 0N/A * Sole constructor. (For invocation by subclass constructors, typically 0N/A * A style specifier for <code>getDisplayName()</code> indicating 0N/A * a short name, such as "PST." 0N/A * A style specifier for <code>getDisplayName()</code> indicating 0N/A * a long name, such as "Pacific Standard Time." 0N/A // Constants used internally; unit is milliseconds 0N/A // Proclaim serialization compatibility with JDK 1.1 0N/A * Gets the time zone offset, for current date, modified in case of 0N/A * daylight savings. This is the offset to add to UTC to get local time. 0N/A * This method returns a historically correct offset if an 0N/A * underlying <code>TimeZone</code> implementation subclass 0N/A * supports historical Daylight Saving Time schedule and GMT 0N/A * @param era the era of the given date. 0N/A * @param year the year in the given date. 0N/A * @param month the month in the given date. 0N/A * Month is 0-based. e.g., 0 for January. 0N/A * @param day the day-in-month of the given date. 0N/A * @param dayOfWeek the day-of-week of the given date. 0N/A * @param milliseconds the milliseconds in day in <em>standard</em> 0N/A * @return the offset in milliseconds to add to GMT to get local time. 0N/A * @see Calendar#ZONE_OFFSET 0N/A * @see Calendar#DST_OFFSET 0N/A * Returns the offset of this time zone from UTC at the specified 0N/A * date. If Daylight Saving Time is in effect at the specified 0N/A * date, the offset value is adjusted with the amount of daylight 0N/A * This method returns a historically correct offset value if an 0N/A * underlying TimeZone implementation subclass supports historical 0N/A * Daylight Saving Time schedule and GMT offset changes. 0N/A * @param date the date represented in milliseconds since January 1, 1970 00:00:00 GMT 0N/A * @return the amount of time in milliseconds to add to UTC to get local time. 0N/A * @see Calendar#ZONE_OFFSET 0N/A * @see Calendar#DST_OFFSET 0N/A * Gets the raw GMT offset and the amount of daylight saving of this 0N/A * time zone at the given time. 0N/A * @param date the milliseconds (since January 1, 1970, 0N/A * 00:00:00.000 GMT) at which the time zone offset and daylight 0N/A * saving amount are found 0N/A * @param offset an array of int where the raw GMT offset 0N/A * (offset[0]) and daylight saving amount (offset[1]) are stored, 0N/A * or null if those values are not needed. The method assumes that 0N/A * the length of the given array is two or larger. 0N/A * @return the total amount of the raw GMT offset and daylight 0N/A * saving at the specified date. 0N/A * @see Calendar#ZONE_OFFSET 0N/A * @see Calendar#DST_OFFSET 0N/A * Sets the base time zone offset to GMT. 0N/A * This is the offset to add to UTC to get local time. 0N/A * If an underlying <code>TimeZone</code> implementation subclass 0N/A * supports historical GMT offset changes, the specified GMT 0N/A * offset is set as the latest GMT offset and the difference from 0N/A * the known latest GMT offset value is used to adjust all 0N/A * historical GMT offset values. 0N/A * @param offsetMillis the given base time zone offset to GMT. 0N/A * Returns the amount of time in milliseconds to add to UTC to get 0N/A * standard time in this time zone. Because this value is not 0N/A * affected by daylight saving time, it is called <I>raw 0N/A * If an underlying <code>TimeZone</code> implementation subclass 0N/A * supports historical GMT offset changes, the method returns the 0N/A * raw offset value of the current date. In Honolulu, for example, 0N/A * its raw offset changed from GMT-10:30 to GMT-10:00 in 1947, and 0N/A * this method always returns -36000000 milliseconds (i.e., -10 0N/A * @return the amount of raw offset time in milliseconds to add to UTC. 0N/A * @see Calendar#ZONE_OFFSET 0N/A * Gets the ID of this time zone. 0N/A * @return the ID of this time zone. 0N/A * Sets the time zone ID. This does not change any other data in 0N/A * the time zone object. 0N/A * @param ID the new time zone ID. 2875N/A * Returns a long standard time name of this {@code TimeZone} suitable for 2875N/A * presentation to the user in the default locale. 2875N/A * <p>This method is equivalent to: 2875N/A * getDisplayName(false, {@link #LONG}, 2875N/A * Locale.getDefault({@link Locale.Category#DISPLAY})) 0N/A * @return the human-readable name of this time zone in the default locale. 2875N/A * @see #getDisplayName(boolean, int, Locale) 2875N/A * @see Locale#getDefault(Locale.Category) 2875N/A * Returns a long standard time name of this {@code TimeZone} suitable for 2875N/A * presentation to the user in the specified {@code locale}. 2875N/A * <p>This method is equivalent to: 2875N/A * getDisplayName(false, {@link #LONG}, locale) 0N/A * @param locale the locale in which to supply the display name. 0N/A * @return the human-readable name of this time zone in the given locale. 2875N/A * @exception NullPointerException if {@code locale} is {@code null}. 2875N/A * @see #getDisplayName(boolean, int, Locale) 2875N/A * Returns a name in the specified {@code style} of this {@code TimeZone} 2875N/A * suitable for presentation to the user in the default locale. If the 3624N/A * specified {@code daylight} is {@code true}, a Daylight Saving Time name 3624N/A * is returned (even if this {@code TimeZone} doesn't observe Daylight Saving 3624N/A * Time). Otherwise, a Standard Time name is returned. 2875N/A * <p>This method is equivalent to: 2875N/A * getDisplayName(daylight, style, 2875N/A * Locale.getDefault({@link Locale.Category#DISPLAY})) 3624N/A * @param daylight {@code true} specifying a Daylight Saving Time name, or 3624N/A * {@code false} specifying a Standard Time name 2875N/A * @param style either {@link #LONG} or {@link #SHORT} 0N/A * @return the human-readable name of this time zone in the default locale. 2875N/A * @exception IllegalArgumentException if {@code style} is invalid. 2875N/A * @see #getDisplayName(boolean, int, Locale) 2875N/A * @see Locale#getDefault(Locale.Category) 3624N/A * @see java.text.DateFormatSymbols#getZoneStrings() 2875N/A * Returns a name in the specified {@code style} of this {@code TimeZone} 2875N/A * suitable for presentation to the user in the specified {@code 3624N/A * locale}. If the specified {@code daylight} is {@code true}, a Daylight 3624N/A * Saving Time name is returned (even if this {@code TimeZone} doesn't 3624N/A * observe Daylight Saving Time). Otherwise, a Standard Time name is 2875N/A * <p>When looking up a time zone name, the {@linkplain 2875N/A * ResourceBundle.Control#getCandidateLocales(String,Locale) default 2875N/A * <code>Locale</code> search path of <code>ResourceBundle</code>} derived 2875N/A * from the specified {@code locale} is used. (No {@linkplain 2875N/A * ResourceBundle.Control#getFallbackLocale(String,Locale) fallback 2875N/A * <code>Locale</code>} search is performed.) If a time zone name in any 2875N/A * {@code Locale} of the search path, including {@link Locale#ROOT}, is 2875N/A * found, the name is returned. Otherwise, a string in the 2875N/A * <a href="#NormalizedCustomID">normalized custom ID format</a> is returned. 3624N/A * @param daylight {@code true} specifying a Daylight Saving Time name, or 3624N/A * {@code false} specifying a Standard Time name 2875N/A * @param style either {@link #LONG} or {@link #SHORT} 3624N/A * @param locale the locale in which to supply the display name. 0N/A * @return the human-readable name of this time zone in the given locale. 2875N/A * @exception IllegalArgumentException if {@code style} is invalid. 2875N/A * @exception NullPointerException if {@code locale} is {@code null}. 3624N/A * @see java.text.DateFormatSymbols#getZoneStrings() 0N/A // Cache for managing display names per timezone per locale 0N/A // The structure is: 0N/A // Map(key=id, value=SoftReference(Map(key=locale, value=displaynames))) 0N/A * Returns the amount of time to be added to local standard time 0N/A * to get local wall clock time. 3558N/A * <p>The default implementation returns 3600000 milliseconds 3558N/A * (i.e., one hour) if a call to {@link #useDaylightTime()} 3558N/A * returns {@code true}. Otherwise, 0 (zero) is returned. 3558N/A * <p>If an underlying {@code TimeZone} implementation subclass 3558N/A * supports historical and future Daylight Saving Time schedule 3558N/A * changes, this method returns the amount of saving time of the 3558N/A * last known Daylight Saving Time rule that can be a future 3558N/A * <p>If the amount of saving time at any given time stamp is 3558N/A * required, construct a {@link Calendar} with this {@code 3558N/A * TimeZone} and the time stamp, and call {@link Calendar#get(int) 3558N/A * Calendar.get}{@code (}{@link Calendar#DST_OFFSET}{@code )}. 0N/A * @return the amount of saving time in milliseconds 3558N/A * @see #inDaylightTime(Date) 3558N/A * @see #getOffset(int,int,int,int,int,int) 3558N/A * @see Calendar#ZONE_OFFSET 3558N/A * Queries if this {@code TimeZone} uses Daylight Saving Time. 3558N/A * <p>If an underlying {@code TimeZone} implementation subclass 3558N/A * supports historical and future Daylight Saving Time schedule 3558N/A * changes, this method refers to the last known Daylight Saving Time 3558N/A * rule that can be a future prediction and may not be the same as 3558N/A * the current rule. Consider calling {@link #observesDaylightTime()} 3558N/A * if the current rule should also be taken into account. 3558N/A * @return {@code true} if this {@code TimeZone} uses Daylight Saving Time, 3558N/A * {@code false}, otherwise. 3558N/A * @see #inDaylightTime(Date) 3558N/A * Returns {@code true} if this {@code TimeZone} is currently in 3558N/A * Daylight Saving Time, or if a transition from Standard Time to 3558N/A * Daylight Saving Time occurs at any future time. 3558N/A * <p>The default implementation returns {@code true} if 3558N/A * {@code useDaylightTime()} or {@code inDaylightTime(new Date())} 3558N/A * @return {@code true} if this {@code TimeZone} is currently in 3558N/A * Daylight Saving Time, or if a transition from Standard Time to 3558N/A * Daylight Saving Time occurs at any future time; {@code false} 3558N/A * @see #inDaylightTime(Date) 3558N/A * Queries if the given {@code date} is in Daylight Saving Time in 3624N/A * @param date the given Date. 3624N/A * @return {@code true} if the given date is in Daylight Saving Time, 3558N/A * {@code false}, otherwise. 0N/A * Gets the <code>TimeZone</code> for the given ID. 0N/A * @param ID the ID for a <code>TimeZone</code>, either an abbreviation 0N/A * ID such as "GMT-8:00". Note that the support of abbreviations is 0N/A * for JDK 1.1.x compatibility only and full names should be used. 0N/A * @return the specified <code>TimeZone</code>, or the GMT zone if the given ID 0N/A * cannot be understood. 0N/A * Gets the available IDs according to the given time zone offset in milliseconds. 0N/A * @param rawOffset the given time zone GMT offset in milliseconds. 0N/A * @return an array of IDs, where the time zone for that ID has 3624N/A * both have GMT-07:00, but differ in daylight saving behavior. 0N/A * @see #getRawOffset() 0N/A * Gets all the available IDs supported. 0N/A * @return an array of IDs. 0N/A * Gets the platform defined TimeZone ID. 0N/A * Gets the custom time zone ID based on the GMT offset of the 0N/A * platform. (e.g., "GMT+08:00") 0N/A * Gets the default <code>TimeZone</code> for this host. 0N/A * The source of the default <code>TimeZone</code> 0N/A * may vary with implementation. 0N/A * @return a default <code>TimeZone</code>. 0N/A * Returns the reference to the default TimeZone object. This 0N/A * method doesn't create a clone. 0N/A // Need to initialize the default time zone. 0N/A // Don't clone here. 0N/A // get the time zone ID from the system properties 0N/A // if the time zone ID is not set (yet), perform the 0N/A // platform to Java time zone ID mapping. 0N/A // Get the time zone for zoneID. But not fall back to 0N/A // If the given zone ID is unknown in Java, try to 0N/A // get the GMT-offset-based time zone ID, 0N/A // a.k.a. custom time zone ID (e.g., "GMT-08:00"). 0N/A (
"user.timezone",
"write"));
0N/A * Sets the <code>TimeZone</code> that is 0N/A * returned by the <code>getDefault</code> method. If <code>zone</code> 0N/A * is null, reset the default to the value it had originally when the 0N/A * @param zone the new default time zone 4741N/A * Returns the default TimeZone in an AppContext if any AppContext 4741N/A * has ever used. null is returned if any AppContext hasn't been 4741N/A * used or if the AppContext doesn't have the default TimeZone. 5641N/A * Note that javaAWTAccess may be null if sun.awt.AppContext class hasn't 5641N/A * been loaded. If so, it implies that AWTSecurityManager is not our 5641N/A * SecurityManager and we can use a local static variable. 5641N/A * This works around a build time issue. 5641N/A // JavaAWTAccess provides access implementation-private methods without using reflection. 4741N/A * Sets the default TimeZone in the AppContext to the given 4741N/A * tz. null is handled special: do nothing if any AppContext 4741N/A * hasn't been used, remove the default TimeZone in the 5641N/A * Note that javaAWTAccess may be null if sun.awt.AppContext class hasn't 5641N/A * been loaded. If so, it implies that AWTSecurityManager is not our 5641N/A * SecurityManager and we can use a local static variable. 5641N/A * This works around a build time issue. 5641N/A // JavaAWTAccess provides access implementation-private methods without using reflection. 0N/A * Returns true if this zone has the same rule and offset as another zone. 0N/A * That is, if this zone differs only in ID, if at all. Returns false 0N/A * if the other zone is null. 0N/A * @param other the <code>TimeZone</code> object to be compared with 0N/A * @return true if the other zone is not null and is the same as this one, 0N/A * with the possible exception of the ID 0N/A * Creates a copy of this <code>TimeZone</code>. 0N/A * @return a clone of this <code>TimeZone</code> 0N/A * The null constant as a TimeZone. 0N/A // =======================privates=============================== 0N/A * The string identifier of this <code>TimeZone</code>. This is a 0N/A * programmatic identifier used internally to look up <code>TimeZone</code> 0N/A * objects from the system table and also to map them to their localized 0N/A * display names. <code>ID</code> values are unique in the system 0N/A * table but may not be for dynamically created zones. 5393N/A // a static TimeZone we can reference if no AppContext is in place 0N/A * Parses a custom time zone identifier and returns a corresponding zone. 0N/A * This method doesn't support the RFC 822 time zone format. (e.g., +hhmm) 0N/A * @param id a string of the <a href="#CustomID">custom ID form</a>. 0N/A * @return a newly created TimeZone with the given offset and 0N/A * no daylight saving time, or null if the id cannot be parsed. 0N/A // Error if the length of id isn't long enough or id doesn't 0N/A // start with "GMT". 0N/A // First, we try to find it in the cache with the given 0N/A // id. Even the id is not normalized, the returned ZoneInfo 0N/A // should have its normalized id. 0N/A }
else if (c !=
'+') {
0N/A if (c <
'0' || c >
'9') {