timezone.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 1,010 行 · 第 1/5 页

JAVA
1,010
字号
    /**
     * Sets time zone to using the given TimeZone.
     * @param zone the given time zone.
     */
    public static synchronized void setDefault(TimeZone zone)
    {
        defaultZone = zone;
    }

    /**
     * Overrides Cloneable
     */
    public Object clone()
    {
        try {
            TimeZone other = (TimeZone) super.clone();
            other.ID = ID;
            return other;
        } catch (CloneNotSupportedException e) {
            throw new InternalError();
        }
    }

    // =======================privates===============================

    private String           ID;
    private static TimeZone  defaultZone = null;

    // These are the default IDs for timezones; we use these if we can't get
    // the host timezone data, or if we don't recognize it.
    private static final String DEFAULT_SHORT_ID = "GMT";
    private static final String DEFAULT_ID       = "Africa/Casablanca";

    /**
     * This array maps from the user.timezone 3-letter ID to a usable
     * TimeZone ID.
     */
    private static final String[] idMap =
    {
        // Windows name               user.timezone ID   TimeZone ID
        // ------------               ----------------   -----------
        /* "GMT Standard Time",              */  "GMT",  "Africa/Casablanca",
        /* "Romance Standard Time",          */  "ECT",  "Europe/Paris",
        /* "Egypt Standard Time",            */  "EET",  "Africa/Cairo",
        /* "Saudi Arabia Standard Time",     */  "EAT",  "Asia/Riyadh",
        /* "Iran Standard Time",             */  "MET",  "Asia/Tehran",
        /* "Arabian Standard Time",          */  "NET",  "Asia/Yerevan",
        /* "West Asia Standard Time",        */  "PLT",  "Asia/Karachi",
        /* "India Standard Time",            */  "IST",  "Asia/Calcutta",
        /* "Central Asia Standard Time",     */  "BST",  "Asia/Dacca",
        /* "Bangkok Standard Time",          */  "VST",  "Asia/Bangkok",
        /* "China Standard Time",            */  "CTT",  "Asia/Shanghai",
        /* "Tokyo Standard Time",            */  "JST",  "Asia/Tokyo",
        /* "Cen. Australia Standard Time",   */  "ACT",  "Australia/Adelaide",
        /* "Sydney Standard Time",           */  "AET",  "Australia/Sydney",
        /* "Central Pacific Standard Time",  */  "SST",  "Pacific/Guadalcanal",
        /* "New Zealand Standard Time",      */  "NST",  "Pacific/Auckland",
        /* "Samoa Standard Time",            */  "MIT",  "Pacific/Apia",
        /* "Hawaiian Standard Time",         */  "HST",  "Pacific/Honolulu",
        /* "Alaskan Standard Time",          */  "AST",  "America/Anchorage",
        /* "Pacific Standard Time",          */  "PST",  "America/Los_Angeles",
        /* "US Mountain Standard Time",      */  "MST",  "America/Denver",
        /* "Central Standard Time",          */  "CST",  "America/Chicago",
        /* "Eastern Standard Time",          */  "EST",  "America/New_York",
        /* "Atlantic Standard Time",         */  "PRT",  "America/Halifax",
        /* "Newfoundland Standard Time",     */  "CNT",  "America/St_Johns",
        /* "SA Eastern Standard Time",       */  "AGT",  "America/Buenos_Aires",
        /* "E. South America Standard Time", */  "BET",  "America/Sao_Paulo",
        /* "Azores Standard Time",           */  "CAT",  "Atlantic/Azores",
    };

    private static Hashtable idlookup = new Hashtable(idMap.length / 2);

    static {
        for (int i=0; i<idMap.length; i+=2)
        {
            if (false)
            {
                // Debugging code
                if (TimeZoneData.get(idMap[i+1]) == null)
                    System.out.println("*** Bad TimeZone.idMap at " + i);
                if (idlookup.get(idMap[i]) != null)
                    System.out.println("*** Duplicate idMap " + idMap[i]);
            }
            idlookup.put(idMap[i], idMap[i+1]);
        }
    }

    // Internal Implementation Notes [LIU]
    //
    // TimeZone data is stored in two parts.  The first is an encoding of the
    // rules for each TimeZone.  A TimeZone rule includes the offset of a zone
    // in milliseconds from GMT, the starting month and day for daylight savings
    // time, if there is any, and the ending month and day for daylight savings
    // time.  The starting and ending days are specified in terms of the n-th
    // day of the week, for instance, the first Sunday or the last ("-1"-th)
    // Sunday of the month.  The rules are stored as statically-constructed
    // SimpleTimeZone objects in the TimeZone class.
    //
    // Each rule has a unique internal identifier string which is used to
    // specify it.  This identifier string is arbitrary, and is not to be shown
    // to the user -- it is for programmatic use only.  In order to instantiate
    // a TimeZone object, you pass its identifier string to
    // TimeZone.getTimeZone().  (This identifier is also used to index the
    // localized string data.)
    //
    // The second part of the data consists of localized string names used by
    // DateFormat to describe various TimeZones.  A TimeZone may have up to four
    // names: The abbreviated and long name for standard time in that zone, and
    // the abbreviated and long name for daylight savings time in that zone.
    // The data also includes a representative city.  For example, [ "PST",
    // "Pacific Standard Time", "PDT", "Pacific Daylight Time", "Los Angeles" ]
    // might be one such set of string names in the en_US locale.  These strings
    // are intended to be shown to the user.  The string data is indexed in the
    // system by a pair (String id, Locale locale).  The id is the unique string
    // identifier for the rule for the given TimeZone (as passed to
    // TimeZone.getTimeZone()).  String names are stored as localized resource
    // data of the class java.text.resources.DateFormatZoneData???  where ??? is
    // the Locale specifier (e.g., DateFormatZoneData_en_US).  This data is a
    // two-dimensional array of strings with N rows and 6 columns.  The columns
    // are id, short standard name, long standard name, short daylight name,
    // long daylight name, representative city name.
    //
    // The mapping between rules (SimpleTimeZone objects) and localized string
    // names (DateFormatZoneData objects) is one-to-many.  That is, there will
    // sometimes be more than one localized string name sets associated with
    // each rule.
    //
    // Each locale can potentially have localized name data for all time zones.
    // Since we support approximately 90 time zones and approximately 50
    // locales, there can be over 4500 sets of localized names.  In practice,
    // only a fraction of these names are provided.  If a time zone needs to be
    // displayed to the user in a given locale, and there is no string data in
    // that locale for that time zone, then the default representation will be
    // shown.  This is a string of the form GMT+HHMM or GMT-HHMM, where HHMM
    // represents the offset in hours and minutes with respect to GMT.  This
    // format is used because it is recognized in all locales.  In order to make
    // this mechanism to work, the root resource data (in the class
    // DateFormatZoneData) is left empty.
    //
    // The current default TimeZone is determined via the system property
    // user.timezone.  This is set by the platform-dependent native code to
    // a three-letter abbreviation.  We interpret these into our own internal
    // IDs using a lookup table.
}

/**
 * Encapsulates data for international timezones.  This package-private class is for
 * internal use only by TimeZone.  It encapsulates the list of recognized international
 * timezones.  By implementing this as a separate class, the loading and initialization
 * cost for this array is delayed until a TimeZone object is actually created from its ID.
 * This class contains only static variables and static methods; it cannot be instantiated.
 */
class TimeZoneData
{
    // The following value must be set to the maximum number of zones sharing
    // a single base offset value.
    static final int MAXIMUM_ZONES_PER_OFFSET = 13;

    static final TimeZone get(String ID)
    {
        Object o = lookup.get(ID);
        return o == null ? null : (TimeZone)((TimeZone)o).clone(); // [sic]
    }

    private static final int millisPerHour = 60*60*1000;

    static SimpleTimeZone[] zones =
    {
        // The following data is current as of 1997
        //----------------------------------------------------------
        new SimpleTimeZone(-11 * millisPerHour, "Pacific/Apia" /*WST*/),
        // Pacific/Apia W Samoa -11:00  -       WST     # W Samoa Time
        // Pacific/Midway       ?       -11:00  -       SST     # S=Samoa
        // Pacific/Niue Niue    -11:00  -       NUT
        // Pacific/Pago_Pago    American Samoa  -11:00  -       SST     # S=Samoa
        //----------------------------------------------------------
        new SimpleTimeZone(-10 * millisPerHour, "Pacific/Honolulu" /*HST*/),
        // Pacific/Honolulu     Hawaii  -10:00  -       HST
        // Pacific/Fakaofo      Tokelau Is      -10:00  -       TKT     # Tokelau Time
        // Pacific/Johnston     Johnston        -10:00  -       HST
        // Pacific/Tahiti       French Polynesia        -10:00  -       TAHT    # Tahiti Time
        //----------------------------------------------------------
//        new SimpleTimeZone(-10 * millisPerHour, "America/Adak" /*HA%sT*/,
//                Calendar.APRIL, 1, -Calendar.SUNDAY /*DOW>=DOM*/, 2 * millisPerHour,
//                Calendar.OCTOBER, -1, Calendar.SUNDAY /*DOW_IN_DOM*/, 2 * millisPerHour, 1 * millisPerHour),
        // Rule US      1967    max     -       Oct     lastSun 2:00    0       S
        // Rule US      1987    max     -       Apr     Sun>=1  2:00    1:00    D
        // America/Adak Alaska  -10:00  US      HA%sT
        //----------------------------------------------------------
//        new SimpleTimeZone(-10 * millisPerHour, "Pacific/Rarotonga" /*CK%sT*/,
//                Calendar.OCTOBER, -1, Calendar.SUNDAY /*DOW_IN_DOM*/, 0 * millisPerHour,
//                Calendar.MARCH, 1, -Calendar.SUNDAY /*DOW>=DOM*/, 0 * millisPerHour, (int)(0.5 * millisPerHour)),
        // Rule Cook    1979    max     -       Mar     Sun>=1  0:00    0       -
        // Rule Cook    1979    max     -       Oct     lastSun 0:00    0:30    HS
        // Pacific/Rarotonga    Cook Is -10:00  Cook    CK%sT
        //----------------------------------------------------------
//        new SimpleTimeZone((int)(-9.5 * millisPerHour), "Pacific/Marquesas" /*MART*/),
        // Pacific/Marquesas    French Polynesia        -9:30   -       MART    # Marquesas Time
        //----------------------------------------------------------
//        new SimpleTimeZone(-9 * millisPerHour, "Pacific/Gambier" /*GAMT*/),
        // Pacific/Gambier      French Polynesia        -9:00   -       GAMT    # Gambier Time

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?