Interface TemporalAccessor
-
- All Known Implementing Classes:
ChronoLocalDate
,ChronoLocalDateTime
,ChronoZonedDateTime
,DayOfWeek
,DefaultInterfaceEra
,DefaultInterfaceTemporal
,DefaultInterfaceTemporalAccessor
,HijrahDate
,HijrahEra
,Instant
,IsoEra
,JapaneseDate
,JapaneseEra
,LocalDate
,LocalDateTime
,LocalTime
,MinguoDate
,MinguoEra
,Month
,MonthDay
,OffsetDateTime
,OffsetTime
,ThaiBuddhistDate
,ThaiBuddhistEra
,Year
,YearMonth
,ZonedDateTime
,ZoneOffset
public interface TemporalAccessor
Framework-level interface defining read-only access to a temporal object, such as a date, time, offset or some combination of these.This is the base interface type for date, time and offset objects. It is implemented by those classes that can provide information as
fields
orqueries
.Most date and time information can be represented as a number. These are modeled using
TemporalField
with the number held using along
to handle large values. Year, month and day-of-month are simple examples of fields, but they also include instant and offsets. SeeChronoField
for the standard set of fields.Two pieces of date/time information cannot be represented by numbers, the
chronology
and thetime-zone
. These can be accessed viaqueries
using the static methods defined onTemporalQueries
.A sub-interface,
Temporal
, extends this definition to one that also supports adjustment and manipulation on more complete temporal objects.This interface is a framework-level interface that should not be widely used in application code. Instead, applications should create and pass around instances of concrete types, such as
LocalDate
. There are many reasons for this, part of which is that implementations of this interface may be in calendar systems other than ISO. SeeChronoLocalDate
for a fuller discussion of the issues.Specification for implementors
This interface places no restrictions on the mutability of implementations, however immutability is strongly recommended.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description int
get(TemporalField field)
Gets the value of the specified field as anint
.long
getLong(TemporalField field)
Gets the value of the specified field as along
.boolean
isSupported(TemporalField field)
Checks if the specified field is supported.<R> R
query(TemporalQuery<R> query)
Queries this date-time.ValueRange
range(TemporalField field)
Gets the range of valid values for the specified field.
-
-
-
Method Detail
-
isSupported
boolean isSupported(TemporalField field)
Checks if the specified field is supported.This checks if the date-time can be queried for the specified field. If false, then calling the
range
andget
methods will throw an exception.Specification for implementors
Implementations must check and handle all fields defined inChronoField
. If the field is supported, then true is returned, otherwise falseIf the field is not a
ChronoField
, then the result of this method is obtained by invokingTemporalField.isSupportedBy(TemporalAccessor)
passingthis
as the argument.Implementations must not alter this object.
- Parameters:
field
- the field to check, null returns false- Returns:
- true if this date-time can be queried for the field, false if not
-
range
ValueRange range(TemporalField field)
Gets the range of valid values for the specified field.All fields can be expressed as a
long
integer. This method returns an object that describes the valid range for that value. The value of this temporal object is used to enhance the accuracy of the returned range. If the date-time cannot return the range, because the field is unsupported or for some other reason, an exception will be thrown.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.
Specification for implementors
Implementations must check and handle all fields defined inChronoField
. If the field is supported, then the range of the field must be returned. If unsupported, then aDateTimeException
must be thrown.If the field is not a
ChronoField
, then the result of this method is obtained by invokingTemporalField.rangeRefinedBy(TemporalAccessorl)
passingthis
as the argument.Implementations must not alter either this object.
- Parameters:
field
- the field to query the range for, not null- Returns:
- the range of valid values for the field, not null
- Throws:
DateTimeException
- if the range for the field cannot be obtained
-
get
int get(TemporalField field)
Gets the value of the specified field as anint
.This queries the date-time for the value for the specified field. The returned value will always be within the valid range of values for the field. If the date-time cannot return the value, because the field is unsupported or for some other reason, an exception will be thrown.
Specification for implementors
Implementations must check and handle all fields defined inChronoField
. If the field is supported and has anint
range, then the value of the field must be returned. If unsupported, then aDateTimeException
must be thrown.If the field is not a
ChronoField
, then the result of this method is obtained by invokingTemporalField.getFrom(TemporalAccessor)
passingthis
as the argument.Implementations must not alter either this object.
- Parameters:
field
- the field to get, not null- Returns:
- the value for the field, within the valid range of values
- Throws:
DateTimeException
- if a value for the field cannot be obtainedDateTimeException
- if the range of valid values for the field exceeds anint
DateTimeException
- if the value is outside the range of valid values for the fieldArithmeticException
- if numeric overflow occurs
-
getLong
long getLong(TemporalField field)
Gets the value of the specified field as along
.This queries the date-time for the value for the specified field. The returned value may be outside the valid range of values for the field. If the date-time cannot return the value, because the field is unsupported or for some other reason, an exception will be thrown.
Specification for implementors
Implementations must check and handle all fields defined inChronoField
. If the field is supported, then the value of the field must be returned. If unsupported, then aDateTimeException
must be thrown.If the field is not a
ChronoField
, then the result of this method is obtained by invokingTemporalField.getFrom(TemporalAccessor)
passingthis
as the argument.Implementations must not alter either this object.
- Parameters:
field
- the field to get, not null- Returns:
- the value for the field
- Throws:
DateTimeException
- if a value for the field cannot be obtainedArithmeticException
- if numeric overflow occurs
-
query
<R> R query(TemporalQuery<R> query)
Queries this date-time.This queries this date-time using the specified query strategy object.
Queries are a key tool for extracting information from date-times. They exists to externalize the process of querying, permitting different approaches, as per the strategy design pattern. Examples might be a query that checks if the date is the day before February 29th in a leap year, or calculates the number of days to your next birthday.
The most common query implementations are method references, such as
LocalDate::from
andZoneId::from
. Further implementations are onTemporalQueries
. Queries may also be defined by applications.Specification for implementors
Implementations of this method must behave as follows:public <R> R query(TemporalQuery<R> type) { // only include an if statement if the implementation can return it if (query == TemporalQueries.zoneId()) return // the ZoneId if (query == TemporalQueries.chronology()) return // the Chrono if (query == TemporalQueries.precision()) return // the precision // call default method return super.query(query); }
- Type Parameters:
R
- the type of the result- Parameters:
query
- the query to invoke, not null- Returns:
- the query result, null may be returned (defined by the query)
- Throws:
DateTimeException
- if unable to queryArithmeticException
- if numeric overflow occurs
-
-