< prev index next >

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

Print this page

  89 import java.time.temporal.TemporalUnit;
  90 import java.time.temporal.UnsupportedTemporalTypeException;
  91 import java.time.temporal.ValueRange;
  92 import java.time.zone.ZoneRules;
  93 import java.util.Objects;
  94 
  95 import jdk.internal.util.DateTimeHelper;
  96 
  97 /**
  98  * A time with an offset from UTC/Greenwich in the ISO-8601 calendar system,
  99  * such as {@code 10:15:30+01:00}.
 100  * <p>
 101  * {@code OffsetTime} is an immutable date-time object that represents a time, often
 102  * viewed as hour-minute-second-offset.
 103  * This class stores all time fields, to a precision of nanoseconds,
 104  * as well as a zone offset.
 105  * For example, the value "13:45:30.123456789+02:00" can be stored
 106  * in an {@code OffsetTime}.
 107  * <p>
 108  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 109  * class; programmers should treat instances that are
 110  * {@linkplain #equals(Object) equal} as interchangeable and should not
 111  * use instances for synchronization, or unpredictable behavior may
 112  * occur. For example, in a future release, synchronization may fail.
 113  * The {@code equals} method should be used for comparisons.







 114  *
 115  * @implSpec
 116  * This class is immutable and thread-safe.
 117  *
 118  * @since 1.8
 119  */
 120 @jdk.internal.ValueBased

 121 public final class OffsetTime
 122         implements Temporal, TemporalAdjuster, Comparable<OffsetTime>, Serializable {
 123 
 124     /**
 125      * The minimum supported {@code OffsetTime}, '00:00:00+18:00'.
 126      * This is the time of midnight at the start of the day in the maximum offset
 127      * (larger offsets are earlier on the time-line).
 128      * This combines {@link LocalTime#MIN} and {@link ZoneOffset#MAX}.
 129      * This could be used by an application as a "far past" date.
 130      */
 131     public static final OffsetTime MIN = LocalTime.MIN.atOffset(ZoneOffset.MAX);
 132     /**
 133      * The maximum supported {@code OffsetTime}, '23:59:59.999999999-18:00'.
 134      * This is the time just before midnight at the end of the day in the minimum offset
 135      * (larger negative offsets are later on the time-line).
 136      * This combines {@link LocalTime#MAX} and {@link ZoneOffset#MIN}.
 137      * This could be used by an application as a "far future" date.
 138      */
 139     public static final OffsetTime MAX = LocalTime.MAX.atOffset(ZoneOffset.MIN);
 140 

1414      * <pre>
1415      *  out.writeByte(9);  // identifies an OffsetTime
1416      *  // the <a href="{@docRoot}/serialized-form.html#java.time.LocalTime">time</a> excluding the one byte header
1417      *  // the <a href="{@docRoot}/serialized-form.html#java.time.ZoneOffset">offset</a> excluding the one byte header
1418      * </pre>
1419      *
1420      * @return the instance of {@code Ser}, not null
1421      */
1422     @java.io.Serial
1423     private Object writeReplace() {
1424         return new Ser(Ser.OFFSET_TIME_TYPE, this);
1425     }
1426 
1427     /**
1428      * Defend against malicious streams.
1429      *
1430      * @param s the stream to read
1431      * @throws InvalidObjectException always
1432      */
1433     @java.io.Serial

1434     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1435         throw new InvalidObjectException("Deserialization via serialization delegate");
1436     }
1437 
1438     void writeExternal(ObjectOutput out) throws IOException {
1439         time.writeExternal(out);
1440         offset.writeExternal(out);
1441     }
1442 
1443     static OffsetTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
1444         LocalTime time = LocalTime.readExternal(in);
1445         ZoneOffset offset = ZoneOffset.readExternal(in);
1446         return OffsetTime.of(time, offset);
1447     }
1448 
1449 }

  89 import java.time.temporal.TemporalUnit;
  90 import java.time.temporal.UnsupportedTemporalTypeException;
  91 import java.time.temporal.ValueRange;
  92 import java.time.zone.ZoneRules;
  93 import java.util.Objects;
  94 
  95 import jdk.internal.util.DateTimeHelper;
  96 
  97 /**
  98  * A time with an offset from UTC/Greenwich in the ISO-8601 calendar system,
  99  * such as {@code 10:15:30+01:00}.
 100  * <p>
 101  * {@code OffsetTime} is an immutable date-time object that represents a time, often
 102  * viewed as hour-minute-second-offset.
 103  * This class stores all time fields, to a precision of nanoseconds,
 104  * as well as a zone offset.
 105  * For example, the value "13:45:30.123456789+02:00" can be stored
 106  * in an {@code OffsetTime}.
 107  * <p>
 108  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 109  * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
 110  * as interchangeable and should not use instances for synchronization, mutexes, or
 111  * with {@linkplain java.lang.ref.Reference object references}.
 112  *
 113  * <div class="preview-block">
 114  *      <div class="preview-comment">
 115  *          When preview features are enabled, {@code OffsetTime} is a {@linkplain Class#isValue value class}.
 116  *          Use of value class instances for synchronization, mutexes, or with
 117  *          {@linkplain java.lang.ref.Reference object references} result in
 118  *          {@link IdentityException}.
 119  *      </div>
 120  * </div>
 121  *
 122  * @implSpec
 123  * This class is immutable and thread-safe.
 124  *
 125  * @since 1.8
 126  */
 127 @jdk.internal.ValueBased
 128 @jdk.internal.MigratedValueClass
 129 public final class OffsetTime
 130         implements Temporal, TemporalAdjuster, Comparable<OffsetTime>, Serializable {
 131 
 132     /**
 133      * The minimum supported {@code OffsetTime}, '00:00:00+18:00'.
 134      * This is the time of midnight at the start of the day in the maximum offset
 135      * (larger offsets are earlier on the time-line).
 136      * This combines {@link LocalTime#MIN} and {@link ZoneOffset#MAX}.
 137      * This could be used by an application as a "far past" date.
 138      */
 139     public static final OffsetTime MIN = LocalTime.MIN.atOffset(ZoneOffset.MAX);
 140     /**
 141      * The maximum supported {@code OffsetTime}, '23:59:59.999999999-18:00'.
 142      * This is the time just before midnight at the end of the day in the minimum offset
 143      * (larger negative offsets are later on the time-line).
 144      * This combines {@link LocalTime#MAX} and {@link ZoneOffset#MIN}.
 145      * This could be used by an application as a "far future" date.
 146      */
 147     public static final OffsetTime MAX = LocalTime.MAX.atOffset(ZoneOffset.MIN);
 148 

1422      * <pre>
1423      *  out.writeByte(9);  // identifies an OffsetTime
1424      *  // the <a href="{@docRoot}/serialized-form.html#java.time.LocalTime">time</a> excluding the one byte header
1425      *  // the <a href="{@docRoot}/serialized-form.html#java.time.ZoneOffset">offset</a> excluding the one byte header
1426      * </pre>
1427      *
1428      * @return the instance of {@code Ser}, not null
1429      */
1430     @java.io.Serial
1431     private Object writeReplace() {
1432         return new Ser(Ser.OFFSET_TIME_TYPE, this);
1433     }
1434 
1435     /**
1436      * Defend against malicious streams.
1437      *
1438      * @param s the stream to read
1439      * @throws InvalidObjectException always
1440      */
1441     @java.io.Serial
1442     @SuppressWarnings("serial") // this method is not invoked for value classes
1443     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1444         throw new InvalidObjectException("Deserialization via serialization delegate");
1445     }
1446 
1447     void writeExternal(ObjectOutput out) throws IOException {
1448         time.writeExternal(out);
1449         offset.writeExternal(out);
1450     }
1451 
1452     static OffsetTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
1453         LocalTime time = LocalTime.readExternal(in);
1454         ZoneOffset offset = ZoneOffset.readExternal(in);
1455         return OffsetTime.of(time, offset);
1456     }
1457 
1458 }
< prev index next >