1706 lines · 70.2 KB · Java
RawDownload
/* * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * (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.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.IOException;import sun.util.calendar.CalendarSystem;import sun.util.calendar.CalendarUtils;import sun.util.calendar.BaseCalendar;import sun.util.calendar.Gregorian; /** * <code>SimpleTimeZone</code> is a concrete subclass of <code>TimeZone</code> * that represents a time zone for use with a Gregorian calendar. * The class holds an offset from GMT, called <em>raw offset</em>, and start * and end rules for a daylight saving time schedule.  Since it only holds * single values for each, it cannot handle historical changes in the offset * from GMT and the daylight saving schedule, except that the {@link * #setStartYear setStartYear} method can specify the year when the daylight * saving time schedule starts in effect. * <p> * To construct a <code>SimpleTimeZone</code> with a daylight saving time * schedule, the schedule can be described with a set of rules, * <em>start-rule</em> and <em>end-rule</em>. A day when daylight saving time * starts or ends is specified by a combination of <em>month</em>, * <em>day-of-month</em>, and <em>day-of-week</em> values. The <em>month</em> * value is represented by a Calendar {@link Calendar#MONTH MONTH} field * value, such as {@link Calendar#MARCH}. The <em>day-of-week</em> value is * represented by a Calendar {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} value, * such as {@link Calendar#SUNDAY SUNDAY}. The meanings of value combinations * are as follows. * * <ul> * <li><b>Exact day of month</b><br> * To specify an exact day of month, set the <em>month</em> and * <em>day-of-month</em> to an exact value, and <em>day-of-week</em> to zero. For * example, to specify March 1, set the <em>month</em> to {@link Calendar#MARCH * MARCH}, <em>day-of-month</em> to 1, and <em>day-of-week</em> to 0.</li> * * <li><b>Day of week on or after day of month</b><br> * To specify a day of week on or after an exact day of month, set the * <em>month</em> to an exact month value, <em>day-of-month</em> to the day on * or after which the rule is applied, and <em>day-of-week</em> to a negative {@link * Calendar#DAY_OF_WEEK DAY_OF_WEEK} field value. For example, to specify the * second Sunday of April, set <em>month</em> to {@link Calendar#APRIL APRIL}, * <em>day-of-month</em> to 8, and <em>day-of-week</em> to <code>-</code>{@link * Calendar#SUNDAY SUNDAY}.</li> * * <li><b>Day of week on or before day of month</b><br> * To specify a day of the week on or before an exact day of the month, set * <em>day-of-month</em> and <em>day-of-week</em> to a negative value. For * example, to specify the last Wednesday on or before the 21st of March, set * <em>month</em> to {@link Calendar#MARCH MARCH}, <em>day-of-month</em> is -21 * and <em>day-of-week</em> is <code>-</code>{@link Calendar#WEDNESDAY WEDNESDAY}. </li> * * <li><b>Last day-of-week of month</b><br> * To specify, the last day-of-week of the month, set <em>day-of-week</em> to a * {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} value and <em>day-of-month</em> to * -1. For example, to specify the last Sunday of October, set <em>month</em> * to {@link Calendar#OCTOBER OCTOBER}, <em>day-of-week</em> to {@link * Calendar#SUNDAY SUNDAY} and <em>day-of-month</em> to -1.  </li> * * </ul> * The time of the day at which daylight saving time starts or ends is * specified by a millisecond value within the day. There are three kinds of * <em>mode</em>s to specify the time: {@link #WALL_TIME}, {@link * #STANDARD_TIME} and {@link #UTC_TIME}. For example, if daylight * saving time ends * at 2:00 am in the wall clock time, it can be specified by 7200000 * milliseconds in the {@link #WALL_TIME} mode. In this case, the wall clock time * for an <em>end-rule</em> means the same thing as the daylight time. * <p> * The following are examples of parameters for constructing time zone objects. * <pre><code> *      // Base GMT offset: -8:00 *      // DST starts:      at 2:00am in standard time *      //                  on the first Sunday in April *      // DST ends:        at 2:00am in daylight time *      //                  on the last Sunday in October *      // Save:            1 hour *      SimpleTimeZone(-28800000, *                     "America/Los_Angeles", *                     Calendar.APRIL, 1, -Calendar.SUNDAY, *                     7200000, *                     Calendar.OCTOBER, -1, Calendar.SUNDAY, *                     7200000, *                     3600000) * *      // Base GMT offset: +1:00 *      // DST starts:      at 1:00am in UTC time *      //                  on the last Sunday in March *      // DST ends:        at 1:00am in UTC time *      //                  on the last Sunday in October *      // Save:            1 hour *      SimpleTimeZone(3600000, *                     "Europe/Paris", *                     Calendar.MARCH, -1, Calendar.SUNDAY, *                     3600000, SimpleTimeZone.UTC_TIME, *                     Calendar.OCTOBER, -1, Calendar.SUNDAY, *                     3600000, SimpleTimeZone.UTC_TIME, *                     3600000) * </code></pre> * These parameter rules are also applicable to the set rule methods, such as * <code>setStartRule</code>. * * @since 1.1 * @see      Calendar * @see      GregorianCalendar * @see      TimeZone * @author   David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu */ public class SimpleTimeZone extends TimeZone {    /**     * Constructs a SimpleTimeZone with the given base time zone offset from GMT     * and time zone ID with no daylight saving time schedule.     *     * @param rawOffset  The base time zone offset in milliseconds to GMT.     * @param ID         The time zone name that is given to this instance.     */    public SimpleTimeZone(int rawOffset, String ID)    {        this.rawOffset = rawOffset;        setID (ID);        dstSavings = millisPerHour; // In case user sets rules later    }     /**     * Constructs a SimpleTimeZone with the given base time zone offset from     * GMT, time zone ID, and rules for starting and ending the daylight     * time.     * Both <code>startTime</code> and <code>endTime</code> are specified to be     * represented in the wall clock time. The amount of daylight saving is     * assumed to be 3600000 milliseconds (i.e., one hour). This constructor is     * equivalent to:     * <pre><code>     *     SimpleTimeZone(rawOffset,     *                    ID,     *                    startMonth,     *                    startDay,     *                    startDayOfWeek,     *                    startTime,     *                    SimpleTimeZone.{@link #WALL_TIME},     *                    endMonth,     *                    endDay,     *                    endDayOfWeek,     *                    endTime,     *                    SimpleTimeZone.{@link #WALL_TIME},     *                    3600000)     * </code></pre>     *     * @param rawOffset       The given base time zone offset from GMT.     * @param ID              The time zone ID which is given to this object.     * @param startMonth      The daylight saving time starting month. Month is     *                        a {@link Calendar#MONTH MONTH} field value (0-based. e.g., 0     *                        for January).     * @param startDay        The day of the month on which the daylight saving time starts.     *                        See the class description for the special cases of this parameter.     * @param startDayOfWeek  The daylight saving time starting day-of-week.     *                        See the class description for the special cases of this parameter.     * @param startTime       The daylight saving time starting time in local wall clock     *                        time (in milliseconds within the day), which is local     *                        standard time in this case.     * @param endMonth        The daylight saving time ending month. Month is     *                        a {@link Calendar#MONTH MONTH} field     *                        value (0-based. e.g., 9 for October).     * @param endDay          The day of the month on which the daylight saving time ends.     *                        See the class description for the special cases of this parameter.     * @param endDayOfWeek    The daylight saving time ending day-of-week.     *                        See the class description for the special cases of this parameter.     * @param endTime         The daylight saving ending time in local wall clock time,     *                        (in milliseconds within the day) which is local daylight     *                        time in this case.     * @exception IllegalArgumentException if the month, day, dayOfWeek, or time     * parameters are out of range for the start or end rule     */    public SimpleTimeZone(int rawOffset, String ID,                          int startMonth, int startDay, int startDayOfWeek, int startTime,                          int endMonth, int endDay, int endDayOfWeek, int endTime)    {        this(rawOffset, ID,             startMonth, startDay, startDayOfWeek, startTime, WALL_TIME,             endMonth, endDay, endDayOfWeek, endTime, WALL_TIME,             millisPerHour);    }     /**     * Constructs a SimpleTimeZone with the given base time zone offset from     * GMT, time zone ID, and rules for starting and ending the daylight     * time.     * Both <code>startTime</code> and <code>endTime</code> are assumed to be     * represented in the wall clock time. This constructor is equivalent to:     * <pre><code>     *     SimpleTimeZone(rawOffset,     *                    ID,     *                    startMonth,     *                    startDay,     *                    startDayOfWeek,     *                    startTime,     *                    SimpleTimeZone.{@link #WALL_TIME},     *                    endMonth,     *                    endDay,     *                    endDayOfWeek,     *                    endTime,     *                    SimpleTimeZone.{@link #WALL_TIME},     *                    dstSavings)     * </code></pre>     *     * @param rawOffset       The given base time zone offset from GMT.     * @param ID              The time zone ID which is given to this object.     * @param startMonth      The daylight saving time starting month. Month is     *                        a {@link Calendar#MONTH MONTH} field     *                        value (0-based. e.g., 0 for January).     * @param startDay        The day of the month on which the daylight saving time starts.     *                        See the class description for the special cases of this parameter.     * @param startDayOfWeek  The daylight saving time starting day-of-week.     *                        See the class description for the special cases of this parameter.     * @param startTime       The daylight saving time starting time in local wall clock     *                        time, which is local standard time in this case.     * @param endMonth        The daylight saving time ending month. Month is     *                        a {@link Calendar#MONTH MONTH} field     *                        value (0-based. e.g., 9 for October).     * @param endDay          The day of the month on which the daylight saving time ends.     *                        See the class description for the special cases of this parameter.     * @param endDayOfWeek    The daylight saving time ending day-of-week.     *                        See the class description for the special cases of this parameter.     * @param endTime         The daylight saving ending time in local wall clock time,     *                        which is local daylight time in this case.     * @param dstSavings      The amount of time in milliseconds saved during     *                        daylight saving time.     * @exception IllegalArgumentException if the month, day, dayOfWeek, or time     * parameters are out of range for the start or end rule     * @since 1.2     */    public SimpleTimeZone(int rawOffset, String ID,                          int startMonth, int startDay, int startDayOfWeek, int startTime,                          int endMonth, int endDay, int endDayOfWeek, int endTime,                          int dstSavings)    {        this(rawOffset, ID,             startMonth, startDay, startDayOfWeek, startTime, WALL_TIME,             endMonth, endDay, endDayOfWeek, endTime, WALL_TIME,             dstSavings);    }     /**     * Constructs a SimpleTimeZone with the given base time zone offset from     * GMT, time zone ID, and rules for starting and ending the daylight     * time.     * This constructor takes the full set of the start and end rules     * parameters, including modes of <code>startTime</code> and     * <code>endTime</code>. The mode specifies either {@link #WALL_TIME wall     * time} or {@link #STANDARD_TIME standard time} or {@link #UTC_TIME UTC     * time}.     *     * @param rawOffset       The given base time zone offset from GMT.     * @param ID              The time zone ID which is given to this object.     * @param startMonth      The daylight saving time starting month. Month is     *                        a {@link Calendar#MONTH MONTH} field     *                        value (0-based. e.g., 0 for January).     * @param startDay        The day of the month on which the daylight saving time starts.     *                        See the class description for the special cases of this parameter.     * @param startDayOfWeek  The daylight saving time starting day-of-week.     *                        See the class description for the special cases of this parameter.     * @param startTime       The daylight saving time starting time in the time mode     *                        specified by <code>startTimeMode</code>.     * @param startTimeMode   The mode of the start time specified by startTime.     * @param endMonth        The daylight saving time ending month. Month is     *                        a {@link Calendar#MONTH MONTH} field     *                        value (0-based. e.g., 9 for October).     * @param endDay          The day of the month on which the daylight saving time ends.     *                        See the class description for the special cases of this parameter.     * @param endDayOfWeek    The daylight saving time ending day-of-week.     *                        See the class description for the special cases of this parameter.     * @param endTime         The daylight saving ending time in time time mode     *                        specified by <code>endTimeMode</code>.     * @param endTimeMode     The mode of the end time specified by endTime     * @param dstSavings      The amount of time in milliseconds saved during     *                        daylight saving time.     *     * @exception IllegalArgumentException if the month, day, dayOfWeek, time more, or     * time parameters are out of range for the start or end rule, or if a time mode     * value is invalid.     *     * @see #WALL_TIME     * @see #STANDARD_TIME     * @see #UTC_TIME     *     * @since 1.4     */    public SimpleTimeZone(int rawOffset, String ID,                          int startMonth, int startDay, int startDayOfWeek,                          int startTime, int startTimeMode,                          int endMonth, int endDay, int endDayOfWeek,                          int endTime, int endTimeMode,                          int dstSavings) {         setID(ID);        this.rawOffset      = rawOffset;        this.startMonth     = startMonth;        this.startDay       = startDay;        this.startDayOfWeek = startDayOfWeek;        this.startTime      = startTime;        this.startTimeMode  = startTimeMode;        this.endMonth       = endMonth;        this.endDay         = endDay;        this.endDayOfWeek   = endDayOfWeek;        this.endTime        = endTime;        this.endTimeMode    = endTimeMode;        this.dstSavings     = dstSavings;         // this.useDaylight is set by decodeRules        decodeRules();        if (dstSavings <= 0) {            throw new IllegalArgumentException("Illegal daylight saving value: " + dstSavings);        }    }     /**     * Sets the daylight saving time starting year.     *     * @param year  The daylight saving starting year.     */    public void setStartYear(int year)    {        startYear = year;        invalidateCache();    }     /**     * Sets the daylight saving time start rule. For example, if daylight saving     * time starts on the first Sunday in April at 2 am in local wall clock     * time, you can set the start rule by calling:     * <pre><code>setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);</code></pre>     *     * @param startMonth      The daylight saving time starting month. Month is     *                        a {@link Calendar#MONTH MONTH} field     *                        value (0-based. e.g., 0 for January).     * @param startDay        The day of the month on which the daylight saving time starts.     *                        See the class description for the special cases of this parameter.     * @param startDayOfWeek  The daylight saving time starting day-of-week.     *                        See the class description for the special cases of this parameter.     * @param startTime       The daylight saving time starting time in local wall clock     *                        time, which is local standard time in this case.     * @exception IllegalArgumentException if the <code>startMonth</code>, <code>startDay</code>,     * <code>startDayOfWeek</code>, or <code>startTime</code> parameters are out of range     */    public void setStartRule(int startMonth, int startDay, int startDayOfWeek, int startTime)    {        this.startMonth = startMonth;        this.startDay = startDay;        this.startDayOfWeek = startDayOfWeek;        this.startTime = startTime;        startTimeMode = WALL_TIME;        decodeStartRule();        invalidateCache();    }     /**     * Sets the daylight saving time start rule to a fixed date within a month.     * This method is equivalent to:     * <pre><code>setStartRule(startMonth, startDay, 0, startTime)</code></pre>     *     * @param startMonth      The daylight saving time starting month. Month is     *                        a {@link Calendar#MONTH MONTH} field     *                        value (0-based. e.g., 0 for January).     * @param startDay        The day of the month on which the daylight saving time starts.     * @param startTime       The daylight saving time starting time in local wall clock     *                        time, which is local standard time in this case.     *                        See the class description for the special cases of this parameter.     * @exception IllegalArgumentException if the <code>startMonth</code>,     * <code>startDayOfMonth</code>, or <code>startTime</code> parameters are out of range     * @since 1.2     */    public void setStartRule(int startMonth, int startDay, int startTime) {        setStartRule(startMonth, startDay, 0, startTime);    }     /**     * Sets the daylight saving time start rule to a weekday before or after the given date within     * a month, e.g., the first Monday on or after the 8th.     *     * @param startMonth      The daylight saving time starting month. Month is     *                        a {@link Calendar#MONTH MONTH} field     *                        value (0-based. e.g., 0 for January).     * @param startDay        The day of the month on which the daylight saving time starts.     * @param startDayOfWeek  The daylight saving time starting day-of-week.     * @param startTime       The daylight saving time starting time in local wall clock     *                        time, which is local standard time in this case.     * @param after           If true, this rule selects the first <code>dayOfWeek</code> on or     *                        <em>after</em> <code>dayOfMonth</code>.  If false, this rule     *                        selects the last <code>dayOfWeek</code> on or <em>before</em>     *                        <code>dayOfMonth</code>.     * @exception IllegalArgumentException if the <code>startMonth</code>, <code>startDay</code>,     * <code>startDayOfWeek</code>, or <code>startTime</code> parameters are out of range     * @since 1.2     */    public void setStartRule(int startMonth, int startDay, int startDayOfWeek,                             int startTime, boolean after)    {        // TODO: this method doesn't check the initial values of dayOfMonth or dayOfWeek.        if (after) {            setStartRule(startMonth, startDay, -startDayOfWeek, startTime);        } else {            setStartRule(startMonth, -startDay, -startDayOfWeek, startTime);        }    }     /**     * Sets the daylight saving time end rule. For example, if daylight saving time     * ends on the last Sunday in October at 2 am in wall clock time,     * you can set the end rule by calling:     * <code>setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);</code>     *     * @param endMonth        The daylight saving time ending month. Month is     *                        a {@link Calendar#MONTH MONTH} field     *                        value (0-based. e.g., 9 for October).     * @param endDay          The day of the month on which the daylight saving time ends.     *                        See the class description for the special cases of this parameter.     * @param endDayOfWeek    The daylight saving time ending day-of-week.     *                        See the class description for the special cases of this parameter.     * @param endTime         The daylight saving ending time in local wall clock time,     *                        (in milliseconds within the day) which is local daylight     *                        time in this case.     * @exception IllegalArgumentException if the <code>endMonth</code>, <code>endDay</code>,     * <code>endDayOfWeek</code>, or <code>endTime</code> parameters are out of range     */    public void setEndRule(int endMonth, int endDay, int endDayOfWeek,                           int endTime)    {        this.endMonth = endMonth;        this.endDay = endDay;        this.endDayOfWeek = endDayOfWeek;        this.endTime = endTime;        this.endTimeMode = WALL_TIME;        decodeEndRule();        invalidateCache();    }     /**     * Sets the daylight saving time end rule to a fixed date within a month.     * This method is equivalent to:     * <pre><code>setEndRule(endMonth, endDay, 0, endTime)</code></pre>     *     * @param endMonth        The daylight saving time ending month. Month is     *                        a {@link Calendar#MONTH MONTH} field     *                        value (0-based. e.g., 9 for October).     * @param endDay          The day of the month on which the daylight saving time ends.     * @param endTime         The daylight saving ending time in local wall clock time,     *                        (in milliseconds within the day) which is local daylight     *                        time in this case.     * @exception IllegalArgumentException the <code>endMonth</code>, <code>endDay</code>,     * or <code>endTime</code> parameters are out of range     * @since 1.2     */    public void setEndRule(int endMonth, int endDay, int endTime)    {        setEndRule(endMonth, endDay, 0, endTime);    }     /**     * Sets the daylight saving time end rule to a weekday before or after the given date within     * a month, e.g., the first Monday on or after the 8th.

Lines 1–500 of 1706.