FileDocCategorySizeDatePackage
SimpleTimeZone.javaAPI DocJava SE 6 API68595Tue Jun 10 00:25:54 BST 2008java.util

SimpleTimeZone

public class SimpleTimeZone extends TimeZone
SimpleTimeZone is a concrete subclass of TimeZone that represents a time zone for use with a Gregorian calendar. The class holds an offset from GMT, called raw offset, 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.

To construct a SimpleTimeZone with a daylight saving time schedule, the schedule can be described with a set of rules, start-rule and end-rule. A day when daylight saving time starts or ends is specified by a combination of month, day-of-month, and day-of-week values. The month value is represented by a Calendar {@link Calendar#MONTH MONTH} field value, such as {@link Calendar#MARCH}. The day-of-week 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.

  • Exact day of month
    To specify an exact day of month, set the month and day-of-month to an exact value, and day-of-week to zero. For example, to specify March 1, set the month to {@link Calendar#MARCH MARCH}, day-of-month to 1, and day-of-week to 0.
  • Day of week on or after day of month
    To specify a day of week on or after an exact day of month, set the month to an exact month value, day-of-month to the day on or after which the rule is applied, and day-of-week to a negative {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} field value. For example, to specify the second Sunday of April, set month to {@link Calendar#APRIL APRIL}, day-of-month to 8, and day-of-week to -{@link Calendar#SUNDAY SUNDAY}.
  • Day of week on or before day of month
    To specify a day of the week on or before an exact day of the month, set day-of-month and day-of-week to a negative value. For example, to specify the last Wednesday on or before the 21st of March, set month to {@link Calendar#MARCH MARCH}, day-of-month is -21 and day-of-week is -{@link Calendar#WEDNESDAY WEDNESDAY}.
  • Last day-of-week of month
    To specify, the last day-of-week of the month, set day-of-week to a {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} value and day-of-month to -1. For example, to specify the last Sunday of October, set month to {@link Calendar#OCTOBER OCTOBER}, day-of-week to {@link Calendar#SUNDAY SUNDAY} and day-of-month to -1.
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 modes 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 end-rule means the same thing as the daylight time.

The following are examples of parameters for constructing time zone objects.


// 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)
These parameter rules are also applicable to the set rule methods, such as setStartRule.
since
1.1
see
Calendar
see
GregorianCalendar
see
TimeZone
version
1.52 05/25/07
author
David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu

Fields Summary
private int
startMonth
The month in which daylight saving time starts. This value must be between Calendar.JANUARY and Calendar.DECEMBER inclusive. This value must not equal endMonth.

If useDaylight is false, this value is ignored.

private int
startDay
This field has two possible interpretations:
startMode == DOW_IN_MONTH
startDay indicates the day of the month of startMonth on which daylight saving time starts, from 1 to 28, 30, or 31, depending on the startMonth.
startMode != DOW_IN_MONTH
startDay indicates which startDayOfWeek in th month startMonth daylight saving time starts on. For example, a value of +1 and a startDayOfWeek of Calendar.SUNDAY indicates the first Sunday of startMonth. Likewise, +2 would indicate the second Sunday, and -1 the last Sunday. A value of 0 is illegal.

If useDaylight is false, this value is ignored.

private int
startDayOfWeek
The day of the week on which daylight saving time starts. This value must be between Calendar.SUNDAY and Calendar.SATURDAY inclusive.

If useDaylight is false or startMode == DAY_OF_MONTH, this value is ignored.

private int
startTime
The time in milliseconds after midnight at which daylight saving time starts. This value is expressed as wall time, standard time, or UTC time, depending on the setting of startTimeMode.

If useDaylight is false, this value is ignored.

private int
startTimeMode
The format of startTime, either WALL_TIME, STANDARD_TIME, or UTC_TIME.
private int
endMonth
The month in which daylight saving time ends. This value must be between Calendar.JANUARY and Calendar.UNDECIMBER. This value must not equal startMonth.

If useDaylight is false, this value is ignored.

private int
endDay
This field has two possible interpretations:
endMode == DOW_IN_MONTH
endDay indicates the day of the month of endMonth on which daylight saving time ends, from 1 to 28, 30, or 31, depending on the endMonth.
endMode != DOW_IN_MONTH
endDay indicates which endDayOfWeek in th month endMonth daylight saving time ends on. For example, a value of +1 and a endDayOfWeek of Calendar.SUNDAY indicates the first Sunday of endMonth. Likewise, +2 would indicate the second Sunday, and -1 the last Sunday. A value of 0 is illegal.

If useDaylight is false, this value is ignored.

private int
endDayOfWeek
The day of the week on which daylight saving time ends. This value must be between Calendar.SUNDAY and Calendar.SATURDAY inclusive.

If useDaylight is false or endMode == DAY_OF_MONTH, this value is ignored.

private int
endTime
The time in milliseconds after midnight at which daylight saving time ends. This value is expressed as wall time, standard time, or UTC time, depending on the setting of endTimeMode.

If useDaylight is false, this value is ignored.

private int
endTimeMode
The format of endTime, either WALL_TIME, STANDARD_TIME, or UTC_TIME.
private int
startYear
The year in which daylight saving time is first observed. This is an {@link GregorianCalendar#AD AD} value. If this value is less than 1 then daylight saving time is observed for all AD years.

If useDaylight is false, this value is ignored.

private int
rawOffset
The offset in milliseconds between this zone and GMT. Negative offsets are to the west of Greenwich. To obtain local standard time, add the offset to GMT time. To obtain local wall time it may also be necessary to add dstSavings.
private boolean
useDaylight
A boolean value which is true if and only if this zone uses daylight saving time. If this value is false, several other fields are ignored.
private static final int
millisPerHour
private static final int
millisPerDay
private final byte[]
monthLength
This field was serialized in JDK 1.1, so we have to keep it that way to maintain serialization compatibility. However, there's no need to recreate the array each time we create a new time zone.
private static final byte[]
staticMonthLength
private static final byte[]
staticLeapMonthLength
private int
startMode
Variables specifying the mode of the start rule. Takes the following values:
DOM_MODE
Exact day of week; e.g., March 1.
DOW_IN_MONTH_MODE
Day of week in month; e.g., last Sunday in March.
DOW_GE_DOM_MODE
Day of week after day of month; e.g., Sunday on or after March 15.
DOW_LE_DOM_MODE
Day of week before day of month; e.g., Sunday on or before March 15.
The setting of this field affects the interpretation of the startDay field.

If useDaylight is false, this value is ignored.

private int
endMode
Variables specifying the mode of the end rule. Takes the following values:
DOM_MODE
Exact day of week; e.g., March 1.
DOW_IN_MONTH_MODE
Day of week in month; e.g., last Sunday in March.
DOW_GE_DOM_MODE
Day of week after day of month; e.g., Sunday on or after March 15.
DOW_LE_DOM_MODE
Day of week before day of month; e.g., Sunday on or before March 15.
The setting of this field affects the interpretation of the endDay field.

If useDaylight is false, this value is ignored.

private int
dstSavings
A positive value indicating the amount of time saved during DST in milliseconds. Typically one hour (3600000); sometimes 30 minutes (1800000).

If useDaylight is false, this value is ignored.

private static final Gregorian
gcal
private transient long
cacheYear
Cache values representing a single period of daylight saving time. When the cache values are valid, cacheStart is the start time (inclusive) of daylight saving time and cacheEnd is the end time (exclusive). cacheYear has a year value if both cacheStart and cacheEnd are in the same year. cacheYear is set to startYear - 1 if cacheStart and cacheEnd are in different years. cacheStart is 0 if the cache values are void. cacheYear is a long to support Integer.MIN_VALUE - 1 (JCK requirement).
private transient long
cacheStart
private transient long
cacheEnd
private static final int
DOM_MODE
Constants specifying values of startMode and endMode.
private static final int
DOW_IN_MONTH_MODE
private static final int
DOW_GE_DOM_MODE
private static final int
DOW_LE_DOM_MODE
public static final int
WALL_TIME
Constant for a mode of start or end time specified as wall clock time. Wall clock time is standard time for the onset rule, and daylight time for the end rule.
public static final int
STANDARD_TIME
Constant for a mode of start or end time specified as standard time.
public static final int
UTC_TIME
Constant for a mode of start or end time specified as UTC. European Union rules are specified as UTC time, for example.
static final long
serialVersionUID
static final int
currentSerialVersion
private int
serialVersionOnStream
The version of the serialized data on the stream. Possible values:
0 or not present on stream
JDK 1.1.3 or earlier.
1
JDK 1.1.4 or later. Includes three new fields: startMode, endMode, and dstSavings.
2
JDK 1.3 or later. Includes two new fields: startTimeMode and endTimeMode.
When streaming out this class, the most recent format and the highest allowable serialVersionOnStream is written.
Constructors Summary
public SimpleTimeZone(int rawOffset, String ID)
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.

        this.rawOffset = rawOffset;
        setID (ID);
        dstSavings = millisPerHour; // In case user sets rules later
    
public SimpleTimeZone(int rawOffset, String ID, int startMonth, int startDay, int startDayOfWeek, int startTime, int endMonth, int endDay, int endDayOfWeek, int endTime)
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 startTime and endTime 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:

SimpleTimeZone(rawOffset,
ID,
startMonth,
startDay,
startDayOfWeek,
startTime,
SimpleTimeZone.{@link #WALL_TIME},
endMonth,
endDay,
endDayOfWeek,
endTime,
SimpleTimeZone.{@link #WALL_TIME},
3600000)

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

        this(rawOffset, ID,
             startMonth, startDay, startDayOfWeek, startTime, WALL_TIME,
             endMonth, endDay, endDayOfWeek, endTime, WALL_TIME,
             millisPerHour);
    
public SimpleTimeZone(int rawOffset, String ID, int startMonth, int startDay, int startDayOfWeek, int startTime, int endMonth, int endDay, int endDayOfWeek, int endTime, int 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. Both startTime and endTime are assumed to be represented in the wall clock time. This constructor is equivalent to:

SimpleTimeZone(rawOffset,
ID,
startMonth,
startDay,
startDayOfWeek,
startTime,
SimpleTimeZone.{@link #WALL_TIME},
endMonth,
endDay,
endDayOfWeek,
endTime,
SimpleTimeZone.{@link #WALL_TIME},
dstSavings)

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

        this(rawOffset, ID,
             startMonth, startDay, startDayOfWeek, startTime, WALL_TIME,
             endMonth, endDay, endDayOfWeek, endTime, WALL_TIME,
             dstSavings);
    
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)
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 startTime and endTime. 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 startTimeMode.
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 endTimeMode.
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


        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);
        }
    
Methods Summary
public java.lang.Objectclone()
Returns a clone of this SimpleTimeZone instance.

return
a clone of this instance.

	return super.clone();
    
private voiddecodeEndRule()
Decode the end rule and validate the parameters. This method is exactly analogous to decodeStartRule().

see
decodeStartRule

        useDaylight = (startDay != 0) && (endDay != 0);
        if (endDay != 0) {
            if (endMonth < Calendar.JANUARY || endMonth > Calendar.DECEMBER) {
                throw new IllegalArgumentException(
                        "Illegal end month " + endMonth);
            }
            if (endTime < 0 || endTime >= millisPerDay) {
                throw new IllegalArgumentException(
                        "Illegal end time " + endTime);
            }
            if (endDayOfWeek == 0) {
                endMode = DOM_MODE;
            } else {
                if (endDayOfWeek > 0) {
                    endMode = DOW_IN_MONTH_MODE;
                } else {
                    endDayOfWeek = -endDayOfWeek;
                    if (endDay > 0) {
                        endMode = DOW_GE_DOM_MODE;
                    } else {
                        endDay = -endDay;
                        endMode = DOW_LE_DOM_MODE;
                    }
                }
                if (endDayOfWeek > Calendar.SATURDAY) {
                    throw new IllegalArgumentException(
                           "Illegal end day of week " + endDayOfWeek);
                }
            }
            if (endMode == DOW_IN_MONTH_MODE) {
                if (endDay < -5 || endDay > 5) {
                    throw new IllegalArgumentException(
                            "Illegal end day of week in month " + endDay);
                }
            } else if (endDay < 1 || endDay > staticMonthLength[endMonth]) {
                throw new IllegalArgumentException(
                        "Illegal end day " + endDay);
            }
        }
    
private voiddecodeRules()
Given a set of encoded rules in startDay and startDayOfMonth, decode them and set the startMode appropriately. Do the same for endDay and endDayOfMonth. Upon entry, the day of week variables may be zero or negative, in order to indicate special modes. The day of month variables may also be negative. Upon exit, the mode variables will be set, and the day of week and day of month variables will be positive. This method also recognizes a startDay or endDay of zero as indicating no DST.

        decodeStartRule();
        decodeEndRule();
    
private voiddecodeStartRule()
Decode the start rule and validate the parameters. The parameters are expected to be in encoded form, which represents the various rule modes by negating or zeroing certain values. Representation formats are:

DOW_IN_MONTH DOM DOW>=DOM DOW<=DOM no DST
------------ ----- -------- -------- ----------
month 0..11 same same same don't care
day -5..5 1..31 1..31 -1..-31 0
dayOfWeek 1..7 0 -1..-7 -1..-7 don't care
time 0..ONEDAY same same same don't care
The range for month does not include UNDECIMBER since this class is really specific to GregorianCalendar, which does not use that month. The range for time includes ONEDAY (vs. ending at ONEDAY-1) because the end rule is an exclusive limit point. That is, the range of times that are in DST include those >= the start and < the end. For this reason, it should be possible to specify an end of ONEDAY in order to include the entire day. Although this is equivalent to time 0 of the following day, it's not always possible to specify that, for example, on December 31. While arguably the start range should still be 0..ONEDAY-1, we keep the start and end ranges the same for consistency.

        useDaylight = (startDay != 0) && (endDay != 0);
        if (startDay != 0) {
            if (startMonth < Calendar.JANUARY || startMonth > Calendar.DECEMBER) {
                throw new IllegalArgumentException(
                        "Illegal start month " + startMonth);
            }
            if (startTime < 0 || startTime >= millisPerDay) {
                throw new IllegalArgumentException(
                        "Illegal start time " + startTime);
            }
            if (startDayOfWeek == 0) {
                startMode = DOM_MODE;
            } else {
                if (startDayOfWeek > 0) {
                    startMode = DOW_IN_MONTH_MODE;
                } else {
                    startDayOfWeek = -startDayOfWeek;
                    if (startDay > 0) {
                        startMode = DOW_GE_DOM_MODE;
                    } else {
                        startDay = -startDay;
                        startMode = DOW_LE_DOM_MODE;
                    }
                }
                if (startDayOfWeek > Calendar.SATURDAY) {
                    throw new IllegalArgumentException(
                           "Illegal start day of week " + startDayOfWeek);
                }
            }
            if (startMode == DOW_IN_MONTH_MODE) {
                if (startDay < -5 || startDay > 5) {
                    throw new IllegalArgumentException(
                            "Illegal start day of week in month " + startDay);
                }
            } else if (startDay < 1 || startDay > staticMonthLength[startMonth]) {
                throw new IllegalArgumentException(
                        "Illegal start day " + startDay);
            }
        }
    
public booleanequals(java.lang.Object obj)
Compares the equality of two SimpleTimeZone objects.

param
obj The SimpleTimeZone object to be compared with.
return
True if the given obj is the same as this SimpleTimeZone object; false otherwise.

        if (this == obj) {
            return true;
	}
        if (!(obj instanceof SimpleTimeZone)) {
            return false;
	}

        SimpleTimeZone that = (SimpleTimeZone) obj;

        return getID().equals(that.getID()) &&
            hasSameRules(that);
    
public intgetDSTSavings()
Returns the amount of time in milliseconds that the clock is advanced during daylight saving time.

return
the number of milliseconds the time is advanced with respect to standard time when the daylight saving rules are in effect, or 0 (zero) if this time zone doesn't observe daylight saving time.
see
#setDSTSavings
since
1.2

	if (useDaylight) {
	    return dstSavings;
	}
	return 0;
    
private longgetEnd(sun.util.calendar.BaseCalendar cal, sun.util.calendar.BaseCalendar$Date cdate, int year)

	int time = endTime;
	if (endTimeMode != UTC_TIME) {
	    time -= rawOffset;
	}
	if (endTimeMode == WALL_TIME) {
	    time -= dstSavings;
	}
	return getTransition(cal, cdate, endMode, year, endMonth, endDay,
					endDayOfWeek, time);
    
public intgetOffset(long date)
Returns the offset of this time zone from UTC at the given time. If daylight saving time is in effect at the given time, the offset value is adjusted with the amount of daylight saving.

param
date the time at which the time zone offset is found
return
the amount of time in milliseconds to add to UTC to get local time.
since
1.4

	return getOffsets(date, null);
    
public intgetOffset(int era, int year, int month, int day, int dayOfWeek, int millis)
Returns the difference in milliseconds between local time and UTC, taking into account both the raw offset and the effect of daylight saving, for the specified date and time. This method assumes that the start and end month are distinct. It also uses a default {@link GregorianCalendar} object as its underlying calendar, such as for determining leap years. Do not use the result of this method with a calendar other than a default GregorianCalendar.

Note: In general, clients should use Calendar.get(ZONE_OFFSET) + Calendar.get(DST_OFFSET) instead of calling this method.

param
era The era of the given date.
param
year The year in the given date.
param
month The month in the given date. Month is 0-based. e.g., 0 for January.
param
day The day-in-month of the given date.
param
dayOfWeek The day-of-week of the given date.
param
millis The milliseconds in day in standard local time.
return
The milliseconds to add to UTC to get local time.
exception
IllegalArgumentException the era, month, day, dayOfWeek, or millis parameters are out of range

	if (era != GregorianCalendar.AD && era != GregorianCalendar.BC) {
            throw new IllegalArgumentException("Illegal era " + era);
        }

	int y = year;
	if (era == GregorianCalendar.BC) {
	    // adjust y with the GregorianCalendar-style year numbering.
	    y = 1 - y;
	}

	// If the year isn't representable with the 64-bit long
	// integer in milliseconds, convert the year to an
	// equivalent year. This is required to pass some JCK test cases
	// which are actually useless though because the specified years
	// can't be supported by the Java time system.
	if (y >= 292278994) {
	    y = 2800 + y % 2800;
	} else if (y <= -292269054) {
	    // y %= 28 also produces an equivalent year, but positive
	    // year numbers would be convenient to use the UNIX cal
	    // command.
	    y = (int) CalendarUtils.mod((long) y, 28);
	}

	// convert year to its 1-based month value
	int m = month + 1;

	// First, calculate time as a Gregorian date.
	BaseCalendar cal = gcal;
	BaseCalendar.Date cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
	cdate.setDate(y, m, day);
	long time = cal.getTime(cdate); // normalize cdate
	time += millis - rawOffset; // UTC time

	// If the time value represents a time before the default
	// Gregorian cutover, recalculate time using the Julian
	// calendar system. For the Julian calendar system, the
	// normalized year numbering is ..., -2 (BCE 2), -1 (BCE 1),
	// 1, 2 ... which is different from the GregorianCalendar
	// style year numbering (..., -1, 0 (BCE 1), 1, 2, ...).
	if (time < GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER) {
	    cal = (BaseCalendar) CalendarSystem.forName("julian");
	    cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
	    cdate.setNormalizedDate(y, m, day);
	    time = cal.getTime(cdate) + millis - rawOffset;
	}

	if ((cdate.getNormalizedYear() != y)
	    || (cdate.getMonth() != m)
	    || (cdate.getDayOfMonth() != day)
	    // The validation should be cdate.getDayOfWeek() ==
	    // dayOfWeek. However, we don't check dayOfWeek for
	    // compatibility.
	    || (dayOfWeek < Calendar.SUNDAY || dayOfWeek > Calendar.SATURDAY)
	    || (millis < 0 || millis >= (24*60*60*1000))) {
            throw new IllegalArgumentException();
	}

        if (!useDaylight || year < startYear || era != GregorianCalendar.CE) {
	    return rawOffset;
	}

	return getOffset(cal, cdate, y, time);
    
private intgetOffset(sun.util.calendar.BaseCalendar cal, sun.util.calendar.BaseCalendar$Date cdate, int year, long time)

	synchronized (this) {
	    if (cacheStart != 0) {
		if (time >= cacheStart && time < cacheEnd) {
		    return rawOffset + dstSavings;
		}
		if (year == cacheYear) {
		    return rawOffset;
		}
	    }
	}

	long start = getStart(cal, cdate, year);
	long end = getEnd(cal, cdate, year);
	int offset = rawOffset;
	if (start <= end) {
	    if (time >= start && time < end) {
		offset += dstSavings;
	    }
	    synchronized (this) {
		cacheYear = year;
		cacheStart = start;
		cacheEnd = end;
	    }
	} else {
	    if (time < end) {
		// TODO: support Gregorian cutover. The previous year
		// may be in the other calendar system.
		start = getStart(cal, cdate, year - 1);
		if (time >= start) {
		    offset += dstSavings;
		}
	    } else if (time >= start) {
		// TODO: support Gregorian cutover. The next year
		// may be in the other calendar system.
		end = getEnd(cal, cdate, year + 1);
		if (time < end) {
		    offset += dstSavings;
		}
	    }
	    if (start <= end) {
		synchronized (this) {
		    // The start and end transitions are in multiple years.
		    cacheYear = (long) startYear - 1;
		    cacheStart = start;
		    cacheEnd = end;
		}
	    }
	}
	return offset;
    
intgetOffsets(long date, int[] offsets)

see
TimeZone#getOffsets

	int offset = rawOffset;

      computeOffset:
	if (useDaylight) {
	    synchronized (this) {
		if (cacheStart != 0) {
		    if (date >= cacheStart && date < cacheEnd) {
			offset += dstSavings;
			break computeOffset;
		    }
		}
	    }
	    BaseCalendar cal = date >= GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER ?
		gcal : (BaseCalendar) CalendarSystem.forName("julian");
	    BaseCalendar.Date cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
	    // Get the year in local time
	    cal.getCalendarDate(date + rawOffset, cdate);
	    int year = cdate.getNormalizedYear();
	    if (year >= startYear) {
		// Clear time elements for the transition calculations
		cdate.setTimeOfDay(0, 0, 0, 0);
		offset = getOffset(cal, cdate, year, date);
	    }
	}

	if (offsets != null) {
	    offsets[0] = rawOffset;
	    offsets[1] = offset - rawOffset;
	}
	return offset;
    
public intgetRawOffset()
Gets the GMT offset for this time zone.

return
the GMT offset value in milliseconds
see
#setRawOffset

        // The given date will be taken into account while
        // we have the historical time zone data in place.
        return rawOffset;
    
private longgetStart(sun.util.calendar.BaseCalendar cal, sun.util.calendar.BaseCalendar$Date cdate, int year)

	int time = startTime;
	if (startTimeMode != UTC_TIME) {
	    time -= rawOffset;
	}
	return getTransition(cal, cdate, startMode, year, startMonth, startDay,
			     startDayOfWeek, time);
    
private longgetTransition(sun.util.calendar.BaseCalendar cal, sun.util.calendar.BaseCalendar$Date cdate, int mode, int year, int month, int dayOfMonth, int dayOfWeek, int timeOfDay)

	cdate.setNormalizedYear(year);
	cdate.setMonth(month + 1);
	switch (mode) {
	case DOM_MODE:
	    cdate.setDayOfMonth(dayOfMonth);
	    break;

	case DOW_IN_MONTH_MODE:
	    cdate.setDayOfMonth(1);
	    if (dayOfMonth < 0) {
		cdate.setDayOfMonth(cal.getMonthLength(cdate));
	    }
	    cdate = (BaseCalendar.Date) cal.getNthDayOfWeek(dayOfMonth, dayOfWeek, cdate);
	    break;

	case DOW_GE_DOM_MODE:
	    cdate.setDayOfMonth(dayOfMonth);
	    cdate = (BaseCalendar.Date) cal.getNthDayOfWeek(1, dayOfWeek, cdate);
	    break;

	case DOW_LE_DOM_MODE:
	    cdate.setDayOfMonth(dayOfMonth);
	    cdate = (BaseCalendar.Date) cal.getNthDayOfWeek(-1, dayOfWeek, cdate);
	    break;
	}
	return cal.getTime(cdate) + timeOfDay;
    
public booleanhasSameRules(java.util.TimeZone other)
Returns true if this zone has the same rules and offset as another zone.

param
other the TimeZone object to be compared with
return
true if the given zone is a SimpleTimeZone and has the same rules and offset as this one
since
1.2

        if (this == other) {
	    return true;
	}
        if (!(other instanceof SimpleTimeZone)) {
	    return false;
	}
        SimpleTimeZone that = (SimpleTimeZone) other;
        return rawOffset == that.rawOffset &&
            useDaylight == that.useDaylight &&
            (!useDaylight
             // Only check rules if using DST
             || (dstSavings == that.dstSavings &&
                 startMode == that.startMode &&
                 startMonth == that.startMonth &&
                 startDay == that.startDay &&
                 startDayOfWeek == that.startDayOfWeek &&
                 startTime == that.startTime &&
                 startTimeMode == that.startTimeMode &&
                 endMode == that.endMode &&
                 endMonth == that.endMonth &&
                 endDay == that.endDay &&
                 endDayOfWeek == that.endDayOfWeek &&
                 endTime == that.endTime &&
                 endTimeMode == that.endTimeMode &&
                 startYear == that.startYear));
    
public synchronized inthashCode()
Generates the hash code for the SimpleDateFormat object.

return
the hash code for this object

        return startMonth ^ startDay ^ startDayOfWeek ^ startTime ^
            endMonth ^ endDay ^ endDayOfWeek ^ endTime ^ rawOffset;
    
public booleaninDaylightTime(java.util.Date date)
Queries if the given date is in daylight saving time.

return
true if daylight saving time is in effective at the given date; false otherwise.

	return (getOffset(date.getTime()) != rawOffset);
    
private synchronized voidinvalidateCache()


        
	cacheYear = startYear - 1;
	cacheStart = cacheEnd = 0;
    
private voidmakeRulesCompatible()
Make rules compatible to 1.1 FCS code. Since 1.1 FCS code only understands day-of-week-in-month rules, we must modify other modes of rules to their approximate equivalent in 1.1 FCS terms. This method is used when streaming out objects of this class. After it is called, the rules will be modified, with a possible loss of information. startMode and endMode will NOT be altered, even though semantically they should be set to DOW_IN_MONTH_MODE, since the rule modification is only intended to be temporary.

        switch (startMode) {
        case DOM_MODE:
            startDay = 1 + (startDay / 7);
            startDayOfWeek = Calendar.SUNDAY;
            break;

        case DOW_GE_DOM_MODE:
            // A day-of-month of 1 is equivalent to DOW_IN_MONTH_MODE
            // that is, Sun>=1 == firstSun.
            if (startDay != 1) {
                startDay = 1 + (startDay / 7);
	    }
            break;

        case DOW_LE_DOM_MODE:
            if (startDay >= 30) {
                startDay = -1;
            } else {
                startDay = 1 + (startDay / 7);
	    }
            break;
        }

        switch (endMode) {
        case DOM_MODE:
            endDay = 1 + (endDay / 7);
            endDayOfWeek = Calendar.SUNDAY;
            break;

        case DOW_GE_DOM_MODE:
            // A day-of-month of 1 is equivalent to DOW_IN_MONTH_MODE
            // that is, Sun>=1 == firstSun.
            if (endDay != 1) {
                endDay = 1 + (endDay / 7);
	    }
            break;

        case DOW_LE_DOM_MODE:
            if (endDay >= 30) {
                endDay = -1;
            } else {
                endDay = 1 + (endDay / 7);
	    }
            break;
        }

        /*
	 * Adjust the start and end times to wall time.  This works perfectly
         * well unless it pushes into the next or previous day.  If that
         * happens, we attempt to adjust the day rule somewhat crudely.  The day
         * rules have been forced into DOW_IN_MONTH mode already, so we change
         * the day of week to move forward or back by a day.  It's possible to
         * make a more refined adjustment of the original rules first, but in
         * most cases this extra effort will go to waste once we adjust the day
         * rules anyway.
	 */
        switch (startTimeMode) {
        case UTC_TIME:
            startTime += rawOffset;
            break;
        }
        while (startTime < 0) {
            startTime += millisPerDay;
            startDayOfWeek = 1 + ((startDayOfWeek+5) % 7); // Back 1 day
        }
        while (startTime >= millisPerDay) {
            startTime -= millisPerDay;
            startDayOfWeek = 1 + (startDayOfWeek % 7); // Forward 1 day
        }

        switch (endTimeMode) {
        case UTC_TIME:
            endTime += rawOffset + dstSavings;
            break;
        case STANDARD_TIME:
            endTime += dstSavings;
        }
        while (endTime < 0) {
            endTime += millisPerDay;
            endDayOfWeek = 1 + ((endDayOfWeek+5) % 7); // Back 1 day
        }
        while (endTime >= millisPerDay) {
            endTime -= millisPerDay;
            endDayOfWeek = 1 + (endDayOfWeek % 7); // Forward 1 day
        }
    
private byte[]packRules()
Pack the start and end rules into an array of bytes. Only pack data which is not preserved by makeRulesCompatible.

        byte[] rules = new byte[6];
        rules[0] = (byte)startDay;
        rules[1] = (byte)startDayOfWeek;
        rules[2] = (byte)endDay;
        rules[3] = (byte)endDayOfWeek;

        // As of serial version 2, include time modes
        rules[4] = (byte)startTimeMode;
        rules[5] = (byte)endTimeMode;

        return rules;
    
private int[]packTimes()
Pack the start and end times into an array of bytes. This is required as of serial version 2.

        int[] times = new int[2];
        times[0] = startTime;
        times[1] = endTime;
        return times;
    
private voidreadObject(java.io.ObjectInputStream stream)
Reconstitute this object from a stream (i.e., deserialize it). We handle both JDK 1.1 binary formats and full formats with a packed byte array.

        stream.defaultReadObject();

        if (serialVersionOnStream < 1) {
            // Fix a bug in the 1.1 SimpleTimeZone code -- namely,
            // startDayOfWeek and endDayOfWeek were usually uninitialized.  We can't do
            // too much, so we assume SUNDAY, which actually works most of the time.
            if (startDayOfWeek == 0) {
		startDayOfWeek = Calendar.SUNDAY;
	    }
            if (endDayOfWeek == 0) {
		endDayOfWeek = Calendar.SUNDAY;
	    }

            // The variables dstSavings, startMode, and endMode are post-1.1, so they
            // won't be present if we're reading from a 1.1 stream.  Fix them up.
            startMode = endMode = DOW_IN_MONTH_MODE;
            dstSavings = millisPerHour;
        } else {
            // For 1.1.4, in addition to the 3 new instance variables, we also
            // store the actual rules (which have not be made compatible with 1.1)
            // in the optional area.  Read them in here and parse them.
            int length = stream.readInt();
            byte[] rules = new byte[length];
            stream.readFully(rules);
            unpackRules(rules);
        }

        if (serialVersionOnStream >= 2) {
            int[] times = (int[]) stream.readObject();
            unpackTimes(times);
        }

        serialVersionOnStream = currentSerialVersion;
    
public voidsetDSTSavings(int millisSavedDuringDST)
Sets the amount of time in milliseconds that the clock is advanced during daylight saving time.

param
millisSavedDuringDST the number of milliseconds the time is advanced with respect to standard time when the daylight saving time rules are in effect. A positive number, typically one hour (3600000).
see
#getDSTSavings
since
1.2

        if (millisSavedDuringDST <= 0) {
            throw new IllegalArgumentException("Illegal daylight saving value: "
					       + millisSavedDuringDST);
        }
        dstSavings = millisSavedDuringDST;
    
public voidsetEndRule(int endMonth, int endDay, int endTime)
Sets the daylight saving time end rule to a fixed date within a month. This method is equivalent to:
setEndRule(endMonth, endDay, 0, endTime)

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 endMonth, endDay, or endTime parameters are out of range
since
1.2

        setEndRule(endMonth, endDay, 0, endTime);
    
public voidsetEndRule(int endMonth, int endDay, int endDayOfWeek, int endTime, boolean after)
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.

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
endDayOfWeek The daylight saving time ending day-of-week.
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.
param
after If true, this rule selects the first endDayOfWeek on or after endDay. If false, this rule selects the last endDayOfWeek on or before endDay of the month.
exception
IllegalArgumentException the endMonth, endDay, endDayOfWeek, or endTime parameters are out of range
since
1.2

        if (after) {
            setEndRule(endMonth, endDay, -endDayOfWeek, endTime);
        } else {
            setEndRule(endMonth, -endDay, -endDayOfWeek, endTime);
	}
    
public voidsetEndRule(int endMonth, int endDay, int endDayOfWeek, int endTime)
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: setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);

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 endMonth, endDay, endDayOfWeek, or endTime parameters are out of range

        this.endMonth = endMonth;
        this.endDay = endDay;
        this.endDayOfWeek = endDayOfWeek;
        this.endTime = endTime;
        this.endTimeMode = WALL_TIME;
        decodeEndRule();
	invalidateCache();
    
public voidsetRawOffset(int offsetMillis)
Sets the base time zone offset to GMT. This is the offset to add to UTC to get local time.

see
#getRawOffset

        this.rawOffset = offsetMillis;
    
public voidsetStartRule(int startMonth, int startDay, int startDayOfWeek, int startTime)
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:
setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);

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 startMonth, startDay, startDayOfWeek, or startTime parameters are out of range

        this.startMonth = startMonth;
        this.startDay = startDay;
        this.startDayOfWeek = startDayOfWeek;
        this.startTime = startTime;
        startTimeMode = WALL_TIME;
        decodeStartRule();
	invalidateCache();
    
public voidsetStartRule(int startMonth, int startDay, int startTime)
Sets the daylight saving time start rule to a fixed date within a month. This method is equivalent to:
setStartRule(startMonth, startDay, 0, startTime)

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 startMonth, startDayOfMonth, or startTime parameters are out of range
since
1.2

        setStartRule(startMonth, startDay, 0, startTime);
    
public voidsetStartRule(int startMonth, int startDay, int startDayOfWeek, int startTime, boolean after)
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 dayOfWeek on or after dayOfMonth. If false, this rule selects the last dayOfWeek on or before dayOfMonth.
exception
IllegalArgumentException if the startMonth, startDay, startDayOfWeek, or startTime parameters are out of range
since
1.2

	// 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);
	}
    
public voidsetStartYear(int year)
Sets the daylight saving time starting year.

param
year The daylight saving starting year.

        startYear = year;
	invalidateCache();
    
public java.lang.StringtoString()
Returns a string representation of this time zone.

return
a string representation of this time zone.

        return getClass().getName() +
            "[id=" + getID() +
            ",offset=" + rawOffset +
            ",dstSavings=" + dstSavings +
            ",useDaylight=" + useDaylight +
            ",startYear=" + startYear +
            ",startMode=" + startMode +
            ",startMonth=" + startMonth +
            ",startDay=" + startDay +
            ",startDayOfWeek=" + startDayOfWeek +
            ",startTime=" + startTime +
            ",startTimeMode=" + startTimeMode +
            ",endMode=" + endMode +
            ",endMonth=" + endMonth +
            ",endDay=" + endDay +
            ",endDayOfWeek=" + endDayOfWeek +
            ",endTime=" + endTime +
            ",endTimeMode=" + endTimeMode + ']";
    
private voidunpackRules(byte[] rules)
Given an array of bytes produced by packRules, interpret them as the start and end rules.

        startDay       = rules[0];
        startDayOfWeek = rules[1];
        endDay         = rules[2];
        endDayOfWeek   = rules[3];

        // As of serial version 2, include time modes
        if (rules.length >= 6) {
            startTimeMode = rules[4];
            endTimeMode   = rules[5];
        }
    
private voidunpackTimes(int[] times)
Unpack the start and end times from an array of bytes. This is required as of serial version 2.

        startTime = times[0];
        endTime = times[1];
    
public booleanuseDaylightTime()
Queries if this time zone uses daylight saving time.

return
true if this time zone uses daylight saving time; false otherwise.

        return useDaylight;
    
private voidwriteObject(java.io.ObjectOutputStream stream)
Save the state of this object to a stream (i.e., serialize it).

serialData
We write out two formats, a JDK 1.1 compatible format, using DOW_IN_MONTH_MODE rules, in the required section, followed by the full rules, in packed format, in the optional section. The optional section will be ignored by JDK 1.1 code upon stream in.

Contents of the optional section: The length of a byte array is emitted (int); this is 4 as of this release. The byte array of the given length is emitted. The contents of the byte array are the true values of the fields startDay, startDayOfWeek, endDay, and endDayOfWeek. The values of these fields in the required section are approximate values suited to the rule mode DOW_IN_MONTH_MODE, which is the only mode recognized by JDK 1.1.

        // Construct a binary rule
        byte[] rules = packRules();
        int[] times = packTimes();

        // Convert to 1.1 FCS rules.  This step may cause us to lose information.
        makeRulesCompatible();

        // Write out the 1.1 FCS rules
        stream.defaultWriteObject();

        // Write out the binary rules in the optional data area of the stream.
        stream.writeInt(rules.length);
        stream.write(rules);
        stream.writeObject(times);

        // Recover the original rules.  This recovers the information lost
        // by makeRulesCompatible.
        unpackRules(rules);
        unpackTimes(times);