timezone.java
来自「java源代码 请看看啊 提点宝贵的意见」· Java 代码 · 共 706 行 · 第 1/2 页
JAVA
706 行
/* * @(#)TimeZone.java 1.63 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. *//* * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved * (C) Copyright IBM Corp. 1996 - All Rights Reserved * * The original version of this source code and documentation is copyrighted * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These * materials are provided under terms of a License Agreement between Taligent * and Sun. This technology is protected by multiple US and International * patents. This notice and attribution to Taligent may not be removed. * Taligent is a registered trademark of Taligent, Inc. * */package java.util;import java.io.Serializable;import java.lang.ref.SoftReference;import java.security.AccessController;import java.security.PrivilegedAction;import java.text.SimpleDateFormat;import sun.security.action.GetPropertyAction;import sun.util.calendar.ZoneInfo;import sun.util.calendar.ZoneInfoFile;/** * <code>TimeZone</code> represents a time zone offset, and also figures out daylight * savings. * * <p> * Typically, you get a <code>TimeZone</code> using <code>getDefault</code> * which creates a <code>TimeZone</code> based on the time zone where the program * is running. For example, for a program running in Japan, <code>getDefault</code> * creates a <code>TimeZone</code> object based on Japanese Standard Time. * * <p> * You can also get a <code>TimeZone</code> using <code>getTimeZone</code> * along with a time zone ID. For instance, the time zone ID for the * U.S. Pacific Time zone is "America/Los_Angeles". So, you can get a * U.S. Pacific Time <code>TimeZone</code> object with: * <blockquote><pre> * TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles"); * </pre></blockquote> * You can use the <code>getAvailableIDs</code> method to iterate through * all the supported time zone IDs. You can then choose a * supported ID to get a <code>TimeZone</code>. * If the time zone you want is not represented by one of the * supported IDs, then a custom time zone ID can be specified to * produce a TimeZone. The syntax of a custom time zone ID is: * * <blockquote><pre> * <a name="CustomID"><i>CustomID:</i></a> * <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i> * <code>GMT</code> <i>Sign</i> <i>Hours</i> <i>Minutes</i> * <code>GMT</code> <i>Sign</i> <i>Hours</i> * <i>Sign:</i> one of * <code>+ -</code> * <i>Hours:</i> * <i>Digit</i> * <i>Digit</i> <i>Digit</i> * <i>Minutes:</i> * <i>Digit</i> <i>Digit</i> * <i>Digit:</i> one of * <code>0 1 2 3 4 5 6 7 8 9</code> * </pre></blockquote> * * <i>Hours</i> must be between 0 to 23 and <i>Minutes</i> must be * between 00 to 59. For example, "GMT+10" and "GMT+0010" mean ten * hours and ten minutes ahead of GMT, respectively. * <p> * The format is locale independent and digits must be taken from the * Basic Latin block of the Unicode standard. No daylight saving time * transition schedule can be specified with a custom time zone ID. If * the specified string doesn't match the syntax, <code>"GMT"</code> * is used. * <p> * When creating a <code>TimeZone</code>, the specified custom time * zone ID is normalized in the following syntax: * <blockquote><pre> * <a name="NormalizedCustomID"><i>NormalizedCustomID:</i></a> * <code>GMT</code> <i>Sign</i> <i>TwoDigitHours</i> <code>:</code> <i>Minutes</i> * <i>Sign:</i> one of * <code>+ -</code> * <i>TwoDigitHours:</i> * <i>Digit</i> <i>Digit</i> * <i>Minutes:</i> * <i>Digit</i> <i>Digit</i> * <i>Digit:</i> one of * <code>0 1 2 3 4 5 6 7 8 9</code> * </pre></blockquote> * For example, TimeZone.getTimeZone("GMT-8").getID() returns "GMT-08:00". * * <h4>Three-letter time zone IDs</h4> * * For compatibility with JDK 1.1.x, some other three-letter time zone IDs * (such as "PST", "CTT", "AST") are also supported. However, <strong>their * use is deprecated</strong> because the same abbreviation is often used * for multiple time zones (for example, "CST" could be U.S. "Central Standard * Time" and "China Standard Time"), and the Java platform can then only * recognize one of them. * * * @see Calendar * @see GregorianCalendar * @see SimpleTimeZone * @version 1.63 01/23/03 * @author Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu * @since JDK1.1 */abstract public class TimeZone implements Serializable, Cloneable { /** * Sole constructor. (For invocation by subclass constructors, typically * implicit.) */ public TimeZone() { } /** * A style specifier for <code>getDisplayName()</code> indicating * a short name, such as "PST." * @see #LONG * @since 1.2 */ public static final int SHORT = 0; /** * A style specifier for <code>getDisplayName()</code> indicating * a long name, such as "Pacific Standard Time." * @see #SHORT * @since 1.2 */ public static final int LONG = 1; // Constants used internally; unit is milliseconds private static final int ONE_MINUTE = 60*1000; private static final int ONE_HOUR = 60*ONE_MINUTE; private static final int ONE_DAY = 24*ONE_HOUR; /** * Cache to hold the SimpleDateFormat objects for a Locale. */ private static Hashtable cachedLocaleData = new Hashtable(3); // Proclaim serialization compatibility with JDK 1.1 static final long serialVersionUID = 3581463369166924961L; /** * Gets the time zone offset, for current date, modified in case of * daylight savings. This is the offset to add to UTC to get local time. * <p> * This method returns a historically correct offset if an * underlying <code>TimeZone</code> implementation subclass * supports historical Daylight Saving Time schedule and GMT * offset changes. * * @param era the era of the given date. * @param year the year in the given date. * @param month the month in the given date. * Month is 0-based. e.g., 0 for January. * @param day the day-in-month of the given date. * @param dayOfWeek the day-of-week of the given date. * @param milliseconds the milliseconds in day in <em>standard</em> * local time. * * @return the offset in milliseconds to add to GMT to get local time. * * @see Calendar#ZONE_OFFSET * @see Calendar#DST_OFFSET */ public abstract int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds); /** * Returns the offset of this time zone from UTC at the specified * date. If Daylight Saving Time is in effect at the specified * date, the offset value is adjusted with the amount of daylight * saving. * <p> * This method returns a historically correct offset value if an * underlying TimeZone implementation subclass supports historical * Daylight Saving Time schedule and GMT offset changes. * * @param date the date represented in milliseconds since January 1, 1970 00:00:00 GMT * @return the amount of time in milliseconds to add to UTC to get local time. * * @see Calendar#ZONE_OFFSET * @see Calendar#DST_OFFSET * @since 1.4 */ public int getOffset(long date) { if (inDaylightTime(new Date(date))) { return getRawOffset() + getDSTSavings(); } return getRawOffset(); } /** * Gets the raw GMT offset and the amount of daylight saving of this * time zone at the given time. * @param date the milliseconds (since January 1, 1970, * 00:00:00.000 GMT) at which the time zone offset and daylight * saving amount are found * @param offset an array of int where the raw GMT offset * (offset[0]) and daylight saving amount (offset[1]) are stored, * or null if those values are not needed. The method assumes that * the length of the given array is two or larger. * @return the total amount of the raw GMT offset and daylight * saving at the specified date. * * @see Calendar#ZONE_OFFSET * @see Calendar#DST_OFFSET */ int getOffsets(long date, int[] offsets) { int rawoffset = getRawOffset(); int dstoffset = 0; if (inDaylightTime(new Date(date))) { dstoffset = getDSTSavings(); } if (offsets != null) { offsets[0] = rawoffset; offsets[1] = dstoffset; } return rawoffset + dstoffset; } /** * Sets the base time zone offset to GMT. * This is the offset to add to UTC to get local time. * <p> * If an underlying <code>TimeZone</code> implementation subclass * supports historical GMT offset changes, the specified GMT * offset is set as the latest GMT offset and the difference from * the known latest GMT offset value is used to adjust all * historical GMT offset values. * * @param offsetMillis the given base time zone offset to GMT. */ abstract public void setRawOffset(int offsetMillis); /** * Returns the amount of time in milliseconds to add to UTC to get * standard time in this time zone. Because this value is not * affected by daylight saving time, it is called <I>raw * offset</I>. * <p> * If an underlying <code>TimeZone</code> implementation subclass * supports historical GMT offset changes, the method returns the * raw offset value of the current date. In Honolulu, for example, * its raw offset changed from GMT-10:30 to GMT-10:00 in 1947, and * this method always returns -36000000 milliseconds (i.e., -10 * hours). * * @return the amount of raw offset time in milliseconds to add to UTC. * @see Calendar#ZONE_OFFSET */ public abstract int getRawOffset(); /** * Gets the ID of this time zone. * @return the ID of this time zone. */ public String getID() { return ID; } /** * Sets the time zone ID. This does not change any other data in * the time zone object. * @param ID the new time zone ID. */ public void setID(String ID) { if (ID == null) { throw new NullPointerException(); } this.ID = ID; } /** * Returns a name of this time zone suitable for presentation to the user * in the default locale. * This method returns the long name, not including daylight savings. * If the display name is not available for the locale, * then this method returns a string in the * <a href="#NormalizedCustomID">normalized custom ID format</a>. * @return the human-readable name of this time zone in the default locale. * @since 1.2 */ public final String getDisplayName() { return getDisplayName(false, LONG, Locale.getDefault()); } /** * Returns a name of this time zone suitable for presentation to the user * in the specified locale. * This method returns the long name, not including daylight savings. * If the display name is not available for the locale, * then this method returns a string in the * <a href="#NormalizedCustomID">normalized custom ID format</a>. * @param locale the locale in which to supply the display name. * @return the human-readable name of this time zone in the given locale * or in the default locale if the given locale is not recognized. * @since 1.2 */ public final String getDisplayName(Locale locale) { return getDisplayName(false, LONG, locale); } /** * Returns a name of this time zone suitable for presentation to the user * in the default locale. * If the display name is not available for the locale, then this * method returns a string in the * <a href="#NormalizedCustomID">normalized custom ID format</a>. * @param daylight if true, return the daylight savings name. * @param style either <code>LONG</code> or <code>SHORT</code> * @return the human-readable name of this time zone in the default locale. * @since 1.2 */ public final String getDisplayName(boolean daylight, int style) { return getDisplayName(daylight, style, Locale.getDefault()); } /** * Returns a name of this time zone suitable for presentation to the user * in the specified locale. * If the display name is not available for the locale, * then this method returns a string in the * <a href="#NormalizedCustomID">normalized custom ID format</a>. * @param daylight if true, return the daylight savings name. * @param style either <code>LONG</code> or <code>SHORT</code> * @param locale the locale in which to supply the display name. * @return the human-readable name of this time zone in the given locale * or in the default locale if the given locale is not recognized. * @exception IllegalArgumentException style is invalid. * @since 1.2 */ public String getDisplayName(boolean daylight, int style, Locale locale) { /* NOTES: * (1) We use SimpleDateFormat for simplicity; we could do this * more efficiently but it would duplicate the SimpleDateFormat code * here, which is undesirable. * (2) Attempts to move the code from SimpleDateFormat to here also run * around because this requires SimpleDateFormat to keep a Locale * object around, which it currently doesn't; to synthesize such a * locale upon resurrection; and to somehow handle the special case of * construction from a DateFormatSymbols object.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?