Top Description Inners Fields Constructors Methods
java.util

public Class SimpleTimeZone

extends TimeZone
Class Inheritance
Imports
java.io.ObjectInputStream, .ObjectOutputStream, .IOException, .InvalidObjectException, sun.util.calendar.CalendarSystem, .CalendarUtils, .BaseCalendar, .Gregorian

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 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 MONTH field value, such as Calendar#MARCH. The day-of-week value is represented by a Calendar DAY_OF_WEEK value, such as SUNDAY. The meanings of value combinations are as follows.

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: WALL_TIME, STANDARD_TIME and 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 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.
Author
David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu
Since
1.1
See Also
Calendar, GregorianCalendar, TimeZone

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
SimpleTimeZone.Cache

Cache values representing a single period of daylight saving time.

Field Summary

Modifier and TypeField and Description
private transient volatile SimpleTimeZone.Cache
pack-priv static final int
private static final int
DOM_MODE

Constants specifying values of startMode and endMode.

private static final int
private static final int
private static final int
private int
dstSavings

A positive value indicating the amount of time saved during DST in milliseconds.

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.
private int
endDayOfWeek

The day of the week on which daylight saving time ends.

private int
endMode

Variables specifying the mode of the end rule.

private int
endMonth

The month in which daylight saving time ends.

private int
endTime

The time in milliseconds after midnight at which daylight saving time ends.

private int
endTimeMode

The format of endTime, either WALL_TIME, STANDARD_TIME, or UTC_TIME.

private static final Gregorian
private static final int
private static final int
private static final int
private final byte[]
monthLength

This field was serialized in JDK 1.1, so we have to keep it that way to maintain serialization compatibility.

private int
rawOffset

The offset in milliseconds between this zone and GMT.

private int
serialVersionOnStream

The version of the serialized data on the stream.

pack-priv static final long
public static final int
STANDARD_TIME

Constant for a mode of start or end time specified as standard time.

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 the 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.
private int
startDayOfWeek

The day of the week on which daylight saving time starts.

private int
startMode

Variables specifying the mode of the start rule.

private int
startMonth

The month in which daylight saving time starts.

private int
startTime

The time in milliseconds after midnight at which daylight saving time starts.

private int
startTimeMode

The format of startTime, either WALL_TIME, STANDARD_TIME, or UTC_TIME.

private int
startYear

The year in which daylight saving time is first observed.

private static final byte[]
private static final byte[]
private boolean
useDaylight

A boolean value which is true if and only if this zone uses daylight saving time.

public static final int
UTC_TIME

Constant for a mode of start or end time specified as UTC.

public static final int
WALL_TIME

Constant for a mode of start or end time specified as wall clock time.

Inherited from java.util.TimeZone:
GMT_IDLONGNO_TIMEZONESHORT

Constructor Summary

AccessConstructor and Description
public
SimpleTimeZone(int
The base time zone offset in milliseconds to GMT.
rawOffset
,
String
The time zone name that is given to this instance.
ID
)

Constructs a SimpleTimeZone with the given base time zone offset from GMT and time zone ID with no daylight saving time schedule.

public
SimpleTimeZone(int
The given base time zone offset from GMT.
rawOffset
,
String
The time zone ID which is given to this object.
ID
,
int
The daylight saving time starting month. Month is a MONTH field value (0-based. e.g., 0 for January).
startMonth
,
int
The day of the month on which the daylight saving time starts. See the class description for the special cases of this parameter.
startDay
,
int
The daylight saving time starting day-of-week. See the class description for the special cases of this parameter.
startDayOfWeek
,
int
The daylight saving time starting time in local wall clock time (in milliseconds within the day), which is local standard time in this case.
startTime
,
int
The daylight saving time ending month. Month is a MONTH field value (0-based. e.g., 9 for October).
endMonth
,
int
The day of the month on which the daylight saving time ends. See the class description for the special cases of this parameter.
endDay
,
int
The daylight saving time ending day-of-week. See the class description for the special cases of this parameter.
endDayOfWeek
,
int
The daylight saving ending time in local wall clock time, (in milliseconds within the day) which is local daylight time in this case.
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.

public
SimpleTimeZone(int
The given base time zone offset from GMT.
rawOffset
,
String
The time zone ID which is given to this object.
ID
,
int
The daylight saving time starting month. Month is a MONTH field value (0-based. e.g., 0 for January).
startMonth
,
int
The day of the month on which the daylight saving time starts. See the class description for the special cases of this parameter.
startDay
,
int
The daylight saving time starting day-of-week. See the class description for the special cases of this parameter.
startDayOfWeek
,
int
The daylight saving time starting time in local wall clock time, which is local standard time in this case.
startTime
,
int
The daylight saving time ending month. Month is a MONTH field value (0-based. e.g., 9 for October).
endMonth
,
int
The day of the month on which the daylight saving time ends. See the class description for the special cases of this parameter.
endDay
,
int
The daylight saving time ending day-of-week. See the class description for the special cases of this parameter.
endDayOfWeek
,
int
The daylight saving ending time in local wall clock time, which is local daylight time in this case.
endTime
,
int
The amount of time in milliseconds saved during daylight saving time.
dstSavings
)

Constructs a SimpleTimeZone with the given base time zone offset from GMT, time zone ID, and rules for starting and ending the daylight time.

public
SimpleTimeZone(int
The given base time zone offset from GMT.
rawOffset
,
String
The time zone ID which is given to this object.
ID
,
int
The daylight saving time starting month. Month is a MONTH field value (0-based. e.g., 0 for January).
startMonth
,
int
The day of the month on which the daylight saving time starts. See the class description for the special cases of this parameter.
startDay
,
int
The daylight saving time starting day-of-week. See the class description for the special cases of this parameter.
startDayOfWeek
,
int
The daylight saving time starting time in the time mode specified by startTimeMode.
startTime
,
int
The mode of the start time specified by startTime.
startTimeMode
,
int
The daylight saving time ending month. Month is a MONTH field value (0-based. e.g., 9 for October).
endMonth
,
int
The day of the month on which the daylight saving time ends. See the class description for the special cases of this parameter.
endDay
,
int
The daylight saving time ending day-of-week. See the class description for the special cases of this parameter.
endDayOfWeek
,
int
The daylight saving ending time in time mode specified by endTimeMode.
endTime
,
int
The mode of the end time specified by endTime
endTimeMode
,
int
The amount of time in milliseconds saved during daylight saving time.
dstSavings
)

Constructs a SimpleTimeZone with the given base time zone offset from GMT, time zone ID, and rules for starting and ending the daylight time.

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

a clone of this instance.
clone
()

Overrides java.util.TimeZone.clone.

Returns a clone of this SimpleTimeZone instance.

private void
decodeEndRule()

Decode the end rule and validate the parameters.

private void
decodeRules()

Given a set of encoded rules in startDay and startDayOfMonth, decode them and set the startMode appropriately.

private void
decodeStartRule()

Decode the start rule and validate the parameters.

public boolean

Returns:

True if the given obj is the same as this SimpleTimeZone object; false otherwise.
equals
(Object
The SimpleTimeZone object to be compared with.
obj
)

Overrides java.lang.Object.equals.

Compares the equality of two SimpleTimeZone objects.

public int

Returns:

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.
getDSTSavings
()

Overrides java.util.TimeZone.getDSTSavings.

Returns the amount of time in milliseconds that the clock is advanced during daylight saving time.

private long
getEnd(BaseCalendar cal, BaseCalendar.Date cdate, int year)

public int

Returns:

the amount of time in milliseconds to add to UTC to get local time.
getOffset
(long
the time at which the time zone offset is found
date
)

Overrides java.util.TimeZone.getOffset.

Returns the offset of this time zone from UTC at the given time.

public int

Returns:

The milliseconds to add to UTC to get local time.
getOffset
(int
The era of the given date.
era
,
int
The year in the given date.
year
,
int
The month in the given date. Month is 0-based. e.g., 0 for January.
month
,
int
The day-in-month of the given date.
day
,
int
The day-of-week of the given date.
dayOfWeek
,
int
The milliseconds in day in standard local time.
millis
)

Implements abstract java.util.TimeZone.getOffset.

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.

private int
getOffset(BaseCalendar cal, BaseCalendar.Date cdate, int year, long time)

pack-priv int
getOffsets(long
the milliseconds (since January 1, 1970, 00:00:00.000 GMT) at which the time zone offset and daylight saving amount are found
date
,
int[]
an array of int where the raw GMT offset (offset[0]) and daylight saving amount (offset[1]) are stored, or null if those values are not needed. The method assumes that the length of the given array is two or larger.
offsets
)

Overrides java.util.TimeZone.getOffsets.

Gets the raw GMT offset and the amount of daylight saving of this time zone at the given time.

public int

Returns:

the GMT offset value in milliseconds
getRawOffset
()

Implements abstract java.util.TimeZone.getRawOffset.

Gets the GMT offset for this time zone.

private long
getStart(BaseCalendar cal, BaseCalendar.Date cdate, int year)

private long
getTransition(BaseCalendar cal, BaseCalendar.Date cdate, int mode, int year, int month, int dayOfMonth, int dayOfWeek, int timeOfDay)

public int

Returns:

the hash code for this object
hashCode
()

Overrides java.lang.Object.hashCode.

Generates the hash code for the SimpleDateFormat object.

public boolean

Returns:

true if the given zone is a SimpleTimeZone and has the same rules and offset as this one
hasSameRules
(TimeZone
the TimeZone object to be compared with
other
)

Overrides java.util.TimeZone.hasSameRules.

Returns true if this zone has the same rules and offset as another zone.

public boolean

Returns:

true if daylight saving time is in effective at the given date; false otherwise.
inDaylightTime
(Date
the given Date.
date
)

Implements abstract java.util.TimeZone.inDaylightTime.

Queries if the given date is in daylight saving time.

private void
private void
makeRulesCompatible()

Make rules compatible to 1.1 FCS code.

public boolean

Returns:

true if this SimpleTimeZone observes Daylight Saving Time; false otherwise.
observesDaylightTime
()

Overrides java.util.TimeZone.observesDaylightTime.

Returns true if this SimpleTimeZone observes Daylight Saving Time.

private byte[]
packRules()

Pack the start and end rules into an array of bytes.

private int[]
packTimes()

Pack the start and end times into an array of bytes.

private void
readObject(ObjectInputStream stream)

Reconstitute this object from a stream (i.e., deserialize it).

public void
setDSTSavings(int
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).
millisSavedDuringDST
)

Sets the amount of time in milliseconds that the clock is advanced during daylight saving time.

public void
setEndRule(int
The daylight saving time ending month. Month is a MONTH field value (0-based. e.g., 9 for October).
endMonth
,
int
The day of the month on which the daylight saving time ends. See the class description for the special cases of this parameter.
endDay
,
int
The daylight saving time ending day-of-week. See the class description for the special cases of this parameter.
endDayOfWeek
,
int
The daylight saving ending time in local wall clock time, (in milliseconds within the day) which is local daylight time in this case.
endTime
)

Sets the daylight saving time end rule.

public void
setEndRule(int
The daylight saving time ending month. Month is a MONTH field value (0-based. e.g., 9 for October).
endMonth
,
int
The day of the month on which the daylight saving time ends.
endDay
,
int
The daylight saving ending time in local wall clock time, (in milliseconds within the day) which is local daylight time in this case.
endTime
)

Sets the daylight saving time end rule to a fixed date within a month.

public void
setEndRule(int
The daylight saving time ending month. Month is a MONTH field value (0-based. e.g., 9 for October).
endMonth
,
int
The day of the month on which the daylight saving time ends.
endDay
,
int
The daylight saving time ending day-of-week.
endDayOfWeek
,
int
The daylight saving ending time in local wall clock time, (in milliseconds within the day) which is local daylight time in this case.
endTime
,
boolean
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.
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.

public void
setRawOffset(int
the given base time zone offset to GMT.
offsetMillis
)

Implements abstract java.util.TimeZone.setRawOffset.

Sets the base time zone offset to GMT.

public void
setStartRule(int
The daylight saving time starting month. Month is a MONTH field value (0-based. e.g., 0 for January).
startMonth
,
int
The day of the month on which the daylight saving time starts. See the class description for the special cases of this parameter.
startDay
,
int
The daylight saving time starting day-of-week. See the class description for the special cases of this parameter.
startDayOfWeek
,
int
The daylight saving time starting time in local wall clock time, which is local standard time in this case.
startTime
)

Sets the daylight saving time start rule.

public void
setStartRule(int
The daylight saving time starting month. Month is a MONTH field value (0-based. e.g., 0 for January).
startMonth
,
int
The day of the month on which the daylight saving time starts.
startDay
,
int
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.
startTime
)

Sets the daylight saving time start rule to a fixed date within a month.

public void
setStartRule(int
The daylight saving time starting month. Month is a MONTH field value (0-based. e.g., 0 for January).
startMonth
,
int
The day of the month on which the daylight saving time starts.
startDay
,
int
The daylight saving time starting day-of-week.
startDayOfWeek
,
int
The daylight saving time starting time in local wall clock time, which is local standard time in this case.
startTime
,
boolean
If true, this rule selects the first dayOfWeek on or after dayOfMonth. If false, this rule selects the last dayOfWeek on or before dayOfMonth.
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.

public void
setStartYear(int
The daylight saving starting year.
year
)

Sets the daylight saving time starting year.

public String

Returns:

a string representation of this time zone.
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of this time zone.

private void
unpackRules(byte[] rules)

Given an array of bytes produced by packRules, interpret them as the start and end rules.

private void
unpackTimes(int[] times)

Unpack the start and end times from an array of bytes.

public boolean

Returns:

true if this time zone uses daylight saving time; false otherwise.
useDaylightTime
()

Implements abstract java.util.TimeZone.useDaylightTime.

Queries if this time zone uses daylight saving time.

private void
writeObject(ObjectOutputStream stream)

Save the state of this object to a stream (i.e., serialize it).

Inherited from java.util.TimeZone:
getAvailableIDsgetAvailableIDsgetDefaultgetDefaultRefgetDisplayNamegetDisplayNamegetDisplayNamegetDisplayNamegetIDgetTimeZonegetTimeZonesetDefaultsetIDtoZoneId