forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalendar.java
3931 lines (3581 loc) · 159 KB
/
Calendar.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, 2017, 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 sun.util.BuddhistCalendar;
import sun.util.calendar.ZoneInfo;
import sun.util.locale.provider.CalendarDataUtility;
import sun.util.locale.provider.LocaleProviderAdapter;
import sun.util.locale.provider.TimeZoneNameUtility;
import sun.util.spi.CalendarProvider;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.Serializable;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PermissionCollection;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.ProtectionDomain;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.time.Instant;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* The <code>Calendar</code> class is an abstract class that provides methods
* for converting between a specific instant in time and a set of {@link
* #fields calendar fields} such as <code>YEAR</code>, <code>MONTH</code>,
* <code>DAY_OF_MONTH</code>, <code>HOUR</code>, and so on, and for
* manipulating the calendar fields, such as getting the date of the next
* week. An instant in time can be represented by a millisecond value that is
* an offset from the <a id="Epoch"><em>Epoch</em></a>, January 1, 1970
* 00:00:00.000 GMT (Gregorian).
*
* <p>The class also provides additional fields and methods for
* implementing a concrete calendar system outside the package. Those
* fields and methods are defined as <code>protected</code>.
*
* <p>
* Like other locale-sensitive classes, <code>Calendar</code> provides a
* class method, <code>getInstance</code>, for getting a generally useful
* object of this type. <code>Calendar</code>'s <code>getInstance</code> method
* returns a <code>Calendar</code> object whose
* calendar fields have been initialized with the current date and time:
* <blockquote>
* <pre>
* Calendar rightNow = Calendar.getInstance();
* </pre>
* </blockquote>
*
* <p>A <code>Calendar</code> object can produce all the calendar field values
* needed to implement the date-time formatting for a particular language and
* calendar style (for example, Japanese-Gregorian, Japanese-Traditional).
* <code>Calendar</code> defines the range of values returned by
* certain calendar fields, as well as their meaning. For example,
* the first month of the calendar system has value <code>MONTH ==
* JANUARY</code> for all calendars. Other values are defined by the
* concrete subclass, such as <code>ERA</code>. See individual field
* documentation and subclass documentation for details.
*
* <h3>Getting and Setting Calendar Field Values</h3>
*
* <p>The calendar field values can be set by calling the <code>set</code>
* methods. Any field values set in a <code>Calendar</code> will not be
* interpreted until it needs to calculate its time value (milliseconds from
* the Epoch) or values of the calendar fields. Calling the
* <code>get</code>, <code>getTimeInMillis</code>, <code>getTime</code>,
* <code>add</code> and <code>roll</code> involves such calculation.
*
* <h4>Leniency</h4>
*
* <p><code>Calendar</code> has two modes for interpreting the calendar
* fields, <em>lenient</em> and <em>non-lenient</em>. When a
* <code>Calendar</code> is in lenient mode, it accepts a wider range of
* calendar field values than it produces. When a <code>Calendar</code>
* recomputes calendar field values for return by <code>get()</code>, all of
* the calendar fields are normalized. For example, a lenient
* <code>GregorianCalendar</code> interprets <code>MONTH == JANUARY</code>,
* <code>DAY_OF_MONTH == 32</code> as February 1.
* <p>When a <code>Calendar</code> is in non-lenient mode, it throws an
* exception if there is any inconsistency in its calendar fields. For
* example, a <code>GregorianCalendar</code> always produces
* <code>DAY_OF_MONTH</code> values between 1 and the length of the month. A
* non-lenient <code>GregorianCalendar</code> throws an exception upon
* calculating its time or calendar field values if any out-of-range field
* value has been set.
*
* <h4><a id="first_week">First Week</a></h4>
*
* <code>Calendar</code> defines a locale-specific seven day week using two
* parameters: the first day of the week and the minimal days in first week
* (from 1 to 7). These numbers are taken from the locale resource data or the
* locale itself when a {@code Calendar} is constructed. If the designated
* locale contains "fw" and/or "rg" <a href="./Locale.html#def_locale_extension">
* Unicode extensions</a>, the first day of the week will be obtained according to
* those extensions. If both "fw" and "rg" are specified, the value from the "fw"
* extension supersedes the implicit one from the "rg" extension.
* They may also be specified explicitly through the methods for setting their
* values.
*
* <p>When setting or getting the <code>WEEK_OF_MONTH</code> or
* <code>WEEK_OF_YEAR</code> fields, <code>Calendar</code> must determine the
* first week of the month or year as a reference point. The first week of a
* month or year is defined as the earliest seven day period beginning on
* <code>getFirstDayOfWeek()</code> and containing at least
* <code>getMinimalDaysInFirstWeek()</code> days of that month or year. Weeks
* numbered ..., -1, 0 precede the first week; weeks numbered 2, 3,... follow
* it. Note that the normalized numbering returned by <code>get()</code> may be
* different. For example, a specific <code>Calendar</code> subclass may
* designate the week before week 1 of a year as week <code><i>n</i></code> of
* the previous year.
*
* <h4>Calendar Fields Resolution</h4>
*
* When computing a date and time from the calendar fields, there
* may be insufficient information for the computation (such as only
* year and month with no day of month), or there may be inconsistent
* information (such as Tuesday, July 15, 1996 (Gregorian) -- July 15,
* 1996 is actually a Monday). <code>Calendar</code> will resolve
* calendar field values to determine the date and time in the
* following way.
*
* <p><a id="resolution">If there is any conflict in calendar field values,
* <code>Calendar</code> gives priorities to calendar fields that have been set
* more recently.</a> The following are the default combinations of the
* calendar fields. The most recent combination, as determined by the
* most recently set single field, will be used.
*
* <p><a id="date_resolution">For the date fields</a>:
* <blockquote>
* <pre>
* YEAR + MONTH + DAY_OF_MONTH
* YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
* YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
* YEAR + DAY_OF_YEAR
* YEAR + DAY_OF_WEEK + WEEK_OF_YEAR
* </pre></blockquote>
*
* <a id="time_resolution">For the time of day fields</a>:
* <blockquote>
* <pre>
* HOUR_OF_DAY
* AM_PM + HOUR
* </pre></blockquote>
*
* <p>If there are any calendar fields whose values haven't been set in the selected
* field combination, <code>Calendar</code> uses their default values. The default
* value of each field may vary by concrete calendar systems. For example, in
* <code>GregorianCalendar</code>, the default of a field is the same as that
* of the start of the Epoch: i.e., <code>YEAR = 1970</code>, <code>MONTH =
* JANUARY</code>, <code>DAY_OF_MONTH = 1</code>, etc.
*
* <p>
* <strong>Note:</strong> There are certain possible ambiguities in
* interpretation of certain singular times, which are resolved in the
* following ways:
* <ol>
* <li> 23:59 is the last minute of the day and 00:00 is the first
* minute of the next day. Thus, 23:59 on Dec 31, 1999 < 00:00 on
* Jan 1, 2000 < 00:01 on Jan 1, 2000.
*
* <li> Although historically not precise, midnight also belongs to "am",
* and noon belongs to "pm", so on the same day,
* 12:00 am (midnight) < 12:01 am, and 12:00 pm (noon) < 12:01 pm
* </ol>
*
* <p>
* The date or time format strings are not part of the definition of a
* calendar, as those must be modifiable or overridable by the user at
* runtime. Use {@link DateFormat}
* to format dates.
*
* <h4>Field Manipulation</h4>
*
* The calendar fields can be changed using three methods:
* <code>set()</code>, <code>add()</code>, and <code>roll()</code>.
*
* <p><strong><code>set(f, value)</code></strong> changes calendar field
* <code>f</code> to <code>value</code>. In addition, it sets an
* internal member variable to indicate that calendar field <code>f</code> has
* been changed. Although calendar field <code>f</code> is changed immediately,
* the calendar's time value in milliseconds is not recomputed until the next call to
* <code>get()</code>, <code>getTime()</code>, <code>getTimeInMillis()</code>,
* <code>add()</code>, or <code>roll()</code> is made. Thus, multiple calls to
* <code>set()</code> do not trigger multiple, unnecessary
* computations. As a result of changing a calendar field using
* <code>set()</code>, other calendar fields may also change, depending on the
* calendar field, the calendar field value, and the calendar system. In addition,
* <code>get(f)</code> will not necessarily return <code>value</code> set by
* the call to the <code>set</code> method
* after the calendar fields have been recomputed. The specifics are determined by
* the concrete calendar class.</p>
*
* <p><em>Example</em>: Consider a <code>GregorianCalendar</code>
* originally set to August 31, 1999. Calling <code>set(Calendar.MONTH,
* Calendar.SEPTEMBER)</code> sets the date to September 31,
* 1999. This is a temporary internal representation that resolves to
* October 1, 1999 if <code>getTime()</code>is then called. However, a
* call to <code>set(Calendar.DAY_OF_MONTH, 30)</code> before the call to
* <code>getTime()</code> sets the date to September 30, 1999, since
* no recomputation occurs after <code>set()</code> itself.</p>
*
* <p><strong><code>add(f, delta)</code></strong> adds <code>delta</code>
* to field <code>f</code>. This is equivalent to calling <code>set(f,
* get(f) + delta)</code> with two adjustments:</p>
*
* <blockquote>
* <p><strong>Add rule 1</strong>. The value of field <code>f</code>
* after the call minus the value of field <code>f</code> before the
* call is <code>delta</code>, modulo any overflow that has occurred in
* field <code>f</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><strong>Add rule 2</strong>. 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 field
* <code>f</code> is changed or other constraints, such as time zone
* offset changes, 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>
* </blockquote>
*
* <p>In addition, unlike <code>set()</code>, <code>add()</code> forces
* an immediate recomputation of the calendar's milliseconds and all
* fields.</p>
*
* <p><em>Example</em>: Consider a <code>GregorianCalendar</code>
* originally set to August 31, 1999. Calling <code>add(Calendar.MONTH,
* 13)</code> sets the calendar to September 30, 2000. <strong>Add rule
* 1</strong> sets the <code>MONTH</code> field to September, since
* adding 13 months to August gives September of the next year. Since
* <code>DAY_OF_MONTH</code> cannot be 31 in September in a
* <code>GregorianCalendar</code>, <strong>add rule 2</strong> sets the
* <code>DAY_OF_MONTH</code> to 30, the closest possible value. Although
* it is a smaller field, <code>DAY_OF_WEEK</code> is not adjusted by
* rule 2, since it is expected to change when the month changes in a
* <code>GregorianCalendar</code>.</p>
*
* <p><strong><code>roll(f, delta)</code></strong> adds
* <code>delta</code> to field <code>f</code> without changing larger
* fields. This is equivalent to calling <code>add(f, delta)</code> with
* the following adjustment:</p>
*
* <blockquote>
* <p><strong>Roll rule</strong>. Larger fields are unchanged after the
* call. A larger field represents a larger unit of
* time. <code>DAY_OF_MONTH</code> is a larger field than
* <code>HOUR</code>.</p>
* </blockquote>
*
* <p><em>Example</em>: See {@link java.util.GregorianCalendar#roll(int, int)}.
*
* <p><strong>Usage model</strong>. To motivate the behavior of
* <code>add()</code> and <code>roll()</code>, consider a user interface
* component with increment and decrement buttons for the month, day, and
* year, and an underlying <code>GregorianCalendar</code>. If the
* interface reads January 31, 1999 and the user presses the month
* increment button, what should it read? If the underlying
* implementation uses <code>set()</code>, it might read March 3, 1999. A
* better result would be February 28, 1999. Furthermore, if the user
* presses the month increment button again, it should read March 31,
* 1999, not March 28, 1999. By saving the original date and using either
* <code>add()</code> or <code>roll()</code>, depending on whether larger
* fields should be affected, the user interface can behave as most users
* will intuitively expect.</p>
*
* @see java.lang.System#currentTimeMillis()
* @see Date
* @see GregorianCalendar
* @see TimeZone
* @see java.text.DateFormat
* @author Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu
* @since 1.1
*/
/*
* 历法系统,应用于JDK8之前
*
* Calendar支持的历法系统包括:
* GregorianCalendar - 公历系统
* BuddhistCalendar - 泰国佛教历
* JapaneseImperialCalendar - 日本历
*
* 在JDK8之前,Calendar可以替代难用的Date,不过Calendar的使用依然不尽人意。
*
* 注:JDK8之后,应当使用Chronology替代Calendar
*/
public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar> {
/*
* Data flow in Calendar
* ---------------------
* The current time is represented in two ways by Calendar: as UTC
* milliseconds from the epoch (1 January 1970 0:00 UTC), and as local
* fields such as MONTH, HOUR, AM_PM, etc. It is possible to compute the
* millis from the fields, and vice versa. The data needed to do this
* conversion is encapsulated by a TimeZone object owned by the Calendar.
* The data provided by the TimeZone object may also be overridden if the
* user sets the ZONE_OFFSET and/or DST_OFFSET fields directly. The class
* keeps track of what information was most recently set by the caller, and
* uses that to compute any other information as needed.
* If the user sets the fields using set(), the data flow is as follows.
* This is implemented by the Calendar subclass's computeTime() method.
* During this process, certain fields may be ignored. The disambiguation
* algorithm for resolving which fields to pay attention to is described
* in the class documentation.
* local fields (YEAR, MONTH, DATE, HOUR, MINUTE, etc.)
* |
* | Using Calendar-specific algorithm
* V
* local standard millis
* |
* | Using TimeZone or user-set ZONE_OFFSET / DST_OFFSET
* V
* UTC millis (in time data member)
* If the user sets the UTC millis using setTime() or setTimeInMillis(),
* the data flow is as follows. This is implemented by the Calendar
* subclass's computeFields() method.
* UTC millis (in time data member)
* |
* | Using TimeZone getOffset()
* V
* local standard millis
* |
* | Using Calendar-specific algorithm
* V
* local fields (YEAR, MONTH, DATE, HOUR, MINUTE, etc.)
* In general, a round trip from fields, through local and UTC millis, and
* back out to fields is made when necessary. This is implemented by the
* complete() method. Resolving a partial set of fields into a UTC millis
* value allows all remaining fields to be generated from that value. If
* the Calendar is lenient, the fields are also renormalized to standard
* ranges when they are regenerated.
*/
/*▼ "周"常量 ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┓ */
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Sunday.
*/
// 周日
public static final int SUNDAY = 1;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Monday.
*/
// 周一
public static final int MONDAY = 2;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Tuesday.
*/
// 周二
public static final int TUESDAY = 3;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Wednesday.
*/
// 周三
public static final int WEDNESDAY = 4;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Thursday.
*/
// 周四
public static final int THURSDAY = 5;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Friday.
*/
// 周五
public static final int FRIDAY = 6;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Saturday.
*/
// 周六
public static final int SATURDAY = 7;
/*▲ "周"常量 ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┛ */
/*▼ "月"常量 ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┓ */
/**
* Value of the {@link #MONTH} field indicating the
* first month of the year in the Gregorian and Julian calendars.
*/
public static final int JANUARY = 0;
/**
* Value of the {@link #MONTH} field indicating the
* second month of the year in the Gregorian and Julian calendars.
*/
public static final int FEBRUARY = 1;
/**
* Value of the {@link #MONTH} field indicating the
* third month of the year in the Gregorian and Julian calendars.
*/
public static final int MARCH = 2;
/**
* Value of the {@link #MONTH} field indicating the
* fourth month of the year in the Gregorian and Julian calendars.
*/
public static final int APRIL = 3;
/**
* Value of the {@link #MONTH} field indicating the
* fifth month of the year in the Gregorian and Julian calendars.
*/
public static final int MAY = 4;
/**
* Value of the {@link #MONTH} field indicating the
* sixth month of the year in the Gregorian and Julian calendars.
*/
public static final int JUNE = 5;
/**
* Value of the {@link #MONTH} field indicating the
* seventh month of the year in the Gregorian and Julian calendars.
*/
public static final int JULY = 6;
/**
* Value of the {@link #MONTH} field indicating the
* eighth month of the year in the Gregorian and Julian calendars.
*/
public static final int AUGUST = 7;
/**
* Value of the {@link #MONTH} field indicating the
* ninth month of the year in the Gregorian and Julian calendars.
*/
public static final int SEPTEMBER = 8;
/**
* Value of the {@link #MONTH} field indicating the
* tenth month of the year in the Gregorian and Julian calendars.
*/
public static final int OCTOBER = 9;
/**
* Value of the {@link #MONTH} field indicating the
* eleventh month of the year in the Gregorian and Julian calendars.
*/
public static final int NOVEMBER = 10;
/**
* Value of the {@link #MONTH} field indicating the
* twelfth month of the year in the Gregorian and Julian calendars.
*/
public static final int DECEMBER = 11;
/**
* Value of the {@link #MONTH} field indicating the
* thirteenth month of the year. Although <code>GregorianCalendar</code>
* does not use this value, lunar calendars do.
*/
public static final int UNDECIMBER = 12;
/*▲ "月"常量 ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┛ */
/*▼ "上/下午"常量 ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┓ */
/**
* Value of the {@link #AM_PM} field indicating the
* period of the day from midnight to just before noon.
*/
public static final int AM = 0;
/**
* Value of the {@link #AM_PM} field indicating the
* period of the day from noon to just before midnight.
*/
public static final int PM = 1;
/*▲ "上/下午"常量 ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┛ */
/*▼ 历法显示格式 ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┓ */
static final int STANDALONE_MASK = 0x8000;
/**
* A style specifier for {@link #getDisplayNames(int, int, Locale)
* getDisplayNames} indicating names in all styles, such as
* "January" and "Jan".
*
* @see #SHORT_FORMAT
* @see #LONG_FORMAT
* @see #SHORT_STANDALONE
* @see #LONG_STANDALONE
* @see #SHORT
* @see #LONG
* @since 1.6
*/
public static final int ALL_STYLES = 0;
/**
* A style specifier for {@link #getDisplayName(int, int, Locale)
* getDisplayName} and {@link #getDisplayNames(int, int, Locale)
* getDisplayNames} equivalent to {@link #SHORT_FORMAT}.
*
* @see #SHORT_STANDALONE
* @see #LONG
* @since 1.6
*/
public static final int SHORT = 1;
/**
* A style specifier for {@link #getDisplayName(int, int, Locale)
* getDisplayName} and {@link #getDisplayNames(int, int, Locale)
* getDisplayNames} equivalent to {@link #LONG_FORMAT}.
*
* @see #LONG_STANDALONE
* @see #SHORT
* @since 1.6
*/
public static final int LONG = 2;
/**
* A style specifier for {@link #getDisplayName(int, int, Locale)
* getDisplayName} and {@link #getDisplayNames(int, int, Locale)
* getDisplayNames} indicating a narrow name used for format. Narrow names
* are typically single character strings, such as "M" for Monday.
*
* @see #NARROW_STANDALONE
* @see #SHORT_FORMAT
* @see #LONG_FORMAT
* @since 1.8
*/
public static final int NARROW_FORMAT = 4;
/**
* A style specifier for {@link #getDisplayName(int, int, Locale)
* getDisplayName} and {@link #getDisplayNames(int, int, Locale)
* getDisplayNames} indicating a narrow name independently. Narrow names
* are typically single character strings, such as "M" for Monday.
*
* @see #NARROW_FORMAT
* @see #SHORT_STANDALONE
* @see #LONG_STANDALONE
* @since 1.8
*/
public static final int NARROW_STANDALONE = NARROW_FORMAT | STANDALONE_MASK;
/**
* A style specifier for {@link #getDisplayName(int, int, Locale)
* getDisplayName} and {@link #getDisplayNames(int, int, Locale)
* getDisplayNames} indicating a short name used for format.
*
* @see #SHORT_STANDALONE
* @see #LONG_FORMAT
* @see #LONG_STANDALONE
* @since 1.8
*/
public static final int SHORT_FORMAT = 1;
/**
* A style specifier for {@link #getDisplayName(int, int, Locale)
* getDisplayName} and {@link #getDisplayNames(int, int, Locale)
* getDisplayNames} indicating a long name used for format.
*
* @see #LONG_STANDALONE
* @see #SHORT_FORMAT
* @see #SHORT_STANDALONE
* @since 1.8
*/
public static final int LONG_FORMAT = 2;
/**
* A style specifier for {@link #getDisplayName(int, int, Locale)
* getDisplayName} and {@link #getDisplayNames(int, int, Locale)
* getDisplayNames} indicating a short name used independently,
* such as a month abbreviation as calendar headers.
*
* @see #SHORT_FORMAT
* @see #LONG_FORMAT
* @see #LONG_STANDALONE
* @since 1.8
*/
public static final int SHORT_STANDALONE = SHORT | STANDALONE_MASK;
/**
* A style specifier for {@link #getDisplayName(int, int, Locale)
* getDisplayName} and {@link #getDisplayNames(int, int, Locale)
* getDisplayNames} indicating a long name used independently,
* such as a month name as calendar headers.
*
* @see #LONG_FORMAT
* @see #SHORT_FORMAT
* @see #SHORT_STANDALONE
* @since 1.8
*/
public static final int LONG_STANDALONE = LONG | STANDALONE_MASK;
/*▲ 历法显示格式 ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┛ */
/*▼ Calendar字段 ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┓ */
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* era, e.g., AD or BC in the Julian calendar. This is a calendar-specific
* value; see subclass documentation.
*
* @see GregorianCalendar#AD
* @see GregorianCalendar#BC
*/
// 纪元,公元前AD/公元(后)BC
public static final int ERA = 0;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* year. This is a calendar-specific value; see subclass documentation.
*/
// 年份
public static final int YEAR = 1;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* month. This is a calendar-specific value. The first month of
* the year in the Gregorian and Julian calendars is
* <code>JANUARY</code> which is 0; the last depends on the number
* of months in a year.
*
* @see #JANUARY
* @see #FEBRUARY
* @see #MARCH
* @see #APRIL
* @see #MAY
* @see #JUNE
* @see #JULY
* @see #AUGUST
* @see #SEPTEMBER
* @see #OCTOBER
* @see #NOVEMBER
* @see #DECEMBER
* @see #UNDECIMBER
*/
// 月份,这里是从0开始计数
public static final int MONTH = 2;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* week number within the current year. The first week of the year, as
* defined by <code>getFirstDayOfWeek()</code> and
* <code>getMinimalDaysInFirstWeek()</code>, has value 1. Subclasses define
* the value of <code>WEEK_OF_YEAR</code> for days before the first week of
* the year.
*
* @see #getFirstDayOfWeek
* @see #getMinimalDaysInFirstWeek
*/
// 月视图中本年第几周(周日作为一周起始)
public static final int WEEK_OF_YEAR = 3;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* week number within the current month. The first week of the month, as
* defined by <code>getFirstDayOfWeek()</code> and
* <code>getMinimalDaysInFirstWeek()</code>, has value 1. Subclasses define
* the value of <code>WEEK_OF_MONTH</code> for days before the first week of
* the month.
*
* @see #getFirstDayOfWeek
* @see #getMinimalDaysInFirstWeek
*/
// 月视图中本月第几周(周日作为一周的起始)
public static final int WEEK_OF_MONTH = 4;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* day of the month. This is a synonym for <code>DAY_OF_MONTH</code>.
* The first day of the month has value 1.
*
* @see #DAY_OF_MONTH
*/
// 日
public static final int DATE = 5;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* day of the month. This is a synonym for <code>DATE</code>.
* The first day of the month has value 1.
*
* @see #DATE
*/
// 当前月中第几天,同DATE
public static final int DAY_OF_MONTH = 5;
/**
* Field number for <code>get</code> and <code>set</code> indicating the day
* number within the current year. The first day of the year has value 1.
*/
// 当前年中第几天
public static final int DAY_OF_YEAR = 6;
/**
* Field number for <code>get</code> and <code>set</code> indicating the day
* of the week. This field takes values <code>SUNDAY</code>,
* <code>MONDAY</code>, <code>TUESDAY</code>, <code>WEDNESDAY</code>,
* <code>THURSDAY</code>, <code>FRIDAY</code>, and <code>SATURDAY</code>.
*
* @see #SUNDAY
* @see #MONDAY
* @see #TUESDAY
* @see #WEDNESDAY
* @see #THURSDAY
* @see #FRIDAY
* @see #SATURDAY
*/
// 当前周中第几天,第一天是星期日
public static final int DAY_OF_WEEK = 7;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* ordinal number of the day of the week within the current month. Together
* with the <code>DAY_OF_WEEK</code> field, this uniquely specifies a day
* within a month. Unlike <code>WEEK_OF_MONTH</code> and
* <code>WEEK_OF_YEAR</code>, this field's value does <em>not</em> depend on
* <code>getFirstDayOfWeek()</code> or
* <code>getMinimalDaysInFirstWeek()</code>. <code>DAY_OF_MONTH 1</code>
* through <code>7</code> always correspond to <code>DAY_OF_WEEK_IN_MONTH
* 1</code>; <code>8</code> through <code>14</code> correspond to
* <code>DAY_OF_WEEK_IN_MONTH 2</code>, and so on.
* <code>DAY_OF_WEEK_IN_MONTH 0</code> indicates the week before
* <code>DAY_OF_WEEK_IN_MONTH 1</code>. Negative values count back from the
* end of the month, so the last Sunday of a month is specified as
* <code>DAY_OF_WEEK = SUNDAY, DAY_OF_WEEK_IN_MONTH = -1</code>. Because
* negative values count backward they will usually be aligned differently
* within the month than positive values. For example, if a month has 31
* days, <code>DAY_OF_WEEK_IN_MONTH -1</code> will overlap
* <code>DAY_OF_WEEK_IN_MONTH 5</code> and the end of <code>4</code>.
*
* @see #DAY_OF_WEEK
* @see #WEEK_OF_MONTH
*/
// 周视图中本月第几周(本月第一天作为第一周的第一天)
public static final int DAY_OF_WEEK_IN_MONTH = 8;
/**
* Field number for <code>get</code> and <code>set</code> indicating
* whether the <code>HOUR</code> is before or after noon.
* E.g., at 10:04:15.250 PM the <code>AM_PM</code> is <code>PM</code>.
*
* @see #AM
* @see #PM
* @see #HOUR
*/
// 上午/下午
public static final int AM_PM = 9;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* hour of the morning or afternoon. <code>HOUR</code> is used for the
* 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12.
* E.g., at 10:04:15.250 PM the <code>HOUR</code> is 10.
*
* @see #AM_PM
* @see #HOUR_OF_DAY
*/
// 时,12小时制
public static final int HOUR = 10;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* hour of the day. <code>HOUR_OF_DAY</code> is used for the 24-hour clock.
* E.g., at 10:04:15.250 PM the <code>HOUR_OF_DAY</code> is 22.
*
* @see #HOUR
*/
// 时,24小时制
public static final int HOUR_OF_DAY = 11;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* minute within the hour.
* E.g., at 10:04:15.250 PM the <code>MINUTE</code> is 4.
*/
// 分
public static final int MINUTE = 12;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* second within the minute.
* E.g., at 10:04:15.250 PM the <code>SECOND</code> is 15.
*/
// 秒
public static final int SECOND = 13;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* millisecond within the second.
* E.g., at 10:04:15.250 PM the <code>MILLISECOND</code> is 250.
*/
// 毫秒
public static final int MILLISECOND = 14;
/**
* Field number for <code>get</code> and <code>set</code>
* indicating the raw offset from GMT in milliseconds.
* <p>
* This field reflects the correct GMT offset value of the time
* zone of this <code>Calendar</code> if the
* <code>TimeZone</code> implementation subclass supports
* historical GMT offset changes.
*/
// 以毫秒为单位的时差
public static final int ZONE_OFFSET = 15;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* daylight saving offset in milliseconds.
* <p>
* This field reflects the correct daylight saving offset value of
* the time zone of this <code>Calendar</code> if the
* <code>TimeZone</code> implementation subclass supports
* historical Daylight Saving Time schedule changes.
*/
// 以毫秒为单位的夏令时偏移量
public static final int DST_OFFSET = 16;
/**
* The number of distinct fields recognized by <code>get</code> and <code>set</code>.
* Field numbers range from <code>0..FIELD_COUNT-1</code>.
*/
// 用来表示上面这些字段的边界,即总共有18个可用字段(其中有两个字段是重复的)
public static final int FIELD_COUNT = 17;
// Calendar字段掩码
@SuppressWarnings("PointlessBitwiseExpression")
static final int ERA_MASK = (1 << ERA);
static final int YEAR_MASK = (1 << YEAR);
static final int MONTH_MASK = (1 << MONTH);
static final int WEEK_OF_YEAR_MASK = (1 << WEEK_OF_YEAR);
static final int WEEK_OF_MONTH_MASK = (1 << WEEK_OF_MONTH);
static final int DAY_OF_MONTH_MASK = (1 << DAY_OF_MONTH);
static final int DATE_MASK = DAY_OF_MONTH_MASK;
static final int DAY_OF_YEAR_MASK = (1 << DAY_OF_YEAR);
static final int DAY_OF_WEEK_MASK = (1 << DAY_OF_WEEK);
static final int DAY_OF_WEEK_IN_MONTH_MASK = (1 << DAY_OF_WEEK_IN_MONTH);
static final int AM_PM_MASK = (1 << AM_PM);
static final int HOUR_MASK = (1 << HOUR);
static final int HOUR_OF_DAY_MASK = (1 << HOUR_OF_DAY);
static final int MINUTE_MASK = (1 << MINUTE);
static final int SECOND_MASK = (1 << SECOND);
static final int MILLISECOND_MASK = (1 << MILLISECOND);
static final int ZONE_OFFSET_MASK = (1 << ZONE_OFFSET);
static final int DST_OFFSET_MASK = (1 << DST_OFFSET);
// 17个Calendar字段名称的集合(未包括DATE,它与DAY_OF_MONTH是等价的)
private static final String[] FIELD_NAME = {"ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH", "DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK", "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY", "MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET", "DST_OFFSET"};
/*▲ Calendar字段 ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┛ */
// Internal notes:
// Calendar contains two kinds of time representations: current "time" in milliseconds, and a set of calendar "fields" representing the current time.
// The two representations are usually in sync, but can get out of sync as follows.
// 1. Initially, no fields are set, and the time is invalid.
// 2. If the time is set, all fields are computed and in sync.
// 3. If a single field is set, the time is invalid.
// Recomputation of the time and fields happens when the object needs to return a result to the user, or use a result for a computation.
/**
* The calendar field values for the currently set time for this calendar.
* This is an array of <code>FIELD_COUNT</code> integers, with index values
* <code>ERA</code> through <code>DST_OFFSET</code>.
*
* @serial
*/
@SuppressWarnings("ProtectedField")
protected int fields[]; // 当前所有Calendar字段的值
/**
* The flags which tell if a specified calendar field for the calendar is set.
* A new object has no fields set. After the first call to a method
* which generates the fields, they all remain set after that.
* This is an array of <code>FIELD_COUNT</code> booleans, with index values
* <code>ERA</code> through <code>DST_OFFSET</code>.
*
* @serial
*/
@SuppressWarnings("ProtectedField")
protected boolean isSet[]; // 某个Calendar字段是否设置了有效值
/**
* Pseudo-time-stamps which specify when each field was set. There
* are two special values, UNSET and COMPUTED. Values from
* MINIMUM_USER_SET to Integer.MAX_VALUE are legal user set values.
*/
private transient int stamp[]; // 用来指示某个Calendar字段是否已设置,顺便会记录该字段的设置顺序(伪时间戳)
/**
* The currently set time for this calendar, expressed in milliseconds after
* January 1, 1970, 0:00:00 GMT.
*
* @serial
* @see #isTimeSet
*/
@SuppressWarnings("ProtectedField")
protected long time; // 纪元毫秒,用来生成Date
/**
* True if then the value of <code>time</code> is valid.
* The time is made invalid by a change to an item of <code>field[]</code>.
*
* @serial
* @see #time
*/
@SuppressWarnings("ProtectedField")
protected boolean isTimeSet; // time属性是否有效,Calendar字段的更改会使time字段失效
/**
* True if <code>fields[]</code> are in sync with the currently set time.
* If false, then the next attempt to get the value of a field will
* force a recomputation of all fields from the current value of
* <code>time</code>.
*
* @serial
*/
@SuppressWarnings("ProtectedField")
protected boolean areFieldsSet; // 当前Calendar字段(fields属性)与当前设置的时间(time属性)是否同步
/**
* True if all fields have been set.
*
* @serial
*/
transient boolean areAllFieldsSet; // 是否所有Calendar字段都已设置
/**
* <code>True</code> if this calendar allows out-of-range field values during computation
* of <code>time</code> from <code>fields[]</code>.
*
* @serial
* @see #setLenient
* @see #isLenient
*/
/*
* 指定对日期-时间的解析是否宽松(默认值为宽松)。
*
* 如果宽松的话,"1996年2月942日"这类日期会被视为等同于1996年2月1日之后的第941天,
* 但如果是使用非宽松(严格)的解析,那么这样的日期将触发异常。