< prev index next >

src/java.base/share/classes/java/time/OffsetDateTime.java

Print this page

  99  * A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system,
 100  * such as {@code 2007-12-03T10:15:30+01:00}.
 101  * <p>
 102  * {@code OffsetDateTime} is an immutable representation of a date-time with an offset.
 103  * This class stores all date and time fields, to a precision of nanoseconds,
 104  * as well as the offset from UTC/Greenwich. For example, the value
 105  * "2nd October 2007 at 13:45:30.123456789 +02:00" can be stored in an {@code OffsetDateTime}.
 106  * <p>
 107  * {@code OffsetDateTime}, {@link java.time.ZonedDateTime} and {@link java.time.Instant} all store an instant
 108  * on the time-line to nanosecond precision.
 109  * {@code Instant} is the simplest, simply representing the instant.
 110  * {@code OffsetDateTime} adds to the instant the offset from UTC/Greenwich, which allows
 111  * the local date-time to be obtained.
 112  * {@code ZonedDateTime} adds full time-zone rules.
 113  * <p>
 114  * It is intended that {@code ZonedDateTime} or {@code Instant} is used to model data
 115  * in simpler applications. This class may be used when modeling date-time concepts in
 116  * more detail, or when communicating to a database or in a network protocol.
 117  * <p>
 118  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 119  * class; programmers should treat instances that are
 120  * {@linkplain #equals(Object) equal} as interchangeable and should not
 121  * use instances for synchronization, or unpredictable behavior may
 122  * occur. For example, in a future release, synchronization may fail.
 123  * The {@code equals} method should be used for comparisons.







 124  *
 125  * @implSpec
 126  * This class is immutable and thread-safe.
 127  *
 128  * @since 1.8
 129  */
 130 @jdk.internal.ValueBased

 131 public final class OffsetDateTime
 132         implements Temporal, TemporalAdjuster, Comparable<OffsetDateTime>, Serializable {
 133 
 134     /**
 135      * The minimum supported {@code OffsetDateTime}, '-999999999-01-01T00:00:00+18:00'.
 136      * This is the local date-time of midnight at the start of the minimum date
 137      * in the maximum offset (larger offsets are earlier on the time-line).
 138      * This combines {@link LocalDateTime#MIN} and {@link ZoneOffset#MAX}.
 139      * This could be used by an application as a "far past" date-time.
 140      */
 141     public static final OffsetDateTime MIN = LocalDateTime.MIN.atOffset(ZoneOffset.MAX);
 142     /**
 143      * The maximum supported {@code OffsetDateTime}, '+999999999-12-31T23:59:59.999999999-18:00'.
 144      * This is the local date-time just before midnight at the end of the maximum date
 145      * in the minimum offset (larger negative offsets are later on the time-line).
 146      * This combines {@link LocalDateTime#MAX} and {@link ZoneOffset#MIN}.
 147      * This could be used by an application as a "far future" date-time.
 148      */
 149     public static final OffsetDateTime MAX = LocalDateTime.MAX.atOffset(ZoneOffset.MIN);
 150 

1939      * <pre>
1940      *  out.writeByte(10);  // identifies an OffsetDateTime
1941      *  // the <a href="{@docRoot}/serialized-form.html#java.time.LocalDateTime">datetime</a> excluding the one byte header
1942      *  // the <a href="{@docRoot}/serialized-form.html#java.time.ZoneOffset">offset</a> excluding the one byte header
1943      * </pre>
1944      *
1945      * @return the instance of {@code Ser}, not null
1946      */
1947     @java.io.Serial
1948     private Object writeReplace() {
1949         return new Ser(Ser.OFFSET_DATE_TIME_TYPE, this);
1950     }
1951 
1952     /**
1953      * Defend against malicious streams.
1954      *
1955      * @param s the stream to read
1956      * @throws InvalidObjectException always
1957      */
1958     @java.io.Serial

1959     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1960         throw new InvalidObjectException("Deserialization via serialization delegate");
1961     }
1962 
1963     void writeExternal(ObjectOutput out) throws IOException {
1964         dateTime.writeExternal(out);
1965         offset.writeExternal(out);
1966     }
1967 
1968     static OffsetDateTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
1969         LocalDateTime dateTime = LocalDateTime.readExternal(in);
1970         ZoneOffset offset = ZoneOffset.readExternal(in);
1971         return OffsetDateTime.of(dateTime, offset);
1972     }
1973 
1974 }

  99  * A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system,
 100  * such as {@code 2007-12-03T10:15:30+01:00}.
 101  * <p>
 102  * {@code OffsetDateTime} is an immutable representation of a date-time with an offset.
 103  * This class stores all date and time fields, to a precision of nanoseconds,
 104  * as well as the offset from UTC/Greenwich. For example, the value
 105  * "2nd October 2007 at 13:45:30.123456789 +02:00" can be stored in an {@code OffsetDateTime}.
 106  * <p>
 107  * {@code OffsetDateTime}, {@link java.time.ZonedDateTime} and {@link java.time.Instant} all store an instant
 108  * on the time-line to nanosecond precision.
 109  * {@code Instant} is the simplest, simply representing the instant.
 110  * {@code OffsetDateTime} adds to the instant the offset from UTC/Greenwich, which allows
 111  * the local date-time to be obtained.
 112  * {@code ZonedDateTime} adds full time-zone rules.
 113  * <p>
 114  * It is intended that {@code ZonedDateTime} or {@code Instant} is used to model data
 115  * in simpler applications. This class may be used when modeling date-time concepts in
 116  * more detail, or when communicating to a database or in a network protocol.
 117  * <p>
 118  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 119  * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
 120  * as interchangeable and should not use instances for synchronization, mutexes, or
 121  * with {@linkplain java.lang.ref.Reference object references}.
 122  *
 123  * <div class="preview-block">
 124  *      <div class="preview-comment">
 125  *          When preview features are enabled, {@code OffsetDateTime} is a {@linkplain Class#isValue value class}.
 126  *          Use of value class instances for synchronization, mutexes, or with
 127  *          {@linkplain java.lang.ref.Reference object references} result in
 128  *          {@link IdentityException}.
 129  *      </div>
 130  * </div>
 131  *
 132  * @implSpec
 133  * This class is immutable and thread-safe.
 134  *
 135  * @since 1.8
 136  */
 137 @jdk.internal.ValueBased
 138 @jdk.internal.MigratedValueClass
 139 public final class OffsetDateTime
 140         implements Temporal, TemporalAdjuster, Comparable<OffsetDateTime>, Serializable {
 141 
 142     /**
 143      * The minimum supported {@code OffsetDateTime}, '-999999999-01-01T00:00:00+18:00'.
 144      * This is the local date-time of midnight at the start of the minimum date
 145      * in the maximum offset (larger offsets are earlier on the time-line).
 146      * This combines {@link LocalDateTime#MIN} and {@link ZoneOffset#MAX}.
 147      * This could be used by an application as a "far past" date-time.
 148      */
 149     public static final OffsetDateTime MIN = LocalDateTime.MIN.atOffset(ZoneOffset.MAX);
 150     /**
 151      * The maximum supported {@code OffsetDateTime}, '+999999999-12-31T23:59:59.999999999-18:00'.
 152      * This is the local date-time just before midnight at the end of the maximum date
 153      * in the minimum offset (larger negative offsets are later on the time-line).
 154      * This combines {@link LocalDateTime#MAX} and {@link ZoneOffset#MIN}.
 155      * This could be used by an application as a "far future" date-time.
 156      */
 157     public static final OffsetDateTime MAX = LocalDateTime.MAX.atOffset(ZoneOffset.MIN);
 158 

1947      * <pre>
1948      *  out.writeByte(10);  // identifies an OffsetDateTime
1949      *  // the <a href="{@docRoot}/serialized-form.html#java.time.LocalDateTime">datetime</a> excluding the one byte header
1950      *  // the <a href="{@docRoot}/serialized-form.html#java.time.ZoneOffset">offset</a> excluding the one byte header
1951      * </pre>
1952      *
1953      * @return the instance of {@code Ser}, not null
1954      */
1955     @java.io.Serial
1956     private Object writeReplace() {
1957         return new Ser(Ser.OFFSET_DATE_TIME_TYPE, this);
1958     }
1959 
1960     /**
1961      * Defend against malicious streams.
1962      *
1963      * @param s the stream to read
1964      * @throws InvalidObjectException always
1965      */
1966     @java.io.Serial
1967     @SuppressWarnings("serial") // this method is not invoked for value classes
1968     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1969         throw new InvalidObjectException("Deserialization via serialization delegate");
1970     }
1971 
1972     void writeExternal(ObjectOutput out) throws IOException {
1973         dateTime.writeExternal(out);
1974         offset.writeExternal(out);
1975     }
1976 
1977     static OffsetDateTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
1978         LocalDateTime dateTime = LocalDateTime.readExternal(in);
1979         ZoneOffset offset = ZoneOffset.readExternal(in);
1980         return OffsetDateTime.of(dateTime, offset);
1981     }
1982 
1983 }
< prev index next >