Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/time/temporal/ChronoField.java
38918 views
/*1* Copyright (c) 2012, 2013, 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.LocaleProviderAdapter;82import sun.util.locale.provider.LocaleResources;8384/**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* @implSpec96* This is a final, immutable and thread-safe enum.97*98* @since 1.899*/100public enum ChronoField implements TemporalField {101102/**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 if110* they can return a value for {@link #SECOND_OF_MINUTE}, {@link #SECOND_OF_DAY} or111* {@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 the114* 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*/122NANO_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 if131* 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*/138NANO_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 if147* they can return a value for {@link #SECOND_OF_MINUTE}, {@link #SECOND_OF_DAY} or148* {@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 as151* 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 produce156* {@code NANO_OF_SECOND}.157*/158MICRO_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 if167* 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 as170* 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*/177MICRO_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 if186* they can return a value for {@link #SECOND_OF_MINUTE}, {@link #SECOND_OF_DAY} or187* {@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 as190* 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 produce195* {@code NANO_OF_SECOND}.196*/197MILLI_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 if206* 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 as209* 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*/216MILLI_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*/226SECOND_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*/238SECOND_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*/248MINUTE_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*/259MINUTE_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 with270* {@code AMPM_OF_DAY} to form {@code HOUR_OF_DAY} by multiplying271* the {AMPM_OF_DAY} value by 12.272*/273HOUR_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 from283* 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*/287CLOCK_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} and298* {@code NANO_OF_SECOND} to produce a {@code LocalTime}.299* In lenient mode, any excess days are added to the parsed date, or300* made available via {@link java.time.format.DateTimeFormatter#parsedExcessDays()}.301*/302HOUR_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 from312* 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*/316CLOCK_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 with326* {@code HOUR_OF_AMPM} to form {@code HOUR_OF_DAY} by multiplying327* the {AMPM_OF_DAY} value by 12.328*/329AMPM_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) to339* 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 field342* if they have a similar concept of named or numbered days within a period similar343* to a week. It is recommended that the numbering starts from 1.344*/345DAY_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 week350* 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-month354* 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 returned356* 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 this361* field in the same way, but using the alternate week length.362*/363ALIGNED_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 week368* 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-year372* 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 returned374* 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 this379* field in the same way, but using the alternate week length.380*/381ALIGNED_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 days388* from 1 to 28, or 29 in a leap year.389* <p>390* Non-ISO calendar systems should implement this field using the most recognized391* 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*/394DAY_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 standard400* years and 1 to 366 in leap years.401* <p>402* Non-ISO calendar systems should implement this field using the most recognized403* 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 changes407* at a different point to the natural reset in the month numbering. An example408* of this is the Japanese calendar system where a change of era, which resets409* the year number to 1, can happen on any date. The era and year reset also cause410* the day-of-year to be reset to 1, but not the month-of-year or day-of-month.411*/412DAY_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*/422EPOCH_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 month427* 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-month431* 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 values433* 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 this436* field in the same way, but using the alternate week length.437*/438ALIGNED_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 year443* 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-year447* 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 values449* 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 this452* field in the same way, but using the alternate week length.453*/454ALIGNED_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 recognized462* month-of-year values for users of the calendar system.463* Normally, this is a count of months starting from 1.464*/465MONTH_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 month470* 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 value477* {@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 must483* be extrapolated in order for this method to be defined.484*/485PROLEPTIC_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 onto496* 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 the512* change between the Julian and Gregorian calendar systems.513* <p>514* Non-ISO calendar systems should implement this field using the most recognized515* year-of-era value for users of the calendar system.516* Since most calendar systems have only two eras, the year-of-era numbering approach517* 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*/520YEAR_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 onto532* 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 the538* 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 be541* defined with any appropriate value, although defining it to be the same as ISO may be542* the best option.543*/544YEAR("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*/561ERA("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 where566* 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*/576INSTANT_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*/590OFFSET_SECONDS("OffsetSeconds", SECONDS, FOREVER, ValueRange.of(-18 * 3600, 18 * 3600));591592private final String name;593private final TemporalUnit baseUnit;594private final TemporalUnit rangeUnit;595private final ValueRange range;596private final String displayNameKey;597598private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, ValueRange range) {599this.name = name;600this.baseUnit = baseUnit;601this.rangeUnit = rangeUnit;602this.range = range;603this.displayNameKey = null;604}605606private ChronoField(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit,607ValueRange range, String displayNameKey) {608this.name = name;609this.baseUnit = baseUnit;610this.rangeUnit = rangeUnit;611this.range = range;612this.displayNameKey = displayNameKey;613}614615@Override616public String getDisplayName(Locale locale) {617Objects.requireNonNull(locale, "locale");618if (displayNameKey == null) {619return name;620}621622LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased()623.getLocaleResources(locale);624ResourceBundle rb = lr.getJavaTimeFormatData();625String key = "field." + displayNameKey;626return rb.containsKey(key) ? rb.getString(key) : name;627}628629@Override630public TemporalUnit getBaseUnit() {631return baseUnit;632}633634@Override635public TemporalUnit getRangeUnit() {636return rangeUnit;637}638639/**640* Gets the range of valid values for the field.641* <p>642* All fields can be expressed as a {@code long} integer.643* This method returns an object that describes the valid range for that value.644* <p>645* This method returns the range of the field in the ISO-8601 calendar system.646* This range may be incorrect for other calendar systems.647* Use {@link Chronology#range(ChronoField)} to access the correct range648* for a different calendar system.649* <p>650* Note that the result only describes the minimum and maximum valid values651* and it is important not to read too much into them. For example, there652* could be values within the range that are invalid for the field.653*654* @return the range of valid values for the field, not null655*/656@Override657public ValueRange range() {658return range;659}660661//-----------------------------------------------------------------------662/**663* Checks if this field represents a component of a date.664* <p>665* Fields from day-of-week to era are date-based.666*667* @return true if it is a component of a date668*/669@Override670public boolean isDateBased() {671return ordinal() >= DAY_OF_WEEK.ordinal() && ordinal() <= ERA.ordinal();672}673674/**675* Checks if this field represents a component of a time.676* <p>677* Fields from nano-of-second to am-pm-of-day are time-based.678*679* @return true if it is a component of a time680*/681@Override682public boolean isTimeBased() {683return ordinal() < DAY_OF_WEEK.ordinal();684}685686//-----------------------------------------------------------------------687/**688* Checks that the specified value is valid for this field.689* <p>690* This validates that the value is within the outer range of valid values691* returned by {@link #range()}.692* <p>693* This method checks against the range of the field in the ISO-8601 calendar system.694* This range may be incorrect for other calendar systems.695* Use {@link Chronology#range(ChronoField)} to access the correct range696* for a different calendar system.697*698* @param value the value to check699* @return the value that was passed in700*/701public long checkValidValue(long value) {702return range().checkValidValue(value, this);703}704705/**706* Checks that the specified value is valid and fits in an {@code int}.707* <p>708* This validates that the value is within the outer range of valid values709* returned by {@link #range()}.710* It also checks that all valid values are within the bounds of an {@code int}.711* <p>712* This method checks against the range of the field in the ISO-8601 calendar system.713* This range may be incorrect for other calendar systems.714* Use {@link Chronology#range(ChronoField)} to access the correct range715* for a different calendar system.716*717* @param value the value to check718* @return the value that was passed in719*/720public int checkValidIntValue(long value) {721return range().checkValidIntValue(value, this);722}723724//-----------------------------------------------------------------------725@Override726public boolean isSupportedBy(TemporalAccessor temporal) {727return temporal.isSupported(this);728}729730@Override731public ValueRange rangeRefinedBy(TemporalAccessor temporal) {732return temporal.range(this);733}734735@Override736public long getFrom(TemporalAccessor temporal) {737return temporal.getLong(this);738}739740@SuppressWarnings("unchecked")741@Override742public <R extends Temporal> R adjustInto(R temporal, long newValue) {743return (R) temporal.with(this, newValue);744}745746//-----------------------------------------------------------------------747@Override748public String toString() {749return name;750}751752}753754755