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
112 * such as {@code 2007-12-03}.
113 * <p>
114 * {@code LocalDate} is an immutable date-time object that represents a date,
115 * often viewed as year-month-day. Other date fields, such as day-of-year,
116 * day-of-week and week-of-year, can also be accessed.
117 * For example, the value "2nd October 2007" can be stored in a {@code LocalDate}.
118 * <p>
119 * This class does not store or represent a time or time-zone.
120 * Instead, it is a description of the date, as used for birthdays.
121 * It cannot represent an instant on the time-line without additional information
122 * such as an offset or time-zone.
123 * <p>
124 * The ISO-8601 calendar system is the modern civil calendar system used today
125 * in most of the world. It is equivalent to the proleptic Gregorian calendar
126 * system, in which today's rules for leap years are applied for all time.
127 * For most applications written today, the ISO-8601 rules are entirely suitable.
128 * However, any application that makes use of historical dates, and requires them
129 * to be accurate will find the ISO-8601 approach unsuitable.
130 * <p>
131 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
132 * class; programmers should treat instances that are
133 * {@linkplain #equals(Object) equal} as interchangeable and should not
134 * use instances for synchronization, or unpredictable behavior may
135 * occur. For example, in a future release, synchronization may fail.
136 * The {@code equals} method should be used for comparisons.
137 *
138 * @implSpec
139 * This class is immutable and thread-safe.
140 *
141 * @since 1.8
142 */
143 @jdk.internal.ValueBased
144 public final class LocalDate
145 implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable {
146
147 /**
148 * For backward compatibility of the serialized {@code LocalDate.class} object,
149 * explicitly declare the types of the serialized fields as defined in Java SE 8.
150 * Instances of {@code LocalDate} are serialized using the dedicated
151 * serialized form by {@code writeReplace}.
152 * @serialField year int The year.
153 * @serialField month short The month-of-year.
154 * @serialField day short The day-of-month.
155 */
156 @Serial
157 private static final ObjectStreamField[] serialPersistentFields = {
158 new ObjectStreamField("year", int.class),
159 new ObjectStreamField("month", short.class),
160 new ObjectStreamField("day", short.class)
161 };
162
163 /**
164 * The minimum supported {@code LocalDate}, '-999999999-01-01'.
2178 * out.writeByte(3); // identifies a LocalDate
2179 * out.writeInt(year);
2180 * out.writeByte(month);
2181 * out.writeByte(day);
2182 * </pre>
2183 *
2184 * @return the instance of {@code Ser}, not null
2185 */
2186 @java.io.Serial
2187 private Object writeReplace() {
2188 return new Ser(Ser.LOCAL_DATE_TYPE, this);
2189 }
2190
2191 /**
2192 * Defend against malicious streams.
2193 *
2194 * @param s the stream to read
2195 * @throws InvalidObjectException always
2196 */
2197 @java.io.Serial
2198 private void readObject(ObjectInputStream s) throws InvalidObjectException {
2199 throw new InvalidObjectException("Deserialization via serialization delegate");
2200 }
2201
2202 void writeExternal(DataOutput out) throws IOException {
2203 out.writeInt(year);
2204 out.writeByte(month);
2205 out.writeByte(day);
2206 }
2207
2208 static LocalDate readExternal(DataInput in) throws IOException {
2209 int year = in.readInt();
2210 int month = in.readByte();
2211 int dayOfMonth = in.readByte();
2212 return LocalDate.of(year, month, dayOfMonth);
2213 }
2214
2215 }
|
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
112 * such as {@code 2007-12-03}.
113 * <p>
114 * {@code LocalDate} is an immutable date-time object that represents a date,
115 * often viewed as year-month-day. Other date fields, such as day-of-year,
116 * day-of-week and week-of-year, can also be accessed.
117 * For example, the value "2nd October 2007" can be stored in a {@code LocalDate}.
118 * <p>
119 * This class does not store or represent a time or time-zone.
120 * Instead, it is a description of the date, as used for birthdays.
121 * It cannot represent an instant on the time-line without additional information
122 * such as an offset or time-zone.
123 * <p>
124 * The ISO-8601 calendar system is the modern civil calendar system used today
125 * in most of the world. It is equivalent to the proleptic Gregorian calendar
126 * system, in which today's rules for leap years are applied for all time.
127 * For most applications written today, the ISO-8601 rules are entirely suitable.
128 * However, any application that makes use of historical dates, and requires them
129 * to be accurate will find the ISO-8601 approach unsuitable.
130 * <p>
131 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
132 * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
133 * as interchangeable and should not use instances for synchronization or
134 * with {@linkplain java.lang.ref.Reference object references}.
135 *
136 * <div class="preview-block">
137 * <div class="preview-comment">
138 * When preview features are enabled, {@code LocalDate} is a {@linkplain Class#isValue value class}.
139 * Use of value class instances for synchronization or with
140 * {@linkplain java.lang.ref.Reference object references} result in
141 * {@link IdentityException}.
142 * </div>
143 * </div>
144 *
145 * @implSpec
146 * This class is immutable and thread-safe.
147 *
148 * @since 1.8
149 */
150 @jdk.internal.ValueBased
151 public final /*value*/ class LocalDate
152 implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable {
153
154 /**
155 * For backward compatibility of the serialized {@code LocalDate.class} object,
156 * explicitly declare the types of the serialized fields as defined in Java SE 8.
157 * Instances of {@code LocalDate} are serialized using the dedicated
158 * serialized form by {@code writeReplace}.
159 * @serialField year int The year.
160 * @serialField month short The month-of-year.
161 * @serialField day short The day-of-month.
162 */
163 @Serial
164 private static final ObjectStreamField[] serialPersistentFields = {
165 new ObjectStreamField("year", int.class),
166 new ObjectStreamField("month", short.class),
167 new ObjectStreamField("day", short.class)
168 };
169
170 /**
171 * The minimum supported {@code LocalDate}, '-999999999-01-01'.
2185 * out.writeByte(3); // identifies a LocalDate
2186 * out.writeInt(year);
2187 * out.writeByte(month);
2188 * out.writeByte(day);
2189 * </pre>
2190 *
2191 * @return the instance of {@code Ser}, not null
2192 */
2193 @java.io.Serial
2194 private Object writeReplace() {
2195 return new Ser(Ser.LOCAL_DATE_TYPE, this);
2196 }
2197
2198 /**
2199 * Defend against malicious streams.
2200 *
2201 * @param s the stream to read
2202 * @throws InvalidObjectException always
2203 */
2204 @java.io.Serial
2205 @SuppressWarnings("serial") // this method is not invoked for value classes
2206 private void readObject(ObjectInputStream s) throws InvalidObjectException {
2207 throw new InvalidObjectException("Deserialization via serialization delegate");
2208 }
2209
2210 void writeExternal(DataOutput out) throws IOException {
2211 out.writeInt(year);
2212 out.writeByte(month);
2213 out.writeByte(day);
2214 }
2215
2216 static LocalDate readExternal(DataInput in) throws IOException {
2217 int year = in.readInt();
2218 int month = in.readByte();
2219 int dayOfMonth = in.readByte();
2220 return LocalDate.of(year, month, dayOfMonth);
2221 }
2222
2223 }
|