Class ChronoZonedDateTimeImpl<D extends ChronoLocalDate>
- java.lang.Object
-
- org.threeten.bp.jdk8.DefaultInterfaceTemporalAccessor
-
- org.threeten.bp.jdk8.DefaultInterfaceTemporal
-
- org.threeten.bp.chrono.ChronoZonedDateTime<D>
-
- org.threeten.bp.chrono.ChronoZonedDateTimeImpl<D>
-
- Type Parameters:
D- the date type
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<ChronoZonedDateTime<?>>,Temporal,TemporalAccessor
final class ChronoZonedDateTimeImpl<D extends ChronoLocalDate> extends ChronoZonedDateTime<D> implements java.io.Serializable
A date-time with a time-zone in the calendar neutral API.ZoneChronoDateTimeis an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, as well as a time-zone and zone offset.The purpose of storing the time-zone is to distinguish the ambiguous case where the local time-line overlaps, typically as a result of the end of daylight time. Information about the local-time can be obtained using methods on the time-zone.
Specification for implementors
This class is immutable and thread-safe.
-
-
Field Summary
Fields Modifier and Type Field Description private ChronoLocalDateTimeImpl<D>dateTimeThe local date-time.private ZoneOffsetoffsetThe zone offset.private static longserialVersionUIDSerialization version.private ZoneIdzoneThe zone ID.
-
Constructor Summary
Constructors Modifier Constructor Description privateChronoZonedDateTimeImpl(ChronoLocalDateTimeImpl<D> dateTime, ZoneOffset offset, ZoneId zone)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private ChronoZonedDateTimeImpl<D>create(Instant instant, ZoneId zone)Obtains an instance from anInstant.booleanequals(java.lang.Object obj)Checks if this date-time is equal to another date-time.ZoneOffsetgetOffset()Gets the zone offset, such as '+01:00'.ZoneIdgetZone()Gets the zone ID, such as 'Europe/Paris'.inthashCode()A hash code for this date-time.booleanisSupported(TemporalField field)Checks if the specified field is supported.booleanisSupported(TemporalUnit unit)Checks if the specified unit is supported.(package private) static <R extends ChronoLocalDate>
ChronoZonedDateTime<R>ofBest(ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset)Obtains an instance from a local date-time using the preferred offset if possible.(package private) static <R extends ChronoLocalDate>
ChronoZonedDateTimeImpl<R>ofInstant(Chronology chrono, Instant instant, ZoneId zone)Obtains an instance from an instant using the specified time-zone.ChronoZonedDateTime<D>plus(long amountToAdd, TemporalUnit unit)Returns an object of the same type as this object with the specified period added.(package private) static ChronoZonedDateTime<?>readExternal(java.io.ObjectInput in)private java.lang.ObjectreadResolve()Defend against malicious streams.ChronoLocalDateTime<D>toLocalDateTime()Gets the local date-time part of this date-time.java.lang.StringtoString()Outputs this date-time as aString.longuntil(Temporal endExclusive, TemporalUnit unit)Calculates the period between this temporal and another temporal in terms of the specified unit.ChronoZonedDateTime<D>with(TemporalField field, long newValue)Returns an object of the same type as this object with the specified field altered.ChronoZonedDateTime<D>withEarlierOffsetAtOverlap()Returns a copy of this date-time changing the zone offset to the earlier of the two valid offsets at a local time-line overlap.ChronoZonedDateTime<D>withLaterOffsetAtOverlap()Returns a copy of this date-time changing the zone offset to the later of the two valid offsets at a local time-line overlap.ChronoZonedDateTime<D>withZoneSameInstant(ZoneId zone)Returns a copy of this date-time with a different time-zone, retaining the instant.ChronoZonedDateTime<D>withZoneSameLocal(ZoneId zone)Returns a copy of this ZonedDateTime with a different time-zone, retaining the local date-time if possible.(package private) voidwriteExternal(java.io.ObjectOutput out)private java.lang.ObjectwriteReplace()-
Methods inherited from class org.threeten.bp.chrono.ChronoZonedDateTime
compareTo, format, from, get, getChronology, getLong, isAfter, isBefore, isEqual, minus, minus, plus, query, range, timeLineOrder, toEpochSecond, toInstant, toLocalDate, toLocalTime, with
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
Serialization version.- See Also:
- Constant Field Values
-
dateTime
private final ChronoLocalDateTimeImpl<D extends ChronoLocalDate> dateTime
The local date-time.
-
offset
private final ZoneOffset offset
The zone offset.
-
zone
private final ZoneId zone
The zone ID.
-
-
Constructor Detail
-
ChronoZonedDateTimeImpl
private ChronoZonedDateTimeImpl(ChronoLocalDateTimeImpl<D> dateTime, ZoneOffset offset, ZoneId zone)
Constructor.- Parameters:
dateTime- the date-time, not nulloffset- the zone offset, not nullzone- the zone ID, not null
-
-
Method Detail
-
ofBest
static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset)
Obtains an instance from a local date-time using the preferred offset if possible.- Parameters:
localDateTime- the local date-time, not nullzone- the zone identifier, not nullpreferredOffset- the zone offset, null if no preference- Returns:
- the zoned date-time, not null
-
ofInstant
static <R extends ChronoLocalDate> ChronoZonedDateTimeImpl<R> ofInstant(Chronology chrono, Instant instant, ZoneId zone)
Obtains an instance from an instant using the specified time-zone.- Parameters:
chrono- the chronology, not nullinstant- the instant, not nullzone- the zone identifier, not null- Returns:
- the zoned date-time, not null
-
create
private ChronoZonedDateTimeImpl<D> create(Instant instant, ZoneId zone)
Obtains an instance from anInstant.- Parameters:
instant- the instant to create the date-time from, not nullzone- the time-zone to use, validated not null- Returns:
- the zoned date-time, validated not null
-
isSupported
public boolean isSupported(TemporalUnit unit)
Description copied from interface:TemporalChecks if the specified unit is supported.This checks if the date-time can be queried for the specified unit. If false, then calling the
plusandminusmethods will throw an exception.Specification for implementors
Implementations must check and handle all fields defined inChronoUnit. If the field is supported, then true is returned, otherwise falseIf the field is not a
ChronoUnit, then the result of this method is obtained by invokingTemporalUnit.isSupportedBy(Temporal)passingthisas the argument.Implementations must not alter this object.
- Specified by:
isSupportedin interfaceTemporal- Parameters:
unit- the unit to check, null returns false- Returns:
- true if this date-time can be queried for the unit, false if not
-
getOffset
public ZoneOffset getOffset()
Description copied from class:ChronoZonedDateTimeGets the zone offset, such as '+01:00'.This is the offset of the local date-time from UTC/Greenwich.
- Specified by:
getOffsetin classChronoZonedDateTime<D extends ChronoLocalDate>- Returns:
- the zone offset, not null
-
withEarlierOffsetAtOverlap
public ChronoZonedDateTime<D> withEarlierOffsetAtOverlap()
Description copied from class:ChronoZonedDateTimeReturns a copy of this date-time changing the zone offset to the earlier of the two valid offsets at a local time-line overlap.This method only has any effect when the local time-line overlaps, such as at an autumn daylight savings cutover. In this scenario, there are two valid offsets for the local date-time. Calling this method will return a zoned date-time with the earlier of the two selected.
If this method is called when it is not an overlap,
thisis returned.This instance is immutable and unaffected by this method call.
- Specified by:
withEarlierOffsetAtOverlapin classChronoZonedDateTime<D extends ChronoLocalDate>- Returns:
- a
ZoneChronoDateTimebased on this date-time with the earlier offset, not null
-
withLaterOffsetAtOverlap
public ChronoZonedDateTime<D> withLaterOffsetAtOverlap()
Description copied from class:ChronoZonedDateTimeReturns a copy of this date-time changing the zone offset to the later of the two valid offsets at a local time-line overlap.This method only has any effect when the local time-line overlaps, such as at an autumn daylight savings cutover. In this scenario, there are two valid offsets for the local date-time. Calling this method will return a zoned date-time with the later of the two selected.
If this method is called when it is not an overlap,
thisis returned.This instance is immutable and unaffected by this method call.
- Specified by:
withLaterOffsetAtOverlapin classChronoZonedDateTime<D extends ChronoLocalDate>- Returns:
- a
ChronoZonedDateTimebased on this date-time with the later offset, not null
-
toLocalDateTime
public ChronoLocalDateTime<D> toLocalDateTime()
Description copied from class:ChronoZonedDateTimeGets the local date-time part of this date-time.This returns a local date with the same year, month and day as this date-time.
- Specified by:
toLocalDateTimein classChronoZonedDateTime<D extends ChronoLocalDate>- Returns:
- the local date-time part of this date-time, not null
-
getZone
public ZoneId getZone()
Description copied from class:ChronoZonedDateTimeGets the zone ID, such as 'Europe/Paris'.This returns the stored time-zone id used to determine the time-zone rules.
- Specified by:
getZonein classChronoZonedDateTime<D extends ChronoLocalDate>- Returns:
- the zone ID, not null
-
withZoneSameLocal
public ChronoZonedDateTime<D> withZoneSameLocal(ZoneId zone)
Description copied from class:ChronoZonedDateTimeReturns a copy of this ZonedDateTime with a different time-zone, retaining the local date-time if possible.This method changes the time-zone and retains the local date-time. The local date-time is only changed if it is invalid for the new zone.
To change the zone and adjust the local date-time, use
ChronoZonedDateTime.withZoneSameInstant(ZoneId).This instance is immutable and unaffected by this method call.
- Specified by:
withZoneSameLocalin classChronoZonedDateTime<D extends ChronoLocalDate>- Parameters:
zone- the time-zone to change to, not null- Returns:
- a
ChronoZonedDateTimebased on this date-time with the requested zone, not null
-
withZoneSameInstant
public ChronoZonedDateTime<D> withZoneSameInstant(ZoneId zone)
Description copied from class:ChronoZonedDateTimeReturns a copy of this date-time with a different time-zone, retaining the instant.This method changes the time-zone and retains the instant. This normally results in a change to the local date-time.
This method is based on retaining the same instant, thus gaps and overlaps in the local time-line have no effect on the result.
To change the offset while keeping the local time, use
ChronoZonedDateTime.withZoneSameLocal(ZoneId).- Specified by:
withZoneSameInstantin classChronoZonedDateTime<D extends ChronoLocalDate>- Parameters:
zone- the time-zone to change to, not null- Returns:
- a
ChronoZonedDateTimebased on this date-time with the requested zone, not null
-
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
-
with
public ChronoZonedDateTime<D> with(TemporalField field, long newValue)
Description copied from interface:TemporalReturns an object of the same type as this object with the specified field altered.This returns a new object based on this one with the value for the specified field changed. For example, on a
LocalDate, this could be used to set the year, month or day-of-month. The returned object will have the same observable type as this 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 field 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.
Specification for implementors
Implementations must check and handle all fields defined inChronoField. If the field is supported, then the adjustment must be performed. If unsupported, then aDateTimeExceptionmust be thrown.If the field is not a
ChronoField, then the result of this method is obtained by invokingTemporalField.adjustInto(Temporal, long)passingthisas the first argument.Implementations must not alter either this object or the specified temporal object. Instead, an adjusted copy of the original must be returned. This provides equivalent, safe behavior for immutable and mutable implementations.
- Specified by:
within interfaceTemporal- Specified by:
within classChronoZonedDateTime<D extends ChronoLocalDate>- Parameters:
field- the field to set in the result, not nullnewValue- the new value of the field in the result- Returns:
- an object of the same type with the specified field set, not null
-
plus
public ChronoZonedDateTime<D> plus(long amountToAdd, TemporalUnit unit)
Description copied from interface:TemporalReturns an object of the same type as this object with the specified period added.This method returns a new object based on this one with the specified period added. For example, on a
LocalDate, this could be used to add a number of years, months or days. The returned object will have the same observable type as this 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 adding one month would be unclear. In cases like this, the field 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.
If the implementation represents a date-time that has boundaries, such as
LocalTime, then the permitted units must include the boundary unit, but no multiples of the boundary unit. For example,LocalTimemust acceptDAYSbut notWEEKSorMONTHS.Specification for implementors
Implementations must check and handle all units defined inChronoUnit. If the unit is supported, then the addition must be performed. If unsupported, then aDateTimeExceptionmust be thrown.If the unit is not a
ChronoUnit, then the result of this method is obtained by invokingTemporalUnit.addTo(Temporal, long)passingthisas the first argument.Implementations must not alter either this object or the specified temporal object. Instead, an adjusted copy of the original must be returned. This provides equivalent, safe behavior for immutable and mutable implementations.
- Specified by:
plusin interfaceTemporal- Specified by:
plusin classChronoZonedDateTime<D extends ChronoLocalDate>- Parameters:
amountToAdd- the amount of the specified unit to add, may be negativeunit- the unit of the period to add, not null- Returns:
- an object of the same type with the specified period added, not null
-
until
public long until(Temporal endExclusive, TemporalUnit unit)
Description copied from interface:TemporalCalculates the period between this temporal and another temporal in terms of the specified unit.This calculates the period between two temporals in terms of a single unit. The start and end points are
thisand the specified temporal. The result will be negative if the end is before the start. For example, the period in hours between two temporal objects can be calculated usingstartTime.until(endTime, HOURS).The calculation returns a whole number, representing the number of complete units between the two temporals. For example, the period in hours between the times 11:30 and 13:29 will only be one hour as it is one minute short of two hours.
There are two equivalent ways of using this method. The first is to invoke this method directly. The second is to use
TemporalUnit.between(Temporal, Temporal):// these two lines are equivalent between = thisUnit.between(start, end); between = start.until(end, thisUnit);
The choice should be made based on which makes the code more readable.For example, this method allows the number of days between two dates to be calculated:
long daysBetween = DAYS.between(start, end); // or alternatively long daysBetween = start.until(end, DAYS);
Specification for implementors
Implementations must begin by checking to ensure that the input temporal object is of the same observable type as the implementation. They must then perform the calculation for all instances ofChronoUnit. ADateTimeExceptionmust be thrown forChronoUnitinstances that are unsupported.If the unit is not a
ChronoUnit, then the result of this method is obtained by invokingTemporalUnit.between(Temporal, Temporal)passingthisas the first argument and the input temporal as the second argument.In summary, implementations must behave in a manner equivalent to this code:
// check input temporal is the same type as this class if (unit instanceof ChronoUnit) { // if unit is supported, then calculate and return result // else throw DateTimeException for unsupported units } return unit.between(this, endTemporal);The target object must not be altered by this method.
-
writeReplace
private java.lang.Object writeReplace()
-
readResolve
private java.lang.Object readResolve() throws java.io.ObjectStreamExceptionDefend against malicious streams.- Returns:
- never
- Throws:
java.io.InvalidObjectException- alwaysjava.io.ObjectStreamException
-
writeExternal
void writeExternal(java.io.ObjectOutput out) throws java.io.IOException- Throws:
java.io.IOException
-
readExternal
static ChronoZonedDateTime<?> readExternal(java.io.ObjectInput in) throws java.io.IOException, java.lang.ClassNotFoundException
- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
equals
public boolean equals(java.lang.Object obj)
Description copied from class:ChronoZonedDateTimeChecks if this date-time is equal to another date-time.The comparison is based on the offset date-time and the zone. To compare for the same instant on the time-line, use
ChronoZonedDateTime.compareTo(org.threeten.bp.chrono.ChronoZonedDateTime<?>). Only objects of typeChronoZoneDateTimeare compared, other types return false.- Overrides:
equalsin classChronoZonedDateTime<D extends ChronoLocalDate>- Parameters:
obj- the object to check, null returns false- Returns:
- true if this is equal to the other date-time
-
hashCode
public int hashCode()
Description copied from class:ChronoZonedDateTimeA hash code for this date-time.- Overrides:
hashCodein classChronoZonedDateTime<D extends ChronoLocalDate>- Returns:
- a suitable hash code
-
toString
public java.lang.String toString()
Description copied from class:ChronoZonedDateTimeOutputs this date-time as aString.The output will include the full zoned date-time and the chronology ID.
- Overrides:
toStringin classChronoZonedDateTime<D extends ChronoLocalDate>- Returns:
- a string representation of this date-time, not null
-
-