This repository has been archived by the owner on Sep 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 286
/
GregorianCalendar.java
3301 lines (3095 loc) · 134 KB
/
GregorianCalendar.java
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, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* (C) Copyright Taligent, Inc. 1996-1998 - All Rights Reserved
* (C) Copyright IBM Corp. 1996-1998 - 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.IOException;
import java.io.ObjectInputStream;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import sun.util.calendar.BaseCalendar;
import sun.util.calendar.CalendarDate;
import sun.util.calendar.CalendarSystem;
import sun.util.calendar.CalendarUtils;
import sun.util.calendar.Era;
import sun.util.calendar.Gregorian;
import sun.util.calendar.JulianCalendar;
import sun.util.calendar.ZoneInfo;
/**
* <code>GregorianCalendar</code> is a concrete subclass of
* <code>Calendar</code> and provides the standard calendar system
* used by most of the world.
*
* <p> <code>GregorianCalendar</code> is a hybrid calendar that
* supports both the Julian and Gregorian calendar systems with the
* support of a single discontinuity, which corresponds by default to
* the Gregorian date when the Gregorian calendar was instituted
* (October 15, 1582 in some countries, later in others). The cutover
* date may be changed by the caller by calling {@link
* #setGregorianChange(Date) setGregorianChange()}.
*
* <p>
* Historically, in those countries which adopted the Gregorian calendar first,
* October 4, 1582 (Julian) was thus followed by October 15, 1582 (Gregorian). This calendar models
* this correctly. Before the Gregorian cutover, <code>GregorianCalendar</code>
* implements the Julian calendar. The only difference between the Gregorian
* and the Julian calendar is the leap year rule. The Julian calendar specifies
* leap years every four years, whereas the Gregorian calendar omits century
* years which are not divisible by 400.
*
* <p>
* <code>GregorianCalendar</code> implements <em>proleptic</em> Gregorian and
* Julian calendars. That is, dates are computed by extrapolating the current
* rules indefinitely far backward and forward in time. As a result,
* <code>GregorianCalendar</code> may be used for all years to generate
* meaningful and consistent results. However, dates obtained using
* <code>GregorianCalendar</code> are historically accurate only from March 1, 4
* AD onward, when modern Julian calendar rules were adopted. Before this date,
* leap year rules were applied irregularly, and before 45 BC the Julian
* calendar did not even exist.
*
* <p>
* Prior to the institution of the Gregorian calendar, New Year's Day was
* March 25. To avoid confusion, this calendar always uses January 1. A manual
* adjustment may be made if desired for dates that are prior to the Gregorian
* changeover and which fall between January 1 and March 24.
*
* <h3><a name="week_and_year">Week Of Year and Week Year</a></h3>
*
* <p>Values calculated for the {@link Calendar#WEEK_OF_YEAR
* WEEK_OF_YEAR} field range from 1 to 53. The first week of a
* calendar year is the earliest seven day period starting on {@link
* Calendar#getFirstDayOfWeek() getFirstDayOfWeek()} that contains at
* least {@link Calendar#getMinimalDaysInFirstWeek()
* getMinimalDaysInFirstWeek()} days from that year. It thus depends
* on the values of {@code getMinimalDaysInFirstWeek()}, {@code
* getFirstDayOfWeek()}, and the day of the week of January 1. Weeks
* between week 1 of one year and week 1 of the following year
* (exclusive) are numbered sequentially from 2 to 52 or 53 (except
* for year(s) involved in the Julian-Gregorian transition).
*
* <p>The {@code getFirstDayOfWeek()} and {@code
* getMinimalDaysInFirstWeek()} values are initialized using
* locale-dependent resources when constructing a {@code
* GregorianCalendar}. <a name="iso8601_compatible_setting">The week
* determination is compatible</a> with the ISO 8601 standard when {@code
* getFirstDayOfWeek()} is {@code MONDAY} and {@code
* getMinimalDaysInFirstWeek()} is 4, which values are used in locales
* where the standard is preferred. These values can explicitly be set by
* calling {@link Calendar#setFirstDayOfWeek(int) setFirstDayOfWeek()} and
* {@link Calendar#setMinimalDaysInFirstWeek(int)
* setMinimalDaysInFirstWeek()}.
*
* <p>A <a name="week_year"><em>week year</em></a> is in sync with a
* {@code WEEK_OF_YEAR} cycle. All weeks between the first and last
* weeks (inclusive) have the same <em>week year</em> value.
* Therefore, the first and last days of a week year may have
* different calendar year values.
*
* <p>For example, January 1, 1998 is a Thursday. If {@code
* getFirstDayOfWeek()} is {@code MONDAY} and {@code
* getMinimalDaysInFirstWeek()} is 4 (ISO 8601 standard compatible
* setting), then week 1 of 1998 starts on December 29, 1997, and ends
* on January 4, 1998. The week year is 1998 for the last three days
* of calendar year 1997. If, however, {@code getFirstDayOfWeek()} is
* {@code SUNDAY}, then week 1 of 1998 starts on January 4, 1998, and
* ends on January 10, 1998; the first three days of 1998 then are
* part of week 53 of 1997 and their week year is 1997.
*
* <h4>Week Of Month</h4>
*
* <p>Values calculated for the <code>WEEK_OF_MONTH</code> field range from 0
* to 6. Week 1 of a month (the days with <code>WEEK_OF_MONTH =
* 1</code>) is the earliest set of at least
* <code>getMinimalDaysInFirstWeek()</code> contiguous days in that month,
* ending on the day before <code>getFirstDayOfWeek()</code>. Unlike
* week 1 of a year, week 1 of a month may be shorter than 7 days, need
* not start on <code>getFirstDayOfWeek()</code>, and will not include days of
* the previous month. Days of a month before week 1 have a
* <code>WEEK_OF_MONTH</code> of 0.
*
* <p>For example, if <code>getFirstDayOfWeek()</code> is <code>SUNDAY</code>
* and <code>getMinimalDaysInFirstWeek()</code> is 4, then the first week of
* January 1998 is Sunday, January 4 through Saturday, January 10. These days
* have a <code>WEEK_OF_MONTH</code> of 1. Thursday, January 1 through
* Saturday, January 3 have a <code>WEEK_OF_MONTH</code> of 0. If
* <code>getMinimalDaysInFirstWeek()</code> is changed to 3, then January 1
* through January 3 have a <code>WEEK_OF_MONTH</code> of 1.
*
* <h4>Default Fields Values</h4>
*
* <p>The <code>clear</code> method sets calendar field(s)
* undefined. <code>GregorianCalendar</code> uses the following
* default value for each calendar field if its value is undefined.
*
* <table cellpadding="0" cellspacing="3" border="0"
* summary="GregorianCalendar default field values"
* style="text-align: left; width: 66%;">
* <tbody>
* <tr>
* <th style="vertical-align: top; background-color: rgb(204, 204, 255);
* text-align: center;">Field<br>
* </th>
* <th style="vertical-align: top; background-color: rgb(204, 204, 255);
* text-align: center;">Default Value<br>
* </th>
* </tr>
* <tr>
* <td style="vertical-align: middle;">
* <code>ERA<br></code>
* </td>
* <td style="vertical-align: middle;">
* <code>AD<br></code>
* </td>
* </tr>
* <tr>
* <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
* <code>YEAR<br></code>
* </td>
* <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
* <code>1970<br></code>
* </td>
* </tr>
* <tr>
* <td style="vertical-align: middle;">
* <code>MONTH<br></code>
* </td>
* <td style="vertical-align: middle;">
* <code>JANUARY<br></code>
* </td>
* </tr>
* <tr>
* <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
* <code>DAY_OF_MONTH<br></code>
* </td>
* <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
* <code>1<br></code>
* </td>
* </tr>
* <tr>
* <td style="vertical-align: middle;">
* <code>DAY_OF_WEEK<br></code>
* </td>
* <td style="vertical-align: middle;">
* <code>the first day of week<br></code>
* </td>
* </tr>
* <tr>
* <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
* <code>WEEK_OF_MONTH<br></code>
* </td>
* <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
* <code>0<br></code>
* </td>
* </tr>
* <tr>
* <td style="vertical-align: top;">
* <code>DAY_OF_WEEK_IN_MONTH<br></code>
* </td>
* <td style="vertical-align: top;">
* <code>1<br></code>
* </td>
* </tr>
* <tr>
* <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
* <code>AM_PM<br></code>
* </td>
* <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
* <code>AM<br></code>
* </td>
* </tr>
* <tr>
* <td style="vertical-align: middle;">
* <code>HOUR, HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND<br></code>
* </td>
* <td style="vertical-align: middle;">
* <code>0<br></code>
* </td>
* </tr>
* </tbody>
* </table>
* <br>Default values are not applicable for the fields not listed above.
*
* <p>
* <strong>Example:</strong>
* <blockquote>
* <pre>
* // get the supported ids for GMT-08:00 (Pacific Standard Time)
* String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
* // if no ids were returned, something is wrong. get out.
* if (ids.length == 0)
* System.exit(0);
*
* // begin output
* System.out.println("Current Time");
*
* // create a Pacific Standard Time time zone
* SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
*
* // set up rules for Daylight Saving Time
* pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
* pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
*
* // create a GregorianCalendar with the Pacific Daylight time zone
* // and the current date and time
* Calendar calendar = new GregorianCalendar(pdt);
* Date trialTime = new Date();
* calendar.setTime(trialTime);
*
* // print out a bunch of interesting things
* System.out.println("ERA: " + calendar.get(Calendar.ERA));
* System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
* System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
* System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
* System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
* System.out.println("DATE: " + calendar.get(Calendar.DATE));
* System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
* System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
* System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
* System.out.println("DAY_OF_WEEK_IN_MONTH: "
* + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
* System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
* System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
* System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
* System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
* System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
* System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
* System.out.println("ZONE_OFFSET: "
* + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
* System.out.println("DST_OFFSET: "
* + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
* System.out.println("Current Time, with hour reset to 3");
* calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override
* calendar.set(Calendar.HOUR, 3);
* System.out.println("ERA: " + calendar.get(Calendar.ERA));
* System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
* System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
* System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
* System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
* System.out.println("DATE: " + calendar.get(Calendar.DATE));
* System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
* System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
* System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
* System.out.println("DAY_OF_WEEK_IN_MONTH: "
* + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
* System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
* System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
* System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
* System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
* System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
* System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
* System.out.println("ZONE_OFFSET: "
* + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours
* System.out.println("DST_OFFSET: "
* + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours
* </pre>
* </blockquote>
*
* @see TimeZone
* @author David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu
* @since JDK1.1
*/
public class GregorianCalendar extends Calendar {
/*
* Implementation Notes
*
* The epoch is the number of days or milliseconds from some defined
* starting point. The epoch for java.util.Date is used here; that is,
* milliseconds from January 1, 1970 (Gregorian), midnight UTC. Other
* epochs which are used are January 1, year 1 (Gregorian), which is day 1
* of the Gregorian calendar, and December 30, year 0 (Gregorian), which is
* day 1 of the Julian calendar.
*
* We implement the proleptic Julian and Gregorian calendars. This means we
* implement the modern definition of the calendar even though the
* historical usage differs. For example, if the Gregorian change is set
* to new Date(Long.MIN_VALUE), we have a pure Gregorian calendar which
* labels dates preceding the invention of the Gregorian calendar in 1582 as
* if the calendar existed then.
*
* Likewise, with the Julian calendar, we assume a consistent
* 4-year leap year rule, even though the historical pattern of
* leap years is irregular, being every 3 years from 45 BCE
* through 9 BCE, then every 4 years from 8 CE onwards, with no
* leap years in-between. Thus date computations and functions
* such as isLeapYear() are not intended to be historically
* accurate.
*/
//////////////////
// Class Variables
//////////////////
/**
* Value of the <code>ERA</code> field indicating
* the period before the common era (before Christ), also known as BCE.
* The sequence of years at the transition from <code>BC</code> to <code>AD</code> is
* ..., 2 BC, 1 BC, 1 AD, 2 AD,...
*
* @see #ERA
*/
public static final int BC = 0;
/**
* Value of the {@link #ERA} field indicating
* the period before the common era, the same value as {@link #BC}.
*
* @see #CE
*/
static final int BCE = 0;
/**
* Value of the <code>ERA</code> field indicating
* the common era (Anno Domini), also known as CE.
* The sequence of years at the transition from <code>BC</code> to <code>AD</code> is
* ..., 2 BC, 1 BC, 1 AD, 2 AD,...
*
* @see #ERA
*/
public static final int AD = 1;
/**
* Value of the {@link #ERA} field indicating
* the common era, the same value as {@link #AD}.
*
* @see #BCE
*/
static final int CE = 1;
private static final int EPOCH_OFFSET = 719163; // Fixed date of January 1, 1970 (Gregorian)
private static final int EPOCH_YEAR = 1970;
static final int MONTH_LENGTH[]
= {31,28,31,30,31,30,31,31,30,31,30,31}; // 0-based
static final int LEAP_MONTH_LENGTH[]
= {31,29,31,30,31,30,31,31,30,31,30,31}; // 0-based
// Useful millisecond constants. Although ONE_DAY and ONE_WEEK can fit
// into ints, they must be longs in order to prevent arithmetic overflow
// when performing (bug 4173516).
private static final int ONE_SECOND = 1000;
private static final int ONE_MINUTE = 60*ONE_SECOND;
private static final int ONE_HOUR = 60*ONE_MINUTE;
private static final long ONE_DAY = 24*ONE_HOUR;
private static final long ONE_WEEK = 7*ONE_DAY;
/*
* <pre>
* Greatest Least
* Field name Minimum Minimum Maximum Maximum
* ---------- ------- ------- ------- -------
* ERA 0 0 1 1
* YEAR 1 1 292269054 292278994
* MONTH 0 0 11 11
* WEEK_OF_YEAR 1 1 52* 53
* WEEK_OF_MONTH 0 0 4* 6
* DAY_OF_MONTH 1 1 28* 31
* DAY_OF_YEAR 1 1 365* 366
* DAY_OF_WEEK 1 1 7 7
* DAY_OF_WEEK_IN_MONTH -1 -1 4* 6
* AM_PM 0 0 1 1
* HOUR 0 0 11 11
* HOUR_OF_DAY 0 0 23 23
* MINUTE 0 0 59 59
* SECOND 0 0 59 59
* MILLISECOND 0 0 999 999
* ZONE_OFFSET -13:00 -13:00 14:00 14:00
* DST_OFFSET 0:00 0:00 0:20 2:00
* </pre>
* *: depends on the Gregorian change date
*/
static final int MIN_VALUES[] = {
BCE, // ERA
1, // YEAR
JANUARY, // MONTH
1, // WEEK_OF_YEAR
0, // WEEK_OF_MONTH
1, // DAY_OF_MONTH
1, // DAY_OF_YEAR
SUNDAY, // DAY_OF_WEEK
1, // DAY_OF_WEEK_IN_MONTH
AM, // AM_PM
0, // HOUR
0, // HOUR_OF_DAY
0, // MINUTE
0, // SECOND
0, // MILLISECOND
-13*ONE_HOUR, // ZONE_OFFSET (UNIX compatibility)
0 // DST_OFFSET
};
static final int LEAST_MAX_VALUES[] = {
CE, // ERA
292269054, // YEAR
DECEMBER, // MONTH
52, // WEEK_OF_YEAR
4, // WEEK_OF_MONTH
28, // DAY_OF_MONTH
365, // DAY_OF_YEAR
SATURDAY, // DAY_OF_WEEK
4, // DAY_OF_WEEK_IN
PM, // AM_PM
11, // HOUR
23, // HOUR_OF_DAY
59, // MINUTE
59, // SECOND
999, // MILLISECOND
14*ONE_HOUR, // ZONE_OFFSET
20*ONE_MINUTE // DST_OFFSET (historical least maximum)
};
static final int MAX_VALUES[] = {
CE, // ERA
292278994, // YEAR
DECEMBER, // MONTH
53, // WEEK_OF_YEAR
6, // WEEK_OF_MONTH
31, // DAY_OF_MONTH
366, // DAY_OF_YEAR
SATURDAY, // DAY_OF_WEEK
6, // DAY_OF_WEEK_IN
PM, // AM_PM
11, // HOUR
23, // HOUR_OF_DAY
59, // MINUTE
59, // SECOND
999, // MILLISECOND
14*ONE_HOUR, // ZONE_OFFSET
2*ONE_HOUR // DST_OFFSET (double summer time)
};
// Proclaim serialization compatibility with JDK 1.1
@SuppressWarnings("FieldNameHidesFieldInSuperclass")
static final long serialVersionUID = -8125100834729963327L;
// Reference to the sun.util.calendar.Gregorian instance (singleton).
private static final Gregorian gcal =
CalendarSystem.getGregorianCalendar();
// Reference to the JulianCalendar instance (singleton), set as needed. See
// getJulianCalendarSystem().
private static JulianCalendar jcal;
// JulianCalendar eras. See getJulianCalendarSystem().
private static Era[] jeras;
// The default value of gregorianCutover.
static final long DEFAULT_GREGORIAN_CUTOVER = -12219292800000L;
/////////////////////
// Instance Variables
/////////////////////
/**
* The point at which the Gregorian calendar rules are used, measured in
* milliseconds from the standard epoch. Default is October 15, 1582
* (Gregorian) 00:00:00 UTC or -12219292800000L. For this value, October 4,
* 1582 (Julian) is followed by October 15, 1582 (Gregorian). This
* corresponds to Julian day number 2299161.
* @serial
*/
private long gregorianCutover = DEFAULT_GREGORIAN_CUTOVER;
/**
* The fixed date of the gregorianCutover.
*/
private transient long gregorianCutoverDate =
(((DEFAULT_GREGORIAN_CUTOVER + 1)/ONE_DAY) - 1) + EPOCH_OFFSET; // == 577736
/**
* The normalized year of the gregorianCutover in Gregorian, with
* 0 representing 1 BCE, -1 representing 2 BCE, etc.
*/
private transient int gregorianCutoverYear = 1582;
/**
* The normalized year of the gregorianCutover in Julian, with 0
* representing 1 BCE, -1 representing 2 BCE, etc.
*/
private transient int gregorianCutoverYearJulian = 1582;
/**
* gdate always has a sun.util.calendar.Gregorian.Date instance to
* avoid overhead of creating it. The assumption is that most
* applications will need only Gregorian calendar calculations.
*/
private transient BaseCalendar.Date gdate;
/**
* Reference to either gdate or a JulianCalendar.Date
* instance. After calling complete(), this value is guaranteed to
* be set.
*/
private transient BaseCalendar.Date cdate;
/**
* The CalendarSystem used to calculate the date in cdate. After
* calling complete(), this value is guaranteed to be set and
* consistent with the cdate value.
*/
private transient BaseCalendar calsys;
/**
* Temporary int[2] to get time zone offsets. zoneOffsets[0] gets
* the GMT offset value and zoneOffsets[1] gets the DST saving
* value.
*/
private transient int[] zoneOffsets;
/**
* Temporary storage for saving original fields[] values in
* non-lenient mode.
*/
private transient int[] originalFields;
///////////////
// Constructors
///////////////
/**
* Constructs a default <code>GregorianCalendar</code> using the current time
* in the default time zone with the default
* {@link Locale.Category#FORMAT FORMAT} locale.
*/
public GregorianCalendar() {
this(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));
setZoneShared(true);
}
/**
* Constructs a <code>GregorianCalendar</code> based on the current time
* in the given time zone with the default
* {@link Locale.Category#FORMAT FORMAT} locale.
*
* @param zone the given time zone.
*/
public GregorianCalendar(TimeZone zone) {
this(zone, Locale.getDefault(Locale.Category.FORMAT));
}
/**
* Constructs a <code>GregorianCalendar</code> based on the current time
* in the default time zone with the given locale.
*
* @param aLocale the given locale.
*/
public GregorianCalendar(Locale aLocale) {
this(TimeZone.getDefaultRef(), aLocale);
setZoneShared(true);
}
/**
* Constructs a <code>GregorianCalendar</code> based on the current time
* in the given time zone with the given locale.
*
* @param zone the given time zone.
* @param aLocale the given locale.
*/
public GregorianCalendar(TimeZone zone, Locale aLocale) {
super(zone, aLocale);
gdate = (BaseCalendar.Date) gcal.newCalendarDate(zone);
setTimeInMillis(System.currentTimeMillis());
}
/**
* Constructs a <code>GregorianCalendar</code> with the given date set
* in the default time zone with the default locale.
*
* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
* Month value is 0-based. e.g., 0 for January.
* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
*/
public GregorianCalendar(int year, int month, int dayOfMonth) {
this(year, month, dayOfMonth, 0, 0, 0, 0);
}
/**
* Constructs a <code>GregorianCalendar</code> with the given date
* and time set for the default time zone with the default locale.
*
* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
* Month value is 0-based. e.g., 0 for January.
* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
* in the calendar.
* @param minute the value used to set the <code>MINUTE</code> calendar field
* in the calendar.
*/
public GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay,
int minute) {
this(year, month, dayOfMonth, hourOfDay, minute, 0, 0);
}
/**
* Constructs a GregorianCalendar with the given date
* and time set for the default time zone with the default locale.
*
* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
* Month value is 0-based. e.g., 0 for January.
* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
* in the calendar.
* @param minute the value used to set the <code>MINUTE</code> calendar field
* in the calendar.
* @param second the value used to set the <code>SECOND</code> calendar field
* in the calendar.
*/
public GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay,
int minute, int second) {
this(year, month, dayOfMonth, hourOfDay, minute, second, 0);
}
/**
* Constructs a <code>GregorianCalendar</code> with the given date
* and time set for the default time zone with the default locale.
*
* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
* Month value is 0-based. e.g., 0 for January.
* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
* in the calendar.
* @param minute the value used to set the <code>MINUTE</code> calendar field
* in the calendar.
* @param second the value used to set the <code>SECOND</code> calendar field
* in the calendar.
* @param millis the value used to set the <code>MILLISECOND</code> calendar field
*/
GregorianCalendar(int year, int month, int dayOfMonth,
int hourOfDay, int minute, int second, int millis) {
super();
gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());
this.set(YEAR, year);
this.set(MONTH, month);
this.set(DAY_OF_MONTH, dayOfMonth);
// Set AM_PM and HOUR here to set their stamp values before
// setting HOUR_OF_DAY (6178071).
if (hourOfDay >= 12 && hourOfDay <= 23) {
// If hourOfDay is a valid PM hour, set the correct PM values
// so that it won't throw an exception in case it's set to
// non-lenient later.
this.internalSet(AM_PM, PM);
this.internalSet(HOUR, hourOfDay - 12);
} else {
// The default value for AM_PM is AM.
// We don't care any out of range value here for leniency.
this.internalSet(HOUR, hourOfDay);
}
// The stamp values of AM_PM and HOUR must be COMPUTED. (6440854)
setFieldsComputed(HOUR_MASK|AM_PM_MASK);
this.set(HOUR_OF_DAY, hourOfDay);
this.set(MINUTE, minute);
this.set(SECOND, second);
// should be changed to set() when this constructor is made
// public.
this.internalSet(MILLISECOND, millis);
}
/**
* Constructs an empty GregorianCalendar.
*
* @param zone the given time zone
* @param aLocale the given locale
* @param flag the flag requesting an empty instance
*/
GregorianCalendar(TimeZone zone, Locale locale, boolean flag) {
super(zone, locale);
gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());
}
/////////////////
// Public methods
/////////////////
/**
* Sets the <code>GregorianCalendar</code> change date. This is the point when the switch
* from Julian dates to Gregorian dates occurred. Default is October 15,
* 1582 (Gregorian). Previous to this, dates will be in the Julian calendar.
* <p>
* To obtain a pure Julian calendar, set the change date to
* <code>Date(Long.MAX_VALUE)</code>. To obtain a pure Gregorian calendar,
* set the change date to <code>Date(Long.MIN_VALUE)</code>.
*
* @param date the given Gregorian cutover date.
*/
public void setGregorianChange(Date date) {
long cutoverTime = date.getTime();
if (cutoverTime == gregorianCutover) {
return;
}
// Before changing the cutover date, make sure to have the
// time of this calendar.
complete();
setGregorianChange(cutoverTime);
}
private void setGregorianChange(long cutoverTime) {
gregorianCutover = cutoverTime;
gregorianCutoverDate = CalendarUtils.floorDivide(cutoverTime, ONE_DAY)
+ EPOCH_OFFSET;
// To provide the "pure" Julian calendar as advertised.
// Strictly speaking, the last millisecond should be a
// Gregorian date. However, the API doc specifies that setting
// the cutover date to Long.MAX_VALUE will make this calendar
// a pure Julian calendar. (See 4167995)
if (cutoverTime == Long.MAX_VALUE) {
gregorianCutoverDate++;
}
BaseCalendar.Date d = getGregorianCutoverDate();
// Set the cutover year (in the Gregorian year numbering)
gregorianCutoverYear = d.getYear();
BaseCalendar julianCal = getJulianCalendarSystem();
d = (BaseCalendar.Date) julianCal.newCalendarDate(TimeZone.NO_TIMEZONE);
julianCal.getCalendarDateFromFixedDate(d, gregorianCutoverDate - 1);
gregorianCutoverYearJulian = d.getNormalizedYear();
if (time < gregorianCutover) {
// The field values are no longer valid under the new
// cutover date.
setUnnormalized();
}
}
/**
* Gets the Gregorian Calendar change date. This is the point when the
* switch from Julian dates to Gregorian dates occurred. Default is
* October 15, 1582 (Gregorian). Previous to this, dates will be in the Julian
* calendar.
*
* @return the Gregorian cutover date for this <code>GregorianCalendar</code> object.
*/
public final Date getGregorianChange() {
return new Date(gregorianCutover);
}
/**
* Determines if the given year is a leap year. Returns <code>true</code> if
* the given year is a leap year. To specify BC year numbers,
* <code>1 - year number</code> must be given. For example, year BC 4 is
* specified as -3.
*
* @param year the given year.
* @return <code>true</code> if the given year is a leap year; <code>false</code> otherwise.
*/
public boolean isLeapYear(int year) {
if ((year & 3) != 0) {
return false;
}
if (year > gregorianCutoverYear) {
return (year%100 != 0) || (year%400 == 0); // Gregorian
}
if (year < gregorianCutoverYearJulian) {
return true; // Julian
}
boolean gregorian;
// If the given year is the Gregorian cutover year, we need to
// determine which calendar system to be applied to February in the year.
if (gregorianCutoverYear == gregorianCutoverYearJulian) {
BaseCalendar.Date d = getCalendarDate(gregorianCutoverDate); // Gregorian
gregorian = d.getMonth() < BaseCalendar.MARCH;
} else {
gregorian = year == gregorianCutoverYear;
}
return gregorian ? (year%100 != 0) || (year%400 == 0) : true;
}
/**
* Returns {@code "gregory"} as the calendar type.
*
* @return {@code "gregory"}
* @since 1.8
*/
@Override
public String getCalendarType() {
return "gregory";
}
/**
* Compares this <code>GregorianCalendar</code> to the specified
* <code>Object</code>. The result is <code>true</code> if and
* only if the argument is a <code>GregorianCalendar</code> object
* that represents the same time value (millisecond offset from
* the <a href="Calendar.html#Epoch">Epoch</a>) under the same
* <code>Calendar</code> parameters and Gregorian change date as
* this object.
*
* @param obj the object to compare with.
* @return <code>true</code> if this object is equal to <code>obj</code>;
* <code>false</code> otherwise.
* @see Calendar#compareTo(Calendar)
*/
@Override
public boolean equals(Object obj) {
return obj instanceof GregorianCalendar &&
super.equals(obj) &&
gregorianCutover == ((GregorianCalendar)obj).gregorianCutover;
}
/**
* Generates the hash code for this <code>GregorianCalendar</code> object.
*/
@Override
public int hashCode() {
return super.hashCode() ^ (int)gregorianCutoverDate;
}
/**
* Adds the specified (signed) amount of time to the given calendar field,
* based on the calendar's rules.
*
* <p><em>Add rule 1</em>. The value of <code>field</code>
* after the call minus the value of <code>field</code> before the
* call is <code>amount</code>, modulo any overflow that has occurred in
* <code>field</code>. Overflow occurs when a field value exceeds its
* range and, as a result, the next larger field is incremented or
* decremented and the field value is adjusted back into its range.</p>
*
* <p><em>Add rule 2</em>. If a smaller field is expected to be
* invariant, but it is impossible for it to be equal to its
* prior value because of changes in its minimum or maximum after
* <code>field</code> is changed, then its value is adjusted to be as close
* as possible to its expected value. A smaller field represents a
* smaller unit of time. <code>HOUR</code> is a smaller field than
* <code>DAY_OF_MONTH</code>. No adjustment is made to smaller fields
* that are not expected to be invariant. The calendar system
* determines what fields are expected to be invariant.</p>
*
* @param field the calendar field.
* @param amount the amount of date or time to be added to the field.
* @exception IllegalArgumentException if <code>field</code> is
* <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown,
* or if any calendar fields have out-of-range values in
* non-lenient mode.
*/
@Override
public void add(int field, int amount) {
// If amount == 0, do nothing even the given field is out of
// range. This is tested by JCK.
if (amount == 0) {
return; // Do nothing!
}
if (field < 0 || field >= ZONE_OFFSET) {
throw new IllegalArgumentException();
}
// Sync the time and calendar fields.
complete();
if (field == YEAR) {
int year = internalGet(YEAR);
if (internalGetEra() == CE) {
year += amount;
if (year > 0) {
set(YEAR, year);
} else { // year <= 0
set(YEAR, 1 - year);
// if year == 0, you get 1 BCE.
set(ERA, BCE);
}
}
else { // era == BCE
year -= amount;
if (year > 0) {
set(YEAR, year);
} else { // year <= 0
set(YEAR, 1 - year);
// if year == 0, you get 1 CE
set(ERA, CE);
}
}
pinDayOfMonth();
} else if (field == MONTH) {
int month = internalGet(MONTH) + amount;
int year = internalGet(YEAR);
int y_amount;
if (month >= 0) {
y_amount = month/12;
} else {
y_amount = (month+1)/12 - 1;
}
if (y_amount != 0) {
if (internalGetEra() == CE) {
year += y_amount;
if (year > 0) {
set(YEAR, year);
} else { // year <= 0
set(YEAR, 1 - year);
// if year == 0, you get 1 BCE
set(ERA, BCE);
}
}
else { // era == BCE
year -= y_amount;
if (year > 0) {
set(YEAR, year);
} else { // year <= 0
set(YEAR, 1 - year);
// if year == 0, you get 1 CE
set(ERA, CE);
}
}
}
if (month >= 0) {
set(MONTH, month % 12);
} else {
// month < 0
month %= 12;
if (month < 0) {
month += 12;
}
set(MONTH, JANUARY + month);
}
pinDayOfMonth();
} else if (field == ERA) {
int era = internalGet(ERA) + amount;
if (era < 0) {
era = 0;
}
if (era > 1) {
era = 1;
}
set(ERA, era);