0N/A/*
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 *
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 *
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 *
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.
0N/A *
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
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
0N/A * (C) Copyright IBM Corp. 1996 - All Rights Reserved
0N/A *
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 *
0N/A */
0N/A
0N/Apackage java.util;
0N/A
0N/Aimport java.io.Serializable;
0N/Aimport java.lang.ref.SoftReference;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.util.concurrent.ConcurrentHashMap;
5393N/Aimport sun.misc.SharedSecrets;
5393N/Aimport sun.misc.JavaAWTAccess;
0N/Aimport sun.security.action.GetPropertyAction;
0N/Aimport sun.util.TimeZoneNameUtility;
0N/Aimport sun.util.calendar.ZoneInfo;
0N/Aimport sun.util.calendar.ZoneInfoFile;
0N/A
0N/A/**
0N/A * <code>TimeZone</code> represents a time zone offset, and also figures out daylight
0N/A * savings.
0N/A *
0N/A * <p>
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 *
0N/A * <p>
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 zone is "America/Los_Angeles". So, you can get a
0N/A * U.S. Pacific Time <code>TimeZone</code> object with:
0N/A * <blockquote><pre>
0N/A * TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
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 *
0N/A * <blockquote><pre>
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 * <code>+ -</code>
0N/A * <i>Hours:</i>
0N/A * <i>Digit</i>
0N/A * <i>Digit</i> <i>Digit</i>
0N/A * <i>Minutes:</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 *
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 * <p>
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 * is used.
0N/A * <p>
0N/A * When creating a <code>TimeZone</code>, the specified custom time
0N/A * zone ID is normalized in the following syntax:
0N/A * <blockquote><pre>
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 * <code>+ -</code>
0N/A * <i>TwoDigitHours:</i>
0N/A * <i>Digit</i> <i>Digit</i>
0N/A * <i>Minutes:</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 *
0N/A * <h4>Three-letter time zone IDs</h4>
0N/A *
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 *
0N/A *
0N/A * @see Calendar
0N/A * @see GregorianCalendar
0N/A * @see SimpleTimeZone
0N/A * @author Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu
0N/A * @since JDK1.1
0N/A */
0N/Aabstract public class TimeZone implements Serializable, Cloneable {
0N/A /**
0N/A * Sole constructor. (For invocation by subclass constructors, typically
0N/A * implicit.)
0N/A */
0N/A public TimeZone() {
0N/A }
0N/A
0N/A /**
0N/A * A style specifier for <code>getDisplayName()</code> indicating
0N/A * a short name, such as "PST."
0N/A * @see #LONG
0N/A * @since 1.2
0N/A */
0N/A public static final int SHORT = 0;
0N/A
0N/A /**
0N/A * A style specifier for <code>getDisplayName()</code> indicating
0N/A * a long name, such as "Pacific Standard Time."
0N/A * @see #SHORT
0N/A * @since 1.2
0N/A */
0N/A public static final int LONG = 1;
0N/A
0N/A // Constants used internally; unit is milliseconds
0N/A private static final int ONE_MINUTE = 60*1000;
0N/A private static final int ONE_HOUR = 60*ONE_MINUTE;
0N/A private static final int ONE_DAY = 24*ONE_HOUR;
0N/A
0N/A // Proclaim serialization compatibility with JDK 1.1
0N/A static final long serialVersionUID = 3581463369166924961L;
0N/A
0N/A /**
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 * <p>
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 * offset changes.
0N/A *
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 * local time.
0N/A *
0N/A * @return the offset in milliseconds to add to GMT to get local time.
0N/A *
0N/A * @see Calendar#ZONE_OFFSET
0N/A * @see Calendar#DST_OFFSET
0N/A */
0N/A public abstract int getOffset(int era, int year, int month, int day,
0N/A int dayOfWeek, int milliseconds);
0N/A
0N/A /**
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 * saving.
0N/A * <p>
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 *
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 *
0N/A * @see Calendar#ZONE_OFFSET
0N/A * @see Calendar#DST_OFFSET
0N/A * @since 1.4
0N/A */
0N/A public int getOffset(long date) {
0N/A if (inDaylightTime(new Date(date))) {
0N/A return getRawOffset() + getDSTSavings();
0N/A }
0N/A return getRawOffset();
0N/A }
0N/A
0N/A /**
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 *
0N/A * @see Calendar#ZONE_OFFSET
0N/A * @see Calendar#DST_OFFSET
0N/A */
0N/A int getOffsets(long date, int[] offsets) {
0N/A int rawoffset = getRawOffset();
0N/A int dstoffset = 0;
0N/A if (inDaylightTime(new Date(date))) {
0N/A dstoffset = getDSTSavings();
0N/A }
0N/A if (offsets != null) {
0N/A offsets[0] = rawoffset;
0N/A offsets[1] = dstoffset;
0N/A }
0N/A return rawoffset + dstoffset;
0N/A }
0N/A
0N/A /**
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 * <p>
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 *
0N/A * @param offsetMillis the given base time zone offset to GMT.
0N/A */
0N/A abstract public void setRawOffset(int offsetMillis);
0N/A
0N/A /**
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 * offset</I>.
0N/A * <p>
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 * hours).
0N/A *
0N/A * @return the amount of raw offset time in milliseconds to add to UTC.
0N/A * @see Calendar#ZONE_OFFSET
0N/A */
0N/A public abstract int getRawOffset();
0N/A
0N/A /**
0N/A * Gets the ID of this time zone.
0N/A * @return the ID of this time zone.
0N/A */
0N/A public String getID()
0N/A {
0N/A return ID;
0N/A }
0N/A
0N/A /**
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.
0N/A */
0N/A public void setID(String ID)
0N/A {
0N/A if (ID == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A this.ID = ID;
0N/A }
0N/A
0N/A /**
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 *
2875N/A * <p>This method is equivalent to:
2875N/A * <pre><blockquote>
2875N/A * getDisplayName(false, {@link #LONG},
2875N/A * Locale.getDefault({@link Locale.Category#DISPLAY}))
2875N/A * </blockquote></pre>
2875N/A *
0N/A * @return the human-readable name of this time zone in the default locale.
0N/A * @since 1.2
2875N/A * @see #getDisplayName(boolean, int, Locale)
2875N/A * @see Locale#getDefault(Locale.Category)
2875N/A * @see Locale.Category
0N/A */
0N/A public final String getDisplayName() {
2875N/A return getDisplayName(false, LONG,
2875N/A Locale.getDefault(Locale.Category.DISPLAY));
0N/A }
0N/A
0N/A /**
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 *
2875N/A * <p>This method is equivalent to:
2875N/A * <pre><blockquote>
2875N/A * getDisplayName(false, {@link #LONG}, locale)
2875N/A * </blockquote></pre>
2875N/A *
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}.
0N/A * @since 1.2
2875N/A * @see #getDisplayName(boolean, int, Locale)
0N/A */
0N/A public final String getDisplayName(Locale locale) {
0N/A return getDisplayName(false, LONG, locale);
0N/A }
0N/A
0N/A /**
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 *
2875N/A * <p>This method is equivalent to:
2875N/A * <pre><blockquote>
2875N/A * getDisplayName(daylight, style,
2875N/A * Locale.getDefault({@link Locale.Category#DISPLAY}))
2875N/A * </blockquote></pre>
2875N/A *
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.
0N/A * @since 1.2
2875N/A * @see #getDisplayName(boolean, int, Locale)
2875N/A * @see Locale#getDefault(Locale.Category)
2875N/A * @see Locale.Category
3624N/A * @see java.text.DateFormatSymbols#getZoneStrings()
0N/A */
0N/A public final String getDisplayName(boolean daylight, int style) {
2875N/A return getDisplayName(daylight, style,
2875N/A Locale.getDefault(Locale.Category.DISPLAY));
0N/A }
0N/A
0N/A /**
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 * returned.
2875N/A *
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.
2875N/A *
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}.
0N/A * @since 1.2
3624N/A * @see java.text.DateFormatSymbols#getZoneStrings()
0N/A */
0N/A public String getDisplayName(boolean daylight, int style, Locale locale) {
0N/A if (style != SHORT && style != LONG) {
0N/A throw new IllegalArgumentException("Illegal style: " + style);
0N/A }
0N/A
0N/A String id = getID();
0N/A String[] names = getDisplayNames(id, locale);
0N/A if (names == null) {
0N/A if (id.startsWith("GMT")) {
0N/A char sign = id.charAt(3);
0N/A if (sign == '+' || sign == '-') {
0N/A return id;
0N/A }
0N/A }
0N/A int offset = getRawOffset();
0N/A if (daylight) {
0N/A offset += getDSTSavings();
0N/A }
0N/A return ZoneInfoFile.toCustomID(offset);
0N/A }
0N/A
0N/A int index = daylight ? 3 : 1;
0N/A if (style == SHORT) {
0N/A index++;
0N/A }
0N/A return names[index];
0N/A }
0N/A
0N/A private static class DisplayNames {
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 private static final Map<String, SoftReference<Map<Locale, String[]>>> CACHE =
0N/A new ConcurrentHashMap<String, SoftReference<Map<Locale, String[]>>>();
0N/A }
0N/A
0N/A private static final String[] getDisplayNames(String id, Locale locale) {
0N/A Map<String, SoftReference<Map<Locale, String[]>>> displayNames = DisplayNames.CACHE;
0N/A
0N/A SoftReference<Map<Locale, String[]>> ref = displayNames.get(id);
0N/A if (ref != null) {
0N/A Map<Locale, String[]> perLocale = ref.get();
0N/A if (perLocale != null) {
0N/A String[] names = perLocale.get(locale);
0N/A if (names != null) {
0N/A return names;
0N/A }
0N/A names = TimeZoneNameUtility.retrieveDisplayNames(id, locale);
0N/A if (names != null) {
0N/A perLocale.put(locale, names);
0N/A }
0N/A return names;
0N/A }
0N/A }
0N/A
0N/A String[] names = TimeZoneNameUtility.retrieveDisplayNames(id, locale);
0N/A if (names != null) {
0N/A Map<Locale, String[]> perLocale = new ConcurrentHashMap<Locale, String[]>();
0N/A perLocale.put(locale, names);
0N/A ref = new SoftReference<Map<Locale, String[]>>(perLocale);
0N/A displayNames.put(id, ref);
0N/A }
0N/A return names;
0N/A }
0N/A
0N/A /**
0N/A * Returns the amount of time to be added to local standard time
0N/A * to get local wall clock time.
3558N/A *
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 *
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 * prediction.
3558N/A *
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 *
0N/A * @return the amount of saving time in milliseconds
0N/A * @since 1.4
3558N/A * @see #inDaylightTime(Date)
3558N/A * @see #getOffset(long)
3558N/A * @see #getOffset(int,int,int,int,int,int)
3558N/A * @see Calendar#ZONE_OFFSET
0N/A */
0N/A public int getDSTSavings() {
0N/A if (useDaylightTime()) {
0N/A return 3600000;
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A /**
3558N/A * Queries if this {@code TimeZone} uses Daylight Saving Time.
0N/A *
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 *
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 * @see Calendar#DST_OFFSET
0N/A */
0N/A public abstract boolean useDaylightTime();
0N/A
0N/A /**
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 *
3558N/A * <p>The default implementation returns {@code true} if
3558N/A * {@code useDaylightTime()} or {@code inDaylightTime(new Date())}
3558N/A * returns {@code true}.
3558N/A *
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 * otherwise.
3558N/A * @since 1.7
3558N/A * @see #useDaylightTime()
3558N/A * @see #inDaylightTime(Date)
3558N/A * @see Calendar#DST_OFFSET
3558N/A */
3558N/A public boolean observesDaylightTime() {
3558N/A return useDaylightTime() || inDaylightTime(new Date());
3558N/A }
3558N/A
3558N/A /**
3558N/A * Queries if the given {@code date} is in Daylight Saving Time in
3624N/A * this time zone.
3558N/A *
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 */
0N/A abstract public boolean inDaylightTime(Date date);
0N/A
0N/A /**
0N/A * Gets the <code>TimeZone</code> for the given ID.
0N/A *
0N/A * @param ID the ID for a <code>TimeZone</code>, either an abbreviation
0N/A * such as "PST", a full name such as "America/Los_Angeles", or a custom
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 *
0N/A * @return the specified <code>TimeZone</code>, or the GMT zone if the given ID
0N/A * cannot be understood.
0N/A */
0N/A public static synchronized TimeZone getTimeZone(String ID) {
0N/A return getTimeZone(ID, true);
0N/A }
0N/A
0N/A private static TimeZone getTimeZone(String ID, boolean fallback) {
0N/A TimeZone tz = ZoneInfo.getTimeZone(ID);
0N/A if (tz == null) {
0N/A tz = parseCustomTimeZone(ID);
0N/A if (tz == null && fallback) {
0N/A tz = new ZoneInfo(GMT_ID, 0);
0N/A }
0N/A }
0N/A return tz;
0N/A }
0N/A
0N/A /**
0N/A * Gets the available IDs according to the given time zone offset in milliseconds.
0N/A *
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
0N/A * the specified GMT offset. For example, "America/Phoenix" and "America/Denver"
3624N/A * both have GMT-07:00, but differ in daylight saving behavior.
0N/A * @see #getRawOffset()
0N/A */
0N/A public static synchronized String[] getAvailableIDs(int rawOffset) {
0N/A return ZoneInfo.getAvailableIDs(rawOffset);
0N/A }
0N/A
0N/A /**
0N/A * Gets all the available IDs supported.
0N/A * @return an array of IDs.
0N/A */
0N/A public static synchronized String[] getAvailableIDs() {
0N/A return ZoneInfo.getAvailableIDs();
0N/A }
0N/A
0N/A /**
0N/A * Gets the platform defined TimeZone ID.
0N/A **/
0N/A private static native String getSystemTimeZoneID(String javaHome,
0N/A String country);
0N/A
0N/A /**
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 */
0N/A private static native String getSystemGMTOffsetID();
0N/A
0N/A /**
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 * @see #setDefault
0N/A */
0N/A public static TimeZone getDefault() {
0N/A return (TimeZone) getDefaultRef().clone();
0N/A }
0N/A
0N/A /**
0N/A * Returns the reference to the default TimeZone object. This
0N/A * method doesn't create a clone.
0N/A */
0N/A static TimeZone getDefaultRef() {
4741N/A TimeZone defaultZone = getDefaultInAppContext();
0N/A if (defaultZone == null) {
0N/A defaultZone = defaultTimeZone;
0N/A if (defaultZone == null) {
0N/A // Need to initialize the default time zone.
0N/A defaultZone = setDefaultZone();
0N/A assert defaultZone != null;
0N/A }
0N/A }
0N/A // Don't clone here.
0N/A return defaultZone;
0N/A }
0N/A
0N/A private static synchronized TimeZone setDefaultZone() {
0N/A TimeZone tz = null;
0N/A // get the time zone ID from the system properties
0N/A String zoneID = AccessController.doPrivileged(
0N/A new GetPropertyAction("user.timezone"));
0N/A
0N/A // if the time zone ID is not set (yet), perform the
0N/A // platform to Java time zone ID mapping.
0N/A if (zoneID == null || zoneID.equals("")) {
0N/A String country = AccessController.doPrivileged(
0N/A new GetPropertyAction("user.country"));
0N/A String javaHome = AccessController.doPrivileged(
0N/A new GetPropertyAction("java.home"));
0N/A try {
0N/A zoneID = getSystemTimeZoneID(javaHome, country);
0N/A if (zoneID == null) {
0N/A zoneID = GMT_ID;
0N/A }
0N/A } catch (NullPointerException e) {
0N/A zoneID = GMT_ID;
0N/A }
0N/A }
0N/A
0N/A // Get the time zone for zoneID. But not fall back to
0N/A // "GMT" here.
0N/A tz = getTimeZone(zoneID, false);
0N/A
0N/A if (tz == null) {
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 String gmtOffsetID = getSystemGMTOffsetID();
0N/A if (gmtOffsetID != null) {
0N/A zoneID = gmtOffsetID;
0N/A }
0N/A tz = getTimeZone(zoneID, true);
0N/A }
0N/A assert tz != null;
0N/A
0N/A final String id = zoneID;
0N/A AccessController.doPrivileged(new PrivilegedAction<Object>() {
0N/A public Object run() {
0N/A System.setProperty("user.timezone", id);
0N/A return null;
0N/A }
0N/A });
0N/A
0N/A defaultTimeZone = tz;
0N/A return tz;
0N/A }
0N/A
0N/A private static boolean hasPermission() {
0N/A boolean hasPermission = true;
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A try {
0N/A sm.checkPermission(new PropertyPermission
0N/A ("user.timezone", "write"));
0N/A } catch (SecurityException e) {
0N/A hasPermission = false;
0N/A }
0N/A }
0N/A return hasPermission;
0N/A }
0N/A
0N/A /**
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 * VM first started.
0N/A * @param zone the new default time zone
0N/A * @see #getDefault
0N/A */
0N/A public static void setDefault(TimeZone zone)
0N/A {
0N/A if (hasPermission()) {
0N/A synchronized (TimeZone.class) {
0N/A defaultTimeZone = zone;
4741N/A setDefaultInAppContext(null);
0N/A }
0N/A } else {
4741N/A setDefaultInAppContext(zone);
4741N/A }
4741N/A }
4741N/A
4741N/A /**
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 *
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.
4741N/A */
5641N/A private static TimeZone getDefaultInAppContext() {
5641N/A // JavaAWTAccess provides access implementation-private methods without using reflection.
5641N/A JavaAWTAccess javaAWTAccess = SharedSecrets.getJavaAWTAccess();
5393N/A if (javaAWTAccess == null) {
5393N/A return mainAppContextDefault;
5393N/A } else {
5393N/A if (!javaAWTAccess.isDisposed()) {
5393N/A TimeZone tz = (TimeZone)
5393N/A javaAWTAccess.get(TimeZone.class);
5393N/A if (tz == null && javaAWTAccess.isMainAppContext()) {
5393N/A return mainAppContextDefault;
5393N/A } else {
5393N/A return tz;
5393N/A }
5393N/A }
4741N/A }
4741N/A return null;
4741N/A }
4741N/A
4741N/A /**
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
4741N/A * AppContext otherwise.
5641N/A *
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.
4741N/A */
5641N/A private static void setDefaultInAppContext(TimeZone tz) {
5641N/A // JavaAWTAccess provides access implementation-private methods without using reflection.
5641N/A JavaAWTAccess javaAWTAccess = SharedSecrets.getJavaAWTAccess();
5393N/A if (javaAWTAccess == null) {
5393N/A mainAppContextDefault = tz;
5393N/A } else {
5393N/A if (!javaAWTAccess.isDisposed()) {
5393N/A javaAWTAccess.put(TimeZone.class, tz);
5393N/A if (javaAWTAccess.isMainAppContext()) {
5393N/A mainAppContextDefault = null;
5393N/A }
4741N/A }
0N/A }
0N/A }
0N/A
0N/A /**
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 * @since 1.2
0N/A */
0N/A public boolean hasSameRules(TimeZone other) {
0N/A return other != null && getRawOffset() == other.getRawOffset() &&
0N/A useDaylightTime() == other.useDaylightTime();
0N/A }
0N/A
0N/A /**
0N/A * Creates a copy of this <code>TimeZone</code>.
0N/A *
0N/A * @return a clone of this <code>TimeZone</code>
0N/A */
0N/A public Object clone()
0N/A {
0N/A try {
0N/A TimeZone other = (TimeZone) super.clone();
0N/A other.ID = ID;
0N/A return other;
0N/A } catch (CloneNotSupportedException e) {
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The null constant as a TimeZone.
0N/A */
0N/A static final TimeZone NO_TIMEZONE = null;
0N/A
0N/A // =======================privates===============================
0N/A
0N/A /**
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.
0N/A * @serial
0N/A */
0N/A private String ID;
0N/A private static volatile TimeZone defaultTimeZone;
0N/A
0N/A static final String GMT_ID = "GMT";
0N/A private static final int GMT_ID_LENGTH = 3;
0N/A
5393N/A // a static TimeZone we can reference if no AppContext is in place
5641N/A private static volatile TimeZone mainAppContextDefault;
4741N/A
0N/A /**
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 *
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 */
0N/A private static final TimeZone parseCustomTimeZone(String id) {
0N/A int length;
0N/A
0N/A // Error if the length of id isn't long enough or id doesn't
0N/A // start with "GMT".
0N/A if ((length = id.length()) < (GMT_ID_LENGTH + 2) ||
0N/A id.indexOf(GMT_ID) != 0) {
0N/A return null;
0N/A }
0N/A
0N/A ZoneInfo zi;
0N/A
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 zi = ZoneInfoFile.getZoneInfo(id);
0N/A if (zi != null) {
0N/A return zi;
0N/A }
0N/A
0N/A int index = GMT_ID_LENGTH;
0N/A boolean negative = false;
0N/A char c = id.charAt(index++);
0N/A if (c == '-') {
0N/A negative = true;
0N/A } else if (c != '+') {
0N/A return null;
0N/A }
0N/A
0N/A int hours = 0;
0N/A int num = 0;
0N/A int countDelim = 0;
0N/A int len = 0;
0N/A while (index < length) {
0N/A c = id.charAt(index++);
0N/A if (c == ':') {
0N/A if (countDelim > 0) {
0N/A return null;
0N/A }
0N/A if (len > 2) {
0N/A return null;
0N/A }
0N/A hours = num;
0N/A countDelim++;
0N/A num = 0;
0N/A len = 0;
0N/A continue;
0N/A }
0N/A if (c < '0' || c > '9') {
0N/A return null;
0N/A }
0N/A num = num * 10 + (c - '0');
0N/A len++;
0N/A }
0N/A if (index != length) {
0N/A return null;
0N/A }
0N/A if (countDelim == 0) {
0N/A if (len <= 2) {
0N/A hours = num;
0N/A num = 0;
0N/A } else {
0N/A hours = num / 100;
0N/A num %= 100;
0N/A }
0N/A } else {
0N/A if (len != 2) {
0N/A return null;
0N/A }
0N/A }
0N/A if (hours > 23 || num > 59) {
0N/A return null;
0N/A }
0N/A int gmtOffset = (hours * 60 + num) * 60 * 1000;
0N/A
0N/A if (gmtOffset == 0) {
0N/A zi = ZoneInfoFile.getZoneInfo(GMT_ID);
0N/A if (negative) {
0N/A zi.setID("GMT-00:00");
0N/A } else {
0N/A zi.setID("GMT+00:00");
0N/A }
0N/A } else {
0N/A zi = ZoneInfoFile.getCustomTimeZone(id, negative ? -gmtOffset : gmtOffset);
0N/A }
0N/A return zi;
0N/A }
0N/A}