< prev index next >

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

Print this page

   1 /*
   2  * Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any

  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 
 141     /**

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 }

   1 /*
   2  * Copyright (c) 2012, 2026, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any

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

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