public interface TemporalField extends java.util.Comparator<TemporalAccessor>
Date and time is expressed using fields which partition the time-line into something meaningful for humans. Implementations of this interface represent those fields.
The most commonly used units are defined in ChronoField
.
Further fields are supplied in ISOFields
, WeekFields
and JulianFields
.
Fields can also be written by application code by implementing this interface.
The field works using double dispatch. Client code calls methods on a date-time like
LocalDateTime
which check if the field is a ChronoField
.
If it is, then the date-time must handle it.
Otherwise, the method call is re-dispatched to the matching method in this interface.
Modifier and Type | Method and Description |
---|---|
default int |
compare(TemporalAccessor temporal1,
TemporalAccessor temporal2)
Compares the value of this field in two temporal objects.
|
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.
|
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 getName()
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'.
TemporalUnit getBaseUnit()
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 getRangeUnit()
.
TemporalUnit getRangeUnit()
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 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'.
default int compare(TemporalAccessor temporal1, TemporalAccessor temporal2)
All fields implement Comparator
on TemporalAccessor
.
This allows a list of date-times to be compared using the value of a field.
For example, you could sort a list of arbitrary temporal objects by the value of
the month-of-year field - Collections.sort(list, MONTH_OF_YEAR)
The default implementation must behave equivalent to this code:
return Long.compare(temporal1.getLong(this), temporal2.getLong(this));
compare
in interface java.util.Comparator<TemporalAccessor>
temporal1
- the first temporal object to compare, not nulltemporal2
- the second temporal object to compare, not nullDateTimeException
- if unable to obtain the value for this fieldValueRange range()
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.
boolean doIsSupported(TemporalAccessor temporal)
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
.
temporal
- the temporal object to query, not nullValueRange doRange(TemporalAccessor temporal)
This uses the temporal object to find the range of valid values for the field.
This is similar to 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.
temporal
- the temporal object used to refine the result, not nullDateTimeException
- if the range for the field cannot be obtainedlong doGet(TemporalAccessor temporal)
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.
temporal
- the temporal object to query, not nullDateTimeException
- if a value for the field cannot be obtained<R extends Temporal> R doWith(R temporal, long newValue)
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.
R
- the type of the Temporal objecttemporal
- the temporal object to adjust, not nullnewValue
- the new value of the fieldDateTimeException
- if the field cannot be setboolean resolve(DateTimeBuilder builder, long value)
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
builder
- the builder to resolve, not nullvalue
- the value of the associated fieldDateTimeException
- if unable to resolve