< prev index next >

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

 104 import jdk.internal.util.DecimalDigits;
 105 
 106 /**
 107  * A year-month in the ISO-8601 calendar system, such as {@code 2007-12}.
 108  * <p>
 109  * {@code YearMonth} is an immutable date-time object that represents the combination
 110  * of a year and month. Any field that can be derived from a year and month, such as
 111  * quarter-of-year, can be obtained.
 112  * <p>
 113  * This class does not store or represent a day, time or time-zone.
 114  * For example, the value "October 2007" can be stored in a {@code YearMonth}.
 115  * <p>
 116  * The ISO-8601 calendar system is the modern civil calendar system used today
 117  * in most of the world. It is equivalent to the proleptic Gregorian calendar
 118  * system, in which today's rules for leap years are applied for all time.
 119  * For most applications written today, the ISO-8601 rules are entirely suitable.
 120  * However, any application that makes use of historical dates, and requires them
 121  * to be accurate will find the ISO-8601 approach unsuitable.
 122  * <p>
 123  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 124  * class; programmers should treat instances that are
 125  * {@linkplain #equals(Object) equal} as interchangeable and should not
 126  * use instances for synchronization, or unpredictable behavior may
 127  * occur. For example, in a future release, synchronization may fail.
 128  * The {@code equals} method should be used for comparisons.







 129  *
 130  * @implSpec
 131  * This class is immutable and thread-safe.
 132  *
 133  * @since 1.8
 134  */
 135 @jdk.internal.ValueBased
 136 public final class YearMonth
 137         implements Temporal, TemporalAdjuster, Comparable<YearMonth>, Serializable {
 138 
 139     /**
 140      * Serialization version.
 141      */
 142     @java.io.Serial
 143     private static final long serialVersionUID = 4183400860270640070L;
 144 
 145     /**
 146      * For backward compatibility of the serialized {@code YearMonth.class} object,
 147      * explicitly declare the types of the serialized fields as defined in Java SE 8.
 148      * Instances of {@code YearMonth} are serialized using the dedicated
 149      * serialized form by {@code writeReplace}.
 150      * @serialField year int The year.
 151      * @serialField month int The month-of-year.
 152      */
 153     @java.io.Serial
 154     private static final ObjectStreamField[] serialPersistentFields = {
 155             new ObjectStreamField("year", int.class),
 156             new ObjectStreamField("month", int.class),

1236      * <pre>
1237      *  out.writeByte(12);  // identifies a YearMonth
1238      *  out.writeInt(year);
1239      *  out.writeByte(month);
1240      * </pre>
1241      *
1242      * @return the instance of {@code Ser}, not null
1243      */
1244     @java.io.Serial
1245     private Object writeReplace() {
1246         return new Ser(Ser.YEAR_MONTH_TYPE, this);
1247     }
1248 
1249     /**
1250      * Defend against malicious streams.
1251      *
1252      * @param s the stream to read
1253      * @throws InvalidObjectException always
1254      */
1255     @java.io.Serial

1256     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1257         throw new InvalidObjectException("Deserialization via serialization delegate");
1258     }
1259 
1260     void writeExternal(DataOutput out) throws IOException {
1261         out.writeInt(year);
1262         out.writeByte(month);
1263     }
1264 
1265     static YearMonth readExternal(DataInput in) throws IOException {
1266         int year = in.readInt();
1267         byte month = in.readByte();
1268         return YearMonth.of(year, month);
1269     }
1270 
1271 }

   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

 104 import jdk.internal.util.DecimalDigits;
 105 
 106 /**
 107  * A year-month in the ISO-8601 calendar system, such as {@code 2007-12}.
 108  * <p>
 109  * {@code YearMonth} is an immutable date-time object that represents the combination
 110  * of a year and month. Any field that can be derived from a year and month, such as
 111  * quarter-of-year, can be obtained.
 112  * <p>
 113  * This class does not store or represent a day, time or time-zone.
 114  * For example, the value "October 2007" can be stored in a {@code YearMonth}.
 115  * <p>
 116  * The ISO-8601 calendar system is the modern civil calendar system used today
 117  * in most of the world. It is equivalent to the proleptic Gregorian calendar
 118  * system, in which today's rules for leap years are applied for all time.
 119  * For most applications written today, the ISO-8601 rules are entirely suitable.
 120  * However, any application that makes use of historical dates, and requires them
 121  * to be accurate will find the ISO-8601 approach unsuitable.
 122  * <p>
 123  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 124  * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
 125  * as interchangeable and should not use instances for synchronization or
 126  * with {@linkplain java.lang.ref.Reference object references}.
 127  *
 128  * <div class="preview-block">
 129  *      <div class="preview-comment">
 130  *          When preview features are enabled, {@code YearMonth} is a {@linkplain Class#isValue value class}.
 131  *          Use of value class instances for synchronization or with
 132  *          {@linkplain java.lang.ref.Reference object references} result in
 133  *          {@link IdentityException}.
 134  *      </div>
 135  * </div>
 136  *
 137  * @implSpec
 138  * This class is immutable and thread-safe.
 139  *
 140  * @since 1.8
 141  */
 142 @jdk.internal.ValueBased
 143 public final /*value*/ class YearMonth
 144         implements Temporal, TemporalAdjuster, Comparable<YearMonth>, Serializable {
 145 
 146     /**
 147      * Serialization version.
 148      */
 149     @java.io.Serial
 150     private static final long serialVersionUID = 4183400860270640070L;
 151 
 152     /**
 153      * For backward compatibility of the serialized {@code YearMonth.class} object,
 154      * explicitly declare the types of the serialized fields as defined in Java SE 8.
 155      * Instances of {@code YearMonth} are serialized using the dedicated
 156      * serialized form by {@code writeReplace}.
 157      * @serialField year int The year.
 158      * @serialField month int The month-of-year.
 159      */
 160     @java.io.Serial
 161     private static final ObjectStreamField[] serialPersistentFields = {
 162             new ObjectStreamField("year", int.class),
 163             new ObjectStreamField("month", int.class),

1243      * <pre>
1244      *  out.writeByte(12);  // identifies a YearMonth
1245      *  out.writeInt(year);
1246      *  out.writeByte(month);
1247      * </pre>
1248      *
1249      * @return the instance of {@code Ser}, not null
1250      */
1251     @java.io.Serial
1252     private Object writeReplace() {
1253         return new Ser(Ser.YEAR_MONTH_TYPE, this);
1254     }
1255 
1256     /**
1257      * Defend against malicious streams.
1258      *
1259      * @param s the stream to read
1260      * @throws InvalidObjectException always
1261      */
1262     @java.io.Serial
1263     @SuppressWarnings("serial") // this method is not invoked for value classes
1264     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1265         throw new InvalidObjectException("Deserialization via serialization delegate");
1266     }
1267 
1268     void writeExternal(DataOutput out) throws IOException {
1269         out.writeInt(year);
1270         out.writeByte(month);
1271     }
1272 
1273     static YearMonth readExternal(DataInput in) throws IOException {
1274         int year = in.readInt();
1275         byte month = in.readByte();
1276         return YearMonth.of(year, month);
1277     }
1278 
1279 }
< prev index next >