• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012, 2013, 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 /**
85  * A standard set of fields.
86  * <p>
87  * This set of fields provide field-based access to manipulate a date, time or date-time.
88  * The standard set of fields can be extended by implementing {@link TemporalField}.
89  * <p>
90  * These fields are intended to be applicable in multiple calendar systems.
91  * For example, most non-ISO calendar systems define dates as a year, month and day,
92  * just with slightly different rules.
93  * The documentation of each field explains how it operates.
94  *
95  * @implSpec
96  * This is a final, immutable and thread-safe enum.
97  *
98  * @since 1.8
99  */
100 public enum ChronoField implements TemporalField {
101 
102     /**
103      * The nano-of-second.
104      * <p>
105      * This counts the nanosecond within the second, from 0 to 999,999,999.
106      * This field has the same meaning for all calendar systems.
107      * <p>
108      * This field is used to represent the nano-of-second handling any fraction of the second.
109      * Implementations of {@code TemporalAccessor} should provide a value for this field if
110      * they can return a value for {@link #SECOND_OF_MINUTE}, {@link #SECOND_OF_DAY} or
111      * {@link #INSTANT_SECONDS} filling unknown precision with zero.
112      * <p>
113      * When this field is used for setting a value, it should set as much precision as the
114      * object stores, using integer division to remove excess precision.
115      * For example, if the {@code TemporalAccessor} stores time to millisecond precision,
116      * then the nano-of-second must be divided by 1,000,000 before replacing the milli-of-second.
117      * <p>
118      * When parsing this field it behaves equivalent to the following:
119      * The value is validated in strict and smart mode but not in lenient mode.
120      * The field is resolved in combination with {@code MILLI_OF_SECOND} and {@code MICRO_OF_SECOND}.
121      */
122     NANO_OF_SECOND("NanoOfSecond", NANOS, SECONDS, ValueRange.of(0, 999_999_999)),
123     /**
124      * The nano-of-day.
125      * <p>
126      * This counts the nanosecond within the day, from 0 to (24 * 60 * 60 * 1,000,000,000) - 1.
127      * This field has the same meaning for all calendar systems.
128      * <p>
129      * This field is used to represent the nano-of-day handling any fraction of the second.
130      * Implementations of {@code TemporalAccessor} should provide a value for this field if
131      * they can return a value for {@link #SECOND_OF_DAY} filling unknown precision with zero.
132      * <p>
133      * When parsing this field it behaves equivalent to the following:
134      * The value is validated in strict and smart mode but not in lenient mode.
135      * The value is split to form {@code NANO_OF_SECOND}, {@code SECOND_OF_MINUTE},
136      * {@code MINUTE_OF_HOUR} and {@code HOUR_OF_DAY} fields.
137      */
138     NANO_OF_DAY("NanoOfDay", NANOS, DAYS, ValueRange.of(0, 86400L * 1000_000_000L - 1)),
139     /**
140      * The micro-of-second.
141      * <p>
142      * This counts the microsecond within the second, from 0 to 999,999.
143      * This field has the same meaning for all calendar systems.
144      * <p>
145      * This field is used to represent the micro-of-second handling any fraction of the second.
146      * Implementations of {@code TemporalAccessor} should provide a value for this field if
147      * they can return a value for {@link #SECOND_OF_MINUTE}, {@link #SECOND_OF_DAY} or
148      * {@link #INSTANT_SECONDS} filling unknown precision with zero.
149      * <p>
150      * When this field is used for setting a value, it should behave in the same way as
151      * setting {@link #NANO_OF_SECOND} with the value multiplied by 1,000.
152      * <p>
153      * When parsing this field it behaves equivalent to the following:
154      * The value is validated in strict and smart mode but not in lenient mode.
155      * The field is resolved in combination with {@code MILLI_OF_SECOND} to produce
156      * {@code NANO_OF_SECOND}.
157      */
158     MICRO_OF_SECOND("MicroOfSecond", MICROS, SECONDS, ValueRange.of(0, 999_999)),
159     /**
160      * The micro-of-day.
161      * <p>
162      * This counts the microsecond within the day, from 0 to (24 * 60 * 60 * 1,000,000) - 1.
163      * This field has the same meaning for all calendar systems.
164      * <p>
165      * This field is used to represent the micro-of-day handling any fraction of the second.
166      * Implementations of {@code TemporalAccessor} should provide a value for this field if
167      * they can return a value for {@link #SECOND_OF_DAY} filling unknown precision with zero.
168      * <p>
169      * When this field is used for setting a value, it should behave in the same way as
170      * setting {@link #NANO_OF_DAY} with the value multiplied by 1,000.
171      * <p>
172      * When parsing this field it behaves equivalent to the following:
173      * The value is validated in strict and smart mode but not in lenient mode.
174      * The value is split to form {@code MICRO_OF_SECOND}, {@code SECOND_OF_MINUTE},
175      * {@code MINUTE_OF_HOUR} and {@code HOUR_OF_DAY} fields.
176      */
177     MICRO_OF_DAY("MicroOfDay", MICROS, DAYS, ValueRange.of(0, 86400L * 1000_000L - 1)),
178     /**
179      * The milli-of-second.
180      * <p>
181      * This counts the millisecond within the second, from 0 to 999.
182      * This field has the same meaning for all calendar systems.
183      * <p>
184      * This field is used to represent the milli-of-second handling any fraction of the second.
185      * Implementations of {@code TemporalAccessor} should provide a value for this field if
186      * they can return a value for {@link #SECOND_OF_MINUTE}, {@link #SECOND_OF_DAY} or
187      * {@link #INSTANT_SECONDS} filling unknown precision with zero.
188      * <p>
189      * When this field is used for setting a value, it should behave in the same way as
190      * setting {@link #NANO_OF_SECOND} with the value multiplied by 1,000,000.
191      * <p>
192      * When parsing this field it behaves equivalent to the following:
193      * The value is validated in strict and smart mode but not in lenient mode.
194      * The field is resolved in combination with {@code MICRO_OF_SECOND} to produce
195      * {@code NANO_OF_SECOND}.
196      */
197     MILLI_OF_SECOND("MilliOfSecond", MILLIS, SECONDS, ValueRange.of(0, 999)),
198     /**
199      * The milli-of-day.
200      * <p>
201      * This counts the millisecond within the day, from 0 to (24 * 60 * 60 * 1,000) - 1.
202      * This field has the same meaning for all calendar systems.
203      * <p>
204      * This field is used to represent the milli-of-day handling any fraction of the second.
205      * Implementations of {@code TemporalAccessor} should provide a value for this field if
206      * they can return a value for {@link #SECOND_OF_DAY} filling unknown precision with zero.
207      * <p>
208      * When this field is used for setting a value, it should behave in the same way as
209      * setting {@link #NANO_OF_DAY} with the value multiplied by 1,000,000.
210      * <p>
211      * When parsing this field it behaves equivalent to the following:
212      * The value is validated in strict and smart mode but not in lenient mode.
213      * The value is split to form {@code MILLI_OF_SECOND}, {@code SECOND_OF_MINUTE},
214      * {@code MINUTE_OF_HOUR} and {@code HOUR_OF_DAY} fields.
215      */
216     MILLI_OF_DAY("MilliOfDay", MILLIS, DAYS, ValueRange.of(0, 86400L * 1000L - 1)),
217     /**
218      * The second-of-minute.
219      * <p>
220      * This counts the second within the minute, from 0 to 59.
221      * This field has the same meaning for all calendar systems.
222      * <p>
223      * When parsing this field it behaves equivalent to the following:
224      * The value is validated in strict and smart mode but not in lenient mode.
225      */
226     SECOND_OF_MINUTE("SecondOfMinute", SECONDS, MINUTES, ValueRange.of(0, 59), "second"),
227     /**
228      * The second-of-day.
229      * <p>
230      * This counts the second within the day, from 0 to (24 * 60 * 60) - 1.
231      * This field has the same meaning for all calendar systems.
232      * <p>
233      * When parsing this field it behaves equivalent to the following:
234      * The value is validated in strict and smart mode but not in lenient mode.
235      * The value is split to form {@code SECOND_OF_MINUTE}, {@code MINUTE_OF_HOUR}
236      * and {@code HOUR_OF_DAY} fields.
237      */
238     SECOND_OF_DAY("SecondOfDay", SECONDS, DAYS, ValueRange.of(0, 86400L - 1)),
239     /**
240      * The minute-of-hour.
241      * <p>
242      * This counts the minute within the hour, from 0 to 59.
243      * This field has the same meaning for all calendar systems.
244      * <p>
245      * When parsing this field it behaves equivalent to the following:
246      * The value is validated in strict and smart mode but not in lenient mode.
247      */
248     MINUTE_OF_HOUR("MinuteOfHour", MINUTES, HOURS, ValueRange.of(0, 59), "minute"),
249     /**
250      * The minute-of-day.
251      * <p>
252      * This counts the minute within the day, from 0 to (24 * 60) - 1.
253      * This field has the same meaning for all calendar systems.
254      * <p>
255      * When parsing this field it behaves equivalent to the following:
256      * The value is validated in strict and smart mode but not in lenient mode.
257      * The value is split to form {@code MINUTE_OF_HOUR} and {@code HOUR_OF_DAY} fields.
258      */
259     MINUTE_OF_DAY("MinuteOfDay", MINUTES, DAYS, ValueRange.of(0, (24 * 60) - 1)),
260     /**
261      * The hour-of-am-pm.
262      * <p>
263      * This counts the hour within the AM/PM, from 0 to 11.
264      * This is the hour that would be observed on a standard 12-hour digital clock.
265      * This field has the same meaning for all calendar systems.
266      * <p>
267      * When parsing this field it behaves equivalent to the following:
268      * The value is validated from 0 to 11 in strict and smart mode.
269      * In lenient mode the value is not validated. It is combined with
270      * {@code AMPM_OF_DAY} to form {@code HOUR_OF_DAY} by multiplying
271      * the {AMPM_OF_DAY} value by 12.
272      */
273     HOUR_OF_AMPM("HourOfAmPm", HOURS, HALF_DAYS, ValueRange.of(0, 11)),
274     /**
275      * The clock-hour-of-am-pm.
276      * <p>
277      * This counts the hour within the AM/PM, from 1 to 12.
278      * This is the hour that would be observed on a standard 12-hour analog wall clock.
279      * This field has the same meaning for all calendar systems.
280      * <p>
281      * When parsing this field it behaves equivalent to the following:
282      * The value is validated from 1 to 12 in strict mode and from
283      * 0 to 12 in smart mode. In lenient mode the value is not validated.
284      * The field is converted to an {@code HOUR_OF_AMPM} with the same value,
285      * unless the value is 12, in which case it is converted to 0.
286      */
287     CLOCK_HOUR_OF_AMPM("ClockHourOfAmPm", HOURS, HALF_DAYS, ValueRange.of(1, 12)),
288     /**
289      * The hour-of-day.
290      * <p>
291      * This counts the hour within the day, from 0 to 23.
292      * This is the hour that would be observed on a standard 24-hour digital clock.
293      * This field has the same meaning for all calendar systems.
294      * <p>
295      * When parsing this field it behaves equivalent to the following:
296      * The value is validated in strict and smart mode but not in lenient mode.
297      * The field is combined with {@code MINUTE_OF_HOUR}, {@code SECOND_OF_MINUTE} and
298      * {@code NANO_OF_SECOND} to produce a {@code LocalTime}.
299      * In lenient mode, any excess days are added to the parsed date, or
300      * made available via {@link java.time.format.DateTimeFormatter#parsedExcessDays()}.
301      */
302     HOUR_OF_DAY("HourOfDay", HOURS, DAYS, ValueRange.of(0, 23), "hour"),
303     /**
304      * The clock-hour-of-day.
305      * <p>
306      * This counts the hour within the AM/PM, from 1 to 24.
307      * This is the hour that would be observed on a 24-hour analog wall 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 from 1 to 24 in strict mode and from
312      * 0 to 24 in smart mode. In lenient mode the value is not validated.
313      * The field is converted to an {@code HOUR_OF_DAY} with the same value,
314      * unless the value is 24, in which case it is converted to 0.
315      */
316     CLOCK_HOUR_OF_DAY("ClockHourOfDay", HOURS, DAYS, ValueRange.of(1, 24)),
317     /**
318      * The am-pm-of-day.
319      * <p>
320      * This counts the AM/PM within the day, from 0 (AM) to 1 (PM).
321      * This field has the same meaning for all calendar systems.
322      * <p>
323      * When parsing this field it behaves equivalent to the following:
324      * The value is validated from 0 to 1 in strict and smart mode.
325      * In lenient mode the value is not validated. It is combined with
326      * {@code HOUR_OF_AMPM} to form {@code HOUR_OF_DAY} by multiplying
327      * the {AMPM_OF_DAY} value by 12.
328      */
329     AMPM_OF_DAY("AmPmOfDay", HALF_DAYS, DAYS, ValueRange.of(0, 1), "dayperiod"),
330     /**
331      * The day-of-week, such as Tuesday.
332      * <p>
333      * This represents the standard concept of the day of the week.
334      * In the default ISO calendar system, this has values from Monday (1) to Sunday (7).
335      * The {@link DayOfWeek} class can be used to interpret the result.
336      * <p>
337      * Most non-ISO calendar systems also define a seven day week that aligns with ISO.
338      * Those calendar systems must also use the same numbering system, from Monday (1) to
339      * Sunday (7), which allows {@code DayOfWeek} to be used.
340      * <p>
341      * Calendar systems that do not have a standard seven day week should implement this field
342      * if they have a similar concept of named or numbered days within a period similar
343      * to a week. It is recommended that the numbering starts from 1.
344      */
345     DAY_OF_WEEK("DayOfWeek", DAYS, WEEKS, ValueRange.of(1, 7), "weekday"),
346     /**
347      * The aligned day-of-week within a month.
348      * <p>
349      * This represents concept of the count of days within the period of a week
350      * where the weeks are aligned to the start of the month.
351      * This field is typically used with {@link #ALIGNED_WEEK_OF_MONTH}.
352      * <p>
353      * For example, in a calendar systems with a seven day week, the first aligned-week-of-month
354      * starts on day-of-month 1, the second aligned-week starts on day-of-month 8, and so on.
355      * Within each of these aligned-weeks, the days are numbered from 1 to 7 and returned
356      * as the value of this field.
357      * As such, day-of-month 1 to 7 will have aligned-day-of-week values from 1 to 7.
358      * And day-of-month 8 to 14 will repeat this with aligned-day-of-week values from 1 to 7.
359      * <p>
360      * Calendar systems that do not have a seven day week should typically implement this
361      * field in the same way, but using the alternate week length.
362      */
363     ALIGNED_DAY_OF_WEEK_IN_MONTH("AlignedDayOfWeekInMonth", DAYS, WEEKS, ValueRange.of(1, 7)),
364     /**
365      * The aligned day-of-week within a year.
366      * <p>
367      * This represents concept of the count of days within the period of a week
368      * where the weeks are aligned to the start of the year.
369      * This field is typically used with {@link #ALIGNED_WEEK_OF_YEAR}.
370      * <p>
371      * For example, in a calendar systems with a seven day week, the first aligned-week-of-year
372      * starts on day-of-year 1, the second aligned-week starts on day-of-year 8, and so on.
373      * Within each of these aligned-weeks, the days are numbered from 1 to 7 and returned
374      * as the value of this field.
375      * As such, day-of-year 1 to 7 will have aligned-day-of-week values from 1 to 7.
376      * And day-of-year 8 to 14 will repeat this with aligned-day-of-week values from 1 to 7.
377      * <p>
378      * Calendar systems that do not have a seven day week should typically implement this
379      * field in the same way, but using the alternate week length.
380      */
381     ALIGNED_DAY_OF_WEEK_IN_YEAR("AlignedDayOfWeekInYear", DAYS, WEEKS, ValueRange.of(1, 7)),
382     /**
383      * The day-of-month.
384      * <p>
385      * This represents the concept of the day within the month.
386      * In the default ISO calendar system, this has values from 1 to 31 in most months.
387      * April, June, September, November have days from 1 to 30, while February has days
388      * from 1 to 28, or 29 in a leap year.
389      * <p>
390      * Non-ISO calendar systems should implement this field using the most recognized
391      * day-of-month values for users of the calendar system.
392      * Normally, this is a count of days from 1 to the length of the month.
393      */
394     DAY_OF_MONTH("DayOfMonth", DAYS, MONTHS, ValueRange.of(1, 28, 31), "day"),
395     /**
396      * The day-of-year.
397      * <p>
398      * This represents the concept of the day within the year.
399      * In the default ISO calendar system, this has values from 1 to 365 in standard
400      * years and 1 to 366 in leap years.
401      * <p>
402      * Non-ISO calendar systems should implement this field using the most recognized
403      * day-of-year values for users of the calendar system.
404      * Normally, this is a count of days from 1 to the length of the year.
405      * <p>
406      * Note that a non-ISO calendar system may have year numbering system that changes
407      * at a different point to the natural reset in the month numbering. An example
408      * of this is the Japanese calendar system where a change of era, which resets
409      * the year number to 1, can happen on any date. The era and year reset also cause
410      * the day-of-year to be reset to 1, but not the month-of-year or day-of-month.
411      */
412     DAY_OF_YEAR("DayOfYear", DAYS, YEARS, ValueRange.of(1, 365, 366)),
413     /**
414      * The epoch-day, based on the Java epoch of 1970-01-01 (ISO).
415      * <p>
416      * This field is the sequential count of days where 1970-01-01 (ISO) is zero.
417      * Note that this uses the <i>local</i> time-line, ignoring offset and time-zone.
418      * <p>
419      * This field is strictly defined to have the same meaning in all calendar systems.
420      * This is necessary to ensure interoperation between calendars.
421      */
422     EPOCH_DAY("EpochDay", DAYS, FOREVER, ValueRange.of((long) (Year.MIN_VALUE * 365.25), (long) (Year.MAX_VALUE * 365.25))),
423     /**
424      * The aligned week within a month.
425      * <p>
426      * This represents concept of the count of weeks within the period of a month
427      * where the weeks are aligned to the start of the month.
428      * This field is typically used with {@link #ALIGNED_DAY_OF_WEEK_IN_MONTH}.
429      * <p>
430      * For example, in a calendar systems with a seven day week, the first aligned-week-of-month
431      * starts on day-of-month 1, the second aligned-week starts on day-of-month 8, and so on.
432      * Thus, day-of-month values 1 to 7 are in aligned-week 1, while day-of-month values
433      * 8 to 14 are in aligned-week 2, and so on.
434      * <p>
435      * Calendar systems that do not have a seven day week should typically implement this
436      * field in the same way, but using the alternate week length.
437      */
438     ALIGNED_WEEK_OF_MONTH("AlignedWeekOfMonth", WEEKS, MONTHS, ValueRange.of(1, 4, 5)),
439     /**
440      * The aligned week within a year.
441      * <p>
442      * This represents concept of the count of weeks within the period of a year
443      * where the weeks are aligned to the start of the year.
444      * This field is typically used with {@link #ALIGNED_DAY_OF_WEEK_IN_YEAR}.
445      * <p>
446      * For example, in a calendar systems with a seven day week, the first aligned-week-of-year
447      * starts on day-of-year 1, the second aligned-week starts on day-of-year 8, and so on.
448      * Thus, day-of-year values 1 to 7 are in aligned-week 1, while day-of-year values
449      * 8 to 14 are in aligned-week 2, and so on.
450      * <p>
451      * Calendar systems that do not have a seven day week should typically implement this
452      * field in the same way, but using the alternate week length.
453      */
454     ALIGNED_WEEK_OF_YEAR("AlignedWeekOfYear", WEEKS, YEARS, ValueRange.of(1, 53)),
455     /**
456      * The month-of-year, such as March.
457      * <p>
458      * This represents the concept of the month within the year.
459      * In the default ISO calendar system, this has values from January (1) to December (12).
460      * <p>
461      * Non-ISO calendar systems should implement this field using the most recognized
462      * month-of-year values for users of the calendar system.
463      * Normally, this is a count of months starting from 1.
464      */
465     MONTH_OF_YEAR("MonthOfYear", MONTHS, YEARS, ValueRange.of(1, 12), "month"),
466     /**
467      * The proleptic-month based, counting months sequentially from year 0.
468      * <p>
469      * This field is the sequential count of months where the first month
470      * in proleptic-year zero has the value zero.
471      * Later months have increasingly larger values.
472      * Earlier months have increasingly small values.
473      * There are no gaps or breaks in the sequence of months.
474      * Note that this uses the <i>local</i> time-line, ignoring offset and time-zone.
475      * <p>
476      * In the default ISO calendar system, June 2012 would have the value
477      * {@code (2012 * 12 + 6 - 1)}. This field is primarily for internal use.
478      * <p>
479      * Non-ISO calendar systems must implement this field as per the definition above.
480      * It is just a simple zero-based count of elapsed months from the start of proleptic-year 0.
481      * All calendar systems with a full proleptic-year definition will have a year zero.
482      * If the calendar system has a minimum year that excludes year zero, then one must
483      * be extrapolated in order for this method to be defined.
484      */
485     PROLEPTIC_MONTH("ProlepticMonth", MONTHS, FOREVER, ValueRange.of(Year.MIN_VALUE * 12L, Year.MAX_VALUE * 12L + 11)),
486     /**
487      * The year within the era.
488      * <p>
489      * This represents the concept of the year within the era.
490      * This field is typically used with {@link #ERA}.
491      * <p>
492      * The standard mental model for a date is based on three concepts - year, month and day.
493      * These map onto the {@code YEAR}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} fields.
494      * Note that there is no reference to eras.
495      * The full model for a date requires four concepts - era, year, month and day. These map onto
496      * the {@code ERA}, {@code YEAR_OF_ERA}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} fields.
497      * Whether this field or {@code YEAR} is used depends on which mental model is being used.
498      * See {@link ChronoLocalDate} for more discussion on this topic.
499      * <p>
500      * In the default ISO calendar system, there are two eras defined, 'BCE' and 'CE'.
501      * The era 'CE' is the one currently in use and year-of-era runs from 1 to the maximum value.
502      * The era 'BCE' is the previous era, and the year-of-era runs backwards.
503      * <p>
504      * For example, subtracting a year each time yield the following:<br>
505      * - year-proleptic 2  = 'CE' year-of-era 2<br>
506      * - year-proleptic 1  = 'CE' year-of-era 1<br>
507      * - year-proleptic 0  = 'BCE' year-of-era 1<br>
508      * - year-proleptic -1 = 'BCE' year-of-era 2<br>
509      * <p>
510      * Note that the ISO-8601 standard does not actually define eras.
511      * Note also that the ISO eras do not align with the well-known AD/BC eras due to the
512      * change between the Julian and Gregorian calendar systems.
513      * <p>
514      * Non-ISO calendar systems should implement this field using the most recognized
515      * year-of-era value for users of the calendar system.
516      * Since most calendar systems have only two eras, the year-of-era numbering approach
517      * will typically be the same as that used by the ISO calendar system.
518      * The year-of-era value should typically always be positive, however this is not required.
519      */
520     YEAR_OF_ERA("YearOfEra", YEARS, FOREVER, ValueRange.of(1, Year.MAX_VALUE, Year.MAX_VALUE + 1)),
521     /**
522      * The proleptic year, such as 2012.
523      * <p>
524      * This represents the concept of the year, counting sequentially and using negative numbers.
525      * The proleptic year is not interpreted in terms of the era.
526      * See {@link #YEAR_OF_ERA} for an example showing the mapping from proleptic year to year-of-era.
527      * <p>
528      * The standard mental model for a date is based on three concepts - year, month and day.
529      * These map onto the {@code YEAR}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} fields.
530      * Note that there is no reference to eras.
531      * The full model for a date requires four concepts - era, year, month and day. These map onto
532      * the {@code ERA}, {@code YEAR_OF_ERA}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} fields.
533      * Whether this field or {@code YEAR_OF_ERA} is used depends on which mental model is being used.
534      * See {@link ChronoLocalDate} for more discussion on this topic.
535      * <p>
536      * Non-ISO calendar systems should implement this field as follows.
537      * If the calendar system has only two eras, before and after a fixed date, then the
538      * proleptic-year value must be the same as the year-of-era value for the later era,
539      * and increasingly negative for the earlier era.
540      * If the calendar system has more than two eras, then the proleptic-year value may be
541      * defined with any appropriate value, although defining it to be the same as ISO may be
542      * the best option.
543      */
544     YEAR("Year", YEARS, FOREVER, ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE), "year"),
545     /**
546      * The era.
547      * <p>
548      * This represents the concept of the era, which is the largest division of the time-line.
549      * This field is typically used with {@link #YEAR_OF_ERA}.
550      * <p>
551      * In the default ISO calendar system, there are two eras defined, 'BCE' and 'CE'.
552      * The era 'CE' is the one currently in use and year-of-era runs from 1 to the maximum value.
553      * The era 'BCE' is the previous era, and the year-of-era runs backwards.
554      * See {@link #YEAR_OF_ERA} for a full example.
555      * <p>
556      * Non-ISO calendar systems should implement this field to define eras.
557      * The value of the era that was active on 1970-01-01 (ISO) must be assigned the value 1.
558      * Earlier eras must have sequentially smaller values.
559      * Later eras must have sequentially larger values,
560      */
561     ERA("Era", ERAS, FOREVER, ValueRange.of(0, 1), "era"),
562     /**
563      * The instant epoch-seconds.
564      * <p>
565      * This represents the concept of the sequential count of seconds where
566      * 1970-01-01T00:00Z (ISO) is zero.
567      * This field may be used with {@link #NANO_OF_SECOND} to represent the fraction of the second.
568      * <p>
569      * An {@link Instant} represents an instantaneous point on the time-line.
570      * On their own, an instant has insufficient information to allow a local date-time to be obtained.
571      * Only when paired with an offset or time-zone can the local date or time be calculated.
572      * <p>
573      * This field is strictly defined to have the same meaning in all calendar systems.
574      * This is necessary to ensure interoperation between calendars.
575      */
576     INSTANT_SECONDS("InstantSeconds", SECONDS, FOREVER, ValueRange.of(Long.MIN_VALUE, Long.MAX_VALUE)),
577     /**
578      * The offset from UTC/Greenwich.
579      * <p>
580      * This represents the concept of the offset in seconds of local time from UTC/Greenwich.
581      * <p>
582      * A {@link ZoneOffset} represents the period of time that local time differs from UTC/Greenwich.
583      * This is usually a fixed number of hours and minutes.
584      * It is equivalent to the {@link ZoneOffset#getTotalSeconds() total amount} of the offset in seconds.
585      * For example, during the winter Paris has an offset of {@code +01:00}, which is 3600 seconds.
586      * <p>
587      * This field is strictly defined to have the same meaning in all calendar systems.
588      * This is necessary to ensure interoperation between calendars.
589      */
590     OFFSET_SECONDS("OffsetSeconds", SECONDS, FOREVER, ValueRange.of(-18 * 3600, 18 * 3600));
591 
592     private final String name;
593     private final TemporalUnit baseUnit;
594     private final TemporalUnit rangeUnit;
595     private final ValueRange range;
596     private final String displayNameKey;
597 
ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range)598     private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range) {
599         this.name = name;
600         this.baseUnit = baseUnit;
601         this.rangeUnit = rangeUnit;
602         this.range = range;
603         this.displayNameKey = null;
604     }
605 
ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range, String displayNameKey)606     private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit,
607             ValueRange range, String displayNameKey) {
608         this.name = name;
609         this.baseUnit = baseUnit;
610         this.rangeUnit = rangeUnit;
611         this.range = range;
612         this.displayNameKey = displayNameKey;
613     }
614 
615     @Override
getDisplayName(Locale locale)616     public String getDisplayName(Locale locale) {
617         Objects.requireNonNull(locale, "locale");
618         if (displayNameKey == null) {
619             return name;
620         }
621 
622         // Android-changed: use ICU names.
623         DateTimePatternGenerator generator = DateTimePatternGenerator
624                 .getInstance(ULocale.forLocale(locale));
625         String icuName = generator.getAppendItemName(getIcuFieldNumber(this));
626         return icuName != null && !icuName.isEmpty() ? icuName : name;
627     }
628 
629     /**
630      * @return the field id according to {@link DateTimePatternGenerator} for the field.
631      */
getIcuFieldNumber(ChronoField field)632     private static int getIcuFieldNumber(ChronoField field) {
633         switch (field) {
634             case SECOND_OF_MINUTE:
635                 return DateTimePatternGenerator.SECOND;
636             case MINUTE_OF_HOUR:
637                 return DateTimePatternGenerator.MINUTE;
638             case HOUR_OF_DAY:
639                 return DateTimePatternGenerator.HOUR;
640             case AMPM_OF_DAY:
641                 return DateTimePatternGenerator.DAYPERIOD;
642             case DAY_OF_WEEK:
643                 return DateTimePatternGenerator.WEEKDAY;
644             case DAY_OF_MONTH:
645                 return DateTimePatternGenerator.DAY;
646             case MONTH_OF_YEAR:
647                 return DateTimePatternGenerator.MONTH;
648             case YEAR:
649                 return DateTimePatternGenerator.YEAR;
650             case ERA:
651                 return DateTimePatternGenerator.ERA;
652             default:
653                 throw new IllegalArgumentException("Unexpected ChronoField " + field.name());
654         }
655     }
656 
657     @Override
getBaseUnit()658     public TemporalUnit getBaseUnit() {
659         return baseUnit;
660     }
661 
662     @Override
getRangeUnit()663     public TemporalUnit getRangeUnit() {
664         return rangeUnit;
665     }
666 
667     /**
668      * Gets the range of valid values for the field.
669      * <p>
670      * All fields can be expressed as a {@code long} integer.
671      * This method returns an object that describes the valid range for that value.
672      * <p>
673      * This method returns the range of the field in the ISO-8601 calendar system.
674      * This range may be incorrect for other calendar systems.
675      * Use {@link Chronology#range(ChronoField)} to access the correct range
676      * for a different calendar system.
677      * <p>
678      * Note that the result only describes the minimum and maximum valid values
679      * and it is important not to read too much into them. For example, there
680      * could be values within the range that are invalid for the field.
681      *
682      * @return the range of valid values for the field, not null
683      */
684     @Override
range()685     public ValueRange range() {
686         return range;
687     }
688 
689     //-----------------------------------------------------------------------
690     /**
691      * Checks if this field represents a component of a date.
692      * <p>
693      * Fields from day-of-week to era are date-based.
694      *
695      * @return true if it is a component of a date
696      */
697     @Override
isDateBased()698     public boolean isDateBased() {
699         return ordinal() >= DAY_OF_WEEK.ordinal() && ordinal() <= ERA.ordinal();
700     }
701 
702     /**
703      * Checks if this field represents a component of a time.
704      * <p>
705      * Fields from nano-of-second to am-pm-of-day are time-based.
706      *
707      * @return true if it is a component of a time
708      */
709     @Override
isTimeBased()710     public boolean isTimeBased() {
711         return ordinal() < DAY_OF_WEEK.ordinal();
712     }
713 
714     //-----------------------------------------------------------------------
715     /**
716      * Checks that the specified value is valid for this field.
717      * <p>
718      * This validates that the value is within the outer range of valid values
719      * returned by {@link #range()}.
720      * <p>
721      * This method checks against the range of the field in the ISO-8601 calendar system.
722      * This range may be incorrect for other calendar systems.
723      * Use {@link Chronology#range(ChronoField)} to access the correct range
724      * for a different calendar system.
725      *
726      * @param value  the value to check
727      * @return the value that was passed in
728      */
checkValidValue(long value)729     public long checkValidValue(long value) {
730         return range().checkValidValue(value, this);
731     }
732 
733     /**
734      * Checks that the specified value is valid and fits in an {@code int}.
735      * <p>
736      * This validates that the value is within the outer range of valid values
737      * returned by {@link #range()}.
738      * It also checks that all valid values are within the bounds of an {@code int}.
739      * <p>
740      * This method checks against the range of the field in the ISO-8601 calendar system.
741      * This range may be incorrect for other calendar systems.
742      * Use {@link Chronology#range(ChronoField)} to access the correct range
743      * for a different calendar system.
744      *
745      * @param value  the value to check
746      * @return the value that was passed in
747      */
checkValidIntValue(long value)748     public int checkValidIntValue(long value) {
749         return range().checkValidIntValue(value, this);
750     }
751 
752     //-----------------------------------------------------------------------
753     @Override
isSupportedBy(TemporalAccessor temporal)754     public boolean isSupportedBy(TemporalAccessor temporal) {
755         return temporal.isSupported(this);
756     }
757 
758     @Override
rangeRefinedBy(TemporalAccessor temporal)759     public ValueRange rangeRefinedBy(TemporalAccessor temporal) {
760         return temporal.range(this);
761     }
762 
763     @Override
getFrom(TemporalAccessor temporal)764     public long getFrom(TemporalAccessor temporal) {
765         return temporal.getLong(this);
766     }
767 
768     @SuppressWarnings("unchecked")
769     @Override
adjustInto(R temporal, long newValue)770     public <R extends Temporal> R adjustInto(R temporal, long newValue) {
771         return (R) temporal.with(this, newValue);
772     }
773 
774     //-----------------------------------------------------------------------
775     @Override
toString()776     public String toString() {
777         return name;
778     }
779 
780 }
781