simpletimezone.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 739 行 · 第 1/3 页
JAVA
739 行
// Bail out if we are before the onset of daylight savings time
if (!useDaylight || year < startYear || era != GregorianCalendar.AD) return result;
// Check for southern hemisphere. We assume that the start and end
// month are different.
boolean southern = (startMonth > endMonth);
// Compare the date to the starting and ending rules. For the ending
// rule comparison, we add the dstSavings to the millis passed in to convert
// them from standard to wall time. +1 = date>rule, -1 = date<rule, 0 =
// date==rule.
int startCompare = compareToRule(month, day, dayOfWeek, millis,
startMode, startMonth, startDayOfWeek,
startDay, startTime);
int endCompare = compareToRule(month, day, dayOfWeek, millis + dstSavings,
endMode, endMonth, endDayOfWeek,
endDay, endTime);
// Check for both the northern and southern hemisphere cases. We
// assume that in the northern hemisphere, the start rule is before the
// end rule within the calendar year, and vice versa for the southern
// hemisphere.
if ((!southern && (startCompare >= 0 && endCompare < 0)) ||
(southern && (startCompare >= 0 || endCompare < 0)))
result += dstSavings;
return result;
}
/**
* Compare a given date in the year to a rule. Return 1, 0, or -1, depending
* on whether the date is after, equal to, or before the rule date. The
* millis are compared directly against the ruleMillis, so any
* standard-daylight adjustments must be handled by the caller. Assume that
* no rule references the end of February (e.g., last Sunday in February).
*
* @return 1 if the date is after the rule date, -1 if the date is before
* the rule date, or 0 if the date is equal to the rule date.
*/
private static int compareToRule(int month, int dayOfMonth, int dayOfWeek, int millis,
int ruleMode, int ruleMonth, int ruleDayOfWeek,
int ruleDay, int ruleMillis)
{
if (month < ruleMonth) return -1;
else if (month > ruleMonth) return 1;
int ruleDayOfMonth = 0;
switch (ruleMode)
{
case DOM_MODE:
ruleDayOfMonth = ruleDay;
break;
case DOW_IN_MONTH_MODE:
// In this case ruleDay is the day-of-week-in-month
if (ruleDay > 0)
ruleDayOfMonth = 1 + (ruleDay - 1) * 7 +
(7 + ruleDayOfWeek - (dayOfWeek - dayOfMonth + 1)) % 7;
else // Assume ruleDay < 0 here
{
int monthLen = staticMonthLength[month];
ruleDayOfMonth = monthLen + (ruleDay + 1) * 7 -
(7 + (dayOfWeek + monthLen - dayOfMonth) - ruleDayOfWeek) % 7;
}
break;
case DOW_GE_DOM_MODE:
ruleDayOfMonth = ruleDay +
(49 + ruleDayOfWeek - ruleDay - dayOfWeek + dayOfMonth) % 7;
break;
case DOW_LE_DOM_MODE:
ruleDayOfMonth = ruleDay -
(49 - ruleDayOfWeek + ruleDay + dayOfWeek - dayOfMonth) % 7;
// Note at this point ruleDayOfMonth may be <1, although it will
// be >=1 for well-formed rules.
break;
}
if (dayOfMonth < ruleDayOfMonth) return -1;
else if (dayOfMonth > ruleDayOfMonth) return 1;
if (millis < ruleMillis) return -1;
else if (millis > ruleMillis) return 1;
else return 0;
}
/**
* Overrides TimeZone
* Gets the GMT offset for this time zone.
*/
public int getRawOffset()
{
// The given date will be taken into account while
// we have the historical time zone data in place.
return rawOffset;
}
/**
* Overrides TimeZone
* Sets the base time zone offset to GMT.
* This is the offset to add *to* UTC to get local time.
* Please see TimeZone.setRawOffset for descriptions on the parameter.
*/
public void setRawOffset(int offsetMillis)
{
this.rawOffset = offsetMillis;
}
/**
* Overrides TimeZone
* Queries if this time zone uses Daylight Savings Time.
*/
public boolean useDaylightTime()
{
return useDaylight;
}
/**
* Overrides TimeZone
* Queries if the given date is in Daylight Savings Time.
*/
public boolean inDaylightTime(Date date)
{
GregorianCalendar gc = new GregorianCalendar(this);
gc.setTime(date);
return gc.inDaylightTime();
}
/**
* Overrides Cloneable
*/
public Object clone()
{
return super.clone();
// other fields are bit-copied
}
/**
* Override hashCode.
* Generates the hash code for the SimpleDateFormat object
*/
public synchronized int hashCode()
{
return startMonth ^ startDay ^ startDayOfWeek ^ startTime ^
endMonth ^ endDay ^ endDayOfWeek ^ endTime ^ rawOffset;
}
/**
* Compares the equality of two SimpleTimeZone objects.
*
* @param obj The SimpleTimeZone object to be compared with.
* @return True if the given obj is the same as this SimpleTimeZone
* object; false otherwise.
*/
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (!(obj instanceof SimpleTimeZone))
return false;
SimpleTimeZone that = (SimpleTimeZone) obj;
return this.getID().equals(that.getID()) &&
rawOffset == that.rawOffset &&
useDaylight == that.useDaylight &&
dstSavings == that.dstSavings &&
startMode == that.startMode &&
startMonth == that.startMonth &&
startDay == that.startDay &&
startDayOfWeek == that.startDayOfWeek &&
startTime == that.startTime &&
endMode == that.endMode &&
endMonth == that.endMonth &&
endDay == that.endDay &&
endDayOfWeek == that.endDayOfWeek &&
endTime == that.endTime &&
startYear == that.startYear;
}
/**
* Return a string representation of this time zone.
* @return a string representation of this time zone.
*/
public String toString() {
return getClass().getName() +
"[id=" + getID() +
",offset=" + rawOffset +
",dstSavings=" + dstSavings +
",useDaylight=" + useDaylight +
",startYear=" + startYear +
",startMode=" + startMode +
",startMonth=" + startMonth +
",startDay=" + startDay +
",startDayOfWeek=" + startDayOfWeek +
",startTime=" + startTime +
",endMode=" + endMode +
",endMonth=" + endMonth +
",endDay=" + endDay +
",endDayOfWeek=" + endDayOfWeek +
",endTime=" + endTime + ']';
}
// =======================privates===============================
private int startMonth, startDay, startDayOfWeek, startTime;
private int endMonth, endDay, endDayOfWeek, endTime;
private int startYear;
private int rawOffset;
private boolean useDaylight=false; // indicate if this time zone uses DST
private static final int millisPerHour = 60*60*1000;
// WARNING: assumes that no rule is measured from the end of February,
// since we don't handle leap years. Could handle assuming always
// Gregorian, since we know they didn't have daylight time when
// Gregorian calendar started.
// monthLength was non-static 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 final byte monthLength[] = staticMonthLength;
private final static byte staticMonthLength[] = {31,28,31,30,31,30,31,31,30,31,30,31};
/**
* Variables specifying the mode of the start and end rules.
*/
private int startMode, endMode; // fields new in JDK 1.1.4
/**
* A positive value indicating the amount of time saved during DST in ms.
* Typically one hour; sometimes 30 minutes.
*/
private int dstSavings; // field new in JDK 1.1.4
/**
* Constants specifying values of startMode and endMode.
*/
private static final int DOM_MODE = 1; // Exact day of month, "Mar 1"
private static final int DOW_IN_MONTH_MODE = 2; // Day of week in month, "lastSun"
private static final int DOW_GE_DOM_MODE = 3; // Day of week after day of month, "Sun>=15"
private static final int DOW_LE_DOM_MODE = 4; // Day of week before day of month, "Sun<=21"
// Proclaim compatibility with 1.1
static final long serialVersionUID = -403250971215465050L;
// the internal serial version which says which version was written
// - 0 (default) for version up to JDK 1.1.3
// - 1 for version from JDK 1.1.4, which includes 3 new fields
static final int currentSerialVersion = 1;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?