Path: blob/master/src/java.base/share/classes/java/time/temporal/ChronoField.java
41159 views
/*1* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos27*28* All rights reserved.29*30* Redistribution and use in source and binary forms, with or without31* modification, are permitted provided that the following conditions are met:32*33* * Redistributions of source code must retain the above copyright notice,34* this list of conditions and the following disclaimer.35*36* * Redistributions in binary form must reproduce the above copyright notice,37* this list of conditions and the following disclaimer in the documentation38* and/or other materials provided with the distribution.39*40* * Neither the name of JSR-310 nor the names of its contributors41* may be used to endorse or promote products derived from this software42* without specific prior written permission.43*44* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS45* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT46* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR47* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR48* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,49* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,50* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR51* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF52* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING53* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS54* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.55*/56package java.time.temporal;5758import static java.time.temporal.ChronoUnit.DAYS;59import static java.time.temporal.ChronoUnit.ERAS;60import static java.time.temporal.ChronoUnit.FOREVER;61import static java.time.temporal.ChronoUnit.HALF_DAYS;62import static java.time.temporal.ChronoUnit.HOURS;63import static java.time.temporal.ChronoUnit.MICROS;64import static java.time.temporal.ChronoUnit.MILLIS;65import static java.time.temporal.ChronoUnit.MINUTES;66import static java.time.temporal.ChronoUnit.MONTHS;67import static java.time.temporal.ChronoUnit.NANOS;68import static java.time.temporal.ChronoUnit.SECONDS;69import static java.time.temporal.ChronoUnit.WEEKS;70import static java.time.temporal.ChronoUnit.YEARS;7172import java.time.DayOfWeek;73import java.time.Instant;74import java.time.Year;75import java.time.ZoneOffset;76import java.time.chrono.ChronoLocalDate;77import java.time.chrono.Chronology;78import java.util.Locale;79import java.util.Objects;80import java.util.ResourceBundle;81import sun.util.locale.provider.CalendarDataUtility;82import sun.util.locale.provider.LocaleProviderAdapter;83import sun.util.locale.provider.LocaleResources;8485/**86* A standard set of fields.87* <p>88* This set of fields provide field-based access to manipulate a date, time or date-time.89* The standard set of fields can be extended by implementing {@link TemporalField}.90* <p>91* These fields are intended to be applicable in multiple calendar systems.92* For example, most non-ISO calendar systems define dates as a year, month and day,93* just with slightly different rules.94* The documentation of each field explains how it operates.95*96* @implSpec97* This is a final, immutable and thread-safe enum.98*99* @since 1.8100*/101public enum ChronoField implements TemporalField {102103/**104* The nano-of-second.105* <p>106* This counts the nanosecond within the second, from 0 to 999,999,999.107* This field has the same meaning for all calendar systems.108* <p>109* This field is used to represent the nano-of-second handling any fraction of the second.110* Implementations of {@code TemporalAccessor} should provide a value for this field if111* they can return a value for {@link #SECOND_OF_MINUTE}, {@link #SECOND_OF_DAY} or112* {@link #INSTANT_SECONDS} filling unknown precision with zero.113* <p>114* When this field is used for setting a value, it should set as much precision as the115* object stores, using integer division to remove excess precision.116* For example, if the {@code TemporalAccessor} stores time to millisecond precision,117* then the nano-of-second must be divided by 1,000,000 before replacing the milli-of-second.118* <p>119* When parsing this field it behaves equivalent to the following:120* The value is validated in strict and smart mode but not in lenient mode.121* The field is resolved in combination with {@code MILLI_OF_SECOND} and {@code MICRO_OF_SECOND}.122*/123NANO_OF_SECOND("NanoOfSecond", NANOS, SECONDS, ValueRange.of(0, 999_999_999)),124/**125* The nano-of-day.126* <p>127* This counts the nanosecond within the day, from 0 to (24 * 60 * 60 * 1,000,000,000) - 1.128* This field has the same meaning for all calendar systems.129* <p>130* This field is used to represent the nano-of-day handling any fraction of the second.131* Implementations of {@code TemporalAccessor} should provide a value for this field if132* they can return a value for {@link #SECOND_OF_DAY} filling unknown precision with zero.133* <p>134* When parsing this field it behaves equivalent to the following:135* The value is validated in strict and smart mode but not in lenient mode.136* The value is split to form {@code NANO_OF_SECOND}, {@code SECOND_OF_MINUTE},137* {@code MINUTE_OF_HOUR} and {@code HOUR_OF_DAY} fields.138*/139NANO_OF_DAY("NanoOfDay", NANOS, DAYS, ValueRange.of(0, 86400L * 1000_000_000L - 1)),140/**141* The micro-of-second.142* <p>143* This counts the microsecond within the second, from 0 to 999,999.144* This field has the same meaning for all calendar systems.145* <p>146* This field is used to represent the micro-of-second handling any fraction of the second.147* Implementations of {@code TemporalAccessor} should provide a value for this field if148* they can return a value for {@link #SECOND_OF_MINUTE}, {@link #SECOND_OF_DAY} or149* {@link #INSTANT_SECONDS} filling unknown precision with zero.150* <p>151* When this field is used for setting a value, it should behave in the same way as152* setting {@link #NANO_OF_SECOND} with the value multiplied by 1,000.153* <p>154* When parsing this field it behaves equivalent to the following:155* The value is validated in strict and smart mode but not in lenient mode.156* The field is resolved in combination with {@code MILLI_OF_SECOND} to produce157* {@code NANO_OF_SECOND}.158*/159MICRO_OF_SECOND("MicroOfSecond", MICROS, SECONDS, ValueRange.of(0, 999_999)),160/**161* The micro-of-day.162* <p>163* This counts the microsecond within the day, from 0 to (24 * 60 * 60 * 1,000,000) - 1.164* This field has the same meaning for all calendar systems.165* <p>166* This field is used to represent the micro-of-day handling any fraction of the second.167* Implementations of {@code TemporalAccessor} should provide a value for this field if168* they can return a value for {@link #SECOND_OF_DAY} filling unknown precision with zero.169* <p>170* When this field is used for setting a value, it should behave in the same way as171* setting {@link #NANO_OF_DAY} with the value multiplied by 1,000.172* <p>173* When parsing this field it behaves equivalent to the following:174* The value is validated in strict and smart mode but not in lenient mode.175* The value is split to form {@code MICRO_OF_SECOND}, {@code SECOND_OF_MINUTE},176* {@code MINUTE_OF_HOUR} and {@code HOUR_OF_DAY} fields.177*/178MICRO_OF_DAY("MicroOfDay", MICROS, DAYS, ValueRange.of(0, 86400L * 1000_000L - 1)),179/**180* The milli-of-second.181* <p>182* This counts the millisecond within the second, from 0 to 999.183* This field has the same meaning for all calendar systems.184* <p>185* This field is used to represent the milli-of-second handling any fraction of the second.186* Implementations of {@code TemporalAccessor} should provide a value for this field if187* they can return a value for {@link #SECOND_OF_MINUTE}, {@link #SECOND_OF_DAY} or188* {@link #INSTANT_SECONDS} filling unknown precision with zero.189* <p>190* When this field is used for setting a value, it should behave in the same way as191* setting {@link #NANO_OF_SECOND} with the value multiplied by 1,000,000.192* <p>193* When parsing this field it behaves equivalent to the following:194* The value is validated in strict and smart mode but not in lenient mode.195* The field is resolved in combination with {@code MICRO_OF_SECOND} to produce196* {@code NANO_OF_SECOND}.197*/198MILLI_OF_SECOND("MilliOfSecond", MILLIS, SECONDS, ValueRange.of(0, 999)),199/**200* The milli-of-day.201* <p>202* This counts the millisecond within the day, from 0 to (24 * 60 * 60 * 1,000) - 1.203* This field has the same meaning for all calendar systems.204* <p>205* This field is used to represent the milli-of-day handling any fraction of the second.206* Implementations of {@code TemporalAccessor} should provide a value for this field if207* they can return a value for {@link #SECOND_OF_DAY} filling unknown precision with zero.208* <p>209* When this field is used for setting a value, it should behave in the same way as210* setting {@link #NANO_OF_DAY} with the value multiplied by 1,000,000.211* <p>212* When parsing this field it behaves equivalent to the following:213* The value is validated in strict and smart mode but not in lenient mode.214* The value is split to form {@code MILLI_OF_SECOND}, {@code SECOND_OF_MINUTE},215* {@code MINUTE_OF_HOUR} and {@code HOUR_OF_DAY} fields.216*/217MILLI_OF_DAY("MilliOfDay", MILLIS, DAYS, ValueRange.of(0, 86400L * 1000L - 1)),218/**219* The second-of-minute.220* <p>221* This counts the second within the minute, from 0 to 59.222* This field has the same meaning for all calendar systems.223* <p>224* When parsing this field it behaves equivalent to the following:225* The value is validated in strict and smart mode but not in lenient mode.226*/227SECOND_OF_MINUTE("SecondOfMinute", SECONDS, MINUTES, ValueRange.of(0, 59), "second"),228/**229* The second-of-day.230* <p>231* This counts the second within the day, from 0 to (24 * 60 * 60) - 1.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* The value is split to form {@code SECOND_OF_MINUTE}, {@code MINUTE_OF_HOUR}237* and {@code HOUR_OF_DAY} fields.238*/239SECOND_OF_DAY("SecondOfDay", SECONDS, DAYS, ValueRange.of(0, 86400L - 1)),240/**241* The minute-of-hour.242* <p>243* This counts the minute within the hour, from 0 to 59.244* This field has the same meaning for all calendar systems.245* <p>246* When parsing this field it behaves equivalent to the following:247* The value is validated in strict and smart mode but not in lenient mode.248*/249MINUTE_OF_HOUR("MinuteOfHour", MINUTES, HOURS, ValueRange.of(0, 59), "minute"),250/**251* The minute-of-day.252* <p>253* This counts the minute within the day, from 0 to (24 * 60) - 1.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* The value is split to form {@code MINUTE_OF_HOUR} and {@code HOUR_OF_DAY} fields.259*/260MINUTE_OF_DAY("MinuteOfDay", MINUTES, DAYS, ValueRange.of(0, (24 * 60) - 1)),261/**262* The hour-of-am-pm.263* <p>264* This counts the hour within the AM/PM, from 0 to 11.265* This is the hour that would be observed on a standard 12-hour digital clock.266* This field has the same meaning for all calendar systems.267* <p>268* When parsing this field it behaves equivalent to the following:269* The value is validated from 0 to 11 in strict and smart mode.270* In lenient mode the value is not validated. It is combined with271* {@code AMPM_OF_DAY} to form {@code HOUR_OF_DAY} by multiplying272* the {@code AMPM_OF_DAY} value by 12.273* <p>274* See {@link #CLOCK_HOUR_OF_AMPM} for the related field that counts hours from 1 to 12.275*/276HOUR_OF_AMPM("HourOfAmPm", HOURS, HALF_DAYS, ValueRange.of(0, 11)),277/**278* The clock-hour-of-am-pm.279* <p>280* This counts the hour within the AM/PM, from 1 to 12.281* This is the hour that would be observed on a standard 12-hour analog wall clock.282* This field has the same meaning for all calendar systems.283* <p>284* When parsing this field it behaves equivalent to the following:285* The value is validated from 1 to 12 in strict mode and from286* 0 to 12 in smart mode. In lenient mode the value is not validated.287* The field is converted to an {@code HOUR_OF_AMPM} with the same value,288* unless the value is 12, in which case it is converted to 0.289* <p>290* See {@link #HOUR_OF_AMPM} for the related field that counts hours from 0 to 11.291*/292CLOCK_HOUR_OF_AMPM("ClockHourOfAmPm", HOURS, HALF_DAYS, ValueRange.of(1, 12)),293/**294* The hour-of-day.295* <p>296* This counts the hour within the day, from 0 to 23.297* This is the hour that would be observed on a standard 24-hour digital clock.298* This field has the same meaning for all calendar systems.299* <p>300* When parsing this field it behaves equivalent to the following:301* The value is validated in strict and smart mode but not in lenient mode.302* The field is combined with {@code MINUTE_OF_HOUR}, {@code SECOND_OF_MINUTE} and303* {@code NANO_OF_SECOND} to produce a {@code LocalTime}.304* In lenient mode, any excess days are added to the parsed date, or305* made available via {@link java.time.format.DateTimeFormatter#parsedExcessDays()}.306* <p>307* See {@link #CLOCK_HOUR_OF_DAY} for the related field that counts hours from 1 to 24.308*/309HOUR_OF_DAY("HourOfDay", HOURS, DAYS, ValueRange.of(0, 23), "hour"),310/**311* The clock-hour-of-day.312* <p>313* This counts the hour within the day, from 1 to 24.314* This is the hour that would be observed on a 24-hour analog wall clock.315* This field has the same meaning for all calendar systems.316* <p>317* When parsing this field it behaves equivalent to the following:318* The value is validated from 1 to 24 in strict mode and from319* 0 to 24 in smart mode. In lenient mode the value is not validated.320* The field is converted to an {@code HOUR_OF_DAY} with the same value,321* unless the value is 24, in which case it is converted to 0.322* <p>323* See {@link #HOUR_OF_DAY} for the related field that counts hours from 0 to 23.324*/325CLOCK_HOUR_OF_DAY("ClockHourOfDay", HOURS, DAYS, ValueRange.of(1, 24)),326/**327* The am-pm-of-day.328* <p>329* This counts the AM/PM within the day, from 0 (AM) to 1 (PM).330* This field has the same meaning for all calendar systems.331* <p>332* When parsing this field it behaves equivalent to the following:333* The value is validated from 0 to 1 in strict and smart mode.334* In lenient mode the value is not validated. It is combined with335* {@code HOUR_OF_AMPM} (if not present, it defaults to '6') to form336* {@code HOUR_OF_DAY} by multiplying the {@code AMPM_OF_DAY} value337* by 12.338*/339AMPM_OF_DAY("AmPmOfDay", HALF_DAYS, DAYS, ValueRange.of(0, 1), "dayperiod"),340/**341* The day-of-week, such as Tuesday.342* <p>343* This represents the standard concept of the day of the week.344* In the default ISO calendar system, this has values from Monday (1) to Sunday (7).345* The {@link DayOfWeek} class can be used to interpret the result.346* <p>347* Most non-ISO calendar systems also define a seven day week that aligns with ISO.348* Those calendar systems must also use the same numbering system, from Monday (1) to349* Sunday (7), which allows {@code DayOfWeek} to be used.350* <p>351* Calendar systems that do not have a standard seven day week should implement this field352* if they have a similar concept of named or numbered days within a period similar353* to a week. It is recommended that the numbering starts from 1.354*/355DAY_OF_WEEK("DayOfWeek", DAYS, WEEKS, ValueRange.of(1, 7), "weekday"),356/**357* The aligned day-of-week within a month.358* <p>359* This represents concept of the count of days within the period of a week360* where the weeks are aligned to the start of the month.361* This field is typically used with {@link #ALIGNED_WEEK_OF_MONTH}.362* <p>363* For example, in a calendar systems with a seven day week, the first aligned-week-of-month364* starts on day-of-month 1, the second aligned-week starts on day-of-month 8, and so on.365* Within each of these aligned-weeks, the days are numbered from 1 to 7 and returned366* as the value of this field.367* As such, day-of-month 1 to 7 will have aligned-day-of-week values from 1 to 7.368* And day-of-month 8 to 14 will repeat this with aligned-day-of-week values from 1 to 7.369* <p>370* Calendar systems that do not have a seven day week should typically implement this371* field in the same way, but using the alternate week length.372*/373ALIGNED_DAY_OF_WEEK_IN_MONTH("AlignedDayOfWeekInMonth", DAYS, WEEKS, ValueRange.of(1, 7)),374/**375* The aligned day-of-week within a year.376* <p>377* This represents concept of the count of days within the period of a week378* where the weeks are aligned to the start of the year.379* This field is typically used with {@link #ALIGNED_WEEK_OF_YEAR}.380* <p>381* For example, in a calendar systems with a seven day week, the first aligned-week-of-year382* starts on day-of-year 1, the second aligned-week starts on day-of-year 8, and so on.383* Within each of these aligned-weeks, the days are numbered from 1 to 7 and returned384* as the value of this field.385* As such, day-of-year 1 to 7 will have aligned-day-of-week values from 1 to 7.386* And day-of-year 8 to 14 will repeat this with aligned-day-of-week values from 1 to 7.387* <p>388* Calendar systems that do not have a seven day week should typically implement this389* field in the same way, but using the alternate week length.390*/391ALIGNED_DAY_OF_WEEK_IN_YEAR("AlignedDayOfWeekInYear", DAYS, WEEKS, ValueRange.of(1, 7)),392/**393* The day-of-month.394* <p>395* This represents the concept of the day within the month.396* In the default ISO calendar system, this has values from 1 to 31 in most months.397* April, June, September, November have days from 1 to 30, while February has days398* from 1 to 28, or 29 in a leap year.399* <p>400* Non-ISO calendar systems should implement this field using the most recognized401* day-of-month values for users of the calendar system.402* Normally, this is a count of days from 1 to the length of the month.403*/404DAY_OF_MONTH("DayOfMonth", DAYS, MONTHS, ValueRange.of(1, 28, 31), "day"),405/**406* The day-of-year.407* <p>408* This represents the concept of the day within the year.409* In the default ISO calendar system, this has values from 1 to 365 in standard410* years and 1 to 366 in leap years.411* <p>412* Non-ISO calendar systems should implement this field using the most recognized413* day-of-year values for users of the calendar system.414* Normally, this is a count of days from 1 to the length of the year.415* <p>416* Note that a non-ISO calendar system may have year numbering system that changes417* at a different point to the natural reset in the month numbering. An example418* of this is the Japanese calendar system where a change of era, which resets419* the year number to 1, can happen on any date. The era and year reset also cause420* the day-of-year to be reset to 1, but not the month-of-year or day-of-month.421*/422DAY_OF_YEAR("DayOfYear", DAYS, YEARS, ValueRange.of(1, 365, 366)),423/**424* The epoch-day, based on the Java epoch of 1970-01-01 (ISO).425* <p>426* This field is the sequential count of days where 1970-01-01 (ISO) is zero.427* Note that this uses the <i>local</i> time-line, ignoring offset and time-zone.428* <p>429* This field is strictly defined to have the same meaning in all calendar systems.430* This is necessary to ensure interoperation between calendars.431* <p>432* Range of EpochDay is between (LocalDate.MIN.toEpochDay(), LocalDate.MAX.toEpochDay())433* both inclusive.434*/435EPOCH_DAY("EpochDay", DAYS, FOREVER, ValueRange.of(-365243219162L, 365241780471L)),436/**437* The aligned week within a month.438* <p>439* This represents concept of the count of weeks within the period of a month440* where the weeks are aligned to the start of the month.441* This field is typically used with {@link #ALIGNED_DAY_OF_WEEK_IN_MONTH}.442* <p>443* For example, in a calendar systems with a seven day week, the first aligned-week-of-month444* starts on day-of-month 1, the second aligned-week starts on day-of-month 8, and so on.445* Thus, day-of-month values 1 to 7 are in aligned-week 1, while day-of-month values446* 8 to 14 are in aligned-week 2, and so on.447* <p>448* Calendar systems that do not have a seven day week should typically implement this449* field in the same way, but using the alternate week length.450*/451ALIGNED_WEEK_OF_MONTH("AlignedWeekOfMonth", WEEKS, MONTHS, ValueRange.of(1, 4, 5)),452/**453* The aligned week within a year.454* <p>455* This represents concept of the count of weeks within the period of a year456* where the weeks are aligned to the start of the year.457* This field is typically used with {@link #ALIGNED_DAY_OF_WEEK_IN_YEAR}.458* <p>459* For example, in a calendar systems with a seven day week, the first aligned-week-of-year460* starts on day-of-year 1, the second aligned-week starts on day-of-year 8, and so on.461* Thus, day-of-year values 1 to 7 are in aligned-week 1, while day-of-year values462* 8 to 14 are in aligned-week 2, and so on.463* <p>464* Calendar systems that do not have a seven day week should typically implement this465* field in the same way, but using the alternate week length.466*/467ALIGNED_WEEK_OF_YEAR("AlignedWeekOfYear", WEEKS, YEARS, ValueRange.of(1, 53)),468/**469* The month-of-year, such as March.470* <p>471* This represents the concept of the month within the year.472* In the default ISO calendar system, this has values from January (1) to December (12).473* <p>474* Non-ISO calendar systems should implement this field using the most recognized475* month-of-year values for users of the calendar system.476* Normally, this is a count of months starting from 1.477*/478MONTH_OF_YEAR("MonthOfYear", MONTHS, YEARS, ValueRange.of(1, 12), "month"),479/**480* The proleptic-month based, counting months sequentially from year 0.481* <p>482* This field is the sequential count of months where the first month483* in proleptic-year zero has the value zero.484* Later months have increasingly larger values.485* Earlier months have increasingly small values.486* There are no gaps or breaks in the sequence of months.487* Note that this uses the <i>local</i> time-line, ignoring offset and time-zone.488* <p>489* In the default ISO calendar system, June 2012 would have the value490* {@code (2012 * 12 + 6 - 1)}. This field is primarily for internal use.491* <p>492* Non-ISO calendar systems must implement this field as per the definition above.493* It is just a simple zero-based count of elapsed months from the start of proleptic-year 0.494* All calendar systems with a full proleptic-year definition will have a year zero.495* If the calendar system has a minimum year that excludes year zero, then one must496* be extrapolated in order for this method to be defined.497*/498PROLEPTIC_MONTH("ProlepticMonth", MONTHS, FOREVER, ValueRange.of(Year.MIN_VALUE * 12L, Year.MAX_VALUE * 12L + 11)),499/**500* The year within the era.501* <p>502* This represents the concept of the year within the era.503* This field is typically used with {@link #ERA}.504* <p>505* The standard mental model for a date is based on three concepts - year, month and day.506* These map onto the {@code YEAR}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} fields.507* Note that there is no reference to eras.508* The full model for a date requires four concepts - era, year, month and day. These map onto509* the {@code ERA}, {@code YEAR_OF_ERA}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} fields.510* Whether this field or {@code YEAR} is used depends on which mental model is being used.511* See {@link ChronoLocalDate} for more discussion on this topic.512* <p>513* In the default ISO calendar system, there are two eras defined, 'BCE' and 'CE'.514* The era 'CE' is the one currently in use and year-of-era runs from 1 to the maximum value.515* The era 'BCE' is the previous era, and the year-of-era runs backwards.516* <p>517* For example, subtracting a year each time yield the following:<br>518* - year-proleptic 2 = 'CE' year-of-era 2<br>519* - year-proleptic 1 = 'CE' year-of-era 1<br>520* - year-proleptic 0 = 'BCE' year-of-era 1<br>521* - year-proleptic -1 = 'BCE' year-of-era 2<br>522* <p>523* Note that the ISO-8601 standard does not actually define eras.524* Note also that the ISO eras do not align with the well-known AD/BC eras due to the525* change between the Julian and Gregorian calendar systems.526* <p>527* Non-ISO calendar systems should implement this field using the most recognized528* year-of-era value for users of the calendar system.529* Since most calendar systems have only two eras, the year-of-era numbering approach530* will typically be the same as that used by the ISO calendar system.531* The year-of-era value should typically always be positive, however this is not required.532*/533YEAR_OF_ERA("YearOfEra", YEARS, FOREVER, ValueRange.of(1, Year.MAX_VALUE, Year.MAX_VALUE + 1)),534/**535* The proleptic year, such as 2012.536* <p>537* This represents the concept of the year, counting sequentially and using negative numbers.538* The proleptic year is not interpreted in terms of the era.539* See {@link #YEAR_OF_ERA} for an example showing the mapping from proleptic year to year-of-era.540* <p>541* The standard mental model for a date is based on three concepts - year, month and day.542* These map onto the {@code YEAR}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} fields.543* Note that there is no reference to eras.544* The full model for a date requires four concepts - era, year, month and day. These map onto545* the {@code ERA}, {@code YEAR_OF_ERA}, {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH} fields.546* Whether this field or {@code YEAR_OF_ERA} is used depends on which mental model is being used.547* See {@link ChronoLocalDate} for more discussion on this topic.548* <p>549* Non-ISO calendar systems should implement this field as follows.550* If the calendar system has only two eras, before and after a fixed date, then the551* proleptic-year value must be the same as the year-of-era value for the later era,552* and increasingly negative for the earlier era.553* If the calendar system has more than two eras, then the proleptic-year value may be554* defined with any appropriate value, although defining it to be the same as ISO may be555* the best option.556*/557YEAR("Year", YEARS, FOREVER, ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE), "year"),558/**559* The era.560* <p>561* This represents the concept of the era, which is the largest division of the time-line.562* This field is typically used with {@link #YEAR_OF_ERA}.563* <p>564* In the default ISO calendar system, there are two eras defined, 'BCE' and 'CE'.565* The era 'CE' is the one currently in use and year-of-era runs from 1 to the maximum value.566* The era 'BCE' is the previous era, and the year-of-era runs backwards.567* See {@link #YEAR_OF_ERA} for a full example.568* <p>569* Non-ISO calendar systems should implement this field to define eras.570* The value of the era that was active on 1970-01-01 (ISO) must be assigned the value 1.571* Earlier eras must have sequentially smaller values.572* Later eras must have sequentially larger values,573*/574ERA("Era", ERAS, FOREVER, ValueRange.of(0, 1), "era"),575/**576* The instant epoch-seconds.577* <p>578* This represents the concept of the sequential count of seconds where579* 1970-01-01T00:00Z (ISO) is zero.580* This field may be used with {@link #NANO_OF_SECOND} to represent the fraction of the second.581* <p>582* An {@link Instant} represents an instantaneous point on the time-line.583* On their own, an instant has insufficient information to allow a local date-time to be obtained.584* Only when paired with an offset or time-zone can the local date or time be calculated.585* <p>586* This field is strictly defined to have the same meaning in all calendar systems.587* This is necessary to ensure interoperation between calendars.588*/589INSTANT_SECONDS("InstantSeconds", SECONDS, FOREVER, ValueRange.of(Long.MIN_VALUE, Long.MAX_VALUE)),590/**591* The offset from UTC/Greenwich.592* <p>593* This represents the concept of the offset in seconds of local time from UTC/Greenwich.594* <p>595* A {@link ZoneOffset} represents the period of time that local time differs from UTC/Greenwich.596* This is usually a fixed number of hours and minutes.597* It is equivalent to the {@link ZoneOffset#getTotalSeconds() total amount} of the offset in seconds.598* For example, during the winter Paris has an offset of {@code +01:00}, which is 3600 seconds.599* <p>600* This field is strictly defined to have the same meaning in all calendar systems.601* This is necessary to ensure interoperation between calendars.602*/603OFFSET_SECONDS("OffsetSeconds", SECONDS, FOREVER, ValueRange.of(-18 * 3600, 18 * 3600));604605private final String name;606private final TemporalUnit baseUnit;607private final TemporalUnit rangeUnit;608private final ValueRange range;609private final String displayNameKey;610611private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range) {612this.name = name;613this.baseUnit = baseUnit;614this.rangeUnit = rangeUnit;615this.range = range;616this.displayNameKey = null;617}618619private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit,620ValueRange range, String displayNameKey) {621this.name = name;622this.baseUnit = baseUnit;623this.rangeUnit = rangeUnit;624this.range = range;625this.displayNameKey = displayNameKey;626}627628@Override629public String getDisplayName(Locale locale) {630Objects.requireNonNull(locale, "locale");631if (displayNameKey == null) {632return name;633}634635LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()636.getLocaleResources(637CalendarDataUtility638.findRegionOverride(locale));639ResourceBundle rb = lr.getJavaTimeFormatData();640String key = "field." + displayNameKey;641return rb.containsKey(key) ? rb.getString(key) : name;642}643644@Override645public TemporalUnit getBaseUnit() {646return baseUnit;647}648649@Override650public TemporalUnit getRangeUnit() {651return rangeUnit;652}653654/**655* Gets the range of valid values for the field.656* <p>657* All fields can be expressed as a {@code long} integer.658* This method returns an object that describes the valid range for that value.659* <p>660* This method returns the range of the field in the ISO-8601 calendar system.661* This range may be incorrect for other calendar systems.662* Use {@link Chronology#range(ChronoField)} to access the correct range663* for a different calendar system.664* <p>665* Note that the result only describes the minimum and maximum valid values666* and it is important not to read too much into them. For example, there667* could be values within the range that are invalid for the field.668*669* @return the range of valid values for the field, not null670*/671@Override672public ValueRange range() {673return range;674}675676//-----------------------------------------------------------------------677/**678* Checks if this field represents a component of a date.679* <p>680* Fields from day-of-week to era are date-based.681*682* @return true if it is a component of a date683*/684@Override685public boolean isDateBased() {686return ordinal() >= DAY_OF_WEEK.ordinal() && ordinal() <= ERA.ordinal();687}688689/**690* Checks if this field represents a component of a time.691* <p>692* Fields from nano-of-second to am-pm-of-day are time-based.693*694* @return true if it is a component of a time695*/696@Override697public boolean isTimeBased() {698return ordinal() < DAY_OF_WEEK.ordinal();699}700701//-----------------------------------------------------------------------702/**703* Checks that the specified value is valid for this field.704* <p>705* This validates that the value is within the outer range of valid values706* returned by {@link #range()}.707* <p>708* This method checks against the range of the field in the ISO-8601 calendar system.709* This range may be incorrect for other calendar systems.710* Use {@link Chronology#range(ChronoField)} to access the correct range711* for a different calendar system.712*713* @param value the value to check714* @return the value that was passed in715*/716public long checkValidValue(long value) {717return range().checkValidValue(value, this);718}719720/**721* Checks that the specified value is valid and fits in an {@code int}.722* <p>723* This validates that the value is within the outer range of valid values724* returned by {@link #range()}.725* It also checks that all valid values are within the bounds of an {@code int}.726* <p>727* This method checks against the range of the field in the ISO-8601 calendar system.728* This range may be incorrect for other calendar systems.729* Use {@link Chronology#range(ChronoField)} to access the correct range730* for a different calendar system.731*732* @param value the value to check733* @return the value that was passed in734*/735public int checkValidIntValue(long value) {736return range().checkValidIntValue(value, this);737}738739//-----------------------------------------------------------------------740@Override741public boolean isSupportedBy(TemporalAccessor temporal) {742return temporal.isSupported(this);743}744745@Override746public ValueRange rangeRefinedBy(TemporalAccessor temporal) {747return temporal.range(this);748}749750@Override751public long getFrom(TemporalAccessor temporal) {752return temporal.getLong(this);753}754755@SuppressWarnings("unchecked")756@Override757public <R extends Temporal> R adjustInto(R temporal, long newValue) {758return (R) temporal.with(this, newValue);759}760761//-----------------------------------------------------------------------762@Override763public String toString() {764return name;765}766767}768769770