forked from yuhailei1992/Java7SourceCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleTimeZone.java
More file actions
1706 lines (1596 loc) · 69.3 KB
/
SimpleTimeZone.java
File metadata and controls
1706 lines (1596 loc) · 69.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
/*
* (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
* (C) Copyright IBM Corp. 1996 - All Rights Reserved
*
* The original version of this source code and documentation is copyrighted
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
* materials are provided under terms of a License Agreement between Taligent
* and Sun. This technology is protected by multiple US and International
* patents. This notice and attribution to Taligent may not be removed.
* Taligent is a registered trademark of Taligent, Inc.
*
*/
package java.util;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import sun.util.calendar.CalendarSystem;
import sun.util.calendar.CalendarUtils;
import sun.util.calendar.BaseCalendar;
import sun.util.calendar.Gregorian;
/**
* <code>SimpleTimeZone</code> is a concrete subclass of <code>TimeZone</code>
* that represents a time zone for use with a Gregorian calendar.
* The class holds an offset from GMT, called <em>raw offset</em>, 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.
* <p>
* To construct a <code>SimpleTimeZone</code> with a daylight saving time
* schedule, the schedule can be described with a set of rules,
* <em>start-rule</em> and <em>end-rule</em>. A day when daylight saving time
* starts or ends is specified by a combination of <em>month</em>,
* <em>day-of-month</em>, and <em>day-of-week</em> values. The <em>month</em>
* value is represented by a Calendar {@link Calendar#MONTH MONTH} field
* value, such as {@link Calendar#MARCH}. The <em>day-of-week</em> 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.
*
* <ul>
* <li><b>Exact day of month</b><br>
* To specify an exact day of month, set the <em>month</em> and
* <em>day-of-month</em> to an exact value, and <em>day-of-week</em> to zero. For
* example, to specify March 1, set the <em>month</em> to {@link Calendar#MARCH
* MARCH}, <em>day-of-month</em> to 1, and <em>day-of-week</em> to 0.</li>
*
* <li><b>Day of week on or after day of month</b><br>
* To specify a day of week on or after an exact day of month, set the
* <em>month</em> to an exact month value, <em>day-of-month</em> to the day on
* or after which the rule is applied, and <em>day-of-week</em> to a negative {@link
* Calendar#DAY_OF_WEEK DAY_OF_WEEK} field value. For example, to specify the
* second Sunday of April, set <em>month</em> to {@link Calendar#APRIL APRIL},
* <em>day-of-month</em> to 8, and <em>day-of-week</em> to <code>-</code>{@link
* Calendar#SUNDAY SUNDAY}.</li>
*
* <li><b>Day of week on or before day of month</b><br>
* To specify a day of the week on or before an exact day of the month, set
* <em>day-of-month</em> and <em>day-of-week</em> to a negative value. For
* example, to specify the last Wednesday on or before the 21st of March, set
* <em>month</em> to {@link Calendar#MARCH MARCH}, <em>day-of-month</em> is -21
* and <em>day-of-week</em> is <code>-</code>{@link Calendar#WEDNESDAY WEDNESDAY}. </li>
*
* <li><b>Last day-of-week of month</b><br>
* To specify, the last day-of-week of the month, set <em>day-of-week</em> to a
* {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} value and <em>day-of-month</em> to
* -1. For example, to specify the last Sunday of October, set <em>month</em>
* to {@link Calendar#OCTOBER OCTOBER}, <em>day-of-week</em> to {@link
* Calendar#SUNDAY SUNDAY} and <em>day-of-month</em> to -1. </li>
*
* </ul>
* 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
* <em>mode</em>s 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 <em>end-rule</em> means the same thing as the daylight time.
* <p>
* The following are examples of parameters for constructing time zone objects.
* <pre><code>
* // 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)
* </code></pre>
* These parameter rules are also applicable to the set rule methods, such as
* <code>setStartRule</code>.
*
* @since 1.1
* @see Calendar
* @see GregorianCalendar
* @see TimeZone
* @author David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu
*/
public class SimpleTimeZone extends TimeZone {
/**
* Constructs a SimpleTimeZone with the given base time zone offset from GMT
* and time zone ID with no daylight saving time schedule.
*
* @param rawOffset The base time zone offset in milliseconds to GMT.
* @param ID The time zone name that is given to this instance.
*/
public SimpleTimeZone(int rawOffset, String ID)
{
this.rawOffset = rawOffset;
setID (ID);
dstSavings = millisPerHour; // In case user sets rules later
}
/**
* 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 <code>startTime</code> and <code>endTime</code> 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:
* <pre><code>
* SimpleTimeZone(rawOffset,
* ID,
* startMonth,
* startDay,
* startDayOfWeek,
* startTime,
* SimpleTimeZone.{@link #WALL_TIME},
* endMonth,
* endDay,
* endDayOfWeek,
* endTime,
* SimpleTimeZone.{@link #WALL_TIME},
* 3600000)
* </code></pre>
*
* @param rawOffset The given base time zone offset from GMT.
* @param ID The time zone ID which is given to this object.
* @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 (in milliseconds within the day), which is local
* standard time in this case.
* @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 month, day, dayOfWeek, or time
* parameters are out of range for the start or end rule
*/
public SimpleTimeZone(int rawOffset, String ID,
int startMonth, int startDay, int startDayOfWeek, int startTime,
int endMonth, int endDay, int endDayOfWeek, int endTime)
{
this(rawOffset, ID,
startMonth, startDay, startDayOfWeek, startTime, WALL_TIME,
endMonth, endDay, endDayOfWeek, endTime, WALL_TIME,
millisPerHour);
}
/**
* 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 <code>startTime</code> and <code>endTime</code> are assumed to be
* represented in the wall clock time. This constructor is equivalent to:
* <pre><code>
* SimpleTimeZone(rawOffset,
* ID,
* startMonth,
* startDay,
* startDayOfWeek,
* startTime,
* SimpleTimeZone.{@link #WALL_TIME},
* endMonth,
* endDay,
* endDayOfWeek,
* endTime,
* SimpleTimeZone.{@link #WALL_TIME},
* dstSavings)
* </code></pre>
*
* @param rawOffset The given base time zone offset from GMT.
* @param ID The time zone ID which is given to this object.
* @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.
* @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,
* which is local daylight time in this case.
* @param dstSavings The amount of time in milliseconds saved during
* daylight saving time.
* @exception IllegalArgumentException if the month, day, dayOfWeek, or time
* parameters are out of range for the start or end rule
* @since 1.2
*/
public SimpleTimeZone(int rawOffset, String ID,
int startMonth, int startDay, int startDayOfWeek, int startTime,
int endMonth, int endDay, int endDayOfWeek, int endTime,
int dstSavings)
{
this(rawOffset, ID,
startMonth, startDay, startDayOfWeek, startTime, WALL_TIME,
endMonth, endDay, endDayOfWeek, endTime, WALL_TIME,
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 <code>startTime</code> and
* <code>endTime</code>. The mode specifies either {@link #WALL_TIME wall
* time} or {@link #STANDARD_TIME standard time} or {@link #UTC_TIME UTC
* time}.
*
* @param rawOffset The given base time zone offset from GMT.
* @param ID The time zone ID which is given to this object.
* @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 the time mode
* specified by <code>startTimeMode</code>.
* @param startTimeMode The mode of the start time specified by startTime.
* @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 time time mode
* specified by <code>endTimeMode</code>.
* @param endTimeMode The mode of the end time specified by endTime
* @param dstSavings The amount of time in milliseconds saved during
* daylight saving time.
*
* @exception IllegalArgumentException if the month, day, dayOfWeek, time more, or
* time parameters are out of range for the start or end rule, or if a time mode
* value is invalid.
*
* @see #WALL_TIME
* @see #STANDARD_TIME
* @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) {
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;
invalidateCache();
}
/**
* 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;
decodeStartRule();
invalidateCache();
}
/**
* 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 dayOfWeek.
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;
decodeEndRule();
invalidateCache();
}
/**
* 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 = 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;
}
/**
* 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)
{
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(BaseCalendar cal, 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;
}
private long getStart(BaseCalendar cal, 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 getEnd(BaseCalendar cal, BaseCalendar.Date cdate, int year) {
int time = endTime;
if (endTimeMode != UTC_TIME) {
time -= rawOffset;
}
if (endTimeMode == WALL_TIME) {
time -= dstSavings;
}
return getTransition(cal, cdate, endMode, year, endMonth, endDay,
endDayOfWeek, time);
}
private long getTransition(BaseCalendar cal, 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;
}
/**
* Gets the GMT offset for this time zone.
* @return the GMT offset value in milliseconds
* @see #setRawOffset
*/
public int getRawOffset()
{
// The given date will be taken into account while
// we have the historical time zone data in place.
return rawOffset;
}
/**
* Sets the base time zone offset to GMT.
* This is the offset to add to UTC to get local time.
* @see #getRawOffset
*/
public void setRawOffset(int offsetMillis)
{
this.rawOffset = offsetMillis;
}
/**
* Sets the amount of time in milliseconds that the clock is advanced
* during daylight saving time.
* @param millisSavedDuringDST the number of milliseconds the time is
* advanced with respect to standard time when the daylight saving time rules
* are in effect. A positive number, typically one hour (3600000).
* @see #getDSTSavings
* @since 1.2
*/
public void setDSTSavings(int millisSavedDuringDST) {
if (millisSavedDuringDST <= 0) {
throw new IllegalArgumentException("Illegal daylight saving value: "
+ millisSavedDuringDST);
}
dstSavings = millisSavedDuringDST;
}
/**
* Returns the amount of time in milliseconds that the clock is
* advanced during daylight saving time.
*
* @return the number of milliseconds the time is advanced with
* respect to standard time when the daylight saving rules are in
* effect, or 0 (zero) if this time zone doesn't observe daylight
* saving time.
*
* @see #setDSTSavings
* @since 1.2
*/
public int getDSTSavings() {
return useDaylight ? dstSavings : 0;
}
/**
* Queries if this time zone uses daylight saving time.
* @return true if this time zone uses daylight saving time;
* false otherwise.
*/
public boolean useDaylightTime()
{
return useDaylight;
}
/**
* Returns {@code true} if this {@code SimpleTimeZone} observes
* Daylight Saving Time. This method is equivalent to {@link
* #useDaylightTime()}.
*
* @return {@code true} if this {@code SimpleTimeZone} observes
* Daylight Saving Time; {@code false} otherwise.
* @since 1.7
*/
@Override
public boolean observesDaylightTime() {
return useDaylightTime();
}
/**
* Queries if the given date is in daylight saving time.
* @return true if daylight saving time is in effective at the
* given date; false otherwise.
*/
public boolean inDaylightTime(Date date)
{
return (getOffset(date.getTime()) != rawOffset);
}
/**
* Returns a clone of this <code>SimpleTimeZone</code> instance.
* @return a clone of this instance.
*/
public Object clone()
{
return super.clone();
}
/**
* Generates the hash code for the SimpleDateFormat object.
* @return the hash code for this object
*/
public synchronized int hashCode()
{
return startMonth ^ startDay ^ startDayOfWeek ^ startTime ^
endMonth ^ endDay ^ endDayOfWeek ^ endTime ^ rawOffset;
}
/**
* Compares the equality of two <code>SimpleTimeZone</code> objects.
*
* @param obj The <code>SimpleTimeZone</code> object to be compared with.
* @return True if the given <code>obj</code> is the same as this
* <code>SimpleTimeZone</code> object; false otherwise.
*/
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (!(obj instanceof SimpleTimeZone)) {
return false;
}
SimpleTimeZone that = (SimpleTimeZone) obj;
return getID().equals(that.getID()) &&
hasSameRules(that);
}
/**
* Returns <code>true</code> if this zone has the same rules and offset as another zone.
* @param other the TimeZone object to be compared with
* @return <code>true</code> if the given zone is a SimpleTimeZone and has the
* same rules and offset as this one
* @since 1.2
*/
public boolean hasSameRules(TimeZone other) {
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));
}
/**
* Returns 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 +
",startTimeMode=" + startTimeMode +
",endMode=" + endMode +
",endMonth=" + endMonth +
",endDay=" + endDay +
",endDayOfWeek=" + endDayOfWeek +
",endTime=" + endTime +
",endTimeMode=" + endTimeMode + ']';
}
// =======================privates===============================
/**
* The month in which daylight saving time starts. This value must be
* between <code>Calendar.JANUARY</code> and
* <code>Calendar.DECEMBER</code> inclusive. This value must not equal
* <code>endMonth</code>.
* <p>If <code>useDaylight</code> is false, this value is ignored.
* @serial
*/
private int startMonth;
/**
* This field has two possible interpretations:
* <dl>
* <dt><code>startMode == DOW_IN_MONTH</code></dt>
* <dd>
* <code>startDay</code> indicates the day of the month of
* <code>startMonth</code> on which daylight
* saving time starts, from 1 to 28, 30, or 31, depending on the
* <code>startMonth</code>.
* </dd>
* <dt><code>startMode != DOW_IN_MONTH</code></dt>
* <dd>
* <code>startDay</code> indicates which <code>startDayOfWeek</code> in the
* month <code>startMonth</code> daylight
* saving time starts on. For example, a value of +1 and a
* <code>startDayOfWeek</code> of <code>Calendar.SUNDAY</code> indicates the
* first Sunday of <code>startMonth</code>. Likewise, +2 would indicate the
* second Sunday, and -1 the last Sunday. A value of 0 is illegal.
* </dd>
* </dl>
* <p>If <code>useDaylight</code> is false, this value is ignored.
* @serial
*/