< prev index next >

src/java.base/share/classes/java/time/LocalTime.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

  96 
  97 /**
  98  * A time without a time-zone in the ISO-8601 calendar system,
  99  * such as {@code 10:15:30}.
 100  * <p>
 101  * {@code LocalTime} is an immutable date-time object that represents a time,
 102  * often viewed as hour-minute-second.
 103  * Time is represented to nanosecond precision.
 104  * For example, the value "13:45.30.123456789" can be stored in a {@code LocalTime}.
 105  * <p>
 106  * This class does not store or represent a date or time-zone.
 107  * Instead, it is a description of the local time as seen on a wall clock.
 108  * It cannot represent an instant on the time-line without additional information
 109  * such as an offset or time-zone.
 110  * <p>
 111  * The ISO-8601 calendar system is the modern civil calendar system used today
 112  * in most of the world. This API assumes that all calendar systems use the same
 113  * representation, this class, for time-of-day.
 114  * <p>
 115  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 116  * class; programmers should treat instances that are
 117  * {@linkplain #equals(Object) equal} as interchangeable and should not
 118  * use instances for synchronization, or unpredictable behavior may
 119  * occur. For example, in a future release, synchronization may fail.
 120  * The {@code equals} method should be used for comparisons.







 121  *
 122  * @implSpec
 123  * This class is immutable and thread-safe.
 124  *
 125  * @since 1.8
 126  */
 127 @jdk.internal.ValueBased
 128 public final class LocalTime
 129         implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable {
 130 
 131     /**
 132      * The minimum supported {@code LocalTime}, '00:00'.
 133      * This is the time of midnight at the start of the day.
 134      */
 135     public static final LocalTime MIN;
 136     /**
 137      * The maximum supported {@code LocalTime}, '23:59:59.999999999'.
 138      * This is the time just before midnight at the end of the day.
 139      */
 140     public static final LocalTime MAX;
 141     /**
 142      * The time of midnight at the start of the day, '00:00'.
 143      */
 144     public static final LocalTime MIDNIGHT;
 145     /**
 146      * The time of noon in the middle of the day, '12:00'.
 147      */
 148     public static final LocalTime NOON;

1663      *    out.writeByte(minute);
1664      *    out.writeByte(second);
1665      *    out.writeInt(nano);
1666      *  }
1667      * </pre>
1668      *
1669      * @return the instance of {@code Ser}, not null
1670      */
1671     @java.io.Serial
1672     private Object writeReplace() {
1673         return new Ser(Ser.LOCAL_TIME_TYPE, this);
1674     }
1675 
1676     /**
1677      * Defend against malicious streams.
1678      *
1679      * @param s the stream to read
1680      * @throws InvalidObjectException always
1681      */
1682     @java.io.Serial

1683     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1684         throw new InvalidObjectException("Deserialization via serialization delegate");
1685     }
1686 
1687     void writeExternal(DataOutput out) throws IOException {
1688         if (nano == 0) {
1689             if (second == 0) {
1690                 if (minute == 0) {
1691                     out.writeByte(~hour);
1692                 } else {
1693                     out.writeByte(hour);
1694                     out.writeByte(~minute);
1695                 }
1696             } else {
1697                 out.writeByte(hour);
1698                 out.writeByte(minute);
1699                 out.writeByte(~second);
1700             }
1701         } else {
1702             out.writeByte(hour);

   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

  96 
  97 /**
  98  * A time without a time-zone in the ISO-8601 calendar system,
  99  * such as {@code 10:15:30}.
 100  * <p>
 101  * {@code LocalTime} is an immutable date-time object that represents a time,
 102  * often viewed as hour-minute-second.
 103  * Time is represented to nanosecond precision.
 104  * For example, the value "13:45.30.123456789" can be stored in a {@code LocalTime}.
 105  * <p>
 106  * This class does not store or represent a date or time-zone.
 107  * Instead, it is a description of the local time as seen on a wall clock.
 108  * It cannot represent an instant on the time-line without additional information
 109  * such as an offset or time-zone.
 110  * <p>
 111  * The ISO-8601 calendar system is the modern civil calendar system used today
 112  * in most of the world. This API assumes that all calendar systems use the same
 113  * representation, this class, for time-of-day.
 114  * <p>
 115  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 116  * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
 117  * as interchangeable and should not use instances for synchronization or
 118  * with {@linkplain java.lang.ref.Reference object references}.
 119  *
 120  * <div class="preview-block">
 121  *      <div class="preview-comment">
 122  *          When preview features are enabled, {@code LocalTime} is a {@linkplain Class#isValue value class}.
 123  *          Use of value class instances for synchronization or with
 124  *          {@linkplain java.lang.ref.Reference object references} result in
 125  *          {@link IdentityException}.
 126  *      </div>
 127  * </div>
 128  *
 129  * @implSpec
 130  * This class is immutable and thread-safe.
 131  *
 132  * @since 1.8
 133  */
 134 @jdk.internal.ValueBased
 135 public final /*value*/ class LocalTime
 136         implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable {
 137 
 138     /**
 139      * The minimum supported {@code LocalTime}, '00:00'.
 140      * This is the time of midnight at the start of the day.
 141      */
 142     public static final LocalTime MIN;
 143     /**
 144      * The maximum supported {@code LocalTime}, '23:59:59.999999999'.
 145      * This is the time just before midnight at the end of the day.
 146      */
 147     public static final LocalTime MAX;
 148     /**
 149      * The time of midnight at the start of the day, '00:00'.
 150      */
 151     public static final LocalTime MIDNIGHT;
 152     /**
 153      * The time of noon in the middle of the day, '12:00'.
 154      */
 155     public static final LocalTime NOON;

1670      *    out.writeByte(minute);
1671      *    out.writeByte(second);
1672      *    out.writeInt(nano);
1673      *  }
1674      * </pre>
1675      *
1676      * @return the instance of {@code Ser}, not null
1677      */
1678     @java.io.Serial
1679     private Object writeReplace() {
1680         return new Ser(Ser.LOCAL_TIME_TYPE, this);
1681     }
1682 
1683     /**
1684      * Defend against malicious streams.
1685      *
1686      * @param s the stream to read
1687      * @throws InvalidObjectException always
1688      */
1689     @java.io.Serial
1690     @SuppressWarnings("serial") // this method is not invoked for value classes
1691     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1692         throw new InvalidObjectException("Deserialization via serialization delegate");
1693     }
1694 
1695     void writeExternal(DataOutput out) throws IOException {
1696         if (nano == 0) {
1697             if (second == 0) {
1698                 if (minute == 0) {
1699                     out.writeByte(~hour);
1700                 } else {
1701                     out.writeByte(hour);
1702                     out.writeByte(~minute);
1703                 }
1704             } else {
1705                 out.writeByte(hour);
1706                 out.writeByte(minute);
1707                 out.writeByte(~second);
1708             }
1709         } else {
1710             out.writeByte(hour);
< prev index next >