SimpleTimeZonepublic 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 . |
Fields Summary |
---|
private int | startMonthThe 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 | startDayThis 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 | startDayOfWeekThe 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 | startTimeThe 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 | startTimeModeThe format of startTime, either WALL_TIME, STANDARD_TIME, or UTC_TIME. | private int | endMonthThe 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 | endDayThis 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 | endDayOfWeekThe 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 | endTimeThe 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 | endTimeModeThe format of endTime, either WALL_TIME ,
STANDARD_TIME , or UTC_TIME . | private int | startYearThe 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 | rawOffsetThe 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 | useDaylightA 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[] | monthLengthThis 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 | startModeVariables 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 | endModeVariables 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 | dstSavingsA 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 | cacheYearCache 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_MODEConstants 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_TIMEConstant 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_TIMEConstant for a mode of start or end time specified as standard time. | public static final int | UTC_TIMEConstant 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 | serialVersionOnStreamThe 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.
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)
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)
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}.
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.Object | clone()Returns a clone of this SimpleTimeZone instance.
return super.clone();
| private void | decodeEndRule()Decode the end rule and validate the parameters. This method is exactly
analogous to 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 void | decodeRules()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 void | decodeStartRule()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 boolean | equals(java.lang.Object obj)Compares the equality of two SimpleTimeZone objects.
if (this == obj) {
return true;
}
if (!(obj instanceof SimpleTimeZone)) {
return false;
}
SimpleTimeZone that = (SimpleTimeZone) obj;
return getID().equals(that.getID()) &&
hasSameRules(that);
| public int | getDSTSavings()Returns the amount of time in milliseconds that the clock is
advanced during daylight saving time.
if (useDaylight) {
return dstSavings;
}
return 0;
| private long | getEnd(sun.util.calendar.BaseCalendar cal, sun.util.calendar.BaseCalendar$Date cdate, int year)
int time = endTime;
if (startTimeMode != UTC_TIME) {
time -= rawOffset;
}
if (startTimeMode == WALL_TIME) {
time -= dstSavings;
}
return getTransition(cal, cdate, endMode, year, endMonth, endDay,
endDayOfWeek, time);
| public int | getOffset(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.
return getOffsets(date, null);
| public int | getOffset(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.
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 int | getOffset(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;
| int | getOffsets(long date, int[] offsets)
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 int | getRawOffset()Gets the GMT offset for this time zone.
// The given date will be taken into account while
// we have the historical time zone data in place.
return rawOffset;
| private long | getStart(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 long | getTransition(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 boolean | hasSameRules(java.util.TimeZone other)Returns true if this zone has the same rules and offset as another zone.
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 int | hashCode()Generates the hash code for the SimpleDateFormat object.
return startMonth ^ startDay ^ startDayOfWeek ^ startTime ^
endMonth ^ endDay ^ endDayOfWeek ^ endTime ^ rawOffset;
| public boolean | inDaylightTime(java.util.Date date)Queries if the given date is in daylight saving time.
return (getOffset(date.getTime()) != rawOffset);
| private synchronized void | invalidateCache()
cacheYear = startYear - 1;
cacheStart = cacheEnd = 0;
| private void | makeRulesCompatible()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 void | readObject(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 void | setDSTSavings(int millisSavedDuringDST)Sets the amount of time in milliseconds that the clock is advanced
during daylight saving time.
if (millisSavedDuringDST <= 0) {
throw new IllegalArgumentException("Illegal daylight saving value: "
+ millisSavedDuringDST);
}
dstSavings = millisSavedDuringDST;
| public void | setEndRule(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)
setEndRule(endMonth, endDay, 0, endTime);
| public void | setEndRule(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.
if (after) {
setEndRule(endMonth, endDay, -endDayOfWeek, endTime);
} else {
setEndRule(endMonth, -endDay, -endDayOfWeek, endTime);
}
| public void | setEndRule(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);
this.endMonth = endMonth;
this.endDay = endDay;
this.endDayOfWeek = endDayOfWeek;
this.endTime = endTime;
this.endTimeMode = WALL_TIME;
decodeEndRule();
invalidateCache();
| public void | setRawOffset(int offsetMillis)Sets the base time zone offset to GMT.
This is the offset to add to UTC to get local time.
this.rawOffset = offsetMillis;
| public void | setStartRule(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);
this.startMonth = startMonth;
this.startDay = startDay;
this.startDayOfWeek = startDayOfWeek;
this.startTime = startTime;
startTimeMode = WALL_TIME;
decodeStartRule();
invalidateCache();
| public void | setStartRule(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)
setStartRule(startMonth, startDay, 0, startTime);
| public void | setStartRule(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.
// 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 void | setStartYear(int year)Sets the daylight saving time starting year.
startYear = year;
invalidateCache();
| public java.lang.String | toString()Returns 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 void | unpackRules(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 void | unpackTimes(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 boolean | useDaylightTime()Queries if this time zone uses daylight saving time.
return useDaylight;
| private void | writeObject(java.io.ObjectOutputStream stream)Save the state of this object to a stream (i.e., serialize it).
// 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);
|
|