* README: Remove.
[official-gcc.git] / libjava / java / util / SimpleTimeZone.java
blob31b85e189725c620af9ffab51413085f0f5f0989
1 /* java.util.SimpleTimeZone
2 Copyright (C) 1998, 1999, 2000, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.util;
41 /**
42 * This class represents a simple time zone offset and handles
43 * daylight savings. It can only handle one daylight savings rule, so
44 * it can't represent historical changes.
46 * This object is tightly bound to the Gregorian calendar. It assumes
47 * a regular seven days week, and the month lengths are that of the
48 * Gregorian Calendar. It can only handle daylight savings for years
49 * lying in the AD era.
51 * @see Calendar
52 * @see GregorianCalender
53 * @author Jochen Hoenicke
55 public class SimpleTimeZone extends TimeZone
57 /**
58 * The raw time zone offset in milliseconds to GMT, ignoring
59 * daylight savings.
60 * @serial
62 private int rawOffset;
64 /**
65 * True, if this timezone uses daylight savings, false otherwise.
66 * @serial
68 private boolean useDaylight;
70 /**
71 * The daylight savings offset. This is a positive offset in
72 * milliseconds with respect to standard time. Typically this
73 * is one hour, but for some time zones this may be half an our.
74 * @serial
75 * @since JDK1.1.4
77 private int dstSavings = 60 * 60 * 1000;
79 /**
80 * The first year, in which daylight savings rules applies.
81 * @serial
83 private int startYear;
85 private static final int DOM_MODE = 1;
86 private static final int DOW_IN_MONTH_MODE = 2;
87 private static final int DOW_GE_DOM_MODE = 3;
88 private static final int DOW_LE_DOM_MODE = 4;
90 /**
91 * The mode of the start rule. This takes one of the following values:
92 * <dl>
93 * <dt>DOM_MODE (1)</dt>
94 * <dd> startDay contains the day in month of the start date,
95 * startDayOfWeek is unused. </dd>
96 * <dt>DOW_IN_MONTH_MODE (2)</dt>
97 * <dd> The startDay gives the day of week in month, and
98 * startDayOfWeek the day of week. For example startDay=2 and
99 * startDayOfWeek=Calender.SUNDAY specifies that the change is on
100 * the second sunday in that month. You must make sure, that this
101 * day always exists (ie. don't specify the 5th sunday).
102 * </dd>
103 * <dt>DOW_GE_DOM_MODE (3)</dt>
104 * <dd> The start is on the first startDayOfWeek on or after
105 * startDay. For example startDay=13 and
106 * startDayOfWeek=Calendar.FRIDAY specifies that the daylight
107 * savings start on the first FRIDAY on or after the 13th of that
108 * Month. Make sure that the change is always in the given month, or
109 * the result is undefined.
110 * </dd>
111 * <dt>DOW_LE_DOM_MONTH (4)</dt>
112 * <dd> The start is on the first startDayOfWeek on or before the
113 * startDay. Make sure that the change is always in the given
114 * month, or the result is undefined.
115 </dd>
116 * </dl>
117 * @serial */
118 private int startMode;
121 * The month in which daylight savings start. This is one of the
122 * constants Calendar.JANUARY, ..., Calendar.DECEMBER.
123 * @serial
125 private int startMonth;
128 * This variable can have different meanings. See startMode for details
129 * @see #startMode;
130 * @serial
132 private int startDay;
135 * This variable specifies the day of week the change takes place. If
136 * startMode == DOM_MODE, this is undefined.
137 * @serial
138 * @see #startMode;
140 private int startDayOfWeek;
143 * This variable specifies the time of change to daylight savings.
144 * This time is given in milliseconds after midnight local
145 * standard time.
146 * @serial
148 private int startTime;
151 * The month in which daylight savings ends. This is one of the
152 * constants Calendar.JANUARY, ..., Calendar.DECEMBER.
153 * @serial
155 private int endMonth;
158 * This variable gives the mode for the end of daylight savings rule.
159 * It can take the same values as startMode.
160 * @serial
161 * @see #startMode
163 private int endMode;
166 * This variable can have different meanings. See startMode for details
167 * @serial
168 * @see #startMode;
170 private int endDay;
173 * This variable specifies the day of week the change takes place. If
174 * endMode == DOM_MODE, this is undefined.
175 * @serial
176 * @see #startMode;
178 private int endDayOfWeek;
181 * This variable specifies the time of change back to standard time.
182 * This time is given in milliseconds after midnight local
183 * standard time.
184 * @serial
186 private int endTime;
189 * This variable points to a deprecated array from JDK 1.1. It is
190 * ignored in JDK 1.2 but streamed out for compatibility with JDK 1.1.
191 * The array contains the lengths of the months in the year and is
192 * assigned from a private static final field to avoid allocating
193 * the array for every instance of the object.
194 * Note that static final fields are not serialized.
195 * @serial
197 private byte[] monthLength = monthArr;
198 private static final byte[] monthArr =
199 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
202 * The version of the serialized data on the stream.
203 * <dl>
204 * <dt>0 or not present on stream</dt>
205 * <dd> JDK 1.1.3 or earlier, only provides this fields:
206 * rawOffset, startDay, startDayOfWeek, startMonth, startTime,
207 * startYear, endDay, endDayOfWeek, endMonth, endTime
208 * </dd>
209 * <dd> JDK 1.1.4 or later. This includes three new fields, namely
210 * startMode, endMode and dstSavings. And there is a optional section
211 * as described in writeObject.
212 * </dd>
214 * XXX - JDK 1.2 Beta 4 docu states 1.1.4, but my 1.1.5 has the old
215 * version.
217 * When streaming out this class it is always written in the latest
218 * version.
219 * @serial
220 * @since JDK1.1.4
222 private int serialVersionOnStream = 1;
224 private static final long serialVersionUID = -403250971215465050L;
227 * Create a <code>SimpleTimeZone</code> with the given time offset
228 * from GMT and without daylight savings.
229 * @param rawOffset the time offset from GMT in milliseconds.
230 * @param id The identifier of this time zone.
232 public SimpleTimeZone(int rawOffset, String id)
234 this.rawOffset = rawOffset;
235 setID(id);
236 useDaylight = false;
237 startYear = 0;
241 * Create a <code>SimpleTimeZone</code> with the given time offset
242 * from GMT and with daylight savings. The start/end parameters
243 * can have different meaning (replace WEEKDAY with a real day of
244 * week). Only the first two meanings were supported by earlier
245 * versions of jdk.
247 * <dl>
248 * <dt><code>day &gt; 0, dayOfWeek = Calendar.WEEKDAY</code></dt>
249 * <dd>The start/end of daylight savings is on the <code>day</code>-th
250 * <code>WEEKDAY</code> in the given month. </dd>
251 * <dt><code>day &lt; 0, dayOfWeek = Calendar.WEEKDAY</code></dt>
252 * <dd>The start/end of daylight savings is on the <code>-day</code>-th
253 * <code>WEEKDAY</code> counted from the <i>end</i> of the month. </dd>
254 * <dt><code>day &gt; 0, dayOfWeek = 0</code></dt>
255 * <dd>The start/end of daylight is on the <code>day</code>-th day of
256 * the month. </dd>
257 * <dt><code>day &gt; 0, dayOfWeek = -Calendar.WEEKDAY</code></dt>
258 * <dd>The start/end of daylight is on the first WEEKDAY on or after
259 * the <code>day</code>-th day of the month. You must make sure that
260 * this day lies in the same month. </dd>
261 * <dt><code>day &lt; 0, dayOfWeek = -Calendar.WEEKDAY</code></dt>
262 * <dd>The start/end of daylight is on the first WEEKDAY on or
263 * <i>before</i> the <code>-day</code>-th day of the month. You
264 * must make sure that this day lies in the same month. </dd>
265 * </dl>
267 * If you give a non existing month, a day that is zero, or too big,
268 * or a dayOfWeek that is too big, the result is undefined.
270 * The start rule must have a different month than the end rule.
271 * This restriction shouldn't hurt for all possible time zones.
273 * @param rawOffset The time offset from GMT in milliseconds.
274 * @param id The identifier of this time zone.
275 * @param startMonth The start month of daylight savings; use the
276 * constants in Calendar.
277 * @param startday A day in month or a day of week number, as
278 * described above.
279 * @param startDayOfWeek The start rule day of week; see above.
280 * @param startTime A time in millis in standard time.
281 * @param endMonth The end month of daylight savings; use the
282 * constants in Calendar.
283 * @param endday A day in month or a day of week number, as
284 * described above.
285 * @param endDayOfWeek The end rule day of week; see above.
286 * @param endTime A time in millis in standard time. */
287 public SimpleTimeZone(int rawOffset, String id,
288 int startMonth, int startDayOfWeekInMonth,
289 int startDayOfWeek, int startTime,
290 int endMonth, int endDayOfWeekInMonth,
291 int endDayOfWeek, int endTime)
293 this.rawOffset = rawOffset;
294 setID(id);
295 useDaylight = true;
297 setStartRule(startMonth, startDayOfWeekInMonth,
298 startDayOfWeek, startTime);
299 setEndRule(endMonth, endDayOfWeekInMonth, endDayOfWeek, endTime);
300 if (startMonth == endMonth)
301 throw new IllegalArgumentException
302 ("startMonth and endMonth must be different");
303 this.startYear = 0;
307 * This constructs a new SimpleTimeZone that supports a daylight savings
308 * rule. The parameter are the same as for the constructor above, except
309 * there is the additional dstSavaings parameter.
311 * @param dstSavings the amount of savings for daylight savings
312 * time in milliseconds. This must be positive.
314 public SimpleTimeZone(int rawOffset, String id,
315 int startMonth, int startDayOfWeekInMonth,
316 int startDayOfWeek, int startTime,
317 int endMonth, int endDayOfWeekInMonth,
318 int endDayOfWeek, int endTime, int dstSavings)
320 this(rawOffset, id,
321 startMonth, startDayOfWeekInMonth, startDayOfWeek, startTime,
322 endMonth, endDayOfWeekInMonth, endDayOfWeek, endTime);
324 this.dstSavings = dstSavings;
328 * Sets the first year, where daylight savings applies. The daylight
329 * savings rule never apply for years in the BC era. Note that this
330 * is gregorian calendar specific.
331 * @param year the start year.
333 public void setStartYear(int year)
335 startYear = year;
336 useDaylight = true;
340 * Checks if the month, day, dayOfWeek arguments are in range and
341 * returns the mode of the rule.
342 * @param month the month parameter as in the constructor
343 * @param day the day parameter as in the constructor
344 * @param dayOfWeek the day of week parameter as in the constructor
345 * @return the mode of this rule see startMode.
346 * @exception IllegalArgumentException if parameters are out of range.
347 * @see #SimpleTimeZone(int, String, int, int, int, int, int, int, int, int)
348 * @see #startMode
350 private int checkRule(int month, int day, int dayOfWeek)
352 int daysInMonth = getDaysInMonth(month, 1);
353 if (dayOfWeek == 0)
355 if (day <= 0 || day > daysInMonth)
356 throw new IllegalArgumentException("day out of range");
357 return DOM_MODE;
359 else if (dayOfWeek > 0)
361 if (Math.abs(day) > (daysInMonth + 6) / 7)
362 throw new IllegalArgumentException("dayOfWeekInMonth out of range");
363 if (dayOfWeek > Calendar.SATURDAY)
364 throw new IllegalArgumentException("dayOfWeek out of range");
365 return DOW_IN_MONTH_MODE;
367 else
369 if (day == 0 || Math.abs(day) > daysInMonth)
370 throw new IllegalArgumentException("day out of range");
371 if (dayOfWeek < -Calendar.SATURDAY)
372 throw new IllegalArgumentException("dayOfWeek out of range");
373 if (day < 0)
374 return DOW_LE_DOM_MODE;
375 else
376 return DOW_GE_DOM_MODE;
382 * Sets the daylight savings start rule. You must also set the
383 * end rule with <code>setEndRule</code> or the result of
384 * getOffset is undefined. For the parameters see the ten-argument
385 * constructor above.
387 * @param month The month where daylight savings start, zero
388 * based. You should use the constants in Calendar.
389 * @param day A day of month or day of week in month.
390 * @param dayOfWeek The day of week where daylight savings start.
391 * @param time The time in milliseconds standard time where daylight
392 * savings start.
393 * @see SimpleTimeZone
395 public void setStartRule(int month, int day, int dayOfWeek, int time)
397 this.startMode = checkRule(month, day, dayOfWeek);
398 this.startMonth = month;
399 // FIXME: XXX: JDK 1.2 allows negative values and has 2 new variations
400 // of this method.
401 this.startDay = Math.abs(day);
402 this.startDayOfWeek = Math.abs(dayOfWeek);
403 this.startTime = time;
404 useDaylight = true;
408 * Sets the daylight savings end rule. You must also set the
409 * start rule with <code>setStartRule</code> or the result of
410 * getOffset is undefined. For the parameters see the ten-argument
411 * constructor above.
413 * @param rawOffset The time offset from GMT.
414 * @param id The identifier of this time zone.
415 * @param month The end month of daylight savings.
416 * @param day A day in month, or a day of week in month.
417 * @param dayOfWeek A day of week, when daylight savings ends.
418 * @param time A time in millis in standard time.
419 * @see #setStartRule
421 public void setEndRule(int month, int day, int dayOfWeek, int time)
423 this.endMode = checkRule(month, day, dayOfWeek);
424 this.endMonth = month;
425 // FIXME: XXX: JDK 1.2 allows negative values and has 2 new variations
426 // of this method.
427 this.endDay = Math.abs(day);
428 this.endDayOfWeek = Math.abs(dayOfWeek);
429 this.endTime = time;
430 useDaylight = true;
434 * Gets the time zone offset, for current date, modified in case of
435 * daylight savings. This is the offset to add to UTC to get the local
436 * time.
438 * In the standard JDK the results given by this method may result in
439 * inaccurate results at the end of February or the beginning of March.
440 * To avoid this, you should use Calendar instead:
441 * <code>offset = cal.get(Calendar.ZONE_OFFSET)
442 * + cal.get(Calendar.DST_OFFSET);</code>
444 * You could also use in
446 * This version doesn't suffer this inaccuracy.
448 * @param era the era of the given date
449 * @param year the year of the given date
450 * @param month the month of the given date, 0 for January.
451 * @param day the day of month
452 * @param dayOfWeek the day of week; this must be matching the
453 * other fields.
454 * @param millis the millis in the day (in local standard time)
455 * @return the time zone offset in milliseconds. */
456 public int getOffset(int era, int year, int month,
457 int day, int dayOfWeek, int millis)
459 // This method is called by Calendar, so we mustn't use that class.
460 int daylightSavings = 0;
461 if (useDaylight && era == GregorianCalendar.AD && year >= startYear)
463 // This does only work for Gregorian calendars :-(
464 // This is mainly because setStartYear doesn't take an era.
466 boolean afterStart = !isBefore(year, month, day, dayOfWeek, millis,
467 startMode, startMonth,
468 startDay, startDayOfWeek, startTime);
469 boolean beforeEnd = isBefore(year, month, day, dayOfWeek, millis,
470 endMode, endMonth,
471 endDay, endDayOfWeek, endTime);
473 if (startMonth < endMonth)
475 // use daylight savings, if the date is after the start of
476 // savings, and before the end of savings.
477 daylightSavings = afterStart && beforeEnd ? dstSavings : 0;
479 else
481 // use daylight savings, if the date is before the end of
482 // savings, or after the start of savings.
483 daylightSavings = beforeEnd || afterStart ? dstSavings : 0;
486 return rawOffset + daylightSavings;
490 * Returns the time zone offset to GMT in milliseconds, ignoring
491 * day light savings.
492 * @return the time zone offset.
494 public int getRawOffset()
496 return rawOffset;
500 * Sets the standard time zone offset to GMT.
501 * @param rawOffset The time offset from GMT in milliseconds.
503 public void setRawOffset(int rawOffset)
505 this.rawOffset = rawOffset;
509 * Gets the daylight savings offset. This is a positive offset in
510 * milliseconds with respect to standard time. Typically this
511 * is one hour, but for some time zones this may be half an our.
512 * @return the daylight savings offset in milliseconds.
514 * @since 1.2
516 public int getDSTSavings()
518 return dstSavings;
522 * Sets the daylight savings offset. This is a positive offset in
523 * milliseconds with respect to standard time.
525 * @param dstSavings the daylight savings offset in milliseconds.
527 * @since 1.2
529 public void setDSTSavings(int dstSavings)
531 if (dstSavings <= 0)
532 throw new IllegalArgumentException("illegal value for dstSavings");
534 this.dstSavings = dstSavings;
538 * Returns if this time zone uses daylight savings time.
539 * @return true, if we use daylight savings time, false otherwise.
541 public boolean useDaylightTime()
543 return useDaylight;
547 * Returns the number of days in the given month. It does always
548 * use the Gregorian leap year rule.
549 * @param month The month, zero based; use one of the Calendar constants.
550 * @param year The year.
552 private int getDaysInMonth(int month, int year)
554 // Most of this is copied from GregorianCalendar.getActualMaximum()
555 if (month == Calendar.FEBRUARY)
557 return ((year & 3) == 0 && (year % 100 != 0 || year % 400 == 0))
558 ? 29 : 28;
560 else if (month < Calendar.AUGUST)
561 return 31 - (month & 1);
562 else
563 return 30 + (month & 1);
567 * Checks if the date given in calXXXX, is before the change between
568 * dst and standard time.
569 * @param calYear the year of the date to check (for leap day cheking).
570 * @param calMonth the month of the date to check.
571 * @param calDay the day of month of the date to check.
572 * @param calDayOfWeek the day of week of the date to check.
573 * @param calMillis the millis of day of the date to check (standard time).
574 * @param mode the change mode; same semantic as startMode.
575 * @param month the change month; same semantic as startMonth.
576 * @param day the change day; same semantic as startDay.
577 * @param dayOfWeek the change day of week;
578 * @param millis the change time in millis since midnight standard time.
579 * same semantic as startDayOfWeek.
580 * @return true, if cal is before the change, false if cal is on
581 * or after the change.
583 private boolean isBefore(int calYear,
584 int calMonth, int calDayOfMonth, int calDayOfWeek,
585 int calMillis, int mode, int month,
586 int day, int dayOfWeek, int millis)
589 // This method is called by Calendar, so we mustn't use that class.
590 // We have to do all calculations by hand.
592 // check the months:
594 // XXX - this is not correct:
595 // for the DOW_GE_DOM and DOW_LE_DOM modes the change date may
596 // be in a different month.
597 if (calMonth != month)
598 return calMonth < month;
600 // check the day:
601 switch (mode)
603 case DOM_MODE:
604 if (calDayOfMonth != day)
605 return calDayOfMonth < day;
606 break;
607 case DOW_IN_MONTH_MODE:
609 // This computes the day of month of the day of type
610 // "dayOfWeek" that lies in the same (sunday based) week as cal.
611 calDayOfMonth += (dayOfWeek - calDayOfWeek);
613 // Now we convert it to 7 based number (to get a one based offset
614 // after dividing by 7). If we count from the end of the
615 // month, we get want a -7 based number counting the days from
616 // the end:
618 if (day < 0)
619 calDayOfMonth -= getDaysInMonth(calMonth, calYear) + 7;
620 else
621 calDayOfMonth += 6;
623 // day > 0 day < 0
624 // S M T W T F S S M T W T F S
625 // 7 8 9 10 11 12 -36-35-34-33-32-31
626 // 13 14 15 16 17 18 19 -30-29-28-27-26-25-24
627 // 20 21 22 23 24 25 26 -23-22-21-20-19-18-17
628 // 27 28 29 30 31 32 33 -16-15-14-13-12-11-10
629 // 34 35 36 -9 -8 -7
631 // Now we calculate the day of week in month:
632 int week = calDayOfMonth / 7;
633 // day > 0 day < 0
634 // S M T W T F S S M T W T F S
635 // 1 1 1 1 1 1 -5 -5 -4 -4 -4 -4
636 // 1 2 2 2 2 2 2 -4 -4 -4 -3 -3 -3 -3
637 // 2 3 3 3 3 3 3 -3 -3 -3 -2 -2 -2 -2
638 // 3 4 4 4 4 4 4 -2 -2 -2 -1 -1 -1 -1
639 // 4 5 5 -1 -1 -1
641 if (week != day)
642 return week < day;
644 if (calDayOfWeek != dayOfWeek)
645 return calDayOfWeek < dayOfWeek;
647 // daylight savings starts/ends on the given day.
648 break;
651 case DOW_LE_DOM_MODE:
652 // The greatest sunday before or equal December, 12
653 // is the same as smallest sunday after or equal December, 6.
654 day -= 6;
656 case DOW_GE_DOM_MODE:
658 // Calculate the day of month of the day of type
659 // "dayOfWeek" that lies before (or on) the given date.
660 calDayOfMonth -= (calDayOfWeek < dayOfWeek ? 7 : 0)
661 + calDayOfWeek - dayOfWeek;
662 if (calDayOfMonth < day)
663 return true;
664 if (calDayOfWeek != dayOfWeek || calDayOfMonth >= day + 7)
665 return false;
666 // now we have the same day
667 break;
669 // the millis decides:
670 return (calMillis < millis);
674 * Determines if the given date is in daylight savings time.
675 * @return true, if it is in daylight savings time, false otherwise.
677 public boolean inDaylightTime(Date date)
679 Calendar cal = Calendar.getInstance(this);
680 cal.setTime(date);
681 return (cal.get(Calendar.DST_OFFSET) != 0);
685 * Generates the hashCode for the SimpleDateFormat object. It is
686 * the rawOffset, possibly, if useDaylightSavings is true, xored
687 * with startYear, startMonth, startDayOfWeekInMonth, ..., endTime.
689 public synchronized int hashCode()
691 return rawOffset ^
692 (useDaylight ?
693 startMonth ^ startDay ^ startDayOfWeek ^ startTime
694 ^ endMonth ^ endDay ^ endDayOfWeek ^ endTime : 0);
697 public synchronized boolean equals(Object o)
699 if (this == o)
700 return true;
701 if (!(o instanceof SimpleTimeZone))
702 return false;
703 SimpleTimeZone zone = (SimpleTimeZone) o;
704 if (zone.hashCode() != hashCode()
705 || !getID().equals(zone.getID())
706 || rawOffset != zone.rawOffset || useDaylight != zone.useDaylight)
707 return false;
708 if (!useDaylight)
709 return true;
710 return (startYear == zone.startYear
711 && startMonth == zone.startMonth
712 && startDay == zone.startDay
713 && startDayOfWeek == zone.startDayOfWeek
714 && startTime == zone.startTime
715 && endMonth == zone.endMonth
716 && endDay == zone.endDay
717 && endDayOfWeek == zone.endDayOfWeek
718 && endTime == zone.endTime);
722 * Test if the other time zone uses the same rule and only
723 * possibly differs in ID. This implementation for this particular
724 * class will return true if the other object is a SimpleTimeZone,
725 * the raw offsets and useDaylight are identical and if useDaylight
726 * is true, also the start and end datas are identical.
727 * @return true if this zone uses the same rule.
729 public boolean hasSameRules(TimeZone other)
731 if (this == other)
732 return true;
733 if (!(other instanceof SimpleTimeZone))
734 return false;
735 SimpleTimeZone zone = (SimpleTimeZone) other;
736 if (zone.hashCode() != hashCode()
737 || rawOffset != zone.rawOffset || useDaylight != zone.useDaylight)
738 return false;
739 if (!useDaylight)
740 return true;
741 return (startYear == zone.startYear
742 && startMonth == zone.startMonth
743 && startDay == zone.startDay
744 && startDayOfWeek == zone.startDayOfWeek
745 && startTime == zone.startTime
746 && endMonth == zone.endMonth
747 && endDay == zone.endDay
748 && endDayOfWeek == zone.endDayOfWeek && endTime == zone.endTime);
752 * Returns a string representation of this SimpleTimeZone object.
753 * @return a string representation of this SimpleTimeZone object.
755 public String toString()
757 // the test for useDaylight is an incompatibility to jdk1.2, but
758 // I think this shouldn't hurt.
759 return getClass().getName() + "["
760 + "id=" + getID()
761 + ",offset=" + rawOffset
762 + ",dstSavings=" + dstSavings
763 + ",useDaylight=" + useDaylight
764 + (useDaylight ?
765 ",startYear=" + startYear
766 + ",startMode=" + startMode
767 + ",startMonth=" + startMonth
768 + ",startDay=" + startDay
769 + ",startDayOfWeek=" + startDayOfWeek
770 + ",startTime=" + startTime
771 + ",endMode=" + endMode
772 + ",endMonth=" + endMonth
773 + ",endDay=" + endDay
774 + ",endDayOfWeek=" + endDayOfWeek
775 + ",endTime=" + endTime : "") + "]";
779 * Reads a serialized simple time zone from stream.
780 * @see #writeObject
782 private void readObject(java.io.ObjectInputStream input)
783 throws java.io.IOException, ClassNotFoundException
785 input.defaultReadObject();
786 if (serialVersionOnStream == 0)
788 // initialize the new fields to default values.
789 dstSavings = 60 * 60 * 1000;
790 endMode = DOW_IN_MONTH_MODE;
791 startMode = DOW_IN_MONTH_MODE;
792 serialVersionOnStream = 1;
794 else
796 int length = input.readInt();
797 byte[] byteArray = new byte[length];
798 input.read(byteArray, 0, length);
799 if (length >= 4)
801 // Lets hope that Sun does extensions to the serialized
802 // form in a sane manner.
803 startDay = byteArray[0];
804 startDayOfWeek = byteArray[1];
805 endDay = byteArray[2];
806 endDayOfWeek = byteArray[3];
812 * Serializes this object to a stream. @serialdata The object is
813 * first written in the old JDK 1.1 format, so that it can be read
814 * by by the old classes. This means, that the
815 * <code>start/endDay(OfWeek)</code>-Fields are written in the
816 * DOW_IN_MONTH_MODE rule, since this was the only supported rule
817 * in 1.1.
819 * In the optional section, we write first the length of an byte
820 * array as int and afterwards the byte array itself. The byte
821 * array contains in this release four elements, namely the real
822 * startDay, startDayOfWeek endDay, endDayOfWeek in that Order.
823 * These fields are needed, because for compatibility reasons only
824 * approximative values are written to the required section, as
825 * described above.
827 private void writeObject(java.io.ObjectOutputStream output)
828 throws java.io.IOException
830 byte[] byteArray = new byte[]
832 (byte) startDay, (byte) startDayOfWeek,
833 (byte) endDay, (byte) endDayOfWeek};
835 /* calculate the approximation for JDK 1.1 */
836 switch (startMode)
838 case DOM_MODE:
839 startDayOfWeek = Calendar.SUNDAY; // random day of week
840 // fall through
841 case DOW_GE_DOM_MODE:
842 case DOW_LE_DOM_MODE:
843 startDay = (startDay + 6) / 7;
845 switch (endMode)
847 case DOM_MODE:
848 endDayOfWeek = Calendar.SUNDAY;
849 // fall through
850 case DOW_GE_DOM_MODE:
851 case DOW_LE_DOM_MODE:
852 endDay = (endDay + 6) / 7;
855 // the required part:
856 output.defaultWriteObject();
857 // the optional part:
858 output.writeInt(byteArray.length);
859 output.write(byteArray, 0, byteArray.length);