Class DateTimeBuilder
- java.lang.Object
-
- org.threeten.bp.jdk8.DefaultInterfaceTemporalAccessor
-
- org.threeten.bp.format.DateTimeBuilder
-
- All Implemented Interfaces:
java.lang.Cloneable,TemporalAccessor
final class DateTimeBuilder extends DefaultInterfaceTemporalAccessor implements TemporalAccessor, java.lang.Cloneable
Builder that can holds date and time fields and related date and time objects.The builder is used to hold onto different elements of date and time. It is designed as two separate maps:
- from
TemporalFieldtolongvalue, where the value may be outside the valid range for the field - from
ClasstoTemporalAccessor, holding larger scale objects likeLocalDateTime.
Specification for implementors
This class is mutable and not thread-safe. It should only be used from a single thread.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) ChronologychronoThe chronology.(package private) ChronoLocalDatedateThe date.(package private) PeriodexcessDaysThe excess days.(package private) java.util.Map<TemporalField,java.lang.Long>fieldValuesThe map of other fields.(package private) booleanleapSecondThe leap second flag.(package private) LocalTimetimeThe time.(package private) ZoneIdzoneThe zone.
-
Constructor Summary
Constructors Constructor Description DateTimeBuilder()Creates an empty instance of the builder.DateTimeBuilder(TemporalField field, long value)Creates a new instance of the builder with a single field-value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) DateTimeBuilderaddFieldValue(TemporalField field, long value)Adds a field-value pair to the builder.(package private) voidaddObject(ChronoLocalDate date)(package private) voidaddObject(LocalTime time)<R> Rbuild(TemporalQuery<R> type)Builds the specified type from the values in this builder.private voidcheckDate(LocalDate date)private voidcrossCheck()private voidcrossCheck(TemporalAccessor temporal)private java.lang.LonggetFieldValue0(TemporalField field)longgetLong(TemporalField field)Gets the value of the specified field as along.booleanisSupported(TemporalField field)Checks if the specified field is supported.private voidmergeDate(ResolverStyle resolverStyle)private voidmergeInstantFields()private voidmergeInstantFields0(ZoneId selectedZone)private voidmergeTime(ResolverStyle resolverStyle)private DateTimeBuilderputFieldValue0(TemporalField field, long value)<R> Rquery(TemporalQuery<R> query)Queries this date-time.DateTimeBuilderresolve(ResolverStyle resolverStyle, java.util.Set<TemporalField> resolverFields)Resolves the builder, evaluating the date and time.private booleanresolveFields(ResolverStyle resolverStyle)private voidresolveFractional()private voidresolveInstant()private voidresolveMakeChanges(TemporalField targetField, ChronoLocalDate date)private voidresolveMakeChanges(TemporalField targetField, LocalTime time)private voidresolveTimeInferZeroes(ResolverStyle resolverStyle)java.lang.StringtoString()-
Methods inherited from class org.threeten.bp.jdk8.DefaultInterfaceTemporalAccessor
get, range
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.threeten.bp.temporal.TemporalAccessor
get, range
-
-
-
-
Field Detail
-
fieldValues
final java.util.Map<TemporalField,java.lang.Long> fieldValues
The map of other fields.
-
chrono
Chronology chrono
The chronology.
-
zone
ZoneId zone
The zone.
-
date
ChronoLocalDate date
The date.
-
time
LocalTime time
The time.
-
leapSecond
boolean leapSecond
The leap second flag.
-
excessDays
Period excessDays
The excess days.
-
-
Constructor Detail
-
DateTimeBuilder
public DateTimeBuilder()
Creates an empty instance of the builder.
-
DateTimeBuilder
public DateTimeBuilder(TemporalField field, long value)
Creates a new instance of the builder with a single field-value.This is equivalent to using
addFieldValue(TemporalField, long)on an empty builder.- Parameters:
field- the field to add, not nullvalue- the value to add, not null
-
-
Method Detail
-
getFieldValue0
private java.lang.Long getFieldValue0(TemporalField field)
-
addFieldValue
DateTimeBuilder addFieldValue(TemporalField field, long value)
Adds a field-value pair to the builder.This adds a field to the builder. If the field is not already present, then the field-value pair is added to the map. If the field is already present and it has the same value as that specified, no action occurs. If the field is already present and it has a different value to that specified, then an exception is thrown.
- Parameters:
field- the field to add, not nullvalue- the value to add, not null- Returns:
this, for method chaining- Throws:
DateTimeException- if the field is already present with a different value
-
putFieldValue0
private DateTimeBuilder putFieldValue0(TemporalField field, long value)
-
addObject
void addObject(ChronoLocalDate date)
-
addObject
void addObject(LocalTime time)
-
resolve
public DateTimeBuilder resolve(ResolverStyle resolverStyle, java.util.Set<TemporalField> resolverFields)
Resolves the builder, evaluating the date and time.This examines the contents of the builder and resolves it to produce the best available date and time, throwing an exception if a problem occurs. Calling this method changes the state of the builder.
- Parameters:
resolverStyle- how to resolve- Returns:
this, for method chaining
-
resolveFields
private boolean resolveFields(ResolverStyle resolverStyle)
-
resolveMakeChanges
private void resolveMakeChanges(TemporalField targetField, ChronoLocalDate date)
-
resolveMakeChanges
private void resolveMakeChanges(TemporalField targetField, LocalTime time)
-
mergeDate
private void mergeDate(ResolverStyle resolverStyle)
-
checkDate
private void checkDate(LocalDate date)
-
mergeTime
private void mergeTime(ResolverStyle resolverStyle)
-
resolveTimeInferZeroes
private void resolveTimeInferZeroes(ResolverStyle resolverStyle)
-
mergeInstantFields
private void mergeInstantFields()
-
mergeInstantFields0
private void mergeInstantFields0(ZoneId selectedZone)
-
crossCheck
private void crossCheck()
-
crossCheck
private void crossCheck(TemporalAccessor temporal)
-
resolveFractional
private void resolveFractional()
-
resolveInstant
private void resolveInstant()
-
build
public <R> R build(TemporalQuery<R> type)
Builds the specified type from the values in this builder.This attempts to build the specified type from this builder. If the builder cannot return the type, an exception is thrown.
- Type Parameters:
R- the type to return- Parameters:
type- the type to invokefromon, not null- Returns:
- the extracted value, not null
- Throws:
DateTimeException- if an error occurs
-
isSupported
public boolean isSupported(TemporalField field)
Description copied from interface:TemporalAccessorChecks if the specified field is supported.This checks if the date-time can be queried for the specified field. If false, then calling the
rangeandgetmethods 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)passingthisas the argument.Implementations must not alter this object.
- Specified by:
isSupportedin interfaceTemporalAccessor- Parameters:
field- the field to check, null returns false- Returns:
- true if this date-time can be queried for the field, false if not
-
getLong
public long getLong(TemporalField field)
Description copied from interface:TemporalAccessorGets 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 aDateTimeExceptionmust be thrown.If the field is not a
ChronoField, then the result of this method is obtained by invokingTemporalField.getFrom(TemporalAccessor)passingthisas the argument.Implementations must not alter either this object.
- Specified by:
getLongin interfaceTemporalAccessor- Parameters:
field- the field to get, not null- Returns:
- the value for the field
-
query
public <R> R query(TemporalQuery<R> query)
Description copied from interface:TemporalAccessorQueries 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::fromandZoneId::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); }- Specified by:
queryin interfaceTemporalAccessor- Overrides:
queryin classDefaultInterfaceTemporalAccessor- 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)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-