rt/emul/compact/src/main/java/java/util/SimpleTimeZone.java
branchjdk7-b147
changeset 1334 588d5bf7a560
child 1340 41046f76a76a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/SimpleTimeZone.java	Thu Oct 03 15:40:35 2013 +0200
     1.3 @@ -0,0 +1,1706 @@
     1.4 +/*
     1.5 + * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
    1.31 + * (C) Copyright IBM Corp. 1996 - All Rights Reserved
    1.32 + *
    1.33 + *   The original version of this source code and documentation is copyrighted
    1.34 + * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
    1.35 + * materials are provided under terms of a License Agreement between Taligent
    1.36 + * and Sun. This technology is protected by multiple US and International
    1.37 + * patents. This notice and attribution to Taligent may not be removed.
    1.38 + *   Taligent is a registered trademark of Taligent, Inc.
    1.39 + *
    1.40 + */
    1.41 +
    1.42 +package java.util;
    1.43 +
    1.44 +import java.io.ObjectInputStream;
    1.45 +import java.io.ObjectOutputStream;
    1.46 +import java.io.IOException;
    1.47 +import sun.util.calendar.CalendarSystem;
    1.48 +import sun.util.calendar.CalendarUtils;
    1.49 +import sun.util.calendar.BaseCalendar;
    1.50 +import sun.util.calendar.Gregorian;
    1.51 +
    1.52 +/**
    1.53 + * <code>SimpleTimeZone</code> is a concrete subclass of <code>TimeZone</code>
    1.54 + * that represents a time zone for use with a Gregorian calendar.
    1.55 + * The class holds an offset from GMT, called <em>raw offset</em>, and start
    1.56 + * and end rules for a daylight saving time schedule.  Since it only holds
    1.57 + * single values for each, it cannot handle historical changes in the offset
    1.58 + * from GMT and the daylight saving schedule, except that the {@link
    1.59 + * #setStartYear setStartYear} method can specify the year when the daylight
    1.60 + * saving time schedule starts in effect.
    1.61 + * <p>
    1.62 + * To construct a <code>SimpleTimeZone</code> with a daylight saving time
    1.63 + * schedule, the schedule can be described with a set of rules,
    1.64 + * <em>start-rule</em> and <em>end-rule</em>. A day when daylight saving time
    1.65 + * starts or ends is specified by a combination of <em>month</em>,
    1.66 + * <em>day-of-month</em>, and <em>day-of-week</em> values. The <em>month</em>
    1.67 + * value is represented by a Calendar {@link Calendar#MONTH MONTH} field
    1.68 + * value, such as {@link Calendar#MARCH}. The <em>day-of-week</em> value is
    1.69 + * represented by a Calendar {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} value,
    1.70 + * such as {@link Calendar#SUNDAY SUNDAY}. The meanings of value combinations
    1.71 + * are as follows.
    1.72 + *
    1.73 + * <ul>
    1.74 + * <li><b>Exact day of month</b><br>
    1.75 + * To specify an exact day of month, set the <em>month</em> and
    1.76 + * <em>day-of-month</em> to an exact value, and <em>day-of-week</em> to zero. For
    1.77 + * example, to specify March 1, set the <em>month</em> to {@link Calendar#MARCH
    1.78 + * MARCH}, <em>day-of-month</em> to 1, and <em>day-of-week</em> to 0.</li>
    1.79 + *
    1.80 + * <li><b>Day of week on or after day of month</b><br>
    1.81 + * To specify a day of week on or after an exact day of month, set the
    1.82 + * <em>month</em> to an exact month value, <em>day-of-month</em> to the day on
    1.83 + * or after which the rule is applied, and <em>day-of-week</em> to a negative {@link
    1.84 + * Calendar#DAY_OF_WEEK DAY_OF_WEEK} field value. For example, to specify the
    1.85 + * second Sunday of April, set <em>month</em> to {@link Calendar#APRIL APRIL},
    1.86 + * <em>day-of-month</em> to 8, and <em>day-of-week</em> to <code>-</code>{@link
    1.87 + * Calendar#SUNDAY SUNDAY}.</li>
    1.88 + *
    1.89 + * <li><b>Day of week on or before day of month</b><br>
    1.90 + * To specify a day of the week on or before an exact day of the month, set
    1.91 + * <em>day-of-month</em> and <em>day-of-week</em> to a negative value. For
    1.92 + * example, to specify the last Wednesday on or before the 21st of March, set
    1.93 + * <em>month</em> to {@link Calendar#MARCH MARCH}, <em>day-of-month</em> is -21
    1.94 + * and <em>day-of-week</em> is <code>-</code>{@link Calendar#WEDNESDAY WEDNESDAY}. </li>
    1.95 + *
    1.96 + * <li><b>Last day-of-week of month</b><br>
    1.97 + * To specify, the last day-of-week of the month, set <em>day-of-week</em> to a
    1.98 + * {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} value and <em>day-of-month</em> to
    1.99 + * -1. For example, to specify the last Sunday of October, set <em>month</em>
   1.100 + * to {@link Calendar#OCTOBER OCTOBER}, <em>day-of-week</em> to {@link
   1.101 + * Calendar#SUNDAY SUNDAY} and <em>day-of-month</em> to -1.  </li>
   1.102 + *
   1.103 + * </ul>
   1.104 + * The time of the day at which daylight saving time starts or ends is
   1.105 + * specified by a millisecond value within the day. There are three kinds of
   1.106 + * <em>mode</em>s to specify the time: {@link #WALL_TIME}, {@link
   1.107 + * #STANDARD_TIME} and {@link #UTC_TIME}. For example, if daylight
   1.108 + * saving time ends
   1.109 + * at 2:00 am in the wall clock time, it can be specified by 7200000
   1.110 + * milliseconds in the {@link #WALL_TIME} mode. In this case, the wall clock time
   1.111 + * for an <em>end-rule</em> means the same thing as the daylight time.
   1.112 + * <p>
   1.113 + * The following are examples of parameters for constructing time zone objects.
   1.114 + * <pre><code>
   1.115 + *      // Base GMT offset: -8:00
   1.116 + *      // DST starts:      at 2:00am in standard time
   1.117 + *      //                  on the first Sunday in April
   1.118 + *      // DST ends:        at 2:00am in daylight time
   1.119 + *      //                  on the last Sunday in October
   1.120 + *      // Save:            1 hour
   1.121 + *      SimpleTimeZone(-28800000,
   1.122 + *                     "America/Los_Angeles",
   1.123 + *                     Calendar.APRIL, 1, -Calendar.SUNDAY,
   1.124 + *                     7200000,
   1.125 + *                     Calendar.OCTOBER, -1, Calendar.SUNDAY,
   1.126 + *                     7200000,
   1.127 + *                     3600000)
   1.128 + *
   1.129 + *      // Base GMT offset: +1:00
   1.130 + *      // DST starts:      at 1:00am in UTC time
   1.131 + *      //                  on the last Sunday in March
   1.132 + *      // DST ends:        at 1:00am in UTC time
   1.133 + *      //                  on the last Sunday in October
   1.134 + *      // Save:            1 hour
   1.135 + *      SimpleTimeZone(3600000,
   1.136 + *                     "Europe/Paris",
   1.137 + *                     Calendar.MARCH, -1, Calendar.SUNDAY,
   1.138 + *                     3600000, SimpleTimeZone.UTC_TIME,
   1.139 + *                     Calendar.OCTOBER, -1, Calendar.SUNDAY,
   1.140 + *                     3600000, SimpleTimeZone.UTC_TIME,
   1.141 + *                     3600000)
   1.142 + * </code></pre>
   1.143 + * These parameter rules are also applicable to the set rule methods, such as
   1.144 + * <code>setStartRule</code>.
   1.145 + *
   1.146 + * @since 1.1
   1.147 + * @see      Calendar
   1.148 + * @see      GregorianCalendar
   1.149 + * @see      TimeZone
   1.150 + * @author   David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu
   1.151 + */
   1.152 +
   1.153 +public class SimpleTimeZone extends TimeZone {
   1.154 +    /**
   1.155 +     * Constructs a SimpleTimeZone with the given base time zone offset from GMT
   1.156 +     * and time zone ID with no daylight saving time schedule.
   1.157 +     *
   1.158 +     * @param rawOffset  The base time zone offset in milliseconds to GMT.
   1.159 +     * @param ID         The time zone name that is given to this instance.
   1.160 +     */
   1.161 +    public SimpleTimeZone(int rawOffset, String ID)
   1.162 +    {
   1.163 +        this.rawOffset = rawOffset;
   1.164 +        setID (ID);
   1.165 +        dstSavings = millisPerHour; // In case user sets rules later
   1.166 +    }
   1.167 +
   1.168 +    /**
   1.169 +     * Constructs a SimpleTimeZone with the given base time zone offset from
   1.170 +     * GMT, time zone ID, and rules for starting and ending the daylight
   1.171 +     * time.
   1.172 +     * Both <code>startTime</code> and <code>endTime</code> are specified to be
   1.173 +     * represented in the wall clock time. The amount of daylight saving is
   1.174 +     * assumed to be 3600000 milliseconds (i.e., one hour). This constructor is
   1.175 +     * equivalent to:
   1.176 +     * <pre><code>
   1.177 +     *     SimpleTimeZone(rawOffset,
   1.178 +     *                    ID,
   1.179 +     *                    startMonth,
   1.180 +     *                    startDay,
   1.181 +     *                    startDayOfWeek,
   1.182 +     *                    startTime,
   1.183 +     *                    SimpleTimeZone.{@link #WALL_TIME},
   1.184 +     *                    endMonth,
   1.185 +     *                    endDay,
   1.186 +     *                    endDayOfWeek,
   1.187 +     *                    endTime,
   1.188 +     *                    SimpleTimeZone.{@link #WALL_TIME},
   1.189 +     *                    3600000)
   1.190 +     * </code></pre>
   1.191 +     *
   1.192 +     * @param rawOffset       The given base time zone offset from GMT.
   1.193 +     * @param ID              The time zone ID which is given to this object.
   1.194 +     * @param startMonth      The daylight saving time starting month. Month is
   1.195 +     *                        a {@link Calendar#MONTH MONTH} field value (0-based. e.g., 0
   1.196 +     *                        for January).
   1.197 +     * @param startDay        The day of the month on which the daylight saving time starts.
   1.198 +     *                        See the class description for the special cases of this parameter.
   1.199 +     * @param startDayOfWeek  The daylight saving time starting day-of-week.
   1.200 +     *                        See the class description for the special cases of this parameter.
   1.201 +     * @param startTime       The daylight saving time starting time in local wall clock
   1.202 +     *                        time (in milliseconds within the day), which is local
   1.203 +     *                        standard time in this case.
   1.204 +     * @param endMonth        The daylight saving time ending month. Month is
   1.205 +     *                        a {@link Calendar#MONTH MONTH} field
   1.206 +     *                        value (0-based. e.g., 9 for October).
   1.207 +     * @param endDay          The day of the month on which the daylight saving time ends.
   1.208 +     *                        See the class description for the special cases of this parameter.
   1.209 +     * @param endDayOfWeek    The daylight saving time ending day-of-week.
   1.210 +     *                        See the class description for the special cases of this parameter.
   1.211 +     * @param endTime         The daylight saving ending time in local wall clock time,
   1.212 +     *                        (in milliseconds within the day) which is local daylight
   1.213 +     *                        time in this case.
   1.214 +     * @exception IllegalArgumentException if the month, day, dayOfWeek, or time
   1.215 +     * parameters are out of range for the start or end rule
   1.216 +     */
   1.217 +    public SimpleTimeZone(int rawOffset, String ID,
   1.218 +                          int startMonth, int startDay, int startDayOfWeek, int startTime,
   1.219 +                          int endMonth, int endDay, int endDayOfWeek, int endTime)
   1.220 +    {
   1.221 +        this(rawOffset, ID,
   1.222 +             startMonth, startDay, startDayOfWeek, startTime, WALL_TIME,
   1.223 +             endMonth, endDay, endDayOfWeek, endTime, WALL_TIME,
   1.224 +             millisPerHour);
   1.225 +    }
   1.226 +
   1.227 +    /**
   1.228 +     * Constructs a SimpleTimeZone with the given base time zone offset from
   1.229 +     * GMT, time zone ID, and rules for starting and ending the daylight
   1.230 +     * time.
   1.231 +     * Both <code>startTime</code> and <code>endTime</code> are assumed to be
   1.232 +     * represented in the wall clock time. This constructor is equivalent to:
   1.233 +     * <pre><code>
   1.234 +     *     SimpleTimeZone(rawOffset,
   1.235 +     *                    ID,
   1.236 +     *                    startMonth,
   1.237 +     *                    startDay,
   1.238 +     *                    startDayOfWeek,
   1.239 +     *                    startTime,
   1.240 +     *                    SimpleTimeZone.{@link #WALL_TIME},
   1.241 +     *                    endMonth,
   1.242 +     *                    endDay,
   1.243 +     *                    endDayOfWeek,
   1.244 +     *                    endTime,
   1.245 +     *                    SimpleTimeZone.{@link #WALL_TIME},
   1.246 +     *                    dstSavings)
   1.247 +     * </code></pre>
   1.248 +     *
   1.249 +     * @param rawOffset       The given base time zone offset from GMT.
   1.250 +     * @param ID              The time zone ID which is given to this object.
   1.251 +     * @param startMonth      The daylight saving time starting month. Month is
   1.252 +     *                        a {@link Calendar#MONTH MONTH} field
   1.253 +     *                        value (0-based. e.g., 0 for January).
   1.254 +     * @param startDay        The day of the month on which the daylight saving time starts.
   1.255 +     *                        See the class description for the special cases of this parameter.
   1.256 +     * @param startDayOfWeek  The daylight saving time starting day-of-week.
   1.257 +     *                        See the class description for the special cases of this parameter.
   1.258 +     * @param startTime       The daylight saving time starting time in local wall clock
   1.259 +     *                        time, which is local standard time in this case.
   1.260 +     * @param endMonth        The daylight saving time ending month. Month is
   1.261 +     *                        a {@link Calendar#MONTH MONTH} field
   1.262 +     *                        value (0-based. e.g., 9 for October).
   1.263 +     * @param endDay          The day of the month on which the daylight saving time ends.
   1.264 +     *                        See the class description for the special cases of this parameter.
   1.265 +     * @param endDayOfWeek    The daylight saving time ending day-of-week.
   1.266 +     *                        See the class description for the special cases of this parameter.
   1.267 +     * @param endTime         The daylight saving ending time in local wall clock time,
   1.268 +     *                        which is local daylight time in this case.
   1.269 +     * @param dstSavings      The amount of time in milliseconds saved during
   1.270 +     *                        daylight saving time.
   1.271 +     * @exception IllegalArgumentException if the month, day, dayOfWeek, or time
   1.272 +     * parameters are out of range for the start or end rule
   1.273 +     * @since 1.2
   1.274 +     */
   1.275 +    public SimpleTimeZone(int rawOffset, String ID,
   1.276 +                          int startMonth, int startDay, int startDayOfWeek, int startTime,
   1.277 +                          int endMonth, int endDay, int endDayOfWeek, int endTime,
   1.278 +                          int dstSavings)
   1.279 +    {
   1.280 +        this(rawOffset, ID,
   1.281 +             startMonth, startDay, startDayOfWeek, startTime, WALL_TIME,
   1.282 +             endMonth, endDay, endDayOfWeek, endTime, WALL_TIME,
   1.283 +             dstSavings);
   1.284 +    }
   1.285 +
   1.286 +    /**
   1.287 +     * Constructs a SimpleTimeZone with the given base time zone offset from
   1.288 +     * GMT, time zone ID, and rules for starting and ending the daylight
   1.289 +     * time.
   1.290 +     * This constructor takes the full set of the start and end rules
   1.291 +     * parameters, including modes of <code>startTime</code> and
   1.292 +     * <code>endTime</code>. The mode specifies either {@link #WALL_TIME wall
   1.293 +     * time} or {@link #STANDARD_TIME standard time} or {@link #UTC_TIME UTC
   1.294 +     * time}.
   1.295 +     *
   1.296 +     * @param rawOffset       The given base time zone offset from GMT.
   1.297 +     * @param ID              The time zone ID which is given to this object.
   1.298 +     * @param startMonth      The daylight saving time starting month. Month is
   1.299 +     *                        a {@link Calendar#MONTH MONTH} field
   1.300 +     *                        value (0-based. e.g., 0 for January).
   1.301 +     * @param startDay        The day of the month on which the daylight saving time starts.
   1.302 +     *                        See the class description for the special cases of this parameter.
   1.303 +     * @param startDayOfWeek  The daylight saving time starting day-of-week.
   1.304 +     *                        See the class description for the special cases of this parameter.
   1.305 +     * @param startTime       The daylight saving time starting time in the time mode
   1.306 +     *                        specified by <code>startTimeMode</code>.
   1.307 +     * @param startTimeMode   The mode of the start time specified by startTime.
   1.308 +     * @param endMonth        The daylight saving time ending month. Month is
   1.309 +     *                        a {@link Calendar#MONTH MONTH} field
   1.310 +     *                        value (0-based. e.g., 9 for October).
   1.311 +     * @param endDay          The day of the month on which the daylight saving time ends.
   1.312 +     *                        See the class description for the special cases of this parameter.
   1.313 +     * @param endDayOfWeek    The daylight saving time ending day-of-week.
   1.314 +     *                        See the class description for the special cases of this parameter.
   1.315 +     * @param endTime         The daylight saving ending time in time time mode
   1.316 +     *                        specified by <code>endTimeMode</code>.
   1.317 +     * @param endTimeMode     The mode of the end time specified by endTime
   1.318 +     * @param dstSavings      The amount of time in milliseconds saved during
   1.319 +     *                        daylight saving time.
   1.320 +     *
   1.321 +     * @exception IllegalArgumentException if the month, day, dayOfWeek, time more, or
   1.322 +     * time parameters are out of range for the start or end rule, or if a time mode
   1.323 +     * value is invalid.
   1.324 +     *
   1.325 +     * @see #WALL_TIME
   1.326 +     * @see #STANDARD_TIME
   1.327 +     * @see #UTC_TIME
   1.328 +     *
   1.329 +     * @since 1.4
   1.330 +     */
   1.331 +    public SimpleTimeZone(int rawOffset, String ID,
   1.332 +                          int startMonth, int startDay, int startDayOfWeek,
   1.333 +                          int startTime, int startTimeMode,
   1.334 +                          int endMonth, int endDay, int endDayOfWeek,
   1.335 +                          int endTime, int endTimeMode,
   1.336 +                          int dstSavings) {
   1.337 +
   1.338 +        setID(ID);
   1.339 +        this.rawOffset      = rawOffset;
   1.340 +        this.startMonth     = startMonth;
   1.341 +        this.startDay       = startDay;
   1.342 +        this.startDayOfWeek = startDayOfWeek;
   1.343 +        this.startTime      = startTime;
   1.344 +        this.startTimeMode  = startTimeMode;
   1.345 +        this.endMonth       = endMonth;
   1.346 +        this.endDay         = endDay;
   1.347 +        this.endDayOfWeek   = endDayOfWeek;
   1.348 +        this.endTime        = endTime;
   1.349 +        this.endTimeMode    = endTimeMode;
   1.350 +        this.dstSavings     = dstSavings;
   1.351 +
   1.352 +        // this.useDaylight is set by decodeRules
   1.353 +        decodeRules();
   1.354 +        if (dstSavings <= 0) {
   1.355 +            throw new IllegalArgumentException("Illegal daylight saving value: " + dstSavings);
   1.356 +        }
   1.357 +    }
   1.358 +
   1.359 +    /**
   1.360 +     * Sets the daylight saving time starting year.
   1.361 +     *
   1.362 +     * @param year  The daylight saving starting year.
   1.363 +     */
   1.364 +    public void setStartYear(int year)
   1.365 +    {
   1.366 +        startYear = year;
   1.367 +        invalidateCache();
   1.368 +    }
   1.369 +
   1.370 +    /**
   1.371 +     * Sets the daylight saving time start rule. For example, if daylight saving
   1.372 +     * time starts on the first Sunday in April at 2 am in local wall clock
   1.373 +     * time, you can set the start rule by calling:
   1.374 +     * <pre><code>setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);</code></pre>
   1.375 +     *
   1.376 +     * @param startMonth      The daylight saving time starting month. Month is
   1.377 +     *                        a {@link Calendar#MONTH MONTH} field
   1.378 +     *                        value (0-based. e.g., 0 for January).
   1.379 +     * @param startDay        The day of the month on which the daylight saving time starts.
   1.380 +     *                        See the class description for the special cases of this parameter.
   1.381 +     * @param startDayOfWeek  The daylight saving time starting day-of-week.
   1.382 +     *                        See the class description for the special cases of this parameter.
   1.383 +     * @param startTime       The daylight saving time starting time in local wall clock
   1.384 +     *                        time, which is local standard time in this case.
   1.385 +     * @exception IllegalArgumentException if the <code>startMonth</code>, <code>startDay</code>,
   1.386 +     * <code>startDayOfWeek</code>, or <code>startTime</code> parameters are out of range
   1.387 +     */
   1.388 +    public void setStartRule(int startMonth, int startDay, int startDayOfWeek, int startTime)
   1.389 +    {
   1.390 +        this.startMonth = startMonth;
   1.391 +        this.startDay = startDay;
   1.392 +        this.startDayOfWeek = startDayOfWeek;
   1.393 +        this.startTime = startTime;
   1.394 +        startTimeMode = WALL_TIME;
   1.395 +        decodeStartRule();
   1.396 +        invalidateCache();
   1.397 +    }
   1.398 +
   1.399 +    /**
   1.400 +     * Sets the daylight saving time start rule to a fixed date within a month.
   1.401 +     * This method is equivalent to:
   1.402 +     * <pre><code>setStartRule(startMonth, startDay, 0, startTime)</code></pre>
   1.403 +     *
   1.404 +     * @param startMonth      The daylight saving time starting month. Month is
   1.405 +     *                        a {@link Calendar#MONTH MONTH} field
   1.406 +     *                        value (0-based. e.g., 0 for January).
   1.407 +     * @param startDay        The day of the month on which the daylight saving time starts.
   1.408 +     * @param startTime       The daylight saving time starting time in local wall clock
   1.409 +     *                        time, which is local standard time in this case.
   1.410 +     *                        See the class description for the special cases of this parameter.
   1.411 +     * @exception IllegalArgumentException if the <code>startMonth</code>,
   1.412 +     * <code>startDayOfMonth</code>, or <code>startTime</code> parameters are out of range
   1.413 +     * @since 1.2
   1.414 +     */
   1.415 +    public void setStartRule(int startMonth, int startDay, int startTime) {
   1.416 +        setStartRule(startMonth, startDay, 0, startTime);
   1.417 +    }
   1.418 +
   1.419 +    /**
   1.420 +     * Sets the daylight saving time start rule to a weekday before or after the given date within
   1.421 +     * a month, e.g., the first Monday on or after the 8th.
   1.422 +     *
   1.423 +     * @param startMonth      The daylight saving time starting month. Month is
   1.424 +     *                        a {@link Calendar#MONTH MONTH} field
   1.425 +     *                        value (0-based. e.g., 0 for January).
   1.426 +     * @param startDay        The day of the month on which the daylight saving time starts.
   1.427 +     * @param startDayOfWeek  The daylight saving time starting day-of-week.
   1.428 +     * @param startTime       The daylight saving time starting time in local wall clock
   1.429 +     *                        time, which is local standard time in this case.
   1.430 +     * @param after           If true, this rule selects the first <code>dayOfWeek</code> on or
   1.431 +     *                        <em>after</em> <code>dayOfMonth</code>.  If false, this rule
   1.432 +     *                        selects the last <code>dayOfWeek</code> on or <em>before</em>
   1.433 +     *                        <code>dayOfMonth</code>.
   1.434 +     * @exception IllegalArgumentException if the <code>startMonth</code>, <code>startDay</code>,
   1.435 +     * <code>startDayOfWeek</code>, or <code>startTime</code> parameters are out of range
   1.436 +     * @since 1.2
   1.437 +     */
   1.438 +    public void setStartRule(int startMonth, int startDay, int startDayOfWeek,
   1.439 +                             int startTime, boolean after)
   1.440 +    {
   1.441 +        // TODO: this method doesn't check the initial values of dayOfMonth or dayOfWeek.
   1.442 +        if (after) {
   1.443 +            setStartRule(startMonth, startDay, -startDayOfWeek, startTime);
   1.444 +        } else {
   1.445 +            setStartRule(startMonth, -startDay, -startDayOfWeek, startTime);
   1.446 +        }
   1.447 +    }
   1.448 +
   1.449 +    /**
   1.450 +     * Sets the daylight saving time end rule. For example, if daylight saving time
   1.451 +     * ends on the last Sunday in October at 2 am in wall clock time,
   1.452 +     * you can set the end rule by calling:
   1.453 +     * <code>setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);</code>
   1.454 +     *
   1.455 +     * @param endMonth        The daylight saving time ending month. Month is
   1.456 +     *                        a {@link Calendar#MONTH MONTH} field
   1.457 +     *                        value (0-based. e.g., 9 for October).
   1.458 +     * @param endDay          The day of the month on which the daylight saving time ends.
   1.459 +     *                        See the class description for the special cases of this parameter.
   1.460 +     * @param endDayOfWeek    The daylight saving time ending day-of-week.
   1.461 +     *                        See the class description for the special cases of this parameter.
   1.462 +     * @param endTime         The daylight saving ending time in local wall clock time,
   1.463 +     *                        (in milliseconds within the day) which is local daylight
   1.464 +     *                        time in this case.
   1.465 +     * @exception IllegalArgumentException if the <code>endMonth</code>, <code>endDay</code>,
   1.466 +     * <code>endDayOfWeek</code>, or <code>endTime</code> parameters are out of range
   1.467 +     */
   1.468 +    public void setEndRule(int endMonth, int endDay, int endDayOfWeek,
   1.469 +                           int endTime)
   1.470 +    {
   1.471 +        this.endMonth = endMonth;
   1.472 +        this.endDay = endDay;
   1.473 +        this.endDayOfWeek = endDayOfWeek;
   1.474 +        this.endTime = endTime;
   1.475 +        this.endTimeMode = WALL_TIME;
   1.476 +        decodeEndRule();
   1.477 +        invalidateCache();
   1.478 +    }
   1.479 +
   1.480 +    /**
   1.481 +     * Sets the daylight saving time end rule to a fixed date within a month.
   1.482 +     * This method is equivalent to:
   1.483 +     * <pre><code>setEndRule(endMonth, endDay, 0, endTime)</code></pre>
   1.484 +     *
   1.485 +     * @param endMonth        The daylight saving time ending month. Month is
   1.486 +     *                        a {@link Calendar#MONTH MONTH} field
   1.487 +     *                        value (0-based. e.g., 9 for October).
   1.488 +     * @param endDay          The day of the month on which the daylight saving time ends.
   1.489 +     * @param endTime         The daylight saving ending time in local wall clock time,
   1.490 +     *                        (in milliseconds within the day) which is local daylight
   1.491 +     *                        time in this case.
   1.492 +     * @exception IllegalArgumentException the <code>endMonth</code>, <code>endDay</code>,
   1.493 +     * or <code>endTime</code> parameters are out of range
   1.494 +     * @since 1.2
   1.495 +     */
   1.496 +    public void setEndRule(int endMonth, int endDay, int endTime)
   1.497 +    {
   1.498 +        setEndRule(endMonth, endDay, 0, endTime);
   1.499 +    }
   1.500 +
   1.501 +    /**
   1.502 +     * Sets the daylight saving time end rule to a weekday before or after the given date within
   1.503 +     * a month, e.g., the first Monday on or after the 8th.
   1.504 +     *
   1.505 +     * @param endMonth        The daylight saving time ending month. Month is
   1.506 +     *                        a {@link Calendar#MONTH MONTH} field
   1.507 +     *                        value (0-based. e.g., 9 for October).
   1.508 +     * @param endDay          The day of the month on which the daylight saving time ends.
   1.509 +     * @param endDayOfWeek    The daylight saving time ending day-of-week.
   1.510 +     * @param endTime         The daylight saving ending time in local wall clock time,
   1.511 +     *                        (in milliseconds within the day) which is local daylight
   1.512 +     *                        time in this case.
   1.513 +     * @param after           If true, this rule selects the first <code>endDayOfWeek</code> on
   1.514 +     *                        or <em>after</em> <code>endDay</code>.  If false, this rule
   1.515 +     *                        selects the last <code>endDayOfWeek</code> on or before
   1.516 +     *                        <code>endDay</code> of the month.
   1.517 +     * @exception IllegalArgumentException the <code>endMonth</code>, <code>endDay</code>,
   1.518 +     * <code>endDayOfWeek</code>, or <code>endTime</code> parameters are out of range
   1.519 +     * @since 1.2
   1.520 +     */
   1.521 +    public void setEndRule(int endMonth, int endDay, int endDayOfWeek, int endTime, boolean after)
   1.522 +    {
   1.523 +        if (after) {
   1.524 +            setEndRule(endMonth, endDay, -endDayOfWeek, endTime);
   1.525 +        } else {
   1.526 +            setEndRule(endMonth, -endDay, -endDayOfWeek, endTime);
   1.527 +        }
   1.528 +    }
   1.529 +
   1.530 +    /**
   1.531 +     * Returns the offset of this time zone from UTC at the given
   1.532 +     * time. If daylight saving time is in effect at the given time,
   1.533 +     * the offset value is adjusted with the amount of daylight
   1.534 +     * saving.
   1.535 +     *
   1.536 +     * @param date the time at which the time zone offset is found
   1.537 +     * @return the amount of time in milliseconds to add to UTC to get
   1.538 +     * local time.
   1.539 +     * @since 1.4
   1.540 +     */
   1.541 +    public int getOffset(long date) {
   1.542 +        return getOffsets(date, null);
   1.543 +    }
   1.544 +
   1.545 +    /**
   1.546 +     * @see TimeZone#getOffsets
   1.547 +     */
   1.548 +    int getOffsets(long date, int[] offsets) {
   1.549 +        int offset = rawOffset;
   1.550 +
   1.551 +      computeOffset:
   1.552 +        if (useDaylight) {
   1.553 +            synchronized (this) {
   1.554 +                if (cacheStart != 0) {
   1.555 +                    if (date >= cacheStart && date < cacheEnd) {
   1.556 +                        offset += dstSavings;
   1.557 +                        break computeOffset;
   1.558 +                    }
   1.559 +                }
   1.560 +            }
   1.561 +            BaseCalendar cal = date >= GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER ?
   1.562 +                gcal : (BaseCalendar) CalendarSystem.forName("julian");
   1.563 +            BaseCalendar.Date cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
   1.564 +            // Get the year in local time
   1.565 +            cal.getCalendarDate(date + rawOffset, cdate);
   1.566 +            int year = cdate.getNormalizedYear();
   1.567 +            if (year >= startYear) {
   1.568 +                // Clear time elements for the transition calculations
   1.569 +                cdate.setTimeOfDay(0, 0, 0, 0);
   1.570 +                offset = getOffset(cal, cdate, year, date);
   1.571 +            }
   1.572 +        }
   1.573 +
   1.574 +        if (offsets != null) {
   1.575 +            offsets[0] = rawOffset;
   1.576 +            offsets[1] = offset - rawOffset;
   1.577 +        }
   1.578 +        return offset;
   1.579 +    }
   1.580 +
   1.581 +   /**
   1.582 +     * Returns the difference in milliseconds between local time and
   1.583 +     * UTC, taking into account both the raw offset and the effect of
   1.584 +     * daylight saving, for the specified date and time.  This method
   1.585 +     * assumes that the start and end month are distinct.  It also
   1.586 +     * uses a default {@link GregorianCalendar} object as its
   1.587 +     * underlying calendar, such as for determining leap years.  Do
   1.588 +     * not use the result of this method with a calendar other than a
   1.589 +     * default <code>GregorianCalendar</code>.
   1.590 +     *
   1.591 +     * <p><em>Note:  In general, clients should use
   1.592 +     * <code>Calendar.get(ZONE_OFFSET) + Calendar.get(DST_OFFSET)</code>
   1.593 +     * instead of calling this method.</em>
   1.594 +     *
   1.595 +     * @param era       The era of the given date.
   1.596 +     * @param year      The year in the given date.
   1.597 +     * @param month     The month in the given date. Month is 0-based. e.g.,
   1.598 +     *                  0 for January.
   1.599 +     * @param day       The day-in-month of the given date.
   1.600 +     * @param dayOfWeek The day-of-week of the given date.
   1.601 +     * @param millis    The milliseconds in day in <em>standard</em> local time.
   1.602 +     * @return          The milliseconds to add to UTC to get local time.
   1.603 +     * @exception       IllegalArgumentException the <code>era</code>,
   1.604 +     *                  <code>month</code>, <code>day</code>, <code>dayOfWeek</code>,
   1.605 +     *                  or <code>millis</code> parameters are out of range
   1.606 +     */
   1.607 +    public int getOffset(int era, int year, int month, int day, int dayOfWeek,
   1.608 +                         int millis)
   1.609 +    {
   1.610 +        if (era != GregorianCalendar.AD && era != GregorianCalendar.BC) {
   1.611 +            throw new IllegalArgumentException("Illegal era " + era);
   1.612 +        }
   1.613 +
   1.614 +        int y = year;
   1.615 +        if (era == GregorianCalendar.BC) {
   1.616 +            // adjust y with the GregorianCalendar-style year numbering.
   1.617 +            y = 1 - y;
   1.618 +        }
   1.619 +
   1.620 +        // If the year isn't representable with the 64-bit long
   1.621 +        // integer in milliseconds, convert the year to an
   1.622 +        // equivalent year. This is required to pass some JCK test cases
   1.623 +        // which are actually useless though because the specified years
   1.624 +        // can't be supported by the Java time system.
   1.625 +        if (y >= 292278994) {
   1.626 +            y = 2800 + y % 2800;
   1.627 +        } else if (y <= -292269054) {
   1.628 +            // y %= 28 also produces an equivalent year, but positive
   1.629 +            // year numbers would be convenient to use the UNIX cal
   1.630 +            // command.
   1.631 +            y = (int) CalendarUtils.mod((long) y, 28);
   1.632 +        }
   1.633 +
   1.634 +        // convert year to its 1-based month value
   1.635 +        int m = month + 1;
   1.636 +
   1.637 +        // First, calculate time as a Gregorian date.
   1.638 +        BaseCalendar cal = gcal;
   1.639 +        BaseCalendar.Date cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
   1.640 +        cdate.setDate(y, m, day);
   1.641 +        long time = cal.getTime(cdate); // normalize cdate
   1.642 +        time += millis - rawOffset; // UTC time
   1.643 +
   1.644 +        // If the time value represents a time before the default
   1.645 +        // Gregorian cutover, recalculate time using the Julian
   1.646 +        // calendar system. For the Julian calendar system, the
   1.647 +        // normalized year numbering is ..., -2 (BCE 2), -1 (BCE 1),
   1.648 +        // 1, 2 ... which is different from the GregorianCalendar
   1.649 +        // style year numbering (..., -1, 0 (BCE 1), 1, 2, ...).
   1.650 +        if (time < GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER) {
   1.651 +            cal = (BaseCalendar) CalendarSystem.forName("julian");
   1.652 +            cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
   1.653 +            cdate.setNormalizedDate(y, m, day);
   1.654 +            time = cal.getTime(cdate) + millis - rawOffset;
   1.655 +        }
   1.656 +
   1.657 +        if ((cdate.getNormalizedYear() != y)
   1.658 +            || (cdate.getMonth() != m)
   1.659 +            || (cdate.getDayOfMonth() != day)
   1.660 +            // The validation should be cdate.getDayOfWeek() ==
   1.661 +            // dayOfWeek. However, we don't check dayOfWeek for
   1.662 +            // compatibility.
   1.663 +            || (dayOfWeek < Calendar.SUNDAY || dayOfWeek > Calendar.SATURDAY)
   1.664 +            || (millis < 0 || millis >= (24*60*60*1000))) {
   1.665 +            throw new IllegalArgumentException();
   1.666 +        }
   1.667 +
   1.668 +        if (!useDaylight || year < startYear || era != GregorianCalendar.CE) {
   1.669 +            return rawOffset;
   1.670 +        }
   1.671 +
   1.672 +        return getOffset(cal, cdate, y, time);
   1.673 +    }
   1.674 +
   1.675 +    private int getOffset(BaseCalendar cal, BaseCalendar.Date cdate, int year, long time) {
   1.676 +        synchronized (this) {
   1.677 +            if (cacheStart != 0) {
   1.678 +                if (time >= cacheStart && time < cacheEnd) {
   1.679 +                    return rawOffset + dstSavings;
   1.680 +                }
   1.681 +                if (year == cacheYear) {
   1.682 +                    return rawOffset;
   1.683 +                }
   1.684 +            }
   1.685 +        }
   1.686 +
   1.687 +        long start = getStart(cal, cdate, year);
   1.688 +        long end = getEnd(cal, cdate, year);
   1.689 +        int offset = rawOffset;
   1.690 +        if (start <= end) {
   1.691 +            if (time >= start && time < end) {
   1.692 +                offset += dstSavings;
   1.693 +            }
   1.694 +            synchronized (this) {
   1.695 +                cacheYear = year;
   1.696 +                cacheStart = start;
   1.697 +                cacheEnd = end;
   1.698 +            }
   1.699 +        } else {
   1.700 +            if (time < end) {
   1.701 +                // TODO: support Gregorian cutover. The previous year
   1.702 +                // may be in the other calendar system.
   1.703 +                start = getStart(cal, cdate, year - 1);
   1.704 +                if (time >= start) {
   1.705 +                    offset += dstSavings;
   1.706 +                }
   1.707 +            } else if (time >= start) {
   1.708 +                // TODO: support Gregorian cutover. The next year
   1.709 +                // may be in the other calendar system.
   1.710 +                end = getEnd(cal, cdate, year + 1);
   1.711 +                if (time < end) {
   1.712 +                    offset += dstSavings;
   1.713 +                }
   1.714 +            }
   1.715 +            if (start <= end) {
   1.716 +                synchronized (this) {
   1.717 +                    // The start and end transitions are in multiple years.
   1.718 +                    cacheYear = (long) startYear - 1;
   1.719 +                    cacheStart = start;
   1.720 +                    cacheEnd = end;
   1.721 +                }
   1.722 +            }
   1.723 +        }
   1.724 +        return offset;
   1.725 +    }
   1.726 +
   1.727 +    private long getStart(BaseCalendar cal, BaseCalendar.Date cdate, int year) {
   1.728 +        int time = startTime;
   1.729 +        if (startTimeMode != UTC_TIME) {
   1.730 +            time -= rawOffset;
   1.731 +        }
   1.732 +        return getTransition(cal, cdate, startMode, year, startMonth, startDay,
   1.733 +                             startDayOfWeek, time);
   1.734 +    }
   1.735 +
   1.736 +    private long getEnd(BaseCalendar cal, BaseCalendar.Date cdate, int year) {
   1.737 +        int time = endTime;
   1.738 +        if (endTimeMode != UTC_TIME) {
   1.739 +            time -= rawOffset;
   1.740 +        }
   1.741 +        if (endTimeMode == WALL_TIME) {
   1.742 +            time -= dstSavings;
   1.743 +        }
   1.744 +        return getTransition(cal, cdate, endMode, year, endMonth, endDay,
   1.745 +                                        endDayOfWeek, time);
   1.746 +    }
   1.747 +
   1.748 +    private long getTransition(BaseCalendar cal, BaseCalendar.Date cdate,
   1.749 +                               int mode, int year, int month, int dayOfMonth,
   1.750 +                               int dayOfWeek, int timeOfDay) {
   1.751 +        cdate.setNormalizedYear(year);
   1.752 +        cdate.setMonth(month + 1);
   1.753 +        switch (mode) {
   1.754 +        case DOM_MODE:
   1.755 +            cdate.setDayOfMonth(dayOfMonth);
   1.756 +            break;
   1.757 +
   1.758 +        case DOW_IN_MONTH_MODE:
   1.759 +            cdate.setDayOfMonth(1);
   1.760 +            if (dayOfMonth < 0) {
   1.761 +                cdate.setDayOfMonth(cal.getMonthLength(cdate));
   1.762 +            }
   1.763 +            cdate = (BaseCalendar.Date) cal.getNthDayOfWeek(dayOfMonth, dayOfWeek, cdate);
   1.764 +            break;
   1.765 +
   1.766 +        case DOW_GE_DOM_MODE:
   1.767 +            cdate.setDayOfMonth(dayOfMonth);
   1.768 +            cdate = (BaseCalendar.Date) cal.getNthDayOfWeek(1, dayOfWeek, cdate);
   1.769 +            break;
   1.770 +
   1.771 +        case DOW_LE_DOM_MODE:
   1.772 +            cdate.setDayOfMonth(dayOfMonth);
   1.773 +            cdate = (BaseCalendar.Date) cal.getNthDayOfWeek(-1, dayOfWeek, cdate);
   1.774 +            break;
   1.775 +        }
   1.776 +        return cal.getTime(cdate) + timeOfDay;
   1.777 +    }
   1.778 +
   1.779 +    /**
   1.780 +     * Gets the GMT offset for this time zone.
   1.781 +     * @return the GMT offset value in milliseconds
   1.782 +     * @see #setRawOffset
   1.783 +     */
   1.784 +    public int getRawOffset()
   1.785 +    {
   1.786 +        // The given date will be taken into account while
   1.787 +        // we have the historical time zone data in place.
   1.788 +        return rawOffset;
   1.789 +    }
   1.790 +
   1.791 +    /**
   1.792 +     * Sets the base time zone offset to GMT.
   1.793 +     * This is the offset to add to UTC to get local time.
   1.794 +     * @see #getRawOffset
   1.795 +     */
   1.796 +    public void setRawOffset(int offsetMillis)
   1.797 +    {
   1.798 +        this.rawOffset = offsetMillis;
   1.799 +    }
   1.800 +
   1.801 +    /**
   1.802 +     * Sets the amount of time in milliseconds that the clock is advanced
   1.803 +     * during daylight saving time.
   1.804 +     * @param millisSavedDuringDST the number of milliseconds the time is
   1.805 +     * advanced with respect to standard time when the daylight saving time rules
   1.806 +     * are in effect. A positive number, typically one hour (3600000).
   1.807 +     * @see #getDSTSavings
   1.808 +     * @since 1.2
   1.809 +     */
   1.810 +    public void setDSTSavings(int millisSavedDuringDST) {
   1.811 +        if (millisSavedDuringDST <= 0) {
   1.812 +            throw new IllegalArgumentException("Illegal daylight saving value: "
   1.813 +                                               + millisSavedDuringDST);
   1.814 +        }
   1.815 +        dstSavings = millisSavedDuringDST;
   1.816 +    }
   1.817 +
   1.818 +    /**
   1.819 +     * Returns the amount of time in milliseconds that the clock is
   1.820 +     * advanced during daylight saving time.
   1.821 +     *
   1.822 +     * @return the number of milliseconds the time is advanced with
   1.823 +     * respect to standard time when the daylight saving rules are in
   1.824 +     * effect, or 0 (zero) if this time zone doesn't observe daylight
   1.825 +     * saving time.
   1.826 +     *
   1.827 +     * @see #setDSTSavings
   1.828 +     * @since 1.2
   1.829 +     */
   1.830 +    public int getDSTSavings() {
   1.831 +        return useDaylight ? dstSavings : 0;
   1.832 +    }
   1.833 +
   1.834 +    /**
   1.835 +     * Queries if this time zone uses daylight saving time.
   1.836 +     * @return true if this time zone uses daylight saving time;
   1.837 +     * false otherwise.
   1.838 +     */
   1.839 +    public boolean useDaylightTime()
   1.840 +    {
   1.841 +        return useDaylight;
   1.842 +    }
   1.843 +
   1.844 +    /**
   1.845 +     * Returns {@code true} if this {@code SimpleTimeZone} observes
   1.846 +     * Daylight Saving Time. This method is equivalent to {@link
   1.847 +     * #useDaylightTime()}.
   1.848 +     *
   1.849 +     * @return {@code true} if this {@code SimpleTimeZone} observes
   1.850 +     * Daylight Saving Time; {@code false} otherwise.
   1.851 +     * @since 1.7
   1.852 +     */
   1.853 +    @Override
   1.854 +    public boolean observesDaylightTime() {
   1.855 +        return useDaylightTime();
   1.856 +    }
   1.857 +
   1.858 +    /**
   1.859 +     * Queries if the given date is in daylight saving time.
   1.860 +     * @return true if daylight saving time is in effective at the
   1.861 +     * given date; false otherwise.
   1.862 +     */
   1.863 +    public boolean inDaylightTime(Date date)
   1.864 +    {
   1.865 +        return (getOffset(date.getTime()) != rawOffset);
   1.866 +    }
   1.867 +
   1.868 +    /**
   1.869 +     * Returns a clone of this <code>SimpleTimeZone</code> instance.
   1.870 +     * @return a clone of this instance.
   1.871 +     */
   1.872 +    public Object clone()
   1.873 +    {
   1.874 +        return super.clone();
   1.875 +    }
   1.876 +
   1.877 +    /**
   1.878 +     * Generates the hash code for the SimpleDateFormat object.
   1.879 +     * @return the hash code for this object
   1.880 +     */
   1.881 +    public synchronized int hashCode()
   1.882 +    {
   1.883 +        return startMonth ^ startDay ^ startDayOfWeek ^ startTime ^
   1.884 +            endMonth ^ endDay ^ endDayOfWeek ^ endTime ^ rawOffset;
   1.885 +    }
   1.886 +
   1.887 +    /**
   1.888 +     * Compares the equality of two <code>SimpleTimeZone</code> objects.
   1.889 +     *
   1.890 +     * @param obj  The <code>SimpleTimeZone</code> object to be compared with.
   1.891 +     * @return     True if the given <code>obj</code> is the same as this
   1.892 +     *             <code>SimpleTimeZone</code> object; false otherwise.
   1.893 +     */
   1.894 +    public boolean equals(Object obj)
   1.895 +    {
   1.896 +        if (this == obj) {
   1.897 +            return true;
   1.898 +        }
   1.899 +        if (!(obj instanceof SimpleTimeZone)) {
   1.900 +            return false;
   1.901 +        }
   1.902 +
   1.903 +        SimpleTimeZone that = (SimpleTimeZone) obj;
   1.904 +
   1.905 +        return getID().equals(that.getID()) &&
   1.906 +            hasSameRules(that);
   1.907 +    }
   1.908 +
   1.909 +    /**
   1.910 +     * Returns <code>true</code> if this zone has the same rules and offset as another zone.
   1.911 +     * @param other the TimeZone object to be compared with
   1.912 +     * @return <code>true</code> if the given zone is a SimpleTimeZone and has the
   1.913 +     * same rules and offset as this one
   1.914 +     * @since 1.2
   1.915 +     */
   1.916 +    public boolean hasSameRules(TimeZone other) {
   1.917 +        if (this == other) {
   1.918 +            return true;
   1.919 +        }
   1.920 +        if (!(other instanceof SimpleTimeZone)) {
   1.921 +            return false;
   1.922 +        }
   1.923 +        SimpleTimeZone that = (SimpleTimeZone) other;
   1.924 +        return rawOffset == that.rawOffset &&
   1.925 +            useDaylight == that.useDaylight &&
   1.926 +            (!useDaylight
   1.927 +             // Only check rules if using DST
   1.928 +             || (dstSavings == that.dstSavings &&
   1.929 +                 startMode == that.startMode &&
   1.930 +                 startMonth == that.startMonth &&
   1.931 +                 startDay == that.startDay &&
   1.932 +                 startDayOfWeek == that.startDayOfWeek &&
   1.933 +                 startTime == that.startTime &&
   1.934 +                 startTimeMode == that.startTimeMode &&
   1.935 +                 endMode == that.endMode &&
   1.936 +                 endMonth == that.endMonth &&
   1.937 +                 endDay == that.endDay &&
   1.938 +                 endDayOfWeek == that.endDayOfWeek &&
   1.939 +                 endTime == that.endTime &&
   1.940 +                 endTimeMode == that.endTimeMode &&
   1.941 +                 startYear == that.startYear));
   1.942 +    }
   1.943 +
   1.944 +    /**
   1.945 +     * Returns a string representation of this time zone.
   1.946 +     * @return a string representation of this time zone.
   1.947 +     */
   1.948 +    public String toString() {
   1.949 +        return getClass().getName() +
   1.950 +            "[id=" + getID() +
   1.951 +            ",offset=" + rawOffset +
   1.952 +            ",dstSavings=" + dstSavings +
   1.953 +            ",useDaylight=" + useDaylight +
   1.954 +            ",startYear=" + startYear +
   1.955 +            ",startMode=" + startMode +
   1.956 +            ",startMonth=" + startMonth +
   1.957 +            ",startDay=" + startDay +
   1.958 +            ",startDayOfWeek=" + startDayOfWeek +
   1.959 +            ",startTime=" + startTime +
   1.960 +            ",startTimeMode=" + startTimeMode +
   1.961 +            ",endMode=" + endMode +
   1.962 +            ",endMonth=" + endMonth +
   1.963 +            ",endDay=" + endDay +
   1.964 +            ",endDayOfWeek=" + endDayOfWeek +
   1.965 +            ",endTime=" + endTime +
   1.966 +            ",endTimeMode=" + endTimeMode + ']';
   1.967 +    }
   1.968 +
   1.969 +    // =======================privates===============================
   1.970 +
   1.971 +    /**
   1.972 +     * The month in which daylight saving time starts.  This value must be
   1.973 +     * between <code>Calendar.JANUARY</code> and
   1.974 +     * <code>Calendar.DECEMBER</code> inclusive.  This value must not equal
   1.975 +     * <code>endMonth</code>.
   1.976 +     * <p>If <code>useDaylight</code> is false, this value is ignored.
   1.977 +     * @serial
   1.978 +     */
   1.979 +    private int startMonth;
   1.980 +
   1.981 +    /**
   1.982 +     * This field has two possible interpretations:
   1.983 +     * <dl>
   1.984 +     * <dt><code>startMode == DOW_IN_MONTH</code></dt>
   1.985 +     * <dd>
   1.986 +     * <code>startDay</code> indicates the day of the month of
   1.987 +     * <code>startMonth</code> on which daylight
   1.988 +     * saving time starts, from 1 to 28, 30, or 31, depending on the
   1.989 +     * <code>startMonth</code>.
   1.990 +     * </dd>
   1.991 +     * <dt><code>startMode != DOW_IN_MONTH</code></dt>
   1.992 +     * <dd>
   1.993 +     * <code>startDay</code> indicates which <code>startDayOfWeek</code> in the
   1.994 +     * month <code>startMonth</code> daylight
   1.995 +     * saving time starts on.  For example, a value of +1 and a
   1.996 +     * <code>startDayOfWeek</code> of <code>Calendar.SUNDAY</code> indicates the
   1.997 +     * first Sunday of <code>startMonth</code>.  Likewise, +2 would indicate the
   1.998 +     * second Sunday, and -1 the last Sunday.  A value of 0 is illegal.
   1.999 +     * </dd>
  1.1000 +     * </dl>
  1.1001 +     * <p>If <code>useDaylight</code> is false, this value is ignored.
  1.1002 +     * @serial
  1.1003 +     */
  1.1004 +    private int startDay;
  1.1005 +
  1.1006 +    /**
  1.1007 +     * The day of the week on which daylight saving time starts.  This value
  1.1008 +     * must be between <code>Calendar.SUNDAY</code> and
  1.1009 +     * <code>Calendar.SATURDAY</code> inclusive.
  1.1010 +     * <p>If <code>useDaylight</code> is false or
  1.1011 +     * <code>startMode == DAY_OF_MONTH</code>, this value is ignored.
  1.1012 +     * @serial
  1.1013 +     */
  1.1014 +    private int startDayOfWeek;
  1.1015 +
  1.1016 +    /**
  1.1017 +     * The time in milliseconds after midnight at which daylight saving
  1.1018 +     * time starts.  This value is expressed as wall time, standard time,
  1.1019 +     * or UTC time, depending on the setting of <code>startTimeMode</code>.
  1.1020 +     * <p>If <code>useDaylight</code> is false, this value is ignored.
  1.1021 +     * @serial
  1.1022 +     */
  1.1023 +    private int startTime;
  1.1024 +
  1.1025 +    /**
  1.1026 +     * The format of startTime, either WALL_TIME, STANDARD_TIME, or UTC_TIME.
  1.1027 +     * @serial
  1.1028 +     * @since 1.3
  1.1029 +     */
  1.1030 +    private int startTimeMode;
  1.1031 +
  1.1032 +    /**
  1.1033 +     * The month in which daylight saving time ends.  This value must be
  1.1034 +     * between <code>Calendar.JANUARY</code> and
  1.1035 +     * <code>Calendar.UNDECIMBER</code>.  This value must not equal
  1.1036 +     * <code>startMonth</code>.
  1.1037 +     * <p>If <code>useDaylight</code> is false, this value is ignored.
  1.1038 +     * @serial
  1.1039 +     */
  1.1040 +    private int endMonth;
  1.1041 +
  1.1042 +    /**
  1.1043 +     * This field has two possible interpretations:
  1.1044 +     * <dl>
  1.1045 +     * <dt><code>endMode == DOW_IN_MONTH</code></dt>
  1.1046 +     * <dd>
  1.1047 +     * <code>endDay</code> indicates the day of the month of
  1.1048 +     * <code>endMonth</code> on which daylight
  1.1049 +     * saving time ends, from 1 to 28, 30, or 31, depending on the
  1.1050 +     * <code>endMonth</code>.
  1.1051 +     * </dd>
  1.1052 +     * <dt><code>endMode != DOW_IN_MONTH</code></dt>
  1.1053 +     * <dd>
  1.1054 +     * <code>endDay</code> indicates which <code>endDayOfWeek</code> in th
  1.1055 +     * month <code>endMonth</code> daylight
  1.1056 +     * saving time ends on.  For example, a value of +1 and a
  1.1057 +     * <code>endDayOfWeek</code> of <code>Calendar.SUNDAY</code> indicates the
  1.1058 +     * first Sunday of <code>endMonth</code>.  Likewise, +2 would indicate the
  1.1059 +     * second Sunday, and -1 the last Sunday.  A value of 0 is illegal.
  1.1060 +     * </dd>
  1.1061 +     * </dl>
  1.1062 +     * <p>If <code>useDaylight</code> is false, this value is ignored.
  1.1063 +     * @serial
  1.1064 +     */
  1.1065 +    private int endDay;
  1.1066 +
  1.1067 +    /**
  1.1068 +     * The day of the week on which daylight saving time ends.  This value
  1.1069 +     * must be between <code>Calendar.SUNDAY</code> and
  1.1070 +     * <code>Calendar.SATURDAY</code> inclusive.
  1.1071 +     * <p>If <code>useDaylight</code> is false or
  1.1072 +     * <code>endMode == DAY_OF_MONTH</code>, this value is ignored.
  1.1073 +     * @serial
  1.1074 +     */
  1.1075 +    private int endDayOfWeek;
  1.1076 +
  1.1077 +    /**
  1.1078 +     * The time in milliseconds after midnight at which daylight saving
  1.1079 +     * time ends.  This value is expressed as wall time, standard time,
  1.1080 +     * or UTC time, depending on the setting of <code>endTimeMode</code>.
  1.1081 +     * <p>If <code>useDaylight</code> is false, this value is ignored.
  1.1082 +     * @serial
  1.1083 +     */
  1.1084 +    private int endTime;
  1.1085 +
  1.1086 +    /**
  1.1087 +     * The format of endTime, either <code>WALL_TIME</code>,
  1.1088 +     * <code>STANDARD_TIME</code>, or <code>UTC_TIME</code>.
  1.1089 +     * @serial
  1.1090 +     * @since 1.3
  1.1091 +     */
  1.1092 +    private int endTimeMode;
  1.1093 +
  1.1094 +    /**
  1.1095 +     * The year in which daylight saving time is first observed.  This is an {@link GregorianCalendar#AD AD}
  1.1096 +     * value.  If this value is less than 1 then daylight saving time is observed
  1.1097 +     * for all <code>AD</code> years.
  1.1098 +     * <p>If <code>useDaylight</code> is false, this value is ignored.
  1.1099 +     * @serial
  1.1100 +     */
  1.1101 +    private int startYear;
  1.1102 +
  1.1103 +    /**
  1.1104 +     * The offset in milliseconds between this zone and GMT.  Negative offsets
  1.1105 +     * are to the west of Greenwich.  To obtain local <em>standard</em> time,
  1.1106 +     * add the offset to GMT time.  To obtain local wall time it may also be
  1.1107 +     * necessary to add <code>dstSavings</code>.
  1.1108 +     * @serial
  1.1109 +     */
  1.1110 +    private int rawOffset;
  1.1111 +
  1.1112 +    /**
  1.1113 +     * A boolean value which is true if and only if this zone uses daylight
  1.1114 +     * saving time.  If this value is false, several other fields are ignored.
  1.1115 +     * @serial
  1.1116 +     */
  1.1117 +    private boolean useDaylight=false; // indicate if this time zone uses DST
  1.1118 +
  1.1119 +    private static final int millisPerHour = 60*60*1000;
  1.1120 +    private static final int millisPerDay  = 24*millisPerHour;
  1.1121 +
  1.1122 +    /**
  1.1123 +     * This field was serialized in JDK 1.1, so we have to keep it that way
  1.1124 +     * to maintain serialization compatibility. However, there's no need to
  1.1125 +     * recreate the array each time we create a new time zone.
  1.1126 +     * @serial An array of bytes containing the values {31, 28, 31, 30, 31, 30,
  1.1127 +     * 31, 31, 30, 31, 30, 31}.  This is ignored as of the Java 2 platform v1.2, however, it must
  1.1128 +     * be streamed out for compatibility with JDK 1.1.
  1.1129 +     */
  1.1130 +    private final byte monthLength[] = staticMonthLength;
  1.1131 +    private final static byte staticMonthLength[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  1.1132 +    private final static byte staticLeapMonthLength[] = {31,29,31,30,31,30,31,31,30,31,30,31};
  1.1133 +
  1.1134 +    /**
  1.1135 +     * Variables specifying the mode of the start rule.  Takes the following
  1.1136 +     * values:
  1.1137 +     * <dl>
  1.1138 +     * <dt><code>DOM_MODE</code></dt>
  1.1139 +     * <dd>
  1.1140 +     * Exact day of week; e.g., March 1.
  1.1141 +     * </dd>
  1.1142 +     * <dt><code>DOW_IN_MONTH_MODE</code></dt>
  1.1143 +     * <dd>
  1.1144 +     * Day of week in month; e.g., last Sunday in March.
  1.1145 +     * </dd>
  1.1146 +     * <dt><code>DOW_GE_DOM_MODE</code></dt>
  1.1147 +     * <dd>
  1.1148 +     * Day of week after day of month; e.g., Sunday on or after March 15.
  1.1149 +     * </dd>
  1.1150 +     * <dt><code>DOW_LE_DOM_MODE</code></dt>
  1.1151 +     * <dd>
  1.1152 +     * Day of week before day of month; e.g., Sunday on or before March 15.
  1.1153 +     * </dd>
  1.1154 +     * </dl>
  1.1155 +     * The setting of this field affects the interpretation of the
  1.1156 +     * <code>startDay</code> field.
  1.1157 +     * <p>If <code>useDaylight</code> is false, this value is ignored.
  1.1158 +     * @serial
  1.1159 +     * @since 1.1.4
  1.1160 +     */
  1.1161 +    private int startMode;
  1.1162 +
  1.1163 +    /**
  1.1164 +     * Variables specifying the mode of the end rule.  Takes the following
  1.1165 +     * values:
  1.1166 +     * <dl>
  1.1167 +     * <dt><code>DOM_MODE</code></dt>
  1.1168 +     * <dd>
  1.1169 +     * Exact day of week; e.g., March 1.
  1.1170 +     * </dd>
  1.1171 +     * <dt><code>DOW_IN_MONTH_MODE</code></dt>
  1.1172 +     * <dd>
  1.1173 +     * Day of week in month; e.g., last Sunday in March.
  1.1174 +     * </dd>
  1.1175 +     * <dt><code>DOW_GE_DOM_MODE</code></dt>
  1.1176 +     * <dd>
  1.1177 +     * Day of week after day of month; e.g., Sunday on or after March 15.
  1.1178 +     * </dd>
  1.1179 +     * <dt><code>DOW_LE_DOM_MODE</code></dt>
  1.1180 +     * <dd>
  1.1181 +     * Day of week before day of month; e.g., Sunday on or before March 15.
  1.1182 +     * </dd>
  1.1183 +     * </dl>
  1.1184 +     * The setting of this field affects the interpretation of the
  1.1185 +     * <code>endDay</code> field.
  1.1186 +     * <p>If <code>useDaylight</code> is false, this value is ignored.
  1.1187 +     * @serial
  1.1188 +     * @since 1.1.4
  1.1189 +     */
  1.1190 +    private int endMode;
  1.1191 +
  1.1192 +    /**
  1.1193 +     * A positive value indicating the amount of time saved during DST in
  1.1194 +     * milliseconds.
  1.1195 +     * Typically one hour (3600000); sometimes 30 minutes (1800000).
  1.1196 +     * <p>If <code>useDaylight</code> is false, this value is ignored.
  1.1197 +     * @serial
  1.1198 +     * @since 1.1.4
  1.1199 +     */
  1.1200 +    private int dstSavings;
  1.1201 +
  1.1202 +    private static final Gregorian gcal = CalendarSystem.getGregorianCalendar();
  1.1203 +
  1.1204 +    /**
  1.1205 +     * Cache values representing a single period of daylight saving
  1.1206 +     * time. When the cache values are valid, cacheStart is the start
  1.1207 +     * time (inclusive) of daylight saving time and cacheEnd is the
  1.1208 +     * end time (exclusive).
  1.1209 +     *
  1.1210 +     * cacheYear has a year value if both cacheStart and cacheEnd are
  1.1211 +     * in the same year. cacheYear is set to startYear - 1 if
  1.1212 +     * cacheStart and cacheEnd are in different years. cacheStart is 0
  1.1213 +     * if the cache values are void. cacheYear is a long to support
  1.1214 +     * Integer.MIN_VALUE - 1 (JCK requirement).
  1.1215 +     */
  1.1216 +    private transient long cacheYear;
  1.1217 +    private transient long cacheStart;
  1.1218 +    private transient long cacheEnd;
  1.1219 +
  1.1220 +    /**
  1.1221 +     * Constants specifying values of startMode and endMode.
  1.1222 +     */
  1.1223 +    private static final int DOM_MODE          = 1; // Exact day of month, "Mar 1"
  1.1224 +    private static final int DOW_IN_MONTH_MODE = 2; // Day of week in month, "lastSun"
  1.1225 +    private static final int DOW_GE_DOM_MODE   = 3; // Day of week after day of month, "Sun>=15"
  1.1226 +    private static final int DOW_LE_DOM_MODE   = 4; // Day of week before day of month, "Sun<=21"
  1.1227 +
  1.1228 +    /**
  1.1229 +     * Constant for a mode of start or end time specified as wall clock
  1.1230 +     * time.  Wall clock time is standard time for the onset rule, and
  1.1231 +     * daylight time for the end rule.
  1.1232 +     * @since 1.4
  1.1233 +     */
  1.1234 +    public static final int WALL_TIME = 0; // Zero for backward compatibility
  1.1235 +
  1.1236 +    /**
  1.1237 +     * Constant for a mode of start or end time specified as standard time.
  1.1238 +     * @since 1.4
  1.1239 +     */
  1.1240 +    public static final int STANDARD_TIME = 1;
  1.1241 +
  1.1242 +    /**
  1.1243 +     * Constant for a mode of start or end time specified as UTC. European
  1.1244 +     * Union rules are specified as UTC time, for example.
  1.1245 +     * @since 1.4
  1.1246 +     */
  1.1247 +    public static final int UTC_TIME = 2;
  1.1248 +
  1.1249 +    // Proclaim compatibility with 1.1
  1.1250 +    static final long serialVersionUID = -403250971215465050L;
  1.1251 +
  1.1252 +    // the internal serial version which says which version was written
  1.1253 +    // - 0 (default) for version up to JDK 1.1.3
  1.1254 +    // - 1 for version from JDK 1.1.4, which includes 3 new fields
  1.1255 +    // - 2 for JDK 1.3, which includes 2 new fields
  1.1256 +    static final int currentSerialVersion = 2;
  1.1257 +
  1.1258 +    /**
  1.1259 +     * The version of the serialized data on the stream.  Possible values:
  1.1260 +     * <dl>
  1.1261 +     * <dt><b>0</b> or not present on stream</dt>
  1.1262 +     * <dd>
  1.1263 +     * JDK 1.1.3 or earlier.
  1.1264 +     * </dd>
  1.1265 +     * <dt><b>1</b></dt>
  1.1266 +     * <dd>
  1.1267 +     * JDK 1.1.4 or later.  Includes three new fields: <code>startMode</code>,
  1.1268 +     * <code>endMode</code>, and <code>dstSavings</code>.
  1.1269 +     * </dd>
  1.1270 +     * <dt><b>2</b></dt>
  1.1271 +     * <dd>
  1.1272 +     * JDK 1.3 or later.  Includes two new fields: <code>startTimeMode</code>
  1.1273 +     * and <code>endTimeMode</code>.
  1.1274 +     * </dd>
  1.1275 +     * </dl>
  1.1276 +     * When streaming out this class, the most recent format
  1.1277 +     * and the highest allowable <code>serialVersionOnStream</code>
  1.1278 +     * is written.
  1.1279 +     * @serial
  1.1280 +     * @since 1.1.4
  1.1281 +     */
  1.1282 +    private int serialVersionOnStream = currentSerialVersion;
  1.1283 +
  1.1284 +    synchronized private void invalidateCache() {
  1.1285 +        cacheYear = startYear - 1;
  1.1286 +        cacheStart = cacheEnd = 0;
  1.1287 +    }
  1.1288 +
  1.1289 +    //----------------------------------------------------------------------
  1.1290 +    // Rule representation
  1.1291 +    //
  1.1292 +    // We represent the following flavors of rules:
  1.1293 +    //       5        the fifth of the month
  1.1294 +    //       lastSun  the last Sunday in the month
  1.1295 +    //       lastMon  the last Monday in the month
  1.1296 +    //       Sun>=8   first Sunday on or after the eighth
  1.1297 +    //       Sun<=25  last Sunday on or before the 25th
  1.1298 +    // This is further complicated by the fact that we need to remain
  1.1299 +    // backward compatible with the 1.1 FCS.  Finally, we need to minimize
  1.1300 +    // API changes.  In order to satisfy these requirements, we support
  1.1301 +    // three representation systems, and we translate between them.
  1.1302 +    //
  1.1303 +    // INTERNAL REPRESENTATION
  1.1304 +    // This is the format SimpleTimeZone objects take after construction or
  1.1305 +    // streaming in is complete.  Rules are represented directly, using an
  1.1306 +    // unencoded format.  We will discuss the start rule only below; the end
  1.1307 +    // rule is analogous.
  1.1308 +    //   startMode      Takes on enumerated values DAY_OF_MONTH,
  1.1309 +    //                  DOW_IN_MONTH, DOW_AFTER_DOM, or DOW_BEFORE_DOM.
  1.1310 +    //   startDay       The day of the month, or for DOW_IN_MONTH mode, a
  1.1311 +    //                  value indicating which DOW, such as +1 for first,
  1.1312 +    //                  +2 for second, -1 for last, etc.
  1.1313 +    //   startDayOfWeek The day of the week.  Ignored for DAY_OF_MONTH.
  1.1314 +    //
  1.1315 +    // ENCODED REPRESENTATION
  1.1316 +    // This is the format accepted by the constructor and by setStartRule()
  1.1317 +    // and setEndRule().  It uses various combinations of positive, negative,
  1.1318 +    // and zero values to encode the different rules.  This representation
  1.1319 +    // allows us to specify all the different rule flavors without altering
  1.1320 +    // the API.
  1.1321 +    //   MODE              startMonth    startDay    startDayOfWeek
  1.1322 +    //   DOW_IN_MONTH_MODE >=0           !=0         >0
  1.1323 +    //   DOM_MODE          >=0           >0          ==0
  1.1324 +    //   DOW_GE_DOM_MODE   >=0           >0          <0
  1.1325 +    //   DOW_LE_DOM_MODE   >=0           <0          <0
  1.1326 +    //   (no DST)          don't care    ==0         don't care
  1.1327 +    //
  1.1328 +    // STREAMED REPRESENTATION
  1.1329 +    // We must retain binary compatibility with the 1.1 FCS.  The 1.1 code only
  1.1330 +    // handles DOW_IN_MONTH_MODE and non-DST mode, the latter indicated by the
  1.1331 +    // flag useDaylight.  When we stream an object out, we translate into an
  1.1332 +    // approximate DOW_IN_MONTH_MODE representation so the object can be parsed
  1.1333 +    // and used by 1.1 code.  Following that, we write out the full
  1.1334 +    // representation separately so that contemporary code can recognize and
  1.1335 +    // parse it.  The full representation is written in a "packed" format,
  1.1336 +    // consisting of a version number, a length, and an array of bytes.  Future
  1.1337 +    // versions of this class may specify different versions.  If they wish to
  1.1338 +    // include additional data, they should do so by storing them after the
  1.1339 +    // packed representation below.
  1.1340 +    //----------------------------------------------------------------------
  1.1341 +
  1.1342 +    /**
  1.1343 +     * Given a set of encoded rules in startDay and startDayOfMonth, decode
  1.1344 +     * them and set the startMode appropriately.  Do the same for endDay and
  1.1345 +     * endDayOfMonth.  Upon entry, the day of week variables may be zero or
  1.1346 +     * negative, in order to indicate special modes.  The day of month
  1.1347 +     * variables may also be negative.  Upon exit, the mode variables will be
  1.1348 +     * set, and the day of week and day of month variables will be positive.
  1.1349 +     * This method also recognizes a startDay or endDay of zero as indicating
  1.1350 +     * no DST.
  1.1351 +     */
  1.1352 +    private void decodeRules()
  1.1353 +    {
  1.1354 +        decodeStartRule();
  1.1355 +        decodeEndRule();
  1.1356 +    }
  1.1357 +
  1.1358 +    /**
  1.1359 +     * Decode the start rule and validate the parameters.  The parameters are
  1.1360 +     * expected to be in encoded form, which represents the various rule modes
  1.1361 +     * by negating or zeroing certain values.  Representation formats are:
  1.1362 +     * <p>
  1.1363 +     * <pre>
  1.1364 +     *            DOW_IN_MONTH  DOM    DOW>=DOM  DOW<=DOM  no DST
  1.1365 +     *            ------------  -----  --------  --------  ----------
  1.1366 +     * month       0..11        same    same      same     don't care
  1.1367 +     * day        -5..5         1..31   1..31    -1..-31   0
  1.1368 +     * dayOfWeek   1..7         0      -1..-7    -1..-7    don't care
  1.1369 +     * time        0..ONEDAY    same    same      same     don't care
  1.1370 +     * </pre>
  1.1371 +     * The range for month does not include UNDECIMBER since this class is
  1.1372 +     * really specific to GregorianCalendar, which does not use that month.
  1.1373 +     * The range for time includes ONEDAY (vs. ending at ONEDAY-1) because the
  1.1374 +     * end rule is an exclusive limit point.  That is, the range of times that
  1.1375 +     * are in DST include those >= the start and < the end.  For this reason,
  1.1376 +     * it should be possible to specify an end of ONEDAY in order to include the
  1.1377 +     * entire day.  Although this is equivalent to time 0 of the following day,
  1.1378 +     * it's not always possible to specify that, for example, on December 31.
  1.1379 +     * While arguably the start range should still be 0..ONEDAY-1, we keep
  1.1380 +     * the start and end ranges the same for consistency.
  1.1381 +     */
  1.1382 +    private void decodeStartRule() {
  1.1383 +        useDaylight = (startDay != 0) && (endDay != 0);
  1.1384 +        if (startDay != 0) {
  1.1385 +            if (startMonth < Calendar.JANUARY || startMonth > Calendar.DECEMBER) {
  1.1386 +                throw new IllegalArgumentException(
  1.1387 +                        "Illegal start month " + startMonth);
  1.1388 +            }
  1.1389 +            if (startTime < 0 || startTime > millisPerDay) {
  1.1390 +                throw new IllegalArgumentException(
  1.1391 +                        "Illegal start time " + startTime);
  1.1392 +            }
  1.1393 +            if (startDayOfWeek == 0) {
  1.1394 +                startMode = DOM_MODE;
  1.1395 +            } else {
  1.1396 +                if (startDayOfWeek > 0) {
  1.1397 +                    startMode = DOW_IN_MONTH_MODE;
  1.1398 +                } else {
  1.1399 +                    startDayOfWeek = -startDayOfWeek;
  1.1400 +                    if (startDay > 0) {
  1.1401 +                        startMode = DOW_GE_DOM_MODE;
  1.1402 +                    } else {
  1.1403 +                        startDay = -startDay;
  1.1404 +                        startMode = DOW_LE_DOM_MODE;
  1.1405 +                    }
  1.1406 +                }
  1.1407 +                if (startDayOfWeek > Calendar.SATURDAY) {
  1.1408 +                    throw new IllegalArgumentException(
  1.1409 +                           "Illegal start day of week " + startDayOfWeek);
  1.1410 +                }
  1.1411 +            }
  1.1412 +            if (startMode == DOW_IN_MONTH_MODE) {
  1.1413 +                if (startDay < -5 || startDay > 5) {
  1.1414 +                    throw new IllegalArgumentException(
  1.1415 +                            "Illegal start day of week in month " + startDay);
  1.1416 +                }
  1.1417 +            } else if (startDay < 1 || startDay > staticMonthLength[startMonth]) {
  1.1418 +                throw new IllegalArgumentException(
  1.1419 +                        "Illegal start day " + startDay);
  1.1420 +            }
  1.1421 +        }
  1.1422 +    }
  1.1423 +
  1.1424 +    /**
  1.1425 +     * Decode the end rule and validate the parameters.  This method is exactly
  1.1426 +     * analogous to decodeStartRule().
  1.1427 +     * @see decodeStartRule
  1.1428 +     */
  1.1429 +    private void decodeEndRule() {
  1.1430 +        useDaylight = (startDay != 0) && (endDay != 0);
  1.1431 +        if (endDay != 0) {
  1.1432 +            if (endMonth < Calendar.JANUARY || endMonth > Calendar.DECEMBER) {
  1.1433 +                throw new IllegalArgumentException(
  1.1434 +                        "Illegal end month " + endMonth);
  1.1435 +            }
  1.1436 +            if (endTime < 0 || endTime > millisPerDay) {
  1.1437 +                throw new IllegalArgumentException(
  1.1438 +                        "Illegal end time " + endTime);
  1.1439 +            }
  1.1440 +            if (endDayOfWeek == 0) {
  1.1441 +                endMode = DOM_MODE;
  1.1442 +            } else {
  1.1443 +                if (endDayOfWeek > 0) {
  1.1444 +                    endMode = DOW_IN_MONTH_MODE;
  1.1445 +                } else {
  1.1446 +                    endDayOfWeek = -endDayOfWeek;
  1.1447 +                    if (endDay > 0) {
  1.1448 +                        endMode = DOW_GE_DOM_MODE;
  1.1449 +                    } else {
  1.1450 +                        endDay = -endDay;
  1.1451 +                        endMode = DOW_LE_DOM_MODE;
  1.1452 +                    }
  1.1453 +                }
  1.1454 +                if (endDayOfWeek > Calendar.SATURDAY) {
  1.1455 +                    throw new IllegalArgumentException(
  1.1456 +                           "Illegal end day of week " + endDayOfWeek);
  1.1457 +                }
  1.1458 +            }
  1.1459 +            if (endMode == DOW_IN_MONTH_MODE) {
  1.1460 +                if (endDay < -5 || endDay > 5) {
  1.1461 +                    throw new IllegalArgumentException(
  1.1462 +                            "Illegal end day of week in month " + endDay);
  1.1463 +                }
  1.1464 +            } else if (endDay < 1 || endDay > staticMonthLength[endMonth]) {
  1.1465 +                throw new IllegalArgumentException(
  1.1466 +                        "Illegal end day " + endDay);
  1.1467 +            }
  1.1468 +        }
  1.1469 +    }
  1.1470 +
  1.1471 +    /**
  1.1472 +     * Make rules compatible to 1.1 FCS code.  Since 1.1 FCS code only understands
  1.1473 +     * day-of-week-in-month rules, we must modify other modes of rules to their
  1.1474 +     * approximate equivalent in 1.1 FCS terms.  This method is used when streaming
  1.1475 +     * out objects of this class.  After it is called, the rules will be modified,
  1.1476 +     * with a possible loss of information.  startMode and endMode will NOT be
  1.1477 +     * altered, even though semantically they should be set to DOW_IN_MONTH_MODE,
  1.1478 +     * since the rule modification is only intended to be temporary.
  1.1479 +     */
  1.1480 +    private void makeRulesCompatible()
  1.1481 +    {
  1.1482 +        switch (startMode) {
  1.1483 +        case DOM_MODE:
  1.1484 +            startDay = 1 + (startDay / 7);
  1.1485 +            startDayOfWeek = Calendar.SUNDAY;
  1.1486 +            break;
  1.1487 +
  1.1488 +        case DOW_GE_DOM_MODE:
  1.1489 +            // A day-of-month of 1 is equivalent to DOW_IN_MONTH_MODE
  1.1490 +            // that is, Sun>=1 == firstSun.
  1.1491 +            if (startDay != 1) {
  1.1492 +                startDay = 1 + (startDay / 7);
  1.1493 +            }
  1.1494 +            break;
  1.1495 +
  1.1496 +        case DOW_LE_DOM_MODE:
  1.1497 +            if (startDay >= 30) {
  1.1498 +                startDay = -1;
  1.1499 +            } else {
  1.1500 +                startDay = 1 + (startDay / 7);
  1.1501 +            }
  1.1502 +            break;
  1.1503 +        }
  1.1504 +
  1.1505 +        switch (endMode) {
  1.1506 +        case DOM_MODE:
  1.1507 +            endDay = 1 + (endDay / 7);
  1.1508 +            endDayOfWeek = Calendar.SUNDAY;
  1.1509 +            break;
  1.1510 +
  1.1511 +        case DOW_GE_DOM_MODE:
  1.1512 +            // A day-of-month of 1 is equivalent to DOW_IN_MONTH_MODE
  1.1513 +            // that is, Sun>=1 == firstSun.
  1.1514 +            if (endDay != 1) {
  1.1515 +                endDay = 1 + (endDay / 7);
  1.1516 +            }
  1.1517 +            break;
  1.1518 +
  1.1519 +        case DOW_LE_DOM_MODE:
  1.1520 +            if (endDay >= 30) {
  1.1521 +                endDay = -1;
  1.1522 +            } else {
  1.1523 +                endDay = 1 + (endDay / 7);
  1.1524 +            }
  1.1525 +            break;
  1.1526 +        }
  1.1527 +
  1.1528 +        /*
  1.1529 +         * Adjust the start and end times to wall time.  This works perfectly
  1.1530 +         * well unless it pushes into the next or previous day.  If that
  1.1531 +         * happens, we attempt to adjust the day rule somewhat crudely.  The day
  1.1532 +         * rules have been forced into DOW_IN_MONTH mode already, so we change
  1.1533 +         * the day of week to move forward or back by a day.  It's possible to
  1.1534 +         * make a more refined adjustment of the original rules first, but in
  1.1535 +         * most cases this extra effort will go to waste once we adjust the day
  1.1536 +         * rules anyway.
  1.1537 +         */
  1.1538 +        switch (startTimeMode) {
  1.1539 +        case UTC_TIME:
  1.1540 +            startTime += rawOffset;
  1.1541 +            break;
  1.1542 +        }
  1.1543 +        while (startTime < 0) {
  1.1544 +            startTime += millisPerDay;
  1.1545 +            startDayOfWeek = 1 + ((startDayOfWeek+5) % 7); // Back 1 day
  1.1546 +        }
  1.1547 +        while (startTime >= millisPerDay) {
  1.1548 +            startTime -= millisPerDay;
  1.1549 +            startDayOfWeek = 1 + (startDayOfWeek % 7); // Forward 1 day
  1.1550 +        }
  1.1551 +
  1.1552 +        switch (endTimeMode) {
  1.1553 +        case UTC_TIME:
  1.1554 +            endTime += rawOffset + dstSavings;
  1.1555 +            break;
  1.1556 +        case STANDARD_TIME:
  1.1557 +            endTime += dstSavings;
  1.1558 +        }
  1.1559 +        while (endTime < 0) {
  1.1560 +            endTime += millisPerDay;
  1.1561 +            endDayOfWeek = 1 + ((endDayOfWeek+5) % 7); // Back 1 day
  1.1562 +        }
  1.1563 +        while (endTime >= millisPerDay) {
  1.1564 +            endTime -= millisPerDay;
  1.1565 +            endDayOfWeek = 1 + (endDayOfWeek % 7); // Forward 1 day
  1.1566 +        }
  1.1567 +    }
  1.1568 +
  1.1569 +    /**
  1.1570 +     * Pack the start and end rules into an array of bytes.  Only pack
  1.1571 +     * data which is not preserved by makeRulesCompatible.
  1.1572 +     */
  1.1573 +    private byte[] packRules()
  1.1574 +    {
  1.1575 +        byte[] rules = new byte[6];
  1.1576 +        rules[0] = (byte)startDay;
  1.1577 +        rules[1] = (byte)startDayOfWeek;
  1.1578 +        rules[2] = (byte)endDay;
  1.1579 +        rules[3] = (byte)endDayOfWeek;
  1.1580 +
  1.1581 +        // As of serial version 2, include time modes
  1.1582 +        rules[4] = (byte)startTimeMode;
  1.1583 +        rules[5] = (byte)endTimeMode;
  1.1584 +
  1.1585 +        return rules;
  1.1586 +    }
  1.1587 +
  1.1588 +    /**
  1.1589 +     * Given an array of bytes produced by packRules, interpret them
  1.1590 +     * as the start and end rules.
  1.1591 +     */
  1.1592 +    private void unpackRules(byte[] rules)
  1.1593 +    {
  1.1594 +        startDay       = rules[0];
  1.1595 +        startDayOfWeek = rules[1];
  1.1596 +        endDay         = rules[2];
  1.1597 +        endDayOfWeek   = rules[3];
  1.1598 +
  1.1599 +        // As of serial version 2, include time modes
  1.1600 +        if (rules.length >= 6) {
  1.1601 +            startTimeMode = rules[4];
  1.1602 +            endTimeMode   = rules[5];
  1.1603 +        }
  1.1604 +    }
  1.1605 +
  1.1606 +    /**
  1.1607 +     * Pack the start and end times into an array of bytes.  This is required
  1.1608 +     * as of serial version 2.
  1.1609 +     */
  1.1610 +    private int[] packTimes() {
  1.1611 +        int[] times = new int[2];
  1.1612 +        times[0] = startTime;
  1.1613 +        times[1] = endTime;
  1.1614 +        return times;
  1.1615 +    }
  1.1616 +
  1.1617 +    /**
  1.1618 +     * Unpack the start and end times from an array of bytes.  This is required
  1.1619 +     * as of serial version 2.
  1.1620 +     */
  1.1621 +    private void unpackTimes(int[] times) {
  1.1622 +        startTime = times[0];
  1.1623 +        endTime = times[1];
  1.1624 +    }
  1.1625 +
  1.1626 +    /**
  1.1627 +     * Save the state of this object to a stream (i.e., serialize it).
  1.1628 +     *
  1.1629 +     * @serialData We write out two formats, a JDK 1.1 compatible format, using
  1.1630 +     * <code>DOW_IN_MONTH_MODE</code> rules, in the required section, followed
  1.1631 +     * by the full rules, in packed format, in the optional section.  The
  1.1632 +     * optional section will be ignored by JDK 1.1 code upon stream in.
  1.1633 +     * <p> Contents of the optional section: The length of a byte array is
  1.1634 +     * emitted (int); this is 4 as of this release. The byte array of the given
  1.1635 +     * length is emitted. The contents of the byte array are the true values of
  1.1636 +     * the fields <code>startDay</code>, <code>startDayOfWeek</code>,
  1.1637 +     * <code>endDay</code>, and <code>endDayOfWeek</code>.  The values of these
  1.1638 +     * fields in the required section are approximate values suited to the rule
  1.1639 +     * mode <code>DOW_IN_MONTH_MODE</code>, which is the only mode recognized by
  1.1640 +     * JDK 1.1.
  1.1641 +     */
  1.1642 +    private void writeObject(ObjectOutputStream stream)
  1.1643 +         throws IOException
  1.1644 +    {
  1.1645 +        // Construct a binary rule
  1.1646 +        byte[] rules = packRules();
  1.1647 +        int[] times = packTimes();
  1.1648 +
  1.1649 +        // Convert to 1.1 FCS rules.  This step may cause us to lose information.
  1.1650 +        makeRulesCompatible();
  1.1651 +
  1.1652 +        // Write out the 1.1 FCS rules
  1.1653 +        stream.defaultWriteObject();
  1.1654 +
  1.1655 +        // Write out the binary rules in the optional data area of the stream.
  1.1656 +        stream.writeInt(rules.length);
  1.1657 +        stream.write(rules);
  1.1658 +        stream.writeObject(times);
  1.1659 +
  1.1660 +        // Recover the original rules.  This recovers the information lost
  1.1661 +        // by makeRulesCompatible.
  1.1662 +        unpackRules(rules);
  1.1663 +        unpackTimes(times);
  1.1664 +    }
  1.1665 +
  1.1666 +    /**
  1.1667 +     * Reconstitute this object from a stream (i.e., deserialize it).
  1.1668 +     *
  1.1669 +     * We handle both JDK 1.1
  1.1670 +     * binary formats and full formats with a packed byte array.
  1.1671 +     */
  1.1672 +    private void readObject(ObjectInputStream stream)
  1.1673 +         throws IOException, ClassNotFoundException
  1.1674 +    {
  1.1675 +        stream.defaultReadObject();
  1.1676 +
  1.1677 +        if (serialVersionOnStream < 1) {
  1.1678 +            // Fix a bug in the 1.1 SimpleTimeZone code -- namely,
  1.1679 +            // startDayOfWeek and endDayOfWeek were usually uninitialized.  We can't do
  1.1680 +            // too much, so we assume SUNDAY, which actually works most of the time.
  1.1681 +            if (startDayOfWeek == 0) {
  1.1682 +                startDayOfWeek = Calendar.SUNDAY;
  1.1683 +            }
  1.1684 +            if (endDayOfWeek == 0) {
  1.1685 +                endDayOfWeek = Calendar.SUNDAY;
  1.1686 +            }
  1.1687 +
  1.1688 +            // The variables dstSavings, startMode, and endMode are post-1.1, so they
  1.1689 +            // won't be present if we're reading from a 1.1 stream.  Fix them up.
  1.1690 +            startMode = endMode = DOW_IN_MONTH_MODE;
  1.1691 +            dstSavings = millisPerHour;
  1.1692 +        } else {
  1.1693 +            // For 1.1.4, in addition to the 3 new instance variables, we also
  1.1694 +            // store the actual rules (which have not be made compatible with 1.1)
  1.1695 +            // in the optional area.  Read them in here and parse them.
  1.1696 +            int length = stream.readInt();
  1.1697 +            byte[] rules = new byte[length];
  1.1698 +            stream.readFully(rules);
  1.1699 +            unpackRules(rules);
  1.1700 +        }
  1.1701 +
  1.1702 +        if (serialVersionOnStream >= 2) {
  1.1703 +            int[] times = (int[]) stream.readObject();
  1.1704 +            unpackTimes(times);
  1.1705 +        }
  1.1706 +
  1.1707 +        serialVersionOnStream = currentSerialVersion;
  1.1708 +    }
  1.1709 +}