public enum ChronoField extends java.lang.Enum<ChronoField> implements TemporalField
This set of fields provide field-based access to manipulate a date, time or date-time.
The standard set of fields can be extended by implementing TemporalField
.
These fields are intended to be applicable in multiple calendar systems. For example, most non-ISO calendar systems define dates as a year, month and day, just with slightly different rules. The documentation of each field explains how it operates.
Enum Constant and Description |
---|
ALIGNED_DAY_OF_WEEK_IN_MONTH
The aligned day-of-week within a month.
|
ALIGNED_DAY_OF_WEEK_IN_YEAR
The aligned day-of-week within a year.
|
ALIGNED_WEEK_OF_MONTH
The aligned week within a month.
|
ALIGNED_WEEK_OF_YEAR
The aligned week within a year.
|
AMPM_OF_DAY
The am-pm-of-day.
|
CLOCK_HOUR_OF_AMPM
The clock-hour-of-am-pm.
|
CLOCK_HOUR_OF_DAY
The clock-hour-of-day.
|
DAY_OF_MONTH
The day-of-month.
|
DAY_OF_WEEK
The day-of-week, such as Tuesday.
|
DAY_OF_YEAR
The day-of-year.
|
EPOCH_DAY
The epoch-day, based on the Java epoch of 1970-01-01 (ISO).
|
EPOCH_MONTH
The epoch-month based on the Java epoch of 1970-01-01.
|
ERA
The era.
|
HOUR_OF_AMPM
The hour-of-am-pm.
|
HOUR_OF_DAY
The hour-of-day.
|
INSTANT_SECONDS
The instant epoch-seconds.
|
MICRO_OF_DAY
The micro-of-day.
|
MICRO_OF_SECOND
The micro-of-second.
|
MILLI_OF_DAY
The milli-of-day.
|
MILLI_OF_SECOND
The milli-of-second.
|
MINUTE_OF_DAY
The minute-of-day.
|
MINUTE_OF_HOUR
The minute-of-hour.
|
MONTH_OF_YEAR
The month-of-year, such as March.
|
NANO_OF_DAY
The nano-of-day.
|
NANO_OF_SECOND
The nano-of-second.
|
OFFSET_SECONDS
The offset from UTC/Greenwich.
|
SECOND_OF_DAY
The second-of-day.
|
SECOND_OF_MINUTE
The second-of-minute.
|
YEAR
The proleptic year, such as 2012.
|
YEAR_OF_ERA
The year within the era.
|
Modifier and Type | Method and Description |
---|---|
int |
checkValidIntValue(long value)
Checks that the specified value is valid and fits in an
int . |
long |
checkValidValue(long value)
Checks that the specified value is valid for this field.
|
long |
doGet(TemporalAccessor temporal)
Gets the value of this field from the specified temporal object.
|
boolean |
doIsSupported(TemporalAccessor temporal)
Checks if this field is supported by the temporal object.
|
ValueRange |
doRange(TemporalAccessor temporal)
Get the range of valid values for this field using the temporal object to
refine the result.
|
<R extends Temporal> |
doWith(R temporal,
long newValue)
Returns a copy of the specified temporal object with the value of this field set.
|
TemporalUnit |
getBaseUnit()
Gets the unit that the field is measured in.
|
java.lang.String |
getName()
Gets a descriptive name for the field.
|
TemporalUnit |
getRangeUnit()
Gets the range that the field is bound by.
|
boolean |
isDateField()
Checks if this field represents a component of a date.
|
boolean |
isTimeField()
Checks if this field represents a component of a time.
|
ValueRange |
range()
Gets the range of valid values for the field.
|
boolean |
resolve(DateTimeBuilder builder,
long value)
Resolves the date/time information in the builder
|
java.lang.String |
toString()
Returns the name of this enum constant, as contained in the
declaration.
|
static ChronoField |
valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name.
|
static ChronoField[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, valueOf
compare
public static final ChronoField NANO_OF_SECOND
This counts the nanosecond within the second, from 0 to 999,999,999. This field has the same meaning for all calendar systems.
This field is used to represent the nano-of-second handling any fraction of the second.
Implementations of TemporalAccessor
should provide a value for this field if
they can return a value for SECOND_OF_MINUTE
, SECOND_OF_DAY
or
INSTANT_SECONDS
filling unknown precision with zero.
When this field is used for setting a value, it should set as much precision as the
object stores, using integer division to remove excess precision.
For example, if the TemporalAccessor
stores time to millisecond precision,
then the nano-of-second must be divided by 1,000,000 before replacing the milli-of-second.
public static final ChronoField NANO_OF_DAY
This counts the nanosecond within the day, from 0 to (24 * 60 * 60 * 1,000,000,000) - 1. This field has the same meaning for all calendar systems.
This field is used to represent the nano-of-day handling any fraction of the second.
Implementations of TemporalAccessor
should provide a value for this field if
they can return a value for SECOND_OF_DAY
filling unknown precision with zero.
public static final ChronoField MICRO_OF_SECOND
This counts the microsecond within the second, from 0 to 999,999. This field has the same meaning for all calendar systems.
This field is used to represent the micro-of-second handling any fraction of the second.
Implementations of TemporalAccessor
should provide a value for this field if
they can return a value for SECOND_OF_MINUTE
, SECOND_OF_DAY
or
INSTANT_SECONDS
filling unknown precision with zero.
When this field is used for setting a value, it should behave in the same way as
setting NANO_OF_SECOND
with the value multiplied by 1,000.
public static final ChronoField MICRO_OF_DAY
This counts the microsecond within the day, from 0 to (24 * 60 * 60 * 1,000,000) - 1. This field has the same meaning for all calendar systems.
This field is used to represent the micro-of-day handling any fraction of the second.
Implementations of TemporalAccessor
should provide a value for this field if
they can return a value for SECOND_OF_DAY
filling unknown precision with zero.
When this field is used for setting a value, it should behave in the same way as
setting NANO_OF_DAY
with the value multiplied by 1,000.
public static final ChronoField MILLI_OF_SECOND
This counts the millisecond within the second, from 0 to 999. This field has the same meaning for all calendar systems.
This field is used to represent the milli-of-second handling any fraction of the second.
Implementations of TemporalAccessor
should provide a value for this field if
they can return a value for SECOND_OF_MINUTE
, SECOND_OF_DAY
or
INSTANT_SECONDS
filling unknown precision with zero.
When this field is used for setting a value, it should behave in the same way as
setting NANO_OF_SECOND
with the value multiplied by 1,000,000.
public static final ChronoField MILLI_OF_DAY
This counts the millisecond within the day, from 0 to (24 * 60 * 60 * 1,000) - 1. This field has the same meaning for all calendar systems.
This field is used to represent the milli-of-day handling any fraction of the second.
Implementations of TemporalAccessor
should provide a value for this field if
they can return a value for SECOND_OF_DAY
filling unknown precision with zero.
When this field is used for setting a value, it should behave in the same way as
setting NANO_OF_DAY
with the value multiplied by 1,000,000.
public static final ChronoField SECOND_OF_MINUTE
This counts the second within the minute, from 0 to 59. This field has the same meaning for all calendar systems.
public static final ChronoField SECOND_OF_DAY
This counts the second within the day, from 0 to (24 * 60 * 60) - 1. This field has the same meaning for all calendar systems.
public static final ChronoField MINUTE_OF_HOUR
This counts the minute within the hour, from 0 to 59. This field has the same meaning for all calendar systems.
public static final ChronoField MINUTE_OF_DAY
This counts the minute within the day, from 0 to (24 * 60) - 1. This field has the same meaning for all calendar systems.
public static final ChronoField HOUR_OF_AMPM
This counts the hour within the AM/PM, from 0 to 11. This is the hour that would be observed on a standard 12-hour digital clock. This field has the same meaning for all calendar systems.
public static final ChronoField CLOCK_HOUR_OF_AMPM
This counts the hour within the AM/PM, from 1 to 12. This is the hour that would be observed on a standard 12-hour analog wall clock. This field has the same meaning for all calendar systems.
public static final ChronoField HOUR_OF_DAY
This counts the hour within the day, from 0 to 23. This is the hour that would be observed on a standard 24-hour digital clock. This field has the same meaning for all calendar systems.
public static final ChronoField CLOCK_HOUR_OF_DAY
This counts the hour within the AM/PM, from 1 to 24. This is the hour that would be observed on a 24-hour analog wall clock. This field has the same meaning for all calendar systems.
public static final ChronoField AMPM_OF_DAY
This counts the AM/PM within the day, from 0 (AM) to 1 (PM). This field has the same meaning for all calendar systems.
public static final ChronoField DAY_OF_WEEK
This represents the standard concept of the day of the week.
In the default ISO calendar system, this has values from Monday (1) to Sunday (7).
The DayOfWeek
class can be used to interpret the result.
Most non-ISO calendar systems also define a seven day week that aligns with ISO.
Those calendar systems must also use the same numbering system, from Monday (1) to
Sunday (7), which allows DayOfWeek
to be used.
Calendar systems that do not have a standard seven day week should implement this field if they have a similar concept of named or numbered days within a period similar to a week. It is recommended that the numbering starts from 1.
public static final ChronoField ALIGNED_DAY_OF_WEEK_IN_MONTH
This represents concept of the count of days within the period of a week
where the weeks are aligned to the start of the month.
This field is typically used with ALIGNED_WEEK_OF_MONTH
.
For example, in a calendar systems with a seven day week, the first aligned-week-of-month starts on day-of-month 1, the second aligned-week starts on day-of-month 8, and so on. Within each of these aligned-weeks, the days are numbered from 1 to 7 and returned as the value of this field. As such, day-of-month 1 to 7 will have aligned-day-of-week values from 1 to 7. And day-of-month 8 to 14 will repeat this with aligned-day-of-week values from 1 to 7.
Calendar systems that do not have a seven day week should typically implement this field in the same way, but using the alternate week length.
public static final ChronoField ALIGNED_DAY_OF_WEEK_IN_YEAR
This represents concept of the count of days within the period of a week
where the weeks are aligned to the start of the year.
This field is typically used with ALIGNED_WEEK_OF_YEAR
.
For example, in a calendar systems with a seven day week, the first aligned-week-of-year starts on day-of-year 1, the second aligned-week starts on day-of-year 8, and so on. Within each of these aligned-weeks, the days are numbered from 1 to 7 and returned as the value of this field. As such, day-of-year 1 to 7 will have aligned-day-of-week values from 1 to 7. And day-of-year 8 to 14 will repeat this with aligned-day-of-week values from 1 to 7.
Calendar systems that do not have a seven day week should typically implement this field in the same way, but using the alternate week length.
public static final ChronoField DAY_OF_MONTH
This represents the concept of the day within the month. In the default ISO calendar system, this has values from 1 to 31 in most months. April, June, September, November have days from 1 to 30, while February has days from 1 to 28, or 29 in a leap year.
Non-ISO calendar systems should implement this field using the most recognized day-of-month values for users of the calendar system. Normally, this is a count of days from 1 to the length of the month.
public static final ChronoField DAY_OF_YEAR
This represents the concept of the day within the year. In the default ISO calendar system, this has values from 1 to 365 in standard years and 1 to 366 in leap years.
Non-ISO calendar systems should implement this field using the most recognized day-of-year values for users of the calendar system. Normally, this is a count of days from 1 to the length of the year.
public static final ChronoField EPOCH_DAY
This field is the sequential count of days where 1970-01-01 (ISO) is zero. Note that this uses the local time-line, ignoring offset and time-zone.
This field is strictly defined to have the same meaning in all calendar systems. This is necessary to ensure interoperation between calendars.
public static final ChronoField ALIGNED_WEEK_OF_MONTH
This represents concept of the count of weeks within the period of a month
where the weeks are aligned to the start of the month.
This field is typically used with ALIGNED_DAY_OF_WEEK_IN_MONTH
.
For example, in a calendar systems with a seven day week, the first aligned-week-of-month starts on day-of-month 1, the second aligned-week starts on day-of-month 8, and so on. Thus, day-of-month values 1 to 7 are in aligned-week 1, while day-of-month values 8 to 14 are in aligned-week 2, and so on.
Calendar systems that do not have a seven day week should typically implement this field in the same way, but using the alternate week length.
public static final ChronoField ALIGNED_WEEK_OF_YEAR
This represents concept of the count of weeks within the period of a year
where the weeks are aligned to the start of the year.
This field is typically used with ALIGNED_DAY_OF_WEEK_IN_YEAR
.
For example, in a calendar systems with a seven day week, the first aligned-week-of-year starts on day-of-year 1, the second aligned-week starts on day-of-year 8, and so on. Thus, day-of-year values 1 to 7 are in aligned-week 1, while day-of-year values 8 to 14 are in aligned-week 2, and so on.
Calendar systems that do not have a seven day week should typically implement this field in the same way, but using the alternate week length.
public static final ChronoField MONTH_OF_YEAR
This represents the concept of the month within the year. In the default ISO calendar system, this has values from January (1) to December (12).
Non-ISO calendar systems should implement this field using the most recognized month-of-year values for users of the calendar system. Normally, this is a count of months starting from 1.
public static final ChronoField EPOCH_MONTH
This field is the sequential count of months where January 1970 (ISO) is zero. Note that this uses the local time-line, ignoring offset and time-zone.
Non-ISO calendar systems should also implement this field to represent a sequential count of months. It is recommended to define zero as the month of 1970-01-01 (ISO).
public static final ChronoField YEAR_OF_ERA
This represents the concept of the year within the era.
This field is typically used with ERA
.
The standard mental model for a date is based on three concepts - year, month and day.
These map onto the YEAR
, MONTH_OF_YEAR
and DAY_OF_MONTH
fields.
Note that there is no reference to eras.
The full model for a date requires four concepts - era, year, month and day. These map onto
the ERA
, YEAR_OF_ERA
, MONTH_OF_YEAR
and DAY_OF_MONTH
fields.
Whether this field or YEAR
is used depends on which mental model is being used.
See ChronoLocalDate
for more discussion on this topic.
In the default ISO calendar system, there are two eras defined, 'BCE' and 'CE'. The era 'CE' is the one currently in use and year-of-era runs from 1 to the maximum value. The era 'BCE' is the previous era, and the year-of-era runs backwards.
For example, subtracting a year each time yield the following:
- year-proleptic 2 = 'CE' year-of-era 2
- year-proleptic 1 = 'CE' year-of-era 1
- year-proleptic 0 = 'BCE' year-of-era 1
- year-proleptic -1 = 'BCE' year-of-era 2
Note that the ISO-8601 standard does not actually define eras. Note also that the ISO eras do not align with the well-known AD/BC eras due to the change between the Julian and Gregorian calendar systems.
Non-ISO calendar systems should implement this field using the most recognized year-of-era value for users of the calendar system. Since most calendar systems have only two eras, the year-of-era numbering approach will typically be the same as that used by the ISO calendar system. The year-of-era value should typically always be positive, however this is not required.
public static final ChronoField YEAR
This represents the concept of the year, counting sequentially and using negative numbers.
The proleptic year is not interpreted in terms of the era.
See YEAR_OF_ERA
for an example showing the mapping from proleptic year to year-of-era.
The standard mental model for a date is based on three concepts - year, month and day.
These map onto the YEAR
, MONTH_OF_YEAR
and DAY_OF_MONTH
fields.
Note that there is no reference to eras.
The full model for a date requires four concepts - era, year, month and day. These map onto
the ERA
, YEAR_OF_ERA
, MONTH_OF_YEAR
and DAY_OF_MONTH
fields.
Whether this field or YEAR_OF_ERA
is used depends on which mental model is being used.
See ChronoLocalDate
for more discussion on this topic.
Non-ISO calendar systems should implement this field as follows. If the calendar system has only two eras, before and after a fixed date, then the proleptic-year value must be the same as the year-of-era value for the later era, and increasingly negative for the earlier era. If the calendar system has more than two eras, then the proleptic-year value may be defined with any appropriate value, although defining it to be the same as ISO may be the best option.
public static final ChronoField ERA
This represents the concept of the era, which is the largest division of the time-line.
This field is typically used with YEAR_OF_ERA
.
In the default ISO calendar system, there are two eras defined, 'BCE' and 'CE'.
The era 'CE' is the one currently in use and year-of-era runs from 1 to the maximum value.
The era 'BCE' is the previous era, and the year-of-era runs backwards.
See YEAR_OF_ERA
for a full example.
Non-ISO calendar systems should implement this field to define eras. The value of the era that was active on 1970-01-01 (ISO) must be assigned the value 1. Earlier eras must have sequentially smaller values. Later eras must have sequentially larger values,
public static final ChronoField INSTANT_SECONDS
This represents the concept of the sequential count of seconds where
1970-01-01T00:00Z (ISO) is zero.
This field may be used with NANO_OF_DAY
to represent the fraction of the day.
An Instant
represents an instantaneous point on the time-line.
On their own they have no elements which allow a local date-time to be obtained.
Only when paired with an offset or time-zone can the local date or time be found.
This field allows the seconds part of the instant to be queried.
This field is strictly defined to have the same meaning in all calendar systems. This is necessary to ensure interoperation between calendars.
public static final ChronoField OFFSET_SECONDS
This represents the concept of the offset in seconds of local time from UTC/Greenwich.
A ZoneOffset
represents the period of time that local time differs from UTC/Greenwich.
This is usually a fixed number of hours and minutes.
It is equivalent to the total amount
of the offset in seconds.
For example, during the winter Paris has an offset of +01:00
, which is 3600 seconds.
This field is strictly defined to have the same meaning in all calendar systems. This is necessary to ensure interoperation between calendars.
public static ChronoField[] values()
for (ChronoField c : ChronoField.values()) System.out.println(c);
public static ChronoField valueOf(java.lang.String name)
name
- the name of the enum constant to be returned.java.lang.IllegalArgumentException
- if this enum type has no constant
with the specified namejava.lang.NullPointerException
- if the argument is nullpublic java.lang.String getName()
TemporalField
The should be of the format 'BaseOfRange', such as 'MonthOfYear',
unless the field has a range of FOREVER
, when only
the base unit is mentioned, such as 'Year' or 'Era'.
getName
in interface TemporalField
public TemporalUnit getBaseUnit()
TemporalField
The unit of the field is the period that varies within the range.
For example, in the field 'MonthOfYear', the unit is 'Months'.
See also TemporalField.getRangeUnit()
.
getBaseUnit
in interface TemporalField
public TemporalUnit getRangeUnit()
TemporalField
The range of the field is the period that the field varies within.
For example, in the field 'MonthOfYear', the range is 'Years'.
See also TemporalField.getBaseUnit()
.
The range is never null. For example, the 'Year' field is shorthand for 'YearOfForever'. It therefore has a unit of 'Years' and a range of 'Forever'.
getRangeUnit
in interface TemporalField
public ValueRange range()
TemporalField
All fields can be expressed as a long
integer.
This method returns an object that describes the valid range for that value.
This method is generally only applicable to the ISO-8601 calendar system.
Note that the result only describes the minimum and maximum valid values and it is important not to read too much into them. For example, there could be values within the range that are invalid for the field.
range
in interface TemporalField
public boolean isDateField()
public boolean isTimeField()
public long checkValidValue(long value)
This validates that the value is within the outer range of valid values
returned by range()
.
value
- the value to checkpublic int checkValidIntValue(long value)
int
.
This validates that the value is within the outer range of valid values
returned by range()
.
It also checks that all valid values are within the bounds of an int
.
value
- the value to checkpublic boolean doIsSupported(TemporalAccessor temporal)
TemporalField
This determines whether the temporal accessor supports this field. If this returns false, the the temporal cannot be queried for this field.
There are two equivalent ways of using this method.
The first is to invoke this method directly.
The second is to use TemporalAccessor.isSupported(TemporalField)
:
// these two lines are equivalent, but the second approach is recommended temporal = thisField.doIsSupported(temporal); temporal = temporal.isSupported(thisField);It is recommended to use the second approach,
isSupported(TemporalField)
,
as it is a lot clearer to read in code.
Implementations should determine whether they are supported using the fields
available in ChronoField
.
doIsSupported
in interface TemporalField
temporal
- the temporal object to query, not nullpublic ValueRange doRange(TemporalAccessor temporal)
TemporalField
This uses the temporal object to find the range of valid values for the field.
This is similar to TemporalField.range()
, however this method refines the result
using the temporal. For example, if the field is DAY_OF_MONTH
the
range
method is not accurate as there are four possible month lengths,
28, 29, 30 and 31 days. Using this method with a date allows the range to be
accurate, returning just one of those four options.
There are two equivalent ways of using this method.
The first is to invoke this method directly.
The second is to use TemporalAccessor.range(TemporalField)
:
// these two lines are equivalent, but the second approach is recommended temporal = thisField.doRange(temporal); temporal = temporal.range(thisField);It is recommended to use the second approach,
range(TemporalField)
,
as it is a lot clearer to read in code.
Implementations should perform any queries or calculations using the fields
available in ChronoField
.
If the field is not supported a DateTimeException
must be thrown.
doRange
in interface TemporalField
temporal
- the temporal object used to refine the result, not nullpublic long doGet(TemporalAccessor temporal)
TemporalField
This queries the temporal object for the value of this field.
There are two equivalent ways of using this method.
The first is to invoke this method directly.
The second is to use TemporalAccessor.getLong(TemporalField)
(or TemporalAccessor.get(TemporalField)
):
// these two lines are equivalent, but the second approach is recommended temporal = thisField.doGet(temporal); temporal = temporal.getLong(thisField);It is recommended to use the second approach,
getLong(TemporalField)
,
as it is a lot clearer to read in code.
Implementations should perform any queries or calculations using the fields
available in ChronoField
.
If the field is not supported a DateTimeException
must be thrown.
doGet
in interface TemporalField
temporal
- the temporal object to query, not nullpublic <R extends Temporal> R doWith(R temporal, long newValue)
TemporalField
This returns a new temporal object based on the specified one with the value for
this field changed. For example, on a LocalDate
, this could be used to
set the year, month or day-of-month.
The returned object has the same observable type as the specified object.
In some cases, changing a field is not fully defined. For example, if the target object is a date representing the 31st January, then changing the month to February would be unclear. In cases like this, the implementation is responsible for resolving the result. Typically it will choose the previous valid date, which would be the last valid day of February in this example.
There are two equivalent ways of using this method.
The first is to invoke this method directly.
The second is to use Temporal.with(TemporalField, long)
:
// these two lines are equivalent, but the second approach is recommended temporal = thisField.doWith(temporal); temporal = temporal.with(thisField);It is recommended to use the second approach,
with(TemporalField)
,
as it is a lot clearer to read in code.
Implementations should perform any queries or calculations using the fields
available in ChronoField
.
If the field is not supported a DateTimeException
must be thrown.
Implementations must not alter the specified temporal object. Instead, an adjusted copy of the original must be returned. This provides equivalent, safe behavior for immutable and mutable implementations.
doWith
in interface TemporalField
R
- the type of the Temporal objecttemporal
- the temporal object to adjust, not nullnewValue
- the new value of the fieldpublic boolean resolve(DateTimeBuilder builder, long value)
TemporalField
This method is invoked during the resolve of the builder.
Implementations should combine the associated field with others to form
objects like LocalDate
, LocalTime
and LocalDateTime
resolve
in interface TemporalField
builder
- the builder to resolve, not nullvalue
- the value of the associated fieldpublic java.lang.String toString()
java.lang.Enum
toString
in class java.lang.Enum<ChronoField>