Class ChronoZonedDateTimeImpl<D extends ChronoLocalDate>

  • 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.

    ZoneChronoDateTime is 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.
    • Constructor Detail

      • ChronoZonedDateTimeImpl

        private ChronoZonedDateTimeImpl​(ChronoLocalDateTimeImpl<D> dateTime,
                                        ZoneOffset offset,
                                        ZoneId zone)
        Constructor.
        Parameters:
        dateTime - the date-time, not null
        offset - the zone offset, not null
        zone - the zone ID, not null
    • Method Detail

      • ofBest

        static <R extends ChronoLocalDateChronoZonedDateTime<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 null
        zone - the zone identifier, not null
        preferredOffset - the zone offset, null if no preference
        Returns:
        the zoned date-time, not null
      • ofInstant

        static <R extends ChronoLocalDateChronoZonedDateTimeImpl<R> ofInstant​(Chronology chrono,
                                                                                Instant instant,
                                                                                ZoneId zone)
        Obtains an instance from an instant using the specified time-zone.
        Parameters:
        chrono - the chronology, not null
        instant - the instant, not null
        zone - 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 an Instant.
        Parameters:
        instant - the instant to create the date-time from, not null
        zone - 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: Temporal
        Checks if the specified unit is supported.

        This checks if the date-time can be queried for the specified unit. If false, then calling the plus and minus methods will throw an exception.

        Specification for implementors

        Implementations must check and handle all fields defined in ChronoUnit. If the field is supported, then true is returned, otherwise false

        If the field is not a ChronoUnit, then the result of this method is obtained by invoking TemporalUnit.isSupportedBy(Temporal) passing this as the argument.

        Implementations must not alter this object.

        Specified by:
        isSupported in interface Temporal
        Parameters:
        unit - the unit to check, null returns false
        Returns:
        true if this date-time can be queried for the unit, false if not
      • withEarlierOffsetAtOverlap

        public ChronoZonedDateTime<D> withEarlierOffsetAtOverlap()
        Description copied from class: ChronoZonedDateTime
        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.

        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, this is returned.

        This instance is immutable and unaffected by this method call.

        Specified by:
        withEarlierOffsetAtOverlap in class ChronoZonedDateTime<D extends ChronoLocalDate>
        Returns:
        a ZoneChronoDateTime based on this date-time with the earlier offset, not null
      • withLaterOffsetAtOverlap

        public ChronoZonedDateTime<D> withLaterOffsetAtOverlap()
        Description copied from class: ChronoZonedDateTime
        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.

        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, this is returned.

        This instance is immutable and unaffected by this method call.

        Specified by:
        withLaterOffsetAtOverlap in class ChronoZonedDateTime<D extends ChronoLocalDate>
        Returns:
        a ChronoZonedDateTime based on this date-time with the later offset, not null
      • withZoneSameLocal

        public ChronoZonedDateTime<D> withZoneSameLocal​(ZoneId zone)
        Description copied from class: ChronoZonedDateTime
        Returns 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:
        withZoneSameLocal in class ChronoZonedDateTime<D extends ChronoLocalDate>
        Parameters:
        zone - the time-zone to change to, not null
        Returns:
        a ChronoZonedDateTime based on this date-time with the requested zone, not null
      • withZoneSameInstant

        public ChronoZonedDateTime<D> withZoneSameInstant​(ZoneId zone)
        Description copied from class: ChronoZonedDateTime
        Returns 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:
        withZoneSameInstant in class ChronoZonedDateTime<D extends ChronoLocalDate>
        Parameters:
        zone - the time-zone to change to, not null
        Returns:
        a ChronoZonedDateTime based on this date-time with the requested zone, not null
      • isSupported

        public boolean isSupported​(TemporalField field)
        Description copied from interface: TemporalAccessor
        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 and get methods will throw an exception.

        Specification for implementors

        Implementations must check and handle all fields defined in 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.isSupportedBy(TemporalAccessor) passing this as the argument.

        Implementations must not alter this object.

        Specified by:
        isSupported in interface TemporalAccessor
        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: Temporal
        Returns 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 in ChronoField. If the field is supported, then the adjustment must be performed. 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.adjustInto(Temporal, long) passing this as 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:
        with in interface Temporal
        Specified by:
        with in class ChronoZonedDateTime<D extends ChronoLocalDate>
        Parameters:
        field - the field to set in the result, not null
        newValue - 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: Temporal
        Returns 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, LocalTime must accept DAYS but not WEEKS or MONTHS.

        Specification for implementors

        Implementations must check and handle all units defined in ChronoUnit. If the unit is supported, then the addition must be performed. If unsupported, then a DateTimeException must be thrown.

        If the unit is not a ChronoUnit, then the result of this method is obtained by invoking TemporalUnit.addTo(Temporal, long) passing this as 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:
        plus in interface Temporal
        Specified by:
        plus in class ChronoZonedDateTime<D extends ChronoLocalDate>
        Parameters:
        amountToAdd - the amount of the specified unit to add, may be negative
        unit - 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: Temporal
        Calculates 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 this and 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 using startTime.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 of ChronoUnit. A DateTimeException must be thrown for ChronoUnit instances that are unsupported.

        If the unit is not a ChronoUnit, then the result of this method is obtained by invoking TemporalUnit.between(Temporal, Temporal) passing this as 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.

        Specified by:
        until in interface Temporal
        Parameters:
        endExclusive - the end temporal, of the same type as this object, not null
        unit - the unit to measure the period in, not null
        Returns:
        the amount of the period between this and the end
      • writeReplace

        private java.lang.Object writeReplace()
      • readResolve

        private java.lang.Object readResolve()
                                      throws java.io.ObjectStreamException
        Defend against malicious streams.
        Returns:
        never
        Throws:
        java.io.InvalidObjectException - always
        java.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.IOException
        java.lang.ClassNotFoundException
      • toString

        public java.lang.String toString()
        Description copied from class: ChronoZonedDateTime
        Outputs this date-time as a String.

        The output will include the full zoned date-time and the chronology ID.

        Overrides:
        toString in class ChronoZonedDateTime<D extends ChronoLocalDate>
        Returns:
        a string representation of this date-time, not null