< prev index next >

src/java.base/share/classes/java/time/chrono/ChronoZonedDateTimeImpl.java

Print this page




  90  * This class stores all date and time fields, to a precision of nanoseconds,
  91  * as well as a time-zone and zone offset.
  92  * <p>
  93  * The purpose of storing the time-zone is to distinguish the ambiguous case where
  94  * the local time-line overlaps, typically as a result of the end of daylight time.
  95  * Information about the local-time can be obtained using methods on the time-zone.
  96  *
  97  * @implSpec
  98  * This class is immutable and thread-safe.
  99  *
 100  * @serial Document the delegation of this class in the serialized-form specification.
 101  * @param <D> the concrete type for the date of this date-time
 102  * @since 1.8
 103  */
 104 final class ChronoZonedDateTimeImpl<D extends ChronoLocalDate>
 105         implements ChronoZonedDateTime<D>, Serializable {
 106 
 107     /**
 108      * Serialization version.
 109      */

 110     private static final long serialVersionUID = -5261813987200935591L;
 111 
 112     /**
 113      * The local date-time.
 114      */
 115     private final transient ChronoLocalDateTimeImpl<D> dateTime;
 116     /**
 117      * The zone offset.
 118      */
 119     private final transient ZoneOffset offset;
 120     /**
 121      * The zone ID.
 122      */
 123     private final transient ZoneId zone;
 124 
 125     //-----------------------------------------------------------------------
 126     /**
 127      * Obtains an instance from a local date-time using the preferred offset if possible.
 128      *
 129      * @param localDateTime  the local date-time, not null


 316             return dateTime.until(end.toLocalDateTime(), unit);
 317         }
 318         Objects.requireNonNull(unit, "unit");
 319         return unit.between(this, end);
 320     }
 321 
 322     //-----------------------------------------------------------------------
 323     /**
 324      * Writes the ChronoZonedDateTime using a
 325      * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
 326      * @serialData
 327      * <pre>
 328      *  out.writeByte(3);                  // identifies a ChronoZonedDateTime
 329      *  out.writeObject(toLocalDateTime());
 330      *  out.writeObject(getOffset());
 331      *  out.writeObject(getZone());
 332      * </pre>
 333      *
 334      * @return the instance of {@code Ser}, not null
 335      */

 336     private Object writeReplace() {
 337         return new Ser(Ser.CHRONO_ZONE_DATE_TIME_TYPE, this);
 338     }
 339 
 340     /**
 341      * Defend against malicious streams.
 342      *
 343      * @param s the stream to read
 344      * @throws InvalidObjectException always
 345      */

 346     private void readObject(ObjectInputStream s) throws InvalidObjectException {
 347         throw new InvalidObjectException("Deserialization via serialization delegate");
 348     }
 349 
 350     void writeExternal(ObjectOutput out) throws IOException {
 351         out.writeObject(dateTime);
 352         out.writeObject(offset);
 353         out.writeObject(zone);
 354     }
 355 
 356     static ChronoZonedDateTime<?> readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
 357         ChronoLocalDateTime<?> dateTime = (ChronoLocalDateTime<?>) in.readObject();
 358         ZoneOffset offset = (ZoneOffset) in.readObject();
 359         ZoneId zone = (ZoneId) in.readObject();
 360         return dateTime.atZone(offset).withZoneSameLocal(zone);
 361         // TODO: ZDT uses ofLenient()
 362     }
 363 
 364     //-------------------------------------------------------------------------
 365     @Override




  90  * This class stores all date and time fields, to a precision of nanoseconds,
  91  * as well as a time-zone and zone offset.
  92  * <p>
  93  * The purpose of storing the time-zone is to distinguish the ambiguous case where
  94  * the local time-line overlaps, typically as a result of the end of daylight time.
  95  * Information about the local-time can be obtained using methods on the time-zone.
  96  *
  97  * @implSpec
  98  * This class is immutable and thread-safe.
  99  *
 100  * @serial Document the delegation of this class in the serialized-form specification.
 101  * @param <D> the concrete type for the date of this date-time
 102  * @since 1.8
 103  */
 104 final class ChronoZonedDateTimeImpl<D extends ChronoLocalDate>
 105         implements ChronoZonedDateTime<D>, Serializable {
 106 
 107     /**
 108      * Serialization version.
 109      */
 110     @java.io.Serial
 111     private static final long serialVersionUID = -5261813987200935591L;
 112 
 113     /**
 114      * The local date-time.
 115      */
 116     private final transient ChronoLocalDateTimeImpl<D> dateTime;
 117     /**
 118      * The zone offset.
 119      */
 120     private final transient ZoneOffset offset;
 121     /**
 122      * The zone ID.
 123      */
 124     private final transient ZoneId zone;
 125 
 126     //-----------------------------------------------------------------------
 127     /**
 128      * Obtains an instance from a local date-time using the preferred offset if possible.
 129      *
 130      * @param localDateTime  the local date-time, not null


 317             return dateTime.until(end.toLocalDateTime(), unit);
 318         }
 319         Objects.requireNonNull(unit, "unit");
 320         return unit.between(this, end);
 321     }
 322 
 323     //-----------------------------------------------------------------------
 324     /**
 325      * Writes the ChronoZonedDateTime using a
 326      * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
 327      * @serialData
 328      * <pre>
 329      *  out.writeByte(3);                  // identifies a ChronoZonedDateTime
 330      *  out.writeObject(toLocalDateTime());
 331      *  out.writeObject(getOffset());
 332      *  out.writeObject(getZone());
 333      * </pre>
 334      *
 335      * @return the instance of {@code Ser}, not null
 336      */
 337     @java.io.Serial
 338     private Object writeReplace() {
 339         return new Ser(Ser.CHRONO_ZONE_DATE_TIME_TYPE, this);
 340     }
 341 
 342     /**
 343      * Defend against malicious streams.
 344      *
 345      * @param s the stream to read
 346      * @throws InvalidObjectException always
 347      */
 348     @java.io.Serial
 349     private void readObject(ObjectInputStream s) throws InvalidObjectException {
 350         throw new InvalidObjectException("Deserialization via serialization delegate");
 351     }
 352 
 353     void writeExternal(ObjectOutput out) throws IOException {
 354         out.writeObject(dateTime);
 355         out.writeObject(offset);
 356         out.writeObject(zone);
 357     }
 358 
 359     static ChronoZonedDateTime<?> readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
 360         ChronoLocalDateTime<?> dateTime = (ChronoLocalDateTime<?>) in.readObject();
 361         ZoneOffset offset = (ZoneOffset) in.readObject();
 362         ZoneId zone = (ZoneId) in.readObject();
 363         return dateTime.atZone(offset).withZoneSameLocal(zone);
 364         // TODO: ZDT uses ofLenient()
 365     }
 366 
 367     //-------------------------------------------------------------------------
 368     @Override


< prev index next >