1 /*
2  * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
28  *
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions are met:
33  *
34  *  * Redistributions of source code must retain the above copyright notice,
35  *    this list of conditions and the following disclaimer.
36  *
37  *  * Redistributions in binary form must reproduce the above copyright notice,
38  *    this list of conditions and the following disclaimer in the documentation
39  *    and/or other materials provided with the distribution.
40  *
41  *  * Neither the name of JSR-310 nor the names of its contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  */
57 package java.time.temporal;
58 
59 import android.icu.text.DateTimePatternGenerator;
60 import android.icu.util.ULocale;
61 import java.time.DayOfWeek;
62 import java.time.Instant;
63 import java.time.Year;
64 import java.time.ZoneOffset;
65 import java.time.chrono.ChronoLocalDate;
66 import java.time.chrono.Chronology;
67 import java.util.Locale;
68 import java.util.Objects;
69 
70 import static java.time.temporal.ChronoUnit.DAYS;
71 import static java.time.temporal.ChronoUnit.ERAS;
72 import static java.time.temporal.ChronoUnit.FOREVER;
73 import static java.time.temporal.ChronoUnit.HALF_DAYS;
74 import static java.time.temporal.ChronoUnit.HOURS;
75 import static java.time.temporal.ChronoUnit.MICROS;
76 import static java.time.temporal.ChronoUnit.MILLIS;
77 import static java.time.temporal.ChronoUnit.MINUTES;
78 import static java.time.temporal.ChronoUnit.MONTHS;
79 import static java.time.temporal.ChronoUnit.NANOS;
80 import static java.time.temporal.ChronoUnit.SECONDS;
81 import static java.time.temporal.ChronoUnit.WEEKS;
82 import static java.time.temporal.ChronoUnit.YEARS;
83 
84 import java.time.DayOfWeek;
85 import java.time.Instant;
86 import java.time.Year;
87 import java.time.ZoneOffset;
88 import java.time.chrono.ChronoLocalDate;
89 import java.time.chrono.Chronology;
90 import java.util.Locale;
91 import java.util.Objects;
92 import java.util.ResourceBundle;
93 import sun.util.locale.provider.CalendarDataUtility;
94 
95 /**
96  * A standard set of fields.
97  * <p>
98  * This set of fields provide field-based access to manipulate a date, time or date-time.
99  * The standard set of fields can be extended by implementing {@link TemporalField}.
100  * <p>
101  * These fields are intended to be applicable in multiple calendar systems.
102  * For example, most non-ISO calendar systems define dates as a year, month and day,
103  * just with slightly different rules.
104  * The documentation of each field explains how it operates.
105  *
106  * @implSpec
107  * This is a final, immutable and thread-safe enum.
108  *
109  * @since 1.8
110  */
111 public enum ChronoField implements TemporalField {
112 
113     /**
114      * The nano-of-second.
115      * <p>
116      * This counts the nanosecond within the second, from 0 to 999,999,999.
117      * This field has the same meaning for all calendar systems.
118      * <p>
119      * This field is used to represent the nano-of-second handling any fraction of the second.
120      * Implementations of {@code TemporalAccessor} should provide a value for this field if
121      * they can return a value for {@link #SECOND_OF_MINUTE}, {@link #SECOND_OF_DAY} or
122      * {@link #INSTANT_SECONDS} filling unknown precision with zero.
123      * <p>
124      * When this field is used for setting a value, it should set as much precision as the
125      * object stores, using integer division to remove excess precision.
126      * For example, if the {@code TemporalAccessor} stores time to millisecond precision,
127      * then the nano-of-second must be divided by 1,000,000 before replacing the milli-of-second.
128      * <p>
129      * When parsing this field it behaves equivalent to the following:
130      * The value is validated in strict and smart mode but not in lenient mode.
131      * The field is resolved in combination with {@code MILLI_OF_SECOND} and {@code MICRO_OF_SECOND}.
132      */
133     NANO_OF_SECOND("NanoOfSecond", NANOS, SECONDS, ValueRange.of(0, 999_999_999)),
134     /**
135      * The nano-of-day.
136      * <p>
137      * This counts the nanosecond within the day, from 0 to (24 * 60 * 60 * 1,000,000,000) - 1.
138      * This field has the same meaning for all calendar systems.
139      * <p>
140      * This field is used to represent the nano-of-day handling any fraction of the second.
141      * Implementations of {@code TemporalAccessor} should provide a value for this field if
142      * they can return a value for {@link #SECOND_OF_DAY} filling unknown precision with zero.
143      * <p>
144      * When parsing this field it behaves equivalent to the following:
145      * The value is validated in strict and smart mode but not in lenient mode.
146      * The value is split to form {@code NANO_OF_SECOND}, {@code SECOND_OF_MINUTE},
147      * {@code MINUTE_OF_HOUR} and {@code HOUR_OF_DAY} fields.
148      */
149     NANO_OF_DAY("NanoOfDay", NANOS, DAYS, ValueRange.of(0, 86400L * 1000_000_000L - 1)),
150     /**
151      * The micro-of-second.
152      * <p>
153      * This counts the microsecond within the second, from 0 to 999,999.
154      * This field has the same meaning for all calendar systems.
155      * <p>
156      * This field is used to represent the micro-of-second handling any fraction of the second.
157      * Implementations of {@code TemporalAccessor} should provide a value for this field if
158      * they can return a value for {@link #SECOND_OF_MINUTE}, {@link #SECOND_OF_DAY} or
159      * {@link #INSTANT_SECONDS} filling unknown precision with zero.
160      * <p>
161      * When this field is used for setting a value, it should behave in the same way as
162      * setting {@link #NANO_OF_SECOND} with the value multiplied by 1,000.
163      * <p>
164      * When parsing this field it behaves equivalent to the following:
165      * The value is validated in strict and smart mode but not in lenient mode.
166      * The field is resolved in combination with {@code MILLI_OF_SECOND} to produce
167      * {@code NANO_OF_SECOND}.
168      */
169     MICRO_OF_SECOND("MicroOfSecond", MICROS, SECONDS, ValueRange.of(0, 999_999)),
170     /**
171      * The micro-of-day.
172      * <p>
173      * This counts the microsecond within the day, from 0 to (24 * 60 * 60 * 1,000,000) - 1.
174      * This field has the same meaning for all calendar systems.
175      * <p>
176      * This field is used to represent the micro-of-day handling any fraction of the second.
177      * Implementations of {@code TemporalAccessor} should provide a value for this field if
178      * they can return a value for {@link #SECOND_OF_DAY} filling unknown precision with zero.
179      * <p>
180      * When this field is used for setting a value, it should behave in the same way as
181      * setting {@link #NANO_OF_DAY} with the value multiplied by 1,000.
182      * <p>
183      * When parsing this field it behaves equivalent to the following:
184      * The value is validated in strict and smart mode but not in lenient mode.
185      * The value is split to form {@code MICRO_OF_SECOND}, {@code SECOND_OF_MINUTE},
186      * {@code MINUTE_OF_HOUR} and {@code HOUR_OF_DAY} fields.
187      */
188     MICRO_OF_DAY("MicroOfDay", MICROS, DAYS, ValueRange.of(0, 86400L * 1000_000L - 1)),
189     /**
190      * The milli-of-second.
191      * <p>
192      * This counts the millisecond within the second, from 0 to 999.
193      * This field has the same meaning for all calendar systems.
194      * <p>
195      * This field is used to represent the milli-of-second handling any fraction of the second.
196      * Implementations of {@code TemporalAccessor} should provide a value for this field if
197      * they can return a value for {@link #SECOND_OF_MINUTE}, {@link #SECOND_OF_DAY} or
198      * {@link #INSTANT_SECONDS} filling unknown precision with zero.
199      * <p>
200      * When this field is used for setting a value, it should behave in the same way as
201      * setting {@link #NANO_OF_SECOND} with the value multiplied by 1,000,000.
202      * <p>
203      * When parsing this field it behaves equivalent to the following:
204      * The value is validated in strict and smart mode but not in lenient mode.
205      * The field is resolved in combination with {@code MICRO_OF_SECOND} to produce
206      * {@code NANO_OF_SECOND}.
207      */
208     MILLI_OF_SECOND("MilliOfSecond", MILLIS, SECONDS, ValueRange.of(0, 999)),
209     /**
210      * The milli-of-day.
211      * <p>
212      * This counts the millisecond within the day, from 0 to (24 * 60 * 60 * 1,000) - 1.
213      * This field has the same meaning for all calendar systems.
214      * <p>
215      * This field is used to represent the milli-of-day handling any fraction of the second.
216      * Implementations of {@code TemporalAccessor} should provide a value for this field if
217      * they can return a value for {@link #SECOND_OF_DAY} filling unknown precision with zero.
218      * <p>
219      * When this field is used for setting a value, it should behave in the same way as
220      * setting {@link #NANO_OF_DAY} with the value multiplied by 1,000,000.
221      * <p>
222      * When parsing this field it behaves equivalent to the following:
223      * The value is validated in strict and smart mode but not in lenient mode.
224      * The value is split to form {@code MILLI_OF_SECOND}, {@code SECOND_OF_MINUTE},
225      * {@code MINUTE_OF_HOUR} and {@code HOUR_OF_DAY} fields.
226      */
227     MILLI_OF_DAY("MilliOfDay", MILLIS, DAYS, ValueRange.of(0, 86400L * 1000L - 1)),
228     /**
229      * The second-of-minute.
230      * <p>
231      * This counts the second within the minute, from 0 to 59.
232      * This field has the same meaning for all calendar systems.
233      * <p>
234      * When parsing this field it behaves equivalent to the following:
235      * The value is validated in strict and smart mode but not in lenient mode.
236      */
237     SECOND_OF_MINUTE("SecondOfMinute", SECONDS, MINUTES, ValueRange.of(0, 59), "second"),
238     /**
239      * The second-of-day.
240      * <p>
241      * This counts the second within the day, from 0 to (24 * 60 * 60) - 1.
242      * This field has the same meaning for all calendar systems.
243      * <p>
244      * When parsing this field it behaves equivalent to the following:
245      * The value is validated in strict and smart mode but not in lenient mode.
246      * The value is split to form {@code SECOND_OF_MINUTE}, {@code MINUTE_OF_HOUR}
247      * and {@code HOUR_OF_DAY} fields.
248      */
249     SECOND_OF_DAY("SecondOfDay", SECONDS, DAYS, ValueRange.of(0, 86400L - 1)),
250     /**
251      * The minute-of-hour.
252      * <p>
253      * This counts the minute within the hour, from 0 to 59.
254      * This field has the same meaning for all calendar systems.
255      * <p>
256      * When parsing this field it behaves equivalent to the following:
257      * The value is validated in strict and smart mode but not in lenient mode.
258      */
259     MINUTE_OF_HOUR("MinuteOfHour", MINUTES, HOURS, ValueRange.of(0, 59), "minute"),
260     /**
261      * The minute-of-day.
262      * <p>
263      * This counts the minute within the day, from 0 to (24 * 60) - 1.
264      * This field has the same meaning for all calendar systems.
265      * <p>
266      * When parsing this field it behaves equivalent to the following:
267      * The value is validated in strict and smart mode but not in lenient mode.
268      * The value is split to form {@code MINUTE_OF_HOUR} and {@code HOUR_OF_DAY} fields.
269      */
270     MINUTE_OF_DAY("MinuteOfDay", MINUTES, DAYS, ValueRange.of(0, (24 * 60) - 1)),
271     /**
272      * The hour-of-am-pm.
273      * <p>
274      * This counts the hour within the AM/PM, from 0 to 11.
275      * This is the hour that would be observed on a standard 12-hour digital clock.
276      * This field has the same meaning for all calendar systems.
277      * <p>
278      * When parsing this field it behaves equivalent to the following:
279      * The value is validated from 0 to 11 in strict and smart mode.
280      * In lenient mode the value is not validated. It is combined with
281      * {@code AMPM_OF_DAY} to form {@code HOUR_OF_DAY} by multiplying
282      * the {AMPM_OF_DAY} value by 12.
283      * <p>
284      * See {@link #CLOCK_HOUR_OF_AMPM} for the related field that counts hours from 1 to 12.
285      */
286     HOUR_OF_AMPM("HourOfAmPm", HOURS, HALF_DAYS, ValueRange.of(0, 11)),
287     /**
288      * The clock-hour-of-am-pm.
289      * <p>
290      * This counts the hour within the AM/PM, from 1 to 12.
291      * This is the hour that would be observed on a standard 12-hour analog wall clock.
292      * This field has the same meaning for all calendar systems.
293      * <p>
294      * When parsing this field it behaves equivalent to the following:
295      * The value is validated from 1 to 12 in strict mode and from
296      * 0 to 12 in smart mode. In lenient mode the value is not validated.
297      * The field is converted to an {@code HOUR_OF_AMPM} with the same value,
298      * unless the value is 12, in which case it is converted to 0.
299      * <p>
300      * See {@link #HOUR_OF_AMPM} for the related field that counts hours from 0 to 11.
301      */
302     CLOCK_HOUR_OF_AMPM("ClockHourOfAmPm", HOURS, HALF_DAYS, ValueRange.of(1, 12)),
303     /**
304      * The hour-of-day.
305      * <p>
306      * This counts the hour within the day, from 0 to 23.
307      * This is the hour that would be observed on a standard 24-hour digital clock.
308      * This field has the same meaning for all calendar systems.
309      * <p>
310      * When parsing this field it behaves equivalent to the following:
311      * The value is validated in strict and smart mode but not in lenient mode.
312      * The field is combined with {@code MINUTE_OF_HOUR}, {@code SECOND_OF_MINUTE} and
313      * {@code NANO_OF_SECOND} to produce a {@code LocalTime}.
314      * In lenient mode, any excess days are added to the parsed date, or
315      * made available via {@link java.time.format.DateTimeFormatter#parsedExcessDays()}.
316      * <p>
317      * See {@link #CLOCK_HOUR_OF_DAY} for the related field that counts hours from 1 to 24.
318      */
319     HOUR_OF_DAY("HourOfDay", HOURS, DAYS, ValueRange.of(0, 23), "hour"),
320     /**
321      * The clock-hour-of-day.
322      * <p>
323      * This counts the hour within the day, from 1 to 24.
324      * This is the hour that would be observed on a 24-hour analog wall clock.
325      * This field has the same meaning for all calendar systems.
326      * <p>
327      * When parsing this field it behaves equivalent to the following:
328      * The value is validated from 1 to 24 in strict mode and from
329      * 0 to 24 in smart mode. In lenient mode the value is not validated.
330      * The field is converted to an {@code HOUR_OF_DAY} with the same value,
331      * unless the value is 24, in which case it is converted to 0.
332      * <p>
333      * See {@link #HOUR_OF_DAY} for the related field that counts hours from 0 to 23.
334      */
335     CLOCK_HOUR_OF_DAY("ClockHourOfDay", HOURS, DAYS, ValueRange.of(1, 24)),
336     /**
337      * The am-pm-of-day.
338      * <p>
339      * This counts the AM/PM within the day, from 0 (AM) to 1 (PM).
340      * This field has the same meaning for all calendar systems.
341      * <p>
342      * When parsing this field it behaves equivalent to the following:
343      * The value is validated from 0 to 1 in strict and smart mode.
344      * In lenient mode the value is not validated. It is combined with
345      * {@code HOUR_OF_AMPM} to form {@code HOUR_OF_DAY} by multiplying
346      * the {AMPM_OF_DAY} value by 12.
347      */
348     AMPM_OF_DAY("AmPmOfDay", HALF_DAYS, DAYS, ValueRange.of(0, 1), "dayperiod"),
349     /**
350      * The day-of-week, such as Tuesday.
351      * <p>
352      * This represents the standard concept of the day of the week.
353      * In the default ISO calendar system, this has values from Monday (1) to Sunday (7).
354      * The {@link DayOfWeek} class can be used to interpret the result.
355      * <p>
356      * Most non-ISO calendar systems also define a seven day week that aligns with ISO.
357      * Those calendar systems must also use the same numbering system, from Monday (1) to
358      * Sunday (7), which allows {@code DayOfWeek} to be used.
359      * <p>
360      * Calendar systems that do not have a standard seven day week should implement this field
361      * if they have a similar concept of named or numbered days within a period similar
362      * to a week. It is recommended that the numbering starts from 1.
363      */
364     DAY_OF_WEEK("DayOfWeek", DAYS, WEEKS, ValueRange.of(1, 7), "weekday"),
365     /**
366      * The aligned day-of-week within a month.
367      * <p>
368      * This represents concept of the count of days within the period of a week
369      * where the weeks are aligned to the start of the month.
370      * This field is typically used with {@link #ALIGNED_WEEK_OF_MONTH}.
371      * <p>
372      * For example, in a calendar systems with a seven day week, the first aligned-week-of-month
373      * starts on day-of-month 1, the second aligned-week starts on day-of-month 8, and so on.
374      * Within each of these aligned-weeks, the days are numbered from 1 to 7 and returned
375      * as the value of this field.
376      * As such, day-of-month 1 to 7 will have aligned-day-of-week values from 1 to 7.
377      * And day-of-month 8 to 14 will repeat this with aligned-day-of-week values from 1 to 7.
378      * <p>
379      * Calendar systems that do not have a seven day week should typically implement this
380      * field in the same way, but using the alternate week length.
381      */
382     ALIGNED_DAY_OF_WEEK_IN_MONTH("AlignedDayOfWeekInMonth", DAYS, WEEKS, ValueRange.of(1, 7)),
383     /**
384      * The aligned day-of-week within a year.
385      * <p>
386      * This represents concept of the count of days within the period of a week
387      * where the weeks are aligned to the start of the year.
388      * This field is typically used with {@link #ALIGNED_WEEK_OF_YEAR}.
389      * <p>
390      * For example, in a calendar systems with a seven day week, the first aligned-week-of-year
391      * starts on day-of-year 1, the second aligned-week starts on day-of-year 8, and so on.
392      * Within each of these aligned-weeks, the days are numbered from 1 to 7 and returned
393      * as the value of this field.
394      * As such, day-of-year 1 to 7 will have aligned-day-of-week values from 1 to 7.
395      * And day-of-year 8 to 14 will repeat this with aligned-day-of-week values from 1 to 7.
396      * <p>
397      * Calendar systems that do not have a seven day week should typically implement this
398      * field in the same way, but using the alternate week length.
399      */
400     ALIGNED_DAY_OF_WEEK_IN_YEAR("AlignedDayOfWeekInYear", DAYS, WEEKS, ValueRange.of(1, 7)),
401     /**
402      * The day-of-month.
403      * <p>
404      * This represents the concept of the day within the month.
405      * In the default ISO calendar system, this has values from 1 to 31 in most months.
406      * April, June, September, November have days from 1 to 30, while February has days
407      * from 1 to 28, or 29 in a leap year.
408      * <p>
409      * Non-ISO calendar systems should implement this field using the most recognized
410      * day-of-month values for users of the calendar system.
411      * Normally, this is a count of days from 1 to the length of the month.
412      */
413     DAY_OF_MONTH("DayOfMonth", DAYS, MONTHS, ValueRange.of(1, 28, 31), "day"),
414     /**
415      * The day-of-year.
416      * <p>
417      * This represents the concept of the day within the year.
418      * In the default ISO calendar system, this has values from 1 to 365 in standard
419      * years and 1 to 366 in leap years.
420      * <p>
421      * Non-ISO calendar systems should implement this field using the most recognized
422      * day-of-year values for users of the calendar system.
423      * Normally, this is a count of days from 1 to the length of the year.
424      * <p>
425      * Note that a non-ISO calendar system may have year numbering system that changes
426      * at a different point to the natural reset in the month numbering. An example
427      * of this is the Japanese calendar system where a change of era, which resets
428      * the year number to 1, can happen on any date. The era and year reset also cause
429      * the day-of-year to be reset to 1, but not the month-of-year or day-of-month.
430      */
431     DAY_OF_YEAR("DayOfYear", DAYS, YEARS, ValueRange.of(1, 365, 366)),
432     /**
433      * The epoch-day, based on the Java epoch of 1970-01-01 (ISO).
434      * <p>
435      * This field is the sequential count of days where 1970-01-01 (ISO) is zero.
436      * Note that this uses the <i>local</i> time-line, ignoring offset and time-zone.
437      * <p>
438      * This field is strictly defined to have the same meaning in all calendar systems.
439      * This is necessary to ensure interoperation between calendars.
440      * <p>
441      * Range of EpochDay is between (LocalDate.MIN.toEpochDay(), LocalDate.MAX.toEpochDay())
442      * both inclusive.
443      */
444     EPOCH_DAY("EpochDay", DAYS, FOREVER, ValueRange.of(-365243219162L, 365241780471L)),
445     /**
446      * The aligned week within a month.
447      * <p>
448      * This represents concept of the count of weeks within the period of a month
449      * where the weeks are aligned to the start of the month.
450      * This field is typically used with {@link #ALIGNED_DAY_OF_WEEK_IN_MONTH}.
451      * <p>
452      * For example, in a calendar systems with a seven day week, the first aligned-week-of-month
453      * starts on day-of-month 1, the second aligned-week starts on day-of-month 8, and so on.
454      * Thus, day-of-month values 1 to 7 are in aligned-week 1, while day-of-month values
455      * 8 to 14 are in aligned-week 2, and so on.
456      * <p>
457      * Calendar systems that do not have a seven day week should typically implement this
458      * field in the same way, but using the alternate week length.
459      */
460     ALIGNED_WEEK_OF_MONTH("AlignedWeekOfMonth", WEEKS, MONTHS, ValueRange.of(1, 4, 5)),
461     /**
462      * The aligned week within a year.
463      * <p>
464      * This represents concept of the count of weeks within the period of a year
465      * where the weeks are aligned to the start of the year.
466      * This field is typically used with {@link #ALIGNED_DAY_OF_WEEK_IN_YEAR}.
467      * <p>
468      * For example, in a calendar systems with a seven day week, the first aligned-week-of-year
469      * starts on day-of-year 1, the second aligned-week starts on day-of-year 8, and so on.
470      * Thus, day-of-year values 1 to 7 are in aligned-week 1, while day-of-year values
471      * 8 to 14 are in aligned-week 2, and so on.
472      * <p>
473      * Calendar systems that do not have a seven day week should typically implement this
474      * field in the same way, but using the alternate week length.
475      */
476     ALIGNED_WEEK_OF_YEAR("AlignedWeekOfYear", WEEKS, YEARS, ValueRange.of(1, 53)),
477     /**
478      * The month-of-year, such as March.
479      * <p>
480      * This represents the concept of the month within the year.
481      * In the default ISO calendar system, this has values from January (1) to December (12).
482      * <p>
483      * Non-ISO calendar systems should implement this field using the most recognized
484      * month-of-year values for users of the calendar system.
485      * Normally, this is a count of months starting from 1.
486      */
487     MONTH_OF_YEAR("MonthOfYear", MONTHS, YEARS, ValueRange.of(1, 12), "month"),
488     /**
489      * The proleptic-month based, counting months sequentially from year 0.
490      * <p>
491      * This field is the sequential count of months where the first month
492      * in proleptic-year zero has the value zero.
493      * Later months have increasingly larger values.
494      * Earlier months have increasingly small values.
495      * There are no gaps or breaks in the sequence of months.
496      * Note that this uses the <i>local</i> time-line, ignoring offset and time-zone.
497      * <p>
498      * In the default ISO calendar system, June 2012 would have the value
499      * {@code (2012 * 12 + 6 - 1)}. This field is primarily for internal use.
500      * <p>
501      * Non-ISO calendar systems must implement this field as per the definition above.
502      * It is just a simple zero-based count of elapsed months from the start of proleptic-year 0.
503      * All calendar systems with a full proleptic-year definition will have a year zero.
504      * If the calendar system has a minimum year that excludes year zero, then one must
505      * be extrapolated in order for this method to be defined.
506      */
507     PROLEPTIC_MONTH("ProlepticMonth", MONTHS, FOREVER, ValueRange.of(Year.MIN_VALUE * 12L, Year.MAX_VALUE * 12L + 11)),
508     /**
509      * The year within the era.
510      * <p>
511      * This represents the concept of the year within the era.
512      * This field is typically used with {@link #ERA}.
513      * <p>
514      * The standard mental model for a date is based on three concepts - year, month and day.
515      * These map onto the {@code YEAR}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} fields.
516      * Note that there is no reference to eras.
517      * The full model for a date requires four concepts - era, year, month and day. These map onto
518      * the {@code ERA}, {@code YEAR_OF_ERA}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} fields.
519      * Whether this field or {@code YEAR} is used depends on which mental model is being used.
520      * See {@link ChronoLocalDate} for more discussion on this topic.
521      * <p>
522      * In the default ISO calendar system, there are two eras defined, 'BCE' and 'CE'.
523      * The era 'CE' is the one currently in use and year-of-era runs from 1 to the maximum value.
524      * The era 'BCE' is the previous era, and the year-of-era runs backwards.
525      * <p>
526      * For example, subtracting a year each time yield the following:<br>
527      * - year-proleptic 2  = 'CE' year-of-era 2<br>
528      * - year-proleptic 1  = 'CE' year-of-era 1<br>
529      * - year-proleptic 0  = 'BCE' year-of-era 1<br>
530      * - year-proleptic -1 = 'BCE' year-of-era 2<br>
531      * <p>
532      * Note that the ISO-8601 standard does not actually define eras.
533      * Note also that the ISO eras do not align with the well-known AD/BC eras due to the
534      * change between the Julian and Gregorian calendar systems.
535      * <p>
536      * Non-ISO calendar systems should implement this field using the most recognized
537      * year-of-era value for users of the calendar system.
538      * Since most calendar systems have only two eras, the year-of-era numbering approach
539      * will typically be the same as that used by the ISO calendar system.
540      * The year-of-era value should typically always be positive, however this is not required.
541      */
542     YEAR_OF_ERA("YearOfEra", YEARS, FOREVER, ValueRange.of(1, Year.MAX_VALUE, Year.MAX_VALUE + 1)),
543     /**
544      * The proleptic year, such as 2012.
545      * <p>
546      * This represents the concept of the year, counting sequentially and using negative numbers.
547      * The proleptic year is not interpreted in terms of the era.
548      * See {@link #YEAR_OF_ERA} for an example showing the mapping from proleptic year to year-of-era.
549      * <p>
550      * The standard mental model for a date is based on three concepts - year, month and day.
551      * These map onto the {@code YEAR}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} fields.
552      * Note that there is no reference to eras.
553      * The full model for a date requires four concepts - era, year, month and day. These map onto
554      * the {@code ERA}, {@code YEAR_OF_ERA}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} fields.
555      * Whether this field or {@code YEAR_OF_ERA} is used depends on which mental model is being used.
556      * See {@link ChronoLocalDate} for more discussion on this topic.
557      * <p>
558      * Non-ISO calendar systems should implement this field as follows.
559      * If the calendar system has only two eras, before and after a fixed date, then the
560      * proleptic-year value must be the same as the year-of-era value for the later era,
561      * and increasingly negative for the earlier era.
562      * If the calendar system has more than two eras, then the proleptic-year value may be
563      * defined with any appropriate value, although defining it to be the same as ISO may be
564      * the best option.
565      */
566     YEAR("Year", YEARS, FOREVER, ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE), "year"),
567     /**
568      * The era.
569      * <p>
570      * This represents the concept of the era, which is the largest division of the time-line.
571      * This field is typically used with {@link #YEAR_OF_ERA}.
572      * <p>
573      * In the default ISO calendar system, there are two eras defined, 'BCE' and 'CE'.
574      * The era 'CE' is the one currently in use and year-of-era runs from 1 to the maximum value.
575      * The era 'BCE' is the previous era, and the year-of-era runs backwards.
576      * See {@link #YEAR_OF_ERA} for a full example.
577      * <p>
578      * Non-ISO calendar systems should implement this field to define eras.
579      * The value of the era that was active on 1970-01-01 (ISO) must be assigned the value 1.
580      * Earlier eras must have sequentially smaller values.
581      * Later eras must have sequentially larger values,
582      */
583     ERA("Era", ERAS, FOREVER, ValueRange.of(0, 1), "era"),
584     /**
585      * The instant epoch-seconds.
586      * <p>
587      * This represents the concept of the sequential count of seconds where
588      * 1970-01-01T00:00Z (ISO) is zero.
589      * This field may be used with {@link #NANO_OF_SECOND} to represent the fraction of the second.
590      * <p>
591      * An {@link Instant} represents an instantaneous point on the time-line.
592      * On their own, an instant has insufficient information to allow a local date-time to be obtained.
593      * Only when paired with an offset or time-zone can the local date or time be calculated.
594      * <p>
595      * This field is strictly defined to have the same meaning in all calendar systems.
596      * This is necessary to ensure interoperation between calendars.
597      */
598     INSTANT_SECONDS("InstantSeconds", SECONDS, FOREVER, ValueRange.of(Long.MIN_VALUE, Long.MAX_VALUE)),
599     /**
600      * The offset from UTC/Greenwich.
601      * <p>
602      * This represents the concept of the offset in seconds of local time from UTC/Greenwich.
603      * <p>
604      * A {@link ZoneOffset} represents the period of time that local time differs from UTC/Greenwich.
605      * This is usually a fixed number of hours and minutes.
606      * It is equivalent to the {@link ZoneOffset#getTotalSeconds() total amount} of the offset in seconds.
607      * For example, during the winter Paris has an offset of {@code +01:00}, which is 3600 seconds.
608      * <p>
609      * This field is strictly defined to have the same meaning in all calendar systems.
610      * This is necessary to ensure interoperation between calendars.
611      */
612     OFFSET_SECONDS("OffsetSeconds", SECONDS, FOREVER, ValueRange.of(-18 * 3600, 18 * 3600));
613 
614     private final String name;
615     private final TemporalUnit baseUnit;
616     private final TemporalUnit rangeUnit;
617     private final ValueRange range;
618     private final String displayNameKey;
619 
ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range)620     private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range) {
621         this.name = name;
622         this.baseUnit = baseUnit;
623         this.rangeUnit = rangeUnit;
624         this.range = range;
625         this.displayNameKey = null;
626     }
627 
ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range, String displayNameKey)628     private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit,
629             ValueRange range, String displayNameKey) {
630         this.name = name;
631         this.baseUnit = baseUnit;
632         this.rangeUnit = rangeUnit;
633         this.range = range;
634         this.displayNameKey = displayNameKey;
635     }
636 
637     @Override
getDisplayName(Locale locale)638     public String getDisplayName(Locale locale) {
639         Objects.requireNonNull(locale, "locale");
640         if (displayNameKey == null) {
641             return name;
642         }
643 
644         // BEGIN Android-changed: use ICU names.
645         /*
646         LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()
647                                     .getLocaleResources(
648                                         CalendarDataUtility
649                                             .findRegionOverride(locale));
650         ResourceBundle rb = lr.getJavaTimeFormatData();
651         String key = "field." + displayNameKey;
652         return rb.containsKey(key) ? rb.getString(key) : name;
653          */
654         DateTimePatternGenerator generator = DateTimePatternGenerator
655                 .getInstance(ULocale.forLocale(locale));
656         String icuName = generator.getAppendItemName(getIcuFieldNumber(this));
657         return icuName != null && !icuName.isEmpty() ? icuName : name;
658     }
659 
660     /**
661      * @return the field id according to {@link DateTimePatternGenerator} for the field.
662      */
getIcuFieldNumber(ChronoField field)663     private static int getIcuFieldNumber(ChronoField field) {
664         switch (field) {
665             case SECOND_OF_MINUTE:
666                 return DateTimePatternGenerator.SECOND;
667             case MINUTE_OF_HOUR:
668                 return DateTimePatternGenerator.MINUTE;
669             case HOUR_OF_DAY:
670                 return DateTimePatternGenerator.HOUR;
671             case AMPM_OF_DAY:
672                 return DateTimePatternGenerator.DAYPERIOD;
673             case DAY_OF_WEEK:
674                 return DateTimePatternGenerator.WEEKDAY;
675             case DAY_OF_MONTH:
676                 return DateTimePatternGenerator.DAY;
677             case MONTH_OF_YEAR:
678                 return DateTimePatternGenerator.MONTH;
679             case YEAR:
680                 return DateTimePatternGenerator.YEAR;
681             case ERA:
682                 return DateTimePatternGenerator.ERA;
683             default:
684                 throw new IllegalArgumentException("Unexpected ChronoField " + field.name());
685         }
686         // END Android-changed: use ICU names.
687     }
688 
689     @Override
getBaseUnit()690     public TemporalUnit getBaseUnit() {
691         return baseUnit;
692     }
693 
694     @Override
getRangeUnit()695     public TemporalUnit getRangeUnit() {
696         return rangeUnit;
697     }
698 
699     /**
700      * Gets the range of valid values for the field.
701      * <p>
702      * All fields can be expressed as a {@code long} integer.
703      * This method returns an object that describes the valid range for that value.
704      * <p>
705      * This method returns the range of the field in the ISO-8601 calendar system.
706      * This range may be incorrect for other calendar systems.
707      * Use {@link Chronology#range(ChronoField)} to access the correct range
708      * for a different calendar system.
709      * <p>
710      * Note that the result only describes the minimum and maximum valid values
711      * and it is important not to read too much into them. For example, there
712      * could be values within the range that are invalid for the field.
713      *
714      * @return the range of valid values for the field, not null
715      */
716     @Override
range()717     public ValueRange range() {
718         return range;
719     }
720 
721     //-----------------------------------------------------------------------
722     /**
723      * Checks if this field represents a component of a date.
724      * <p>
725      * Fields from day-of-week to era are date-based.
726      *
727      * @return true if it is a component of a date
728      */
729     @Override
isDateBased()730     public boolean isDateBased() {
731         return ordinal() >= DAY_OF_WEEK.ordinal() && ordinal() <= ERA.ordinal();
732     }
733 
734     /**
735      * Checks if this field represents a component of a time.
736      * <p>
737      * Fields from nano-of-second to am-pm-of-day are time-based.
738      *
739      * @return true if it is a component of a time
740      */
741     @Override
isTimeBased()742     public boolean isTimeBased() {
743         return ordinal() < DAY_OF_WEEK.ordinal();
744     }
745 
746     //-----------------------------------------------------------------------
747     /**
748      * Checks that the specified value is valid for this field.
749      * <p>
750      * This validates that the value is within the outer range of valid values
751      * returned by {@link #range()}.
752      * <p>
753      * This method checks against the range of the field in the ISO-8601 calendar system.
754      * This range may be incorrect for other calendar systems.
755      * Use {@link Chronology#range(ChronoField)} to access the correct range
756      * for a different calendar system.
757      *
758      * @param value  the value to check
759      * @return the value that was passed in
760      */
checkValidValue(long value)761     public long checkValidValue(long value) {
762         return range().checkValidValue(value, this);
763     }
764 
765     /**
766      * Checks that the specified value is valid and fits in an {@code int}.
767      * <p>
768      * This validates that the value is within the outer range of valid values
769      * returned by {@link #range()}.
770      * It also checks that all valid values are within the bounds of an {@code int}.
771      * <p>
772      * This method checks against the range of the field in the ISO-8601 calendar system.
773      * This range may be incorrect for other calendar systems.
774      * Use {@link Chronology#range(ChronoField)} to access the correct range
775      * for a different calendar system.
776      *
777      * @param value  the value to check
778      * @return the value that was passed in
779      */
checkValidIntValue(long value)780     public int checkValidIntValue(long value) {
781         return range().checkValidIntValue(value, this);
782     }
783 
784     //-----------------------------------------------------------------------
785     @Override
isSupportedBy(TemporalAccessor temporal)786     public boolean isSupportedBy(TemporalAccessor temporal) {
787         return temporal.isSupported(this);
788     }
789 
790     @Override
rangeRefinedBy(TemporalAccessor temporal)791     public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
792         return temporal.range(this);
793     }
794 
795     @Override
getFrom(TemporalAccessor temporal)796     public long getFrom(TemporalAccessor temporal) {
797         return temporal.getLong(this);
798     }
799 
800     @SuppressWarnings("unchecked")
801     @Override
adjustInto(R temporal, long newValue)802     public <R extends Temporal> R adjustInto(R temporal, long newValue) {
803         return (R) temporal.with(this, newValue);
804     }
805 
806     //-----------------------------------------------------------------------
807     @Override
toString()808     public String toString() {
809         return name;
810     }
811 
812 }
813