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 */
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);
|
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, mutexes, 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, mutexes, 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 @jdk.internal.MigratedValueClass
136 public final class LocalTime
137 implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable {
138
139 /**
140 * The minimum supported {@code LocalTime}, '00:00'.
141 * This is the time of midnight at the start of the day.
142 */
143 public static final LocalTime MIN;
144 /**
145 * The maximum supported {@code LocalTime}, '23:59:59.999999999'.
146 * This is the time just before midnight at the end of the day.
147 */
148 public static final LocalTime MAX;
149 /**
150 * The time of midnight at the start of the day, '00:00'.
151 */
152 public static final LocalTime MIDNIGHT;
153 /**
154 * The time of noon in the middle of the day, '12:00'.
155 */
1671 * out.writeByte(minute);
1672 * out.writeByte(second);
1673 * out.writeInt(nano);
1674 * }
1675 * </pre>
1676 *
1677 * @return the instance of {@code Ser}, not null
1678 */
1679 @java.io.Serial
1680 private Object writeReplace() {
1681 return new Ser(Ser.LOCAL_TIME_TYPE, this);
1682 }
1683
1684 /**
1685 * Defend against malicious streams.
1686 *
1687 * @param s the stream to read
1688 * @throws InvalidObjectException always
1689 */
1690 @java.io.Serial
1691 @SuppressWarnings("serial") // this method is not invoked for value classes
1692 private void readObject(ObjectInputStream s) throws InvalidObjectException {
1693 throw new InvalidObjectException("Deserialization via serialization delegate");
1694 }
1695
1696 void writeExternal(DataOutput out) throws IOException {
1697 if (nano == 0) {
1698 if (second == 0) {
1699 if (minute == 0) {
1700 out.writeByte(~hour);
1701 } else {
1702 out.writeByte(hour);
1703 out.writeByte(~minute);
1704 }
1705 } else {
1706 out.writeByte(hour);
1707 out.writeByte(minute);
1708 out.writeByte(~second);
1709 }
1710 } else {
1711 out.writeByte(hour);
|