public final class DateTimeBuilder extends DefaultInterfaceTemporalAccessor implements TemporalAccessor, Cloneable
The builder is used to hold onto different elements of date and time. It is designed as two separate maps:
TemporalField to long value, where the value may be
outside the valid range for the field
Class to TemporalAccessor, holding larger scale objects
like LocalDateTime.
| Constructor and 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.
|
DateTimeBuilder(ZoneId zone,
Chrono<?> chrono)
Creates a new instance of the builder.
|
| Modifier and Type | Method and Description |
|---|---|
DateTimeBuilder |
addCalendrical(Object object)
Adds a date-time object to the builder.
|
DateTimeBuilder |
addFieldValue(TemporalField field,
long value)
Adds a field-value pair to the builder.
|
<R> R |
build(Class<R> type)
Builds the specified type from the values in this builder.
|
DateTimeBuilder |
clone()
Clones this builder, creating a new independent copy referring to the
same map of fields and objects.
|
boolean |
containsFieldValue(TemporalField field)
Checks whether the specified field is present in the builder.
|
<R> R |
extract(Class<?> type) |
List<Object> |
getCalendricalList()
Gets the list of date-time objects in the builder.
|
long |
getFieldValue(TemporalField field)
Gets the value of the specified field from the builder.
|
Map<TemporalField,Long> |
getFieldValueMap()
Gets the map of field-value pairs in the builder.
|
long |
getLong(TemporalField field)
Gets the value of the specified field as a
long. |
long |
getValidFieldValue(TemporalField field)
Gets the value of the specified field from the builder ensuring it is valid.
|
boolean |
isSupported(TemporalField field)
Checks if the specified field is supported.
|
<R> R |
query(TemporalQuery<R> query)
Queries this date-time.
|
Long[] |
queryFieldValues(TemporalField... fields)
Queries a list of fields from the builder.
|
long |
removeFieldValue(TemporalField field)
Removes a field-value pair from the builder.
|
void |
removeFieldValues(TemporalField... fields)
Removes a list of fields from the builder.
|
DateTimeBuilder |
resolve()
Resolves the builder, evaluating the date and time.
|
String |
toString() |
get, rangeequals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitget, rangepublic DateTimeBuilder()
public DateTimeBuilder(TemporalField field, long value)
This is equivalent to using addFieldValue(TemporalField, long) on an empty builder.
field - the field to add, not nullvalue - the value to add, not nullpublic DateTimeBuilder(ZoneId zone, Chrono<?> chrono)
zone - the zone, may be nullchrono - the chronology, may be nullpublic Map<TemporalField,Long> getFieldValueMap()
public boolean containsFieldValue(TemporalField field)
field - the field to find in the field-value map, not nullpublic long getFieldValue(TemporalField field)
field - the field to query in the field-value map, not nullDateTimeException - if the field is not presentpublic long getValidFieldValue(TemporalField field)
field - the field to query in the field-value map, not nullDateTimeException - if the field is not presentpublic DateTimeBuilder addFieldValue(TemporalField field, long value)
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.
field - the field to add, not nullvalue - the value to add, not nullthis, for method chainingDateTimeException - if the field is already present with a different valuepublic long removeFieldValue(TemporalField field)
This removes a field, which must exist, from the builder.
See removeFieldValues(TemporalField...) for a version which does not throw an exception
field - the field to remove, not nullDateTimeException - if the field is not foundpublic void removeFieldValues(TemporalField... fields)
This removes the specified fields from the builder. No exception is thrown if the fields are not present.
fields - the fields to remove, not nullpublic Long[] queryFieldValues(TemporalField... fields)
This gets the value of the specified fields from the builder into an array where the positions match the order of the fields. If a field is not present, the array will contain null in that position.
fields - the fields to query, not nullpublic List<Object> getCalendricalList()
This map is intended for use with ZoneOffset and ZoneId.
The returned map is live and may be edited.
public DateTimeBuilder addCalendrical(Object object)
This adds a date-time object to the builder.
If the object is a DateTimeBuilder, each field is added using addFieldValue(org.threeten.bp.temporal.TemporalField, long).
If the object is not already present, then the object is added.
If the object is already present and it is equal to that specified, no action occurs.
If the object is already present and it is not equal to that specified, then an exception is thrown.
object - the object to add, not nullthis, for method chainingDateTimeException - if the field is already present with a different valuepublic DateTimeBuilder resolve()
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.
this, for method chainingpublic <R> R query(TemporalQuery<R> query)
TemporalAccessorThis 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 and ZoneId::from.
Further implementations are on TemporalQueries.
Queries may also be defined by applications.
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.chrono()) return // the Chrono
if (query == TemporalQueries.precision()) return // the precision
// call default method
return super.query(query);
}
query in interface TemporalAccessorquery in class DefaultInterfaceTemporalAccessorR - the type of the resultquery - the query to invoke, not nullpublic <R> R build(Class<R> type)
This attempts to build the specified type from this builder. If the builder cannot return the type, an exception is thrown.
R - the type to returntype - the type to invoke from on, not nullDateTimeException - if an error occurspublic DateTimeBuilder clone()
public boolean isSupported(TemporalField field)
TemporalAccessor
This checks if the date-time can be queried for the specified field.
If false, then calling the range and get
methods will throw an exception.
ChronoField.
If the field is supported, then true is returned, otherwise false
If the field is not a ChronoField, then the result of this method
is obtained by invoking TemporalField.doIsSupported(TemporalAccessor)
passing this as the argument.
Implementations must not alter either this object.
isSupported in interface TemporalAccessorfield - the field to check, null returns falsepublic long getLong(TemporalField field)
TemporalAccessorlong.
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.
ChronoField.
If the field is supported, then the value of the field must be returned.
If unsupported, then a DateTimeException must be thrown.
If the field is not a ChronoField, then the result of this method
is obtained by invoking TemporalField.doGet(TemporalAccessor)
passing this as the argument.
Implementations must not alter either this object.
getLong in interface TemporalAccessorfield - the field to get, not nullCopyright © 2007-2013 ThreeTen.org. All Rights Reserved.