java.util

Class SimpleTimeZone

Implemented Interfaces:
Cloneable, Serializable

public class SimpleTimeZone
extends TimeZone

This class represents a simple time zone offset and handles daylight savings. It can only handle one daylight savings rule, so it can't represent historical changes. This object is tightly bound to the Gregorian calendar. It assumes a regular seven days week, and the month lengths are that of the Gregorian Calendar. It can only handle daylight savings for years lying in the AD era.
See Also:
Calendar, GregorianCalendar, Serialized Form

Field Summary

static int
STANDARD_TIME
Constant to indicate that start and end times are specified in standard time, without adjusting for daylight savings.
static int
UTC_TIME
Constant to indicate that start and end times are specified in UTC.
static int
WALL_TIME
Constant to indicate that start and end times are specified in wall time, adjusting for daylight savings.

Fields inherited from class java.util.TimeZone

LONG, SHORT

Constructor Summary

SimpleTimeZone(int rawOffset, String id)
Create a SimpleTimeZone with the given time offset from GMT and without daylight savings.
SimpleTimeZone(int rawOffset, String id, int startMonth, int startDayOfWeekInMonth, int startDayOfWeek, int startTime, int endMonth, int endDayOfWeekInMonth, int endDayOfWeek, int endTime)
Create a SimpleTimeZone with the given time offset from GMT and with daylight savings.
SimpleTimeZone(int rawOffset, String id, int startMonth, int startDayOfWeekInMonth, int startDayOfWeek, int startTime, int endMonth, int endDayOfWeekInMonth, int endDayOfWeek, int endTime, int dstSavings)
This constructs a new SimpleTimeZone that supports a daylight savings rule.
SimpleTimeZone(int rawOffset, String id, int startMonth, int startDayOfWeekInMonth, int startDayOfWeek, int startTime, int startTimeMode, int endMonth, int endDayOfWeekInMonth, int endDayOfWeek, int endTime, int endTimeMode, int dstSavings)
This constructs a new SimpleTimeZone that supports a daylight savings rule.

Method Summary

boolean
equals(Object o)
int
getDSTSavings()
Gets the daylight savings offset.
int
getOffset(int era, int year, int month, int day, int dayOfWeek, int millis)
Gets the time zone offset, for current date, modified in case of daylight savings.
int
getRawOffset()
Returns the time zone offset to GMT in milliseconds, ignoring day light savings.
boolean
hasSameRules(TimeZone other)
Test if the other time zone uses the same rule and only possibly differs in ID.
int
hashCode()
Generates the hashCode for the SimpleDateFormat object.
boolean
inDaylightTime(Date date)
Determines if the given date is in daylight savings time.
void
setDSTSavings(int dstSavings)
Sets the daylight savings offset.
void
setEndRule(int month, int day, int time)
Sets the daylight savings end rule.
void
setEndRule(int month, int day, int dayOfWeek, int time)
Sets the daylight savings end rule.
void
setEndRule(int month, int day, int dayOfWeek, int time, boolean after)
Sets the daylight savings end rule.
void
setRawOffset(int rawOffset)
Sets the standard time zone offset to GMT.
void
setStartRule(int month, int day, int time)
Sets the daylight savings start rule.
void
setStartRule(int month, int day, int dayOfWeek, int time)
Sets the daylight savings start rule.
void
setStartRule(int month, int day, int dayOfWeek, int time, boolean after)
Sets the daylight savings start rule.
void
setStartYear(int year)
Sets the first year, where daylight savings applies.
String
toString()
Returns a string representation of this SimpleTimeZone object.
boolean
useDaylightTime()
Returns if this time zone uses daylight savings time.

Methods inherited from class java.util.TimeZone

clone, getAvailableIDs, getAvailableIDs, getDSTSavings, getDefault, getDisplayName, getDisplayName, getDisplayName, getDisplayName, getID, getOffset, getOffset, getRawOffset, getTimeZone, hasSameRules, inDaylightTime, setDefault, setID, setRawOffset, useDaylightTime

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

STANDARD_TIME

public static final int STANDARD_TIME
Constant to indicate that start and end times are specified in standard time, without adjusting for daylight savings.
Field Value:
1

UTC_TIME

public static final int UTC_TIME
Constant to indicate that start and end times are specified in UTC.
Field Value:
2

WALL_TIME

public static final int WALL_TIME
Constant to indicate that start and end times are specified in wall time, adjusting for daylight savings. This is the default.
Field Value:
0

Constructor Details

SimpleTimeZone

public SimpleTimeZone(int rawOffset,
                      String id)
Create a SimpleTimeZone with the given time offset from GMT and without daylight savings.
Parameters:
rawOffset - the time offset from GMT in milliseconds.
id - The identifier of this time zone.

SimpleTimeZone

public SimpleTimeZone(int rawOffset,
                      String id,
                      int startMonth,
                      int startDayOfWeekInMonth,
                      int startDayOfWeek,
                      int startTime,
                      int endMonth,
                      int endDayOfWeekInMonth,
                      int endDayOfWeek,
                      int endTime)
Create a SimpleTimeZone with the given time offset from GMT and with daylight savings. The start/end parameters can have different meaning (replace WEEKDAY with a real day of week). Only the first two meanings were supported by earlier versions of jdk.
day > 0, dayOfWeek = Calendar.WEEKDAY
dayWEEKDAY
day < 0, dayOfWeek = Calendar.WEEKDAY
-dayWEEKDAYend
day > 0, dayOfWeek = 0
day
day > 0, dayOfWeek = -Calendar.WEEKDAY
day
day < 0, dayOfWeek = -Calendar.WEEKDAY
before-day
If you give a non existing month, a day that is zero, or too big, or a dayOfWeek that is too big, the result is undefined. The start rule must have a different month than the end rule. This restriction shouldn't hurt for all possible time zones.
Parameters:
rawOffset - The time offset from GMT in milliseconds.
id - The identifier of this time zone.
startMonth - The start month of daylight savings; use the constants in Calendar.
startDayOfWeekInMonth - A day in month or a day of week number, as described above.
startDayOfWeek - The start rule day of week; see above.
startTime - A time in millis in standard time.
endMonth - The end month of daylight savings; use the constants in Calendar.
endDayOfWeekInMonth - A day in month or a day of week number, as described above.
endDayOfWeek - The end rule day of week; see above.
endTime - A time in millis in standard time.
Throws:
IllegalArgumentException - if parameters are invalid or out of range.

SimpleTimeZone

public SimpleTimeZone(int rawOffset,
                      String id,
                      int startMonth,
                      int startDayOfWeekInMonth,
                      int startDayOfWeek,
                      int startTime,
                      int endMonth,
                      int endDayOfWeekInMonth,
                      int endDayOfWeek,
                      int endTime,
                      int dstSavings)
This constructs a new SimpleTimeZone that supports a daylight savings rule. The parameter are the same as for the constructor above, except there is the additional dstSavaings parameter.
Parameters:
dstSavings - the amount of savings for daylight savings time in milliseconds. This must be positive.
Since:
1.2

SimpleTimeZone

public SimpleTimeZone(int rawOffset,
                      String id,
                      int startMonth,
                      int startDayOfWeekInMonth,
                      int startDayOfWeek,
                      int startTime,
                      int startTimeMode,
                      int endMonth,
                      int endDayOfWeekInMonth,
                      int endDayOfWeek,
                      int endTime,
                      int endTimeMode,
                      int dstSavings)
This constructs a new SimpleTimeZone that supports a daylight savings rule. The parameter are the same as for the constructor above, except there are the additional startTimeMode, endTimeMode, and dstSavings parameters.
Parameters:
startTimeMode - the mode that start times are specified in. One of WALL_TIME, STANDARD_TIME, or UTC_TIME.
endTimeMode - the mode that end times are specified in. One of WALL_TIME, STANDARD_TIME, or UTC_TIME.
dstSavings - the amount of savings for daylight savings time in milliseconds. This must be positive.
Throws:
IllegalArgumentException - if parameters are invalid or out of range.
Since:
1.4

Method Details

equals

public boolean equals(Object o)
Overrides:
equals in interface Object

getDSTSavings

public int getDSTSavings()
Gets the daylight savings offset. This is a positive offset in milliseconds with respect to standard time. Typically this is one hour, but for some time zones this may be half an our.
Overrides:
getDSTSavings in interface TimeZone
Returns:
the daylight savings offset in milliseconds.
Since:
1.2

getOffset

public int getOffset(int era,
                     int year,
                     int month,
                     int day,
                     int dayOfWeek,
                     int millis)
Gets the time zone offset, for current date, modified in case of daylight savings. This is the offset to add to UTC to get the local time. In the standard JDK the results given by this method may result in inaccurate results at the end of February or the beginning of March. To avoid this, you should use Calendar instead: offset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET); This version doesn't suffer this inaccuracy. The arguments don't follow the approach for setting start and end rules. The day must be a positive number and dayOfWeek must be a positive value from Calendar. dayOfWeek is redundant, but must match the other values or an inaccurate result may be returned.
Overrides:
getOffset in interface TimeZone
Parameters:
era - the era of the given date
year - the year of the given date
month - the month of the given date, 0 for January.
day - the day of month
dayOfWeek - the day of week; this must match the other fields.
millis - the millis in the day (in local standard time)
Returns:
the time zone offset in milliseconds.
Throws:
IllegalArgumentException - if arguments are incorrect.

getRawOffset

public int getRawOffset()
Returns the time zone offset to GMT in milliseconds, ignoring day light savings.
Overrides:
getRawOffset in interface TimeZone
Returns:
the time zone offset.

hasSameRules

public boolean hasSameRules(TimeZone other)
Test if the other time zone uses the same rule and only possibly differs in ID. This implementation for this particular class will return true if the other object is a SimpleTimeZone, the raw offsets and useDaylight are identical and if useDaylight is true, also the start and end datas are identical.
Overrides:
hasSameRules in interface TimeZone
Returns:
true if this zone uses the same rule.

hashCode

public int hashCode()
Generates the hashCode for the SimpleDateFormat object. It is the rawOffset, possibly, if useDaylightSavings is true, xored with startYear, startMonth, startDayOfWeekInMonth, ..., endTime.
Overrides:
hashCode in interface Object

inDaylightTime

public boolean inDaylightTime(Date date)
Determines if the given date is in daylight savings time.
Overrides:
inDaylightTime in interface TimeZone
Returns:
true, if it is in daylight savings time, false otherwise.

setDSTSavings

public void setDSTSavings(int dstSavings)
Sets the daylight savings offset. This is a positive offset in milliseconds with respect to standard time.
Parameters:
dstSavings - the daylight savings offset in milliseconds.
Since:
1.2

setEndRule

public void setEndRule(int month,
                       int day,
                       int time)
Sets the daylight savings end rule. You must also set the start rule with setStartRule or the result of getOffset is undefined. For the parameters see the ten-argument constructor above.
Parameters:
month - The end month of daylight savings.
day - A day in month, or a day of week in month.
time - A time in millis in standard time.

setEndRule

public void setEndRule(int month,
                       int day,
                       int dayOfWeek,
                       int time)
Sets the daylight savings end rule. You must also set the start rule with setStartRule or the result of getOffset is undefined. For the parameters see the ten-argument constructor above.
Parameters:
month - The end month of daylight savings.
day - A day in month, or a day of week in month.
dayOfWeek - A day of week, when daylight savings ends.
time - A time in millis in standard time.

setEndRule

public void setEndRule(int month,
                       int day,
                       int dayOfWeek,
                       int time,
                       boolean after)
Sets the daylight savings end rule. You must also set the start rule with setStartRule or the result of getOffset is undefined. For the parameters see the ten-argument constructor above. Note that this API isn't incredibly well specified. It appears that the after flag must override the parameters, since normally, the day and dayofweek can select this. I.e., if day <320 and dayOfWeek <320, on or before mode is chosen. But if after == true, this implementation overrides the signs of the other arguments. And if dayOfWeek == 0, it falls back to the behavior in the other APIs. I guess this should be checked against Sun's implementation.
Parameters:
month - The end month of daylight savings.
day - A day in month, or a day of week in month.
dayOfWeek - A day of week, when daylight savings ends.
time - A time in millis in standard time.
after - If true, day and dayOfWeek specify first day of week on or after day, else first day of week on or before.
Since:
1.2

setRawOffset

public void setRawOffset(int rawOffset)
Sets the standard time zone offset to GMT.
Overrides:
setRawOffset in interface TimeZone
Parameters:
rawOffset - The time offset from GMT in milliseconds.

setStartRule

public void setStartRule(int month,
                         int day,
                         int time)
Sets the daylight savings start rule. You must also set the end rule with setEndRule or the result of getOffset is undefined. For the parameters see the ten-argument constructor above.
Parameters:
month - The month where daylight savings start, zero based. You should use the constants in Calendar.
day - A day of month or day of week in month.
time - The time in milliseconds standard time where daylight savings start.
Since:
1.2

setStartRule

public void setStartRule(int month,
                         int day,
                         int dayOfWeek,
                         int time)
Sets the daylight savings start rule. You must also set the end rule with setEndRule or the result of getOffset is undefined. For the parameters see the ten-argument constructor above.
Parameters:
month - The month where daylight savings start, zero based. You should use the constants in Calendar.
day - A day of month or day of week in month.
dayOfWeek - The day of week where daylight savings start.
time - The time in milliseconds standard time where daylight savings start.
Throws:
IllegalArgumentException - if parameters are out of range.

setStartRule

public void setStartRule(int month,
                         int day,
                         int dayOfWeek,
                         int time,
                         boolean after)
Sets the daylight savings start rule. You must also set the end rule with setEndRule or the result of getOffset is undefined. For the parameters see the ten-argument constructor above. Note that this API isn't incredibly well specified. It appears that the after flag must override the parameters, since normally, the day and dayofweek can select this. I.e., if day <320 and dayOfWeek <320, on or before mode is chosen. But if after == true, this implementation overrides the signs of the other arguments. And if dayOfWeek == 0, it falls back to the behavior in the other APIs. I guess this should be checked against Sun's implementation.
Parameters:
month - The month where daylight savings start, zero based. You should use the constants in Calendar.
day - A day of month or day of week in month.
dayOfWeek - The day of week where daylight savings start.
time - The time in milliseconds standard time where daylight savings start.
after - If true, day and dayOfWeek specify first day of week on or after day, else first day of week on or before.
Since:
1.2

setStartYear

public void setStartYear(int year)
Sets the first year, where daylight savings applies. The daylight savings rule never apply for years in the BC era. Note that this is gregorian calendar specific.
Parameters:
year - the start year.

toString

public String toString()
Returns a string representation of this SimpleTimeZone object.
Overrides:
toString in interface Object
Returns:
a string representation of this SimpleTimeZone object.

useDaylightTime

public boolean useDaylightTime()
Returns if this time zone uses daylight savings time.
Overrides:
useDaylightTime in interface TimeZone
Returns:
true, if we use daylight savings time, false otherwise.

java.util.SimpleTimeZone Copyright (C) 1998, 1999, 2000, 2003, 2004, 2005, 2007 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.