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
100 * This class does not store or represent a year, time or time-zone.
101 * For example, the value "December 3rd" can be stored in a {@code MonthDay}.
102 * <p>
103 * Since a {@code MonthDay} does not possess a year, the leap day of
104 * February 29th is considered valid.
105 * <p>
106 * This class implements {@link TemporalAccessor} rather than {@link Temporal}.
107 * This is because it is not possible to define whether February 29th is valid or not
108 * without external information, preventing the implementation of plus/minus.
109 * Related to this, {@code MonthDay} only provides access to query and set the fields
110 * {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH}.
111 * <p>
112 * The ISO-8601 calendar system is the modern civil calendar system used today
113 * in most of the world. It is equivalent to the proleptic Gregorian calendar
114 * system, in which today's rules for leap years are applied for all time.
115 * For most applications written today, the ISO-8601 rules are entirely suitable.
116 * However, any application that makes use of historical dates, and requires them
117 * to be accurate will find the ISO-8601 approach unsuitable.
118 * <p>
119 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
120 * class; programmers should treat instances that are
121 * {@linkplain #equals(Object) equal} as interchangeable and should not
122 * use instances for synchronization, or unpredictable behavior may
123 * occur. For example, in a future release, synchronization may fail.
124 * The {@code equals} method should be used for comparisons.
125 *
126 * @implSpec
127 * This class is immutable and thread-safe.
128 *
129 * @since 1.8
130 */
131 @jdk.internal.ValueBased
132 public final class MonthDay
133 implements TemporalAccessor, TemporalAdjuster, Comparable<MonthDay>, Serializable {
134
135 /**
136 * Serialization version.
137 */
138 @java.io.Serial
139 private static final long serialVersionUID = -939150713474957432L;
140
141 /**
142 * For backward compatibility of the serialized {@code MonthDay.class} object,
143 * explicitly declare the types of the serialized fields as defined in Java SE 8.
144 * Instances of {@code MonthDay} are serialized using the dedicated
145 * serialized form by {@code writeReplace}.
146 * @serialField month int The month-of-year.
147 * @serialField day int The day-of-month.
148 */
149 @Serial
150 private static final ObjectStreamField[] serialPersistentFields = {
151 new ObjectStreamField("month", int.class),
152 new ObjectStreamField("day", int.class)
782 * <pre>
783 * out.writeByte(13); // identifies a MonthDay
784 * out.writeByte(month);
785 * out.writeByte(day);
786 * </pre>
787 *
788 * @return the instance of {@code Ser}, not null
789 */
790 @java.io.Serial
791 private Object writeReplace() {
792 return new Ser(Ser.MONTH_DAY_TYPE, this);
793 }
794
795 /**
796 * Defend against malicious streams.
797 *
798 * @param s the stream to read
799 * @throws InvalidObjectException always
800 */
801 @java.io.Serial
802 private void readObject(ObjectInputStream s) throws InvalidObjectException {
803 throw new InvalidObjectException("Deserialization via serialization delegate");
804 }
805
806 void writeExternal(DataOutput out) throws IOException {
807 out.writeByte(month);
808 out.writeByte(day);
809 }
810
811 static MonthDay readExternal(DataInput in) throws IOException {
812 byte month = in.readByte();
813 byte day = in.readByte();
814 return MonthDay.of(month, day);
815 }
816
817 }
|
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
100 * This class does not store or represent a year, time or time-zone.
101 * For example, the value "December 3rd" can be stored in a {@code MonthDay}.
102 * <p>
103 * Since a {@code MonthDay} does not possess a year, the leap day of
104 * February 29th is considered valid.
105 * <p>
106 * This class implements {@link TemporalAccessor} rather than {@link Temporal}.
107 * This is because it is not possible to define whether February 29th is valid or not
108 * without external information, preventing the implementation of plus/minus.
109 * Related to this, {@code MonthDay} only provides access to query and set the fields
110 * {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH}.
111 * <p>
112 * The ISO-8601 calendar system is the modern civil calendar system used today
113 * in most of the world. It is equivalent to the proleptic Gregorian calendar
114 * system, in which today's rules for leap years are applied for all time.
115 * For most applications written today, the ISO-8601 rules are entirely suitable.
116 * However, any application that makes use of historical dates, and requires them
117 * to be accurate will find the ISO-8601 approach unsuitable.
118 * <p>
119 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
120 * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
121 * as interchangeable and should not use instances for synchronization or
122 * with {@linkplain java.lang.ref.Reference object references}.
123 *
124 * <div class="preview-block">
125 * <div class="preview-comment">
126 * When preview features are enabled, {@code MonthDay} is a {@linkplain Class#isValue value class}.
127 * Use of value class instances for synchronization or with
128 * {@linkplain java.lang.ref.Reference object references} result in
129 * {@link IdentityException}.
130 * </div>
131 * </div>
132 *
133 * @implSpec
134 * This class is immutable and thread-safe.
135 *
136 * @since 1.8
137 */
138 @jdk.internal.ValueBased
139 public final /*value*/ class MonthDay
140 implements TemporalAccessor, TemporalAdjuster, Comparable<MonthDay>, Serializable {
141
142 /**
143 * Serialization version.
144 */
145 @java.io.Serial
146 private static final long serialVersionUID = -939150713474957432L;
147
148 /**
149 * For backward compatibility of the serialized {@code MonthDay.class} object,
150 * explicitly declare the types of the serialized fields as defined in Java SE 8.
151 * Instances of {@code MonthDay} are serialized using the dedicated
152 * serialized form by {@code writeReplace}.
153 * @serialField month int The month-of-year.
154 * @serialField day int The day-of-month.
155 */
156 @Serial
157 private static final ObjectStreamField[] serialPersistentFields = {
158 new ObjectStreamField("month", int.class),
159 new ObjectStreamField("day", int.class)
789 * <pre>
790 * out.writeByte(13); // identifies a MonthDay
791 * out.writeByte(month);
792 * out.writeByte(day);
793 * </pre>
794 *
795 * @return the instance of {@code Ser}, not null
796 */
797 @java.io.Serial
798 private Object writeReplace() {
799 return new Ser(Ser.MONTH_DAY_TYPE, this);
800 }
801
802 /**
803 * Defend against malicious streams.
804 *
805 * @param s the stream to read
806 * @throws InvalidObjectException always
807 */
808 @java.io.Serial
809 @SuppressWarnings("serial") // this method is not invoked for value classes
810 private void readObject(ObjectInputStream s) throws InvalidObjectException {
811 throw new InvalidObjectException("Deserialization via serialization delegate");
812 }
813
814 void writeExternal(DataOutput out) throws IOException {
815 out.writeByte(month);
816 out.writeByte(day);
817 }
818
819 static MonthDay readExternal(DataInput in) throws IOException {
820 byte month = in.readByte();
821 byte day = in.readByte();
822 return MonthDay.of(month, day);
823 }
824
825 }
|