📄 simpletimezone.java
字号:
* @see #UTC_TIME * * @since 1.4 */ 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) { // Workaround fix for 4278609 (JCK failure) if (endMonth == Calendar.JANUARY && endDay == 1 && endDayOfWeek == 0 && endTime == 0 && endTimeMode == WALL_TIME && dstSavings > 0) { endMonth = Calendar.DECEMBER; endDay = 31; endTime = (24 * 60 * 60 * 1000) - dstSavings; endTimeMode = STANDARD_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); } } /** * Sets the daylight saving time starting year. * * @param year The daylight saving starting year. */ public void setStartYear(int year) { startYear = year; } /** * 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: * <pre><code>setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);</code></pre> * * @param startMonth The daylight saving time starting month. Month is * a {@link Calendar#MONTH MONTH} field * value (0-based. e.g., 0 for January). * @param startDay The day of the month on which the daylight saving time starts. * See the class description for the special cases of this parameter. * @param startDayOfWeek The daylight saving time starting day-of-week. * See the class description for the special cases of this parameter. * @param startTime The daylight saving time starting time in local wall clock * time, which is local standard time in this case. * @exception IllegalArgumentException if the <code>startMonth</code>, <code>startDay</code>, * <code>startDayOfWeek</code>, or <code>startTime</code> parameters are out of range */ public void setStartRule(int startMonth, int startDay, int startDayOfWeek, int startTime) { this.startMonth = startMonth; this.startDay = startDay; this.startDayOfWeek = startDayOfWeek; this.startTime = startTime; startTimeMode = WALL_TIME; // useDaylight = true; // Set by decodeRules decodeStartRule(); } /** * Sets the daylight saving time start rule to a fixed date within a month. * This method is equivalent to: * <pre><code>setStartRule(startMonth, startDay, 0, startTime)</code></pre> * * @param startMonth The daylight saving time starting month. Month is * a {@link Calendar#MONTH MONTH} field * value (0-based. e.g., 0 for January). * @param startDay The day of the month on which the daylight saving time starts. * @param startTime 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. * @exception IllegalArgumentException if the <code>startMonth</code>, * <code>startDayOfMonth</code>, or <code>startTime</code> parameters are out of range * @since 1.2 */ public void setStartRule(int startMonth, int startDay, int startTime) { setStartRule(startMonth, startDay, 0, startTime); } /** * 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. * * @param startMonth The daylight saving time starting month. Month is * a {@link Calendar#MONTH MONTH} field * value (0-based. e.g., 0 for January). * @param startDay The day of the month on which the daylight saving time starts. * @param startDayOfWeek The daylight saving time starting day-of-week. * @param startTime The daylight saving time starting time in local wall clock * time, which is local standard time in this case. * @param after If true, this rule selects the first <code>dayOfWeek</code> on or * <em>after</em> <code>dayOfMonth</code>. If false, this rule * selects the last <code>dayOfWeek</code> on or <em>before</em> * <code>dayOfMonth</code>. * @exception IllegalArgumentException if the <code>startMonth</code>, <code>startDay</code>, * <code>startDayOfWeek</code>, or <code>startTime</code> parameters are out of range * @since 1.2 */ public void setStartRule(int startMonth, int startDay, int startDayOfWeek, int startTime, boolean after) { // TODO: this method doesn't check the initial values of dayOfMonth or dsyOfWeek. if (after) { setStartRule(startMonth, startDay, -startDayOfWeek, startTime); } else { setStartRule(startMonth, -startDay, -startDayOfWeek, startTime); } } /** * 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: * <code>setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);</code> * * @param endMonth The daylight saving time ending month. Month is * a {@link Calendar#MONTH MONTH} field * value (0-based. e.g., 9 for October). * @param endDay The day of the month on which the daylight saving time ends. * See the class description for the special cases of this parameter. * @param endDayOfWeek The daylight saving time ending day-of-week. * See the class description for the special cases of this parameter. * @param endTime The daylight saving ending time in local wall clock time, * (in milliseconds within the day) which is local daylight * time in this case. * @exception IllegalArgumentException if the <code>endMonth</code>, <code>endDay</code>, * <code>endDayOfWeek</code>, or <code>endTime</code> parameters are out of range */ public void setEndRule(int endMonth, int endDay, int endDayOfWeek, int endTime) { this.endMonth = endMonth; this.endDay = endDay; this.endDayOfWeek = endDayOfWeek; this.endTime = endTime; this.endTimeMode = WALL_TIME; // useDaylight = true; // Set by decodeRules decodeEndRule(); } /** * Sets the daylight saving time end rule to a fixed date within a month. * This method is equivalent to: * <pre><code>setEndRule(endMonth, endDay, 0, endTime)</code></pre> * * @param endMonth The daylight saving time ending month. Month is * a {@link Calendar#MONTH MONTH} field * value (0-based. e.g., 9 for October). * @param endDay The day of the month on which the daylight saving time ends. * @param endTime The daylight saving ending time in local wall clock time, * (in milliseconds within the day) which is local daylight * time in this case. * @exception IllegalArgumentException the <code>endMonth</code>, <code>endDay</code>, * or <code>endTime</code> parameters are out of range * @since 1.2 */ public void setEndRule(int endMonth, int endDay, int endTime) { setEndRule(endMonth, endDay, 0, endTime); } /** * 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. * * @param endMonth The daylight saving time ending month. Month is * a {@link Calendar#MONTH MONTH} field * value (0-based. e.g., 9 for October). * @param endDay The day of the month on which the daylight saving time ends. * @param endDayOfWeek The daylight saving time ending day-of-week. * @param endTime The daylight saving ending time in local wall clock time, * (in milliseconds within the day) which is local daylight * time in this case. * @param after If true, this rule selects the first <code>endDayOfWeek</code> on * or <em>after</em> <code>endDay</code>. If false, this rule * selects the last <code>endDayOfWeek</code> on or before * <code>endDay</code> of the month. * @exception IllegalArgumentException the <code>endMonth</code>, <code>endDay</code>, * <code>endDayOfWeek</code>, or <code>endTime</code> parameters are out of range * @since 1.2 */ public void setEndRule(int endMonth, int endDay, int endDayOfWeek, int endTime, boolean after) { if (after) { setEndRule(endMonth, endDay, -endDayOfWeek, endTime); } else { setEndRule(endMonth, -endDay, -endDayOfWeek, endTime); } } /** * 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. * * @param date the time at which the time zone offset is found * @return the amount of time in milliseconds to add to UTC to get * local time. * @since 1.4 */ public int getOffset(long date) { return getOffsets(date, null); } /** * @see TimeZone#getOffsets */ int getOffsets(long date, int[] offsets) { int offset; calc: { if (!useDaylight) { offset = rawOffset; break calc; } // get standard local time CalendarDate cdate = Gregorian.getCalendarDate(date + rawOffset); int year = cdate.getYear(); // if it's BC, assume no DST. if (year <= 0) { offset = rawOffset; break calc; } int month = cdate.getMonth(); int monthLength = staticMonthLength[month]; int prevMonthLength = staticMonthLength[(month + Calendar.DECEMBER) % 12]; if (Gregorian.isLeapYear(year)) { if (month == Calendar.FEBRUARY) { ++monthLength; } else if (month == Calendar.MARCH) { ++prevMonthLength; } } offset = getOffset(GregorianCalendar.AD, year, month, cdate.getDate(), cdate.getDayOfWeek(), cdate.getTimeOfDay(), monthLength, prevMonthLength); } if (offsets != null) { offsets[0] = rawOffset; offsets[1] = offset - rawOffset; } return offset; } /** * 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 <code>GregorianCalendar</code>. * * <p><em>Note: In general, clients should use * <code>Calendar.get(ZONE_OFFSET) + Calendar.get(DST_OFFSET)</code> * instead of calling this method.</em> * * @param era The era of the given date. * @param year The year in the given date. * @param month The month in the given date. Month is 0-based. e.g., * 0 for January. * @param day The day-in-month of the given date. * @param dayOfWeek The day-of-week of the given date. * @param millis The milliseconds in day in <em>standard</em> local time. * @return The milliseconds to add to UTC to get local time. * @exception IllegalArgumentException the <code>era</code>, * <code>month</code>, <code>day</code>, <code>dayOfWeek</code>, * or <code>millis</code> parameters are out of range */ public int getOffset(int era, int year, int month, int day, int dayOfWeek, int millis) { // Check the month before indexing into staticMonthLength. This // duplicates the test that occurs in the 7-argument getOffset(), // however, this is unavoidable. We don't mind because this method, in // fact, should not be called; internal code should always call the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -