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 /**
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 }
|
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, mutexes, 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, mutexes, 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 @jdk.internal.MigratedValueClass
152 public final class LocalDate
153 implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable {
154
155 /**
156 * For backward compatibility of the serialized {@code LocalDate.class} object,
157 * explicitly declare the types of the serialized fields as defined in Java SE 8.
158 * Instances of {@code LocalDate} are serialized using the dedicated
159 * serialized form by {@code writeReplace}.
160 * @serialField year int The year.
161 * @serialField month short The month-of-year.
162 * @serialField day short The day-of-month.
163 */
164 @Serial
165 private static final ObjectStreamField[] serialPersistentFields = {
166 new ObjectStreamField("year", int.class),
167 new ObjectStreamField("month", short.class),
168 new ObjectStreamField("day", short.class)
169 };
170
171 /**
2186 * out.writeByte(3); // identifies a LocalDate
2187 * out.writeInt(year);
2188 * out.writeByte(month);
2189 * out.writeByte(day);
2190 * </pre>
2191 *
2192 * @return the instance of {@code Ser}, not null
2193 */
2194 @java.io.Serial
2195 private Object writeReplace() {
2196 return new Ser(Ser.LOCAL_DATE_TYPE, this);
2197 }
2198
2199 /**
2200 * Defend against malicious streams.
2201 *
2202 * @param s the stream to read
2203 * @throws InvalidObjectException always
2204 */
2205 @java.io.Serial
2206 @SuppressWarnings("serial") // this method is not invoked for value classes
2207 private void readObject(ObjectInputStream s) throws InvalidObjectException {
2208 throw new InvalidObjectException("Deserialization via serialization delegate");
2209 }
2210
2211 void writeExternal(DataOutput out) throws IOException {
2212 out.writeInt(year);
2213 out.writeByte(month);
2214 out.writeByte(day);
2215 }
2216
2217 static LocalDate readExternal(DataInput in) throws IOException {
2218 int year = in.readInt();
2219 int month = in.readByte();
2220 int dayOfMonth = in.readByte();
2221 return LocalDate.of(year, month, dayOfMonth);
2222 }
2223
2224 }
|