Class SimpleTimeZone
- All Implemented Interfaces:
Serializable,Cloneable
public class SimpleTimeZone extends TimeZone
SimpleTimeZone is a concrete subclass of TimeZone
that represents a time zone for use with a Gregorian calendar. This class
does not handle historical changes.
Use a negative value for dayOfWeekInMonth to indicate that
SimpleTimeZone should count from the end of the month
backwards. For example, Daylight Savings Time ends at the last
(dayOfWeekInMonth = -1) Sunday in October, at 2 AM in standard time.
- See Also:
Calendar,GregorianCalendar,TimeZone, Serialized Form
-
Field Summary
Fields Modifier and Type Field Description static intSTANDARD_TIMEThe constant for representing a start or end time in standard local time mode, based on timezone's raw offset from GMT; does not include Daylight savings.static intUTC_TIMEThe constant for representing a start or end time in GMT time mode.static intWALL_TIMEThe constant for representing a start or end time in local wall clock time mode, based on timezone's adjusted offset from GMT; includes Daylight savings. -
Constructor Summary
Constructors Constructor Description SimpleTimeZone(int offset, String name)Constructs aSimpleTimeZonewith the given base time zone offset from GMT and time zone ID.SimpleTimeZone(int offset, String name, int startMonth, int startDay, int startDayOfWeek, int startTime, int endMonth, int endDay, int endDayOfWeek, int endTime)Constructs aSimpleTimeZonewith the given base time zone offset from GMT, time zone ID, and times to start and end the daylight savings time.SimpleTimeZone(int offset, String name, int startMonth, int startDay, int startDayOfWeek, int startTime, int endMonth, int endDay, int endDayOfWeek, int endTime, int daylightSavings)Constructs aSimpleTimeZonewith the given base time zone offset from GMT, time zone ID, times to start and end the daylight savings time, and the daylight savings time difference in milliseconds.SimpleTimeZone(int offset, String name, int startMonth, int startDay, int startDayOfWeek, int startTime, int startTimeMode, int endMonth, int endDay, int endDayOfWeek, int endTime, int endTimeMode, int daylightSavings)Construct aSimpleTimeZonewith the given base time zone offset from GMT, time zone ID, times to start and end the daylight savings time including a mode specifier, the daylight savings time difference in milliseconds. -
Method Summary
Modifier and Type Method Description Objectclone()Returns a newSimpleTimeZonewith the same ID,rawOffsetand daylight savings time rules as this SimpleTimeZone.booleanequals(Object object)Compares the specified object to thisSimpleTimeZoneand returns whether they are equal.intgetDSTSavings()Returns the latest daylight savings in milliseconds for this time zone, relative to this time zone's regular UTC offset (as returned byTimeZone.getRawOffset()).intgetOffset(int era, int year, int month, int day, int dayOfWeek, int time)Returns this time zone's offset in milliseconds from UTC at the specified date and time.intgetOffset(long time)Returns the offset in milliseconds from UTC for this time zone attime.intgetRawOffset()Returns the offset in milliseconds from UTC of this time zone's standard time.inthashCode()Returns an integer hash code for the receiver.booleanhasSameRules(TimeZone zone)Returns true iftimeZonehas the same rules as this time zone.booleaninDaylightTime(Date time)Returns true iftimeis in a daylight savings time period for this time zone.voidsetDSTSavings(int milliseconds)Sets the daylight savings offset in milliseconds for thisSimpleTimeZone.voidsetEndRule(int month, int dayOfMonth, int time)Sets the rule which specifies the end of daylight savings time.voidsetEndRule(int month, int day, int dayOfWeek, int time)Sets the rule which specifies the end of daylight savings time.voidsetEndRule(int month, int day, int dayOfWeek, int time, boolean after)Sets the rule which specifies the end of daylight savings time.voidsetRawOffset(int offset)Sets the offset for standard time from GMT for thisSimpleTimeZone.voidsetStartRule(int month, int dayOfMonth, int time)Sets the rule which specifies the start of daylight savings time.voidsetStartRule(int month, int day, int dayOfWeek, int time)Sets the rule which specifies the start of daylight savings time.voidsetStartRule(int month, int day, int dayOfWeek, int time, boolean after)Sets the rule which specifies the start of daylight savings time.voidsetStartYear(int year)Sets the starting year for daylight savings time in thisSimpleTimeZone.StringtoString()Returns the string representation of thisSimpleTimeZone.booleanuseDaylightTime()Returns true if this time zone has a future transition to or from daylight savings time.Methods inherited from class java.util.TimeZone
getAvailableIDs, getAvailableIDs, getDefault, getDisplayName, getDisplayName, getDisplayName, getDisplayName, getID, getTimeZone, setDefault, setID
-
Field Details
-
UTC_TIME
public static final int UTC_TIMEThe constant for representing a start or end time in GMT time mode.- See Also:
- Constant Field Values
-
STANDARD_TIME
public static final int STANDARD_TIMEThe constant for representing a start or end time in standard local time mode, based on timezone's raw offset from GMT; does not include Daylight savings.- See Also:
- Constant Field Values
-
WALL_TIME
public static final int WALL_TIMEThe constant for representing a start or end time in local wall clock time mode, based on timezone's adjusted offset from GMT; includes Daylight savings.- See Also:
- Constant Field Values
-
-
Constructor Details
-
SimpleTimeZone
Constructs aSimpleTimeZonewith the given base time zone offset from GMT and time zone ID. Timezone IDs can be obtained fromTimeZone.getAvailableIDs. Normally you should useTimeZone.getDefaultto construct aTimeZone.- Parameters:
offset- the given base time zone offset to GMT.name- the time zone ID which is obtained fromTimeZone.getAvailableIDs.
-
SimpleTimeZone
public SimpleTimeZone(int offset, String name, int startMonth, int startDay, int startDayOfWeek, int startTime, int endMonth, int endDay, int endDayOfWeek, int endTime)Constructs aSimpleTimeZonewith the given base time zone offset from GMT, time zone ID, and times to start and end the daylight savings time. Timezone IDs can be obtained fromTimeZone.getAvailableIDs. Normally you should useTimeZone.getDefaultto create aTimeZone. For a time zone that does not use daylight saving time, do not use this constructor; instead you should useSimpleTimeZone(rawOffset, ID).By default, this constructor specifies day-of-week-in-month rules. That is, if the
startDayis 1, and thestartDayOfWeekisSUNDAY, then this indicates the first Sunday in thestartMonth. AstartDayof -1 likewise indicates the last Sunday. However, by using negative or zero values for certain parameters, other types of rules can be specified.Day of month: To specify an exact day of the month, such as March 1, set
startDayOfWeekto zero.Day of week after day of month: To specify the first day of the week occurring on or after an exact day of the month, make the day of the week negative. For example, if
startDayis 5 andstartDayOfWeekis-MONDAY, this indicates the first Monday on or after the 5th day of thestartMonth.Day of week before day of month: To specify the last day of the week occurring on or before an exact day of the month, make the day of the week and the day of the month negative. For example, if
startDayis-21andstartDayOfWeekis-WEDNESDAY, this indicates the last Wednesday on or before the 21st of thestartMonth.The above examples refer to the
startMonth,startDay, andstartDayOfWeek; the same applies for theendMonth,endDay, andendDayOfWeek.The daylight savings time difference is set to the default value: one hour.
- Parameters:
offset- the given base time zone offset to GMT.name- the time zone ID which is obtained fromTimeZone.getAvailableIDs.startMonth- the daylight savings starting month. The month indexing is 0-based. eg, 0 for January.startDay- the daylight savings starting day-of-week-in-month. Please see the member description for an example.startDayOfWeek- the daylight savings starting day-of-week. Please see the member description for an example.startTime- the daylight savings starting time in local wall time, which is standard time in this case. Please see the member description for an example.endMonth- the daylight savings ending month. The month indexing is 0-based. eg, 0 for January.endDay- the daylight savings ending day-of-week-in-month. Please see the member description for an example.endDayOfWeek- the daylight savings ending day-of-week. Please see the member description for an example.endTime- the daylight savings ending time in local wall time, which is daylight time in this case. Please see the member description for an example.- Throws:
IllegalArgumentException- if the month, day, dayOfWeek, or time parameters are out of range for the start or end rule.
-
SimpleTimeZone
public SimpleTimeZone(int offset, String name, int startMonth, int startDay, int startDayOfWeek, int startTime, int endMonth, int endDay, int endDayOfWeek, int endTime, int daylightSavings)Constructs aSimpleTimeZonewith the given base time zone offset from GMT, time zone ID, times to start and end the daylight savings time, and the daylight savings time difference in milliseconds.- Parameters:
offset- the given base time zone offset to GMT.name- the time zone ID which is obtained fromTimeZone.getAvailableIDs.startMonth- the daylight savings starting month. Month is 0-based. eg, 0 for January.startDay- the daylight savings starting day-of-week-in-month. Please see the description ofSimpleTimeZone(int, String, int, int, int, int, int, int, int, int)for an example.startDayOfWeek- the daylight savings starting day-of-week. Please see the description ofSimpleTimeZone(int, String, int, int, int, int, int, int, int, int)for an example.startTime- The daylight savings starting time in local wall time, which is standard time in this case. Please see the description ofSimpleTimeZone(int, String, int, int, int, int, int, int, int, int)for an example.endMonth- the daylight savings ending month. Month is 0-based. eg, 0 for January.endDay- the daylight savings ending day-of-week-in-month. Please see the description ofSimpleTimeZone(int, String, int, int, int, int, int, int, int, int)for an example.endDayOfWeek- the daylight savings ending day-of-week. Please see the description ofSimpleTimeZone(int, String, int, int, int, int, int, int, int, int)for an example.endTime- the daylight savings ending time in local wall time, which is daylight time in this case. Please see the description ofSimpleTimeZone(int, String, int, int, int, int, int, int, int, int)for an example.daylightSavings- the daylight savings time difference in milliseconds.- Throws:
IllegalArgumentException- if the month, day, dayOfWeek, or time parameters are out of range for the start or end rule.
-
SimpleTimeZone
public SimpleTimeZone(int offset, String name, int startMonth, int startDay, int startDayOfWeek, int startTime, int startTimeMode, int endMonth, int endDay, int endDayOfWeek, int endTime, int endTimeMode, int daylightSavings)Construct aSimpleTimeZonewith the given base time zone offset from GMT, time zone ID, times to start and end the daylight savings time including a mode specifier, the daylight savings time difference in milliseconds. The mode specifies eitherWALL_TIME,STANDARD_TIME, orUTC_TIME.- Parameters:
offset- the given base time zone offset to GMT.name- the time zone ID which is obtained fromTimeZone.getAvailableIDs.startMonth- the daylight savings starting month. The month indexing is 0-based. eg, 0 for January.startDay- the daylight savings starting day-of-week-in-month. Please see the description ofSimpleTimeZone(int, String, int, int, int, int, int, int, int, int)for an example.startDayOfWeek- the daylight savings starting day-of-week. Please see the description ofSimpleTimeZone(int, String, int, int, int, int, int, int, int, int)for an example.startTime- the time of day in milliseconds on which daylight savings time starts, based on thestartTimeMode.startTimeMode- the mode (UTC, standard, or wall time) of the start time value.endDay- the day of the week on which daylight savings time ends.endMonth- the daylight savings ending month. The month indexing is 0-based. eg, 0 for January.endDayOfWeek- the daylight savings ending day-of-week. Please see the description ofSimpleTimeZone(int, String, int, int, int, int, int, int, int, int)for an example.endTime- the time of day in milliseconds on which daylight savings time ends, based on theendTimeMode.endTimeMode- the mode (UTC, standard, or wall time) of the end time value.daylightSavings- the daylight savings time difference in milliseconds.- Throws:
IllegalArgumentException- if the month, day, dayOfWeek, or time parameters are out of range for the start or end rule.
-
-
Method Details
-
clone
Returns a newSimpleTimeZonewith the same ID,rawOffsetand daylight savings time rules as this SimpleTimeZone. -
equals
Compares the specified object to thisSimpleTimeZoneand returns whether they are equal. The object must be an instance ofSimpleTimeZoneand have the same internal data.- Overrides:
equalsin classObject- Parameters:
object- the object to compare with this object.- Returns:
trueif the specified object is equal to thisSimpleTimeZone,falseotherwise.- See Also:
hashCode()
-
getDSTSavings
public int getDSTSavings()Description copied from class:TimeZoneReturns the latest daylight savings in milliseconds for this time zone, relative to this time zone's regular UTC offset (as returned byTimeZone.getRawOffset()).This class returns
3600000(1 hour) for time zones that use daylight savings time and0for timezones that do not, leaving it to subclasses to override this method for other daylight savings offsets. (There are time zones, such asAustralia/Lord_Howe, that use other values.)Note that this method doesn't tell you whether or not to apply the offset: you need to call
inDaylightTimefor the specific time you're interested in. If this method returns a non-zero offset, that only tells you that thisTimeZonesometimes observes daylight savings.Note also that this method doesn't necessarily return the value you need to apply to the time you're working with. This value can and does change over time for a given time zone.
It's highly unlikely that you should ever call this method. You probably want
TimeZone.getOffset(long)instead, which tells you the offset for a specific point in time, and takes daylight savings into account for you.- Overrides:
getDSTSavingsin classTimeZone
-
getOffset
public int getOffset(int era, int year, int month, int day, int dayOfWeek, int time)Description copied from class:TimeZoneReturns this time zone's offset in milliseconds from UTC at the specified date and time. The offset includes daylight savings time if the date and time is within the daylight savings time period.This method is intended to be used by
Calendarto computeCalendar.DST_OFFSETandCalendar.ZONE_OFFSET. Application code should have no reason to call this method directly. Each parameter is interpreted in the same way as the correspondingCalendarfield. Refer toCalendarfor specific definitions of this method's parameters. -
getOffset
public int getOffset(long time)Description copied from class:TimeZoneReturns the offset in milliseconds from UTC for this time zone attime. The offset includes daylight savings time if the specified date is within the daylight savings time period. -
getRawOffset
public int getRawOffset()Description copied from class:TimeZoneReturns the offset in milliseconds from UTC of this time zone's standard time.- Specified by:
getRawOffsetin classTimeZone
-
hashCode
public int hashCode()Returns an integer hash code for the receiver. Objects which are equal return the same value for this method.- Overrides:
hashCodein classObject- Returns:
- the receiver's hash.
- See Also:
equals(java.lang.Object)
-
hasSameRules
Description copied from class:TimeZoneReturns true iftimeZonehas the same rules as this time zone.The base implementation returns true if both time zones have the same raw offset.
- Overrides:
hasSameRulesin classTimeZone
-
inDaylightTime
Description copied from class:TimeZoneReturns true iftimeis in a daylight savings time period for this time zone.- Specified by:
inDaylightTimein classTimeZone
-
setDSTSavings
public void setDSTSavings(int milliseconds)Sets the daylight savings offset in milliseconds for thisSimpleTimeZone.- Parameters:
milliseconds- the daylight savings offset in milliseconds.
-
setEndRule
public void setEndRule(int month, int dayOfMonth, int time)Sets the rule which specifies the end of daylight savings time.- Parameters:
month- theCalendarmonth in which daylight savings time ends.dayOfMonth- theCalendarday of the month on which daylight savings time ends.time- the time of day in milliseconds standard time on which daylight savings time ends.
-
setEndRule
public void setEndRule(int month, int day, int dayOfWeek, int time)Sets the rule which specifies the end of daylight savings time.- Parameters:
month- theCalendarmonth in which daylight savings time ends.day- the occurrence of the day of the week on which daylight savings time ends.dayOfWeek- theCalendarday of the week on which daylight savings time ends.time- the time of day in milliseconds standard time on which daylight savings time ends.
-
setEndRule
public void setEndRule(int month, int day, int dayOfWeek, int time, boolean after)Sets the rule which specifies the end of daylight savings time.- Parameters:
month- theCalendarmonth in which daylight savings time ends.day- theCalendarday of the month.dayOfWeek- theCalendarday of the week on which daylight savings time ends.time- the time of day in milliseconds on which daylight savings time ends.after- selects the day after or before the day of month.
-
setRawOffset
public void setRawOffset(int offset)Sets the offset for standard time from GMT for thisSimpleTimeZone.- Specified by:
setRawOffsetin classTimeZone- Parameters:
offset- the offset from GMT of standard time in milliseconds.
-
setStartRule
public void setStartRule(int month, int dayOfMonth, int time)Sets the rule which specifies the start of daylight savings time.- Parameters:
month- theCalendarmonth in which daylight savings time starts.dayOfMonth- theCalendarday of the month on which daylight savings time starts.time- the time of day in milliseconds on which daylight savings time starts.
-
setStartRule
public void setStartRule(int month, int day, int dayOfWeek, int time)Sets the rule which specifies the start of daylight savings time.- Parameters:
month- theCalendarmonth in which daylight savings time starts.day- the occurrence of the day of the week on which daylight savings time starts.dayOfWeek- theCalendarday of the week on which daylight savings time starts.time- the time of day in milliseconds on which daylight savings time starts.
-
setStartRule
public void setStartRule(int month, int day, int dayOfWeek, int time, boolean after)Sets the rule which specifies the start of daylight savings time.- Parameters:
month- theCalendarmonth in which daylight savings time starts.day- theCalendarday of the month.dayOfWeek- theCalendarday of the week on which daylight savings time starts.time- the time of day in milliseconds on which daylight savings time starts.after- selects the day after or before the day of month.
-
setStartYear
public void setStartYear(int year)Sets the starting year for daylight savings time in thisSimpleTimeZone. Years before this start year will always be in standard time.- Parameters:
year- the starting year.
-
toString
Returns the string representation of thisSimpleTimeZone. -
useDaylightTime
public boolean useDaylightTime()Description copied from class:TimeZoneReturns true if this time zone has a future transition to or from daylight savings time.Warning: this returns false for time zones like
Asia/Kuala_Lumpurthat have previously used DST but do not currently. A hypothetical country that has never observed daylight savings before but plans to start next year would return true.Warning: this returns true for time zones that use DST, even when it is not active.
Use
TimeZone.inDaylightTime(java.util.Date)to find out whether daylight savings is in effect at a specific time.Most applications should not use this method.
- Specified by:
useDaylightTimein classTimeZone
-