forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathIsoChronology.java
868 lines (779 loc) · 37.7 KB
/
IsoChronology.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
/*
* Copyright (c) 2012, 2016, 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.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of JSR-310 nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package java.time.chrono;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.time.Clock;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.Period;
import java.time.Year;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.ResolverStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
import java.time.temporal.ValueRange;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
import static java.time.temporal.ChronoField.ERA;
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
import static java.time.temporal.ChronoField.YEAR;
import static java.time.temporal.ChronoField.YEAR_OF_ERA;
/**
* The ISO calendar system.
* <p>
* This chronology defines the rules of the ISO calendar system.
* This calendar system is based on the ISO-8601 standard, which is the
* <i>de facto</i> world calendar.
* <p>
* The fields are defined as follows:
* <ul>
* <li>era - There are two eras, 'Current Era' (CE) and 'Before Current Era' (BCE).
* <li>year-of-era - The year-of-era is the same as the proleptic-year for the current CE era.
* For the BCE era before the ISO epoch the year increases from 1 upwards as time goes backwards.
* <li>proleptic-year - The proleptic year is the same as the year-of-era for the
* current era. For the previous era, years have zero, then negative values.
* <li>month-of-year - There are 12 months in an ISO year, numbered from 1 to 12.
* <li>day-of-month - There are between 28 and 31 days in each of the ISO month, numbered from 1 to 31.
* Months 4, 6, 9 and 11 have 30 days, Months 1, 3, 5, 7, 8, 10 and 12 have 31 days.
* Month 2 has 28 days, or 29 in a leap year.
* <li>day-of-year - There are 365 days in a standard ISO year and 366 in a leap year.
* The days are numbered from 1 to 365 or 1 to 366.
* <li>leap-year - Leap years occur every 4 years, except where the year is divisble by 100 and not divisble by 400.
* </ul>
*
* @implSpec This class is immutable and thread-safe.
* @since 1.8
*/
// ISO历法系统,可以在一定程度上认为等同于公历
public final class IsoChronology extends AbstractChronology implements Serializable {
/**
* Singleton instance of the ISO chronology.
*/
public static final IsoChronology INSTANCE = new IsoChronology();
/*
* 公历0年到公历1970年包含的天数
*
* 从0年到2000年之间有5个400年周期。
* 从1970年到2000年有7个闰年。
*/
private static final long DAYS_0000_TO_1970 = (146097 * 5L) - (30L * 365L + 7L); // taken from LocalDate
/*▼ 构造器 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Restricted constructor.
*/
private IsoChronology() {
}
/*▲ 构造器 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 历法系统 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Gets the ID of the chronology - 'ISO'.
* <p>
* The ID uniquely identifies the {@code Chronology}.
* It can be used to lookup the {@code Chronology} using {@link Chronology#of(String)}.
*
* @return the chronology ID - 'ISO'
*
* @see #getCalendarType()
*/
// 返回历法的ID
@Override
public String getId() {
return "ISO";
}
/**
* Gets the calendar type of the underlying calendar system - 'iso8601'.
* <p>
* The calendar type is an identifier defined by the
* <em>Unicode Locale Data Markup Language (LDML)</em> specification.
* It can be used to lookup the {@code Chronology} using {@link Chronology#of(String)}.
* It can also be used as part of a locale, accessible via
* {@link Locale#getUnicodeLocaleType(String)} with the key 'ca'.
*
* @return the calendar system type - 'iso8601'
*
* @see #getId()
*/
// 返回历法的类型
@Override
public String getCalendarType() {
return "iso8601";
}
/*▲ 历法系统 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 本地日期 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Obtains the current ISO local date from the system clock in the default time-zone.
* <p>
* This will query the {@link Clock#systemDefaultZone() system clock} in the default
* time-zone to obtain the current date.
* <p>
* Using this method will prevent the ability to use an alternate clock for testing
* because the clock is hard-coded.
*
* @return the current ISO local date using the system clock and default time-zone, not null
*
* @throws DateTimeException if unable to create the date
*/
// 基于此刻的UTC时间,构造属于系统默认时区的"本地日期"对象
@Override
public LocalDate dateNow() {
// 获取一个系统时钟,其预设的时区ID为系统默认的时区ID
Clock clock = Clock.systemDefaultZone();
return dateNow(clock);
}
/**
* Obtains the current ISO local date from the system clock in the specified time-zone.
* <p>
* This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date.
* Specifying the time-zone avoids dependence on the default time-zone.
* <p>
* Using this method will prevent the ability to use an alternate clock for testing
* because the clock is hard-coded.
*
* @return the current ISO local date using the system clock, not null
*
* @throws DateTimeException if unable to create the date
*/
// 基于此刻的UTC时间,构造属于zone时区的"本地日期"对象
@Override
public LocalDate dateNow(ZoneId zone) {
// 获取一个系统时钟,其预设的时区ID为zoneId
Clock clock = Clock.system(zone);
return dateNow(clock);
}
/**
* Obtains the current ISO local date from the specified clock.
* <p>
* This will query the specified clock to obtain the current date - today.
* Using this method allows the use of an alternate clock for testing.
* The alternate clock may be introduced using {@link Clock dependency injection}.
*
* @param clock the clock to use, not null
*
* @return the current ISO local date, not null
*
* @throws DateTimeException if unable to create the date
*/
// 基于clock提供的时间戳和时区ID构造"本地日期"对象
@Override
public LocalDate dateNow(Clock clock) {
Objects.requireNonNull(clock, "clock");
// 基于clock提供的时间戳和时区ID构造"本地日期"对象
LocalDate localDate = LocalDate.now(clock);
return date(localDate);
}
/**
* Obtains an ISO local date from another date-time object.
* <p>
* This is equivalent to {@link LocalDate#from(TemporalAccessor)}.
*
* @param temporal the date-time object to convert, not null
*
* @return the ISO local date, not null
*
* @throws DateTimeException if unable to create the date
*/
/*
* 从temporal中查询LocalDate部件。
*
* 如果没有现成的部件,通常需要从temporal中解析出纪元天,
* 然后使用纪元天构造LocalDate后返回。
*/
@Override
public LocalDate date(TemporalAccessor temporal) {
return LocalDate.from(temporal);
}
/**
* Obtains an ISO local date from the era, year-of-era, month-of-year
* and day-of-month fields.
*
* @param era the ISO era, not null
* @param yearOfEra the ISO year-of-era
* @param month the ISO month-of-year
* @param dayOfMonth the ISO day-of-month
*
* @return the ISO local date, not null
*
* @throws DateTimeException if unable to create the date
* @throws ClassCastException if the type of {@code era} is not {@code IsoEra}
*/
/*
* 根据纪元、ISO年份、月份和月份中的天数构造"本地日期"对象
*
* era : 纪元
* year : 年份,这里应当传入ISO年份(公历年)
* month : 月份
* dayOfMonth: 一月中的第几天
*/
@Override
public LocalDate date(Era era, int yearOfEra, int month, int dayOfMonth) {
int prolepticYear = prolepticYear(era, yearOfEra);
return date(prolepticYear, month, dayOfMonth);
}
/**
* Obtains an ISO local date from the era, year-of-era and day-of-year fields.
*
* @param era the ISO era, not null
* @param yearOfEra the ISO year-of-era
* @param dayOfYear the ISO day-of-year
*
* @return the ISO local date, not null
*
* @throws DateTimeException if unable to create the date
*/
// 根据纪元、ISO年份和年份中的天数构造"本地日期"对象
@Override
public LocalDate dateYearDay(Era era, int yearOfEra, int dayOfYear) {
// 将era处的ISO年份(公历年)转换为"Proleptic年"
int prolepticYear = prolepticYear(era, yearOfEra);
return dateYearDay(prolepticYear, dayOfYear);
}
/**
* Obtains an ISO local date from the proleptic-year, month-of-year
* and day-of-month fields.
* <p>
* This is equivalent to {@link LocalDate#of(int, int, int)}.
*
* @param prolepticYear the ISO proleptic-year
* @param month the ISO month-of-year
* @param dayOfMonth the ISO day-of-month
*
* @return the ISO local date, not null
*
* @throws DateTimeException if unable to create the date
*/
/*
* 根据Proleptic年份、月份和月份中的天数构造"本地日期"对象
*
* year : 年份,这里应当传入"Proleptic年"
* month : 月份
* dayOfMonth: 一月中的第几天
*/
@Override
public LocalDate date(int prolepticYear, int month, int dayOfMonth) {
return LocalDate.of(prolepticYear, month, dayOfMonth);
}
/**
* Obtains an ISO local date from the proleptic-year and day-of-year fields.
* <p>
* This is equivalent to {@link LocalDate#ofYearDay(int, int)}.
*
* @param prolepticYear the ISO proleptic-year
* @param dayOfYear the ISO day-of-year
*
* @return the ISO local date, not null
*
* @throws DateTimeException if unable to create the date
*/
// 根据Proleptic年份和年份中的天数构造"本地日期"对象
@Override
public LocalDate dateYearDay(int prolepticYear, int dayOfYear) {
return LocalDate.ofYearDay(prolepticYear, dayOfYear);
}
/**
* Obtains an ISO local date from the epoch-day.
* <p>
* This is equivalent to {@link LocalDate#ofEpochDay(long)}.
*
* @param epochDay the epoch day
*
* @return the ISO local date, not null
*
* @throws DateTimeException if unable to create the date
*/
// 根据给定的纪元天构造"本地日期"对象
@Override
public LocalDate dateEpochDay(long epochDay) {
return LocalDate.ofEpochDay(epochDay);
}
/**
* Resolves parsed {@code ChronoField} values into a date during parsing.
* <p>
* Most {@code TemporalField} implementations are resolved using the
* resolve method on the field. By contrast, the {@code ChronoField} class
* defines fields that only have meaning relative to the chronology.
* As such, {@code ChronoField} date fields are resolved here in the
* context of a specific chronology.
* <p>
* {@code ChronoField} instances on the ISO calendar system are resolved
* as follows.
* <ul>
* <li>{@code EPOCH_DAY} - If present, this is converted to a {@code LocalDate}
* and all other date fields are then cross-checked against the date.
* <li>{@code PROLEPTIC_MONTH} - If present, then it is split into the
* {@code YEAR} and {@code MONTH_OF_YEAR}. If the mode is strict or smart
* then the field is validated.
* <li>{@code YEAR_OF_ERA} and {@code ERA} - If both are present, then they
* are combined to form a {@code YEAR}. In lenient mode, the {@code YEAR_OF_ERA}
* range is not validated, in smart and strict mode it is. The {@code ERA} is
* validated for range in all three modes. If only the {@code YEAR_OF_ERA} is
* present, and the mode is smart or lenient, then the current era (CE/AD)
* is assumed. In strict mode, no era is assumed and the {@code YEAR_OF_ERA} is
* left untouched. If only the {@code ERA} is present, then it is left untouched.
* <li>{@code YEAR}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} -
* If all three are present, then they are combined to form a {@code LocalDate}.
* In all three modes, the {@code YEAR} is validated. If the mode is smart or strict,
* then the month and day are validated, with the day validated from 1 to 31.
* If the mode is lenient, then the date is combined in a manner equivalent to
* creating a date on the first of January in the requested year, then adding
* the difference in months, then the difference in days.
* If the mode is smart, and the day-of-month is greater than the maximum for
* the year-month, then the day-of-month is adjusted to the last day-of-month.
* If the mode is strict, then the three fields must form a valid date.
* <li>{@code YEAR} and {@code DAY_OF_YEAR} -
* If both are present, then they are combined to form a {@code LocalDate}.
* In all three modes, the {@code YEAR} is validated.
* If the mode is lenient, then the date is combined in a manner equivalent to
* creating a date on the first of January in the requested year, then adding
* the difference in days.
* If the mode is smart or strict, then the two fields must form a valid date.
* <li>{@code YEAR}, {@code MONTH_OF_YEAR}, {@code ALIGNED_WEEK_OF_MONTH} and
* {@code ALIGNED_DAY_OF_WEEK_IN_MONTH} -
* If all four are present, then they are combined to form a {@code LocalDate}.
* In all three modes, the {@code YEAR} is validated.
* If the mode is lenient, then the date is combined in a manner equivalent to
* creating a date on the first of January in the requested year, then adding
* the difference in months, then the difference in weeks, then in days.
* If the mode is smart or strict, then the all four fields are validated to
* their outer ranges. The date is then combined in a manner equivalent to
* creating a date on the first day of the requested year and month, then adding
* the amount in weeks and days to reach their values. If the mode is strict,
* the date is additionally validated to check that the day and week adjustment
* did not change the month.
* <li>{@code YEAR}, {@code MONTH_OF_YEAR}, {@code ALIGNED_WEEK_OF_MONTH} and
* {@code DAY_OF_WEEK} - If all four are present, then they are combined to
* form a {@code LocalDate}. The approach is the same as described above for
* years, months and weeks in {@code ALIGNED_DAY_OF_WEEK_IN_MONTH}.
* The day-of-week is adjusted as the next or same matching day-of-week once
* the years, months and weeks have been handled.
* <li>{@code YEAR}, {@code ALIGNED_WEEK_OF_YEAR} and {@code ALIGNED_DAY_OF_WEEK_IN_YEAR} -
* If all three are present, then they are combined to form a {@code LocalDate}.
* In all three modes, the {@code YEAR} is validated.
* If the mode is lenient, then the date is combined in a manner equivalent to
* creating a date on the first of January in the requested year, then adding
* the difference in weeks, then in days.
* If the mode is smart or strict, then the all three fields are validated to
* their outer ranges. The date is then combined in a manner equivalent to
* creating a date on the first day of the requested year, then adding
* the amount in weeks and days to reach their values. If the mode is strict,
* the date is additionally validated to check that the day and week adjustment
* did not change the year.
* <li>{@code YEAR}, {@code ALIGNED_WEEK_OF_YEAR} and {@code DAY_OF_WEEK} -
* If all three are present, then they are combined to form a {@code LocalDate}.
* The approach is the same as described above for years and weeks in
* {@code ALIGNED_DAY_OF_WEEK_IN_YEAR}. The day-of-week is adjusted as the
* next or same matching day-of-week once the years and weeks have been handled.
* </ul>
*
* @param fieldValues the map of fields to values, which can be updated, not null
* @param resolverStyle the requested type of resolve, not null
*
* @return the resolved date, null if insufficient information to create a date
*
* @throws DateTimeException if the date cannot be resolved, typically
* because of a conflict in the input data
*/
/*
* 从fieldValues中解析出时间量字段的值,然后用这些值来构造本地日期。
*
* resolverStyle: 指示解析时间量字段的策略
*/
@Override
public LocalDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
return (LocalDate) super.resolveDate(fieldValues, resolverStyle);
}
/*▲ 本地日期 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 本地日期-时间 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Obtains an ISO local date-time from another date-time object.
* <p>
* This is equivalent to {@link LocalDateTime#from(TemporalAccessor)}.
*
* @param temporal the date-time object to convert, not null
*
* @return the ISO local date-time, not null
*
* @throws DateTimeException if unable to create the date-time
*/
// 从temporal中获取/构造ChronoLocalDateTime对象
@Override
public LocalDateTime localDateTime(TemporalAccessor temporal) {
return LocalDateTime.from(temporal);
}
/**
* Obtains an ISO zoned date-time from another date-time object.
* <p>
* This is equivalent to {@link ZonedDateTime#from(TemporalAccessor)}.
*
* @param temporal the date-time object to convert, not null
*
* @return the ISO zoned date-time, not null
*
* @throws DateTimeException if unable to create the date-time
*/
// 从temporal中解析出时间戳和时区ID,然后用它们构造一个属于zone时区的"本地日期-时间"
@Override
public ZonedDateTime zonedDateTime(TemporalAccessor temporal) {
return ZonedDateTime.from(temporal);
}
/**
* Obtains an ISO zoned date-time in this chronology from an {@code Instant}.
* <p>
* This is equivalent to {@link ZonedDateTime#ofInstant(Instant, ZoneId)}.
*
* @param instant the instant to create the date-time from, not null
* @param zone the time-zone, not null
*
* @return the zoned date-time, not null
*
* @throws DateTimeException if the result exceeds the supported range
*/
// 使用时间戳和时区ID构造一个属于zone时区的"本地日期-时间"(时区偏移时间准确)
@Override
public ZonedDateTime zonedDateTime(Instant instant, ZoneId zone) {
return ZonedDateTime.ofInstant(instant, zone);
}
/*▲ 本地日期-时间 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 时间段 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Obtains a period for this chronology based on years, months and days.
* <p>
* This returns a period tied to the ISO chronology using the specified
* years, months and days. See {@link Period} for further details.
*
* @param years the number of years, may be negative
* @param months the number of years, may be negative
* @param days the number of years, may be negative
*
* @return the ISO period, not null
*/
// 返回由"年/月/天"构造的时间段
@Override
public Period period(int years, int months, int days) {
return Period.of(years, months, days);
}
/*▲ 时间段 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 转换 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Gets the number of seconds from the epoch of 1970-01-01T00:00:00Z.
* <p>
* The number of seconds is calculated using the year,
* month, day-of-month, hour, minute, second, and zoneOffset.
*
* @param prolepticYear the year, from MIN_YEAR to MAX_YEAR
* @param month the month-of-year, from 1 to 12
* @param dayOfMonth the day-of-month, from 1 to 31
* @param hour the hour-of-day, from 0 to 23
* @param minute the minute-of-hour, from 0 to 59
* @param second the second-of-minute, from 0 to 59
* @param zoneOffset the zone offset, not null
*
* @return the number of seconds relative to 1970-01-01T00:00:00Z, may be negative
*
* @throws DateTimeException if the value of any argument is out of range,
* or if the day-of-month is invalid for the month-of-year
* @since 9
*/
/*
* 将给定的日期部件和时间部件视为在zoneOffset时区的一个时间点,然后求该时间点下,UTC时区的纪元秒
*
* 注:这里传入的年份信息是"Proleptic年"
*/
@Override
public long epochSecond(int prolepticYear, int month, int dayOfMonth, int hour, int minute, int second, ZoneOffset zoneOffset) {
Objects.requireNonNull(zoneOffset, "zoneOffset");
YEAR.checkValidValue(prolepticYear);
MONTH_OF_YEAR.checkValidValue(month);
DAY_OF_MONTH.checkValidValue(dayOfMonth);
HOUR_OF_DAY.checkValidValue(hour);
MINUTE_OF_HOUR.checkValidValue(minute);
SECOND_OF_MINUTE.checkValidValue(second);
if(dayOfMonth>28) {
int dom = numberOfDaysOfMonth(prolepticYear, month);
if(dayOfMonth>dom) {
if(dayOfMonth == 29) {
throw new DateTimeException("Invalid date 'February 29' as '" + prolepticYear + "' is not a leap year");
} else {
throw new DateTimeException("Invalid date '" + Month.of(month).name() + " " + dayOfMonth + "'");
}
}
}
long totalDays = 0;
int timeinSec = 0;
totalDays += 365L * prolepticYear;
if(prolepticYear >= 0) {
totalDays += (prolepticYear + 3L) / 4 - (prolepticYear + 99L) / 100 + (prolepticYear + 399L) / 400;
} else {
totalDays -= prolepticYear / -4 - prolepticYear / -100 + prolepticYear / -400;
}
totalDays += (367 * month - 362) / 12;
totalDays += dayOfMonth - 1;
if(month>2) {
totalDays--;
if(!IsoChronology.INSTANCE.isLeapYear(prolepticYear)) {
totalDays--;
}
}
totalDays -= DAYS_0000_TO_1970;
timeinSec = (hour * 60 + minute) * 60 + second;
return Math.addExact(Math.multiplyExact(totalDays, 86400L), timeinSec - zoneOffset.getTotalSeconds());
}
// 将era处的ISO年份(公历年)转换为"Proleptic年"
@Override
public int prolepticYear(Era era, int yearOfEra) {
if(!(era instanceof IsoEra)) {
throw new ClassCastException("Era must be IsoEra");
}
return (era == IsoEra.CE ? yearOfEra : 1 - yearOfEra);
}
// 将指定的整数转换为纪元枚举
@Override
public IsoEra eraOf(int eraValue) {
return IsoEra.of(eraValue);
}
/*▲ 转换 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 杂项 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Checks if the year is a leap year, according to the ISO proleptic
* calendar system rules.
* <p>
* This method applies the current rules for leap years across the whole time-line.
* In general, a year is a leap year if it is divisible by four without
* remainder. However, years divisible by 100, are not leap years, with
* the exception of years divisible by 400 which are.
* <p>
* For example, 1904 is a leap year it is divisible by 4.
* 1900 was not a leap year as it is divisible by 100, however 2000 was a
* leap year as it is divisible by 400.
* <p>
* The calculation is proleptic - applying the same rules into the far future and far past.
* This is historically inaccurate, but is correct for the ISO-8601 standard.
*
* @param prolepticYear the ISO proleptic year to check
*
* @return true if the year is leap, false otherwise
*/
// 判断当前年份是否为闰年
@Override
public boolean isLeapYear(long prolepticYear) {
return ((prolepticYear & 3) == 0) && ((prolepticYear % 100) != 0 || (prolepticYear % 400) == 0);
}
// 返回当前历法系统下可用的纪元
@Override
public List<Era> eras() {
return List.of(IsoEra.values());
}
// 返回时间量字段field的取值区间
@Override
public ValueRange range(ChronoField field) {
return field.range();
}
/*▲ 杂项 ████████████████████████████████████████████████████████████████████████████████┛ */
/**
* Gets the number of days for the given month in the given year.
*
* @param year the year to represent, from MIN_YEAR to MAX_YEAR
* @param month the month-of-year to represent, from 1 to 12
*
* @return the number of days for the given month in the given year
*/
// 返回year年month月的天数
private int numberOfDaysOfMonth(int year, int month) {
int dom;
switch(month) {
case 2:
dom = (IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
break;
case 4:
case 6:
case 9:
case 11:
dom = 30;
break;
default:
dom = 31;
break;
}
return dom;
}
// 从fieldValues中解析[Proleptic-月]信息
@Override
void resolveProlepticMonth(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
Long pMonth = fieldValues.remove(PROLEPTIC_MONTH);
if(pMonth == null) {
return;
}
if(resolverStyle != ResolverStyle.LENIENT) {
PROLEPTIC_MONTH.checkValidValue(pMonth);
}
addFieldValue(fieldValues, MONTH_OF_YEAR, Math.floorMod(pMonth, 12) + 1);
addFieldValue(fieldValues, YEAR, Math.floorDiv(pMonth, 12));
}
// 尝试从中解析[iSO年份]信息,并将其转换为[Proleptic年]后存入fieldValues
@Override
LocalDate resolveYearOfEra(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
// 从fieldValues中获取字段YEAR_OF_ERA的值(ISO年份)
Long yoeLong = fieldValues.remove(YEAR_OF_ERA);
// 如果包含[iSO年份]字段
if(yoeLong != null) {
if(resolverStyle != ResolverStyle.LENIENT) {
YEAR_OF_ERA.checkValidValue(yoeLong);
}
// 从fieldValues中获取字段YEAR_OF_ERA的值(纪元)
Long era = fieldValues.remove(ERA);
// 如果不包含[纪元]字段
if(era == null) {
// 获取[Proleptic年]字段
Long year = fieldValues.get(YEAR);
// 严格模式下
if(resolverStyle == ResolverStyle.STRICT) {
// do not invent era if strict, but do cross-check with year
if(year != null) {
// 如果包含[Proleptic年]字段,则尝试使用[Proleptic年]年所在的纪元与[iSO年份]构造为一个新的"Proleptic年"存入fieldValues
addFieldValue(fieldValues, YEAR, (year>0 ? yoeLong : Math.subtractExact(1, yoeLong)));
// 否则,将该[iSO年份]还原到fieldValues中
} else {
// reinstate the field removed earlier, no cross-check issues
fieldValues.put(YEAR_OF_ERA, yoeLong);
}
// 非严格模式下,允许将该[iSO年份]回退为[Proleptic年]
} else {
// invent era
addFieldValue(fieldValues, YEAR, (year == null || year>0 ? yoeLong : Math.subtractExact(1, yoeLong)));
}
} else if(era == 1L) {
addFieldValue(fieldValues, YEAR, yoeLong);
} else if(era == 0L) {
addFieldValue(fieldValues, YEAR, Math.subtractExact(1, yoeLong));
} else {
throw new DateTimeException("Invalid value for era: " + era);
}
} else if(fieldValues.containsKey(ERA)) {
ERA.checkValidValue(fieldValues.get(ERA)); // always validated
}
return null;
}
// 从fieldValues中解析出年/月/日信息
@Override
LocalDate resolveYMD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
int y = YEAR.checkValidIntValue(fieldValues.remove(YEAR));
if(resolverStyle == ResolverStyle.LENIENT) {
long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1);
long days = Math.subtractExact(fieldValues.remove(DAY_OF_MONTH), 1);
return LocalDate.of(y, 1, 1).plusMonths(months).plusDays(days);
}
int moy = MONTH_OF_YEAR.checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR));
int dom = DAY_OF_MONTH.checkValidIntValue(fieldValues.remove(DAY_OF_MONTH));
if(resolverStyle == ResolverStyle.SMART) { // previous valid
if(moy == 4 || moy == 6 || moy == 9 || moy == 11) {
dom = Math.min(dom, 30);
} else if(moy == 2) {
dom = Math.min(dom, Month.FEBRUARY.length(Year.isLeap(y)));
}
}
return LocalDate.of(y, moy, dom);
}
/*▼ 序列化 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Serialization version.
*/
private static final long serialVersionUID = -1440403870442975015L;
/**
* Defend against malicious streams.
*
* @param s the stream to read
*
* @throws InvalidObjectException always
*/
private void readObject(ObjectInputStream s) throws InvalidObjectException {
throw new InvalidObjectException("Deserialization via serialization delegate");
}
/**
* Writes the Chronology using a
* <a href="../../../serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
*
* @return the instance of {@code Ser}, not null
*
* @serialData <pre>
* out.writeByte(1); // identifies a Chronology
* out.writeUTF(getId());
* </pre>
*/
@Override
Object writeReplace() {
return super.writeReplace();
}
/*▲ 序列化 ████████████████████████████████████████████████████████████████████████████████┛ */
}