< prev index next >

src/java.base/share/classes/java/time/Year.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  * <p>
 105  * <b>Note that years in the ISO chronology only align with years in the
 106  * Gregorian-Julian system for modern years. Parts of Russia did not switch to the
 107  * modern Gregorian/ISO rules until 1920.
 108  * As such, historical years must be treated with caution.</b>
 109  * <p>
 110  * This class does not store or represent a month, day, time or time-zone.
 111  * For example, the value "2007" can be stored in a {@code Year}.
 112  * <p>
 113  * Years represented by this class follow the ISO-8601 standard and use
 114  * the proleptic numbering system. Year 1 is preceded by year 0, then by year -1.
 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 Year
 137         implements Temporal, TemporalAdjuster, Comparable<Year>, Serializable {
 138 
 139     /**
 140      * The minimum supported year, '-999,999,999'.
 141      */
 142     public static final int MIN_VALUE = -999_999_999;
 143     /**
 144      * The maximum supported year, '+999,999,999'.
 145      */
 146     public static final int MAX_VALUE = 999_999_999;
 147 
 148     /**
 149      * Serialization version.
 150      */
 151     @java.io.Serial
 152     private static final long serialVersionUID = -23038383694477807L;
 153     /**
 154      * Parser.
 155      */
 156     private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder()

1099      * @serialData
1100      * <pre>
1101      *  out.writeByte(11);  // identifies a Year
1102      *  out.writeInt(year);
1103      * </pre>
1104      *
1105      * @return the instance of {@code Ser}, not null
1106      */
1107     @java.io.Serial
1108     private Object writeReplace() {
1109         return new Ser(Ser.YEAR_TYPE, this);
1110     }
1111 
1112     /**
1113      * Defend against malicious streams.
1114      *
1115      * @param s the stream to read
1116      * @throws InvalidObjectException always
1117      */
1118     @java.io.Serial

1119     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1120         throw new InvalidObjectException("Deserialization via serialization delegate");
1121     }
1122 
1123     void writeExternal(DataOutput out) throws IOException {
1124         out.writeInt(year);
1125     }
1126 
1127     static Year readExternal(DataInput in) throws IOException {
1128         return Year.of(in.readInt());
1129     }
1130 
1131 }

   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  * <p>
 105  * <b>Note that years in the ISO chronology only align with years in the
 106  * Gregorian-Julian system for modern years. Parts of Russia did not switch to the
 107  * modern Gregorian/ISO rules until 1920.
 108  * As such, historical years must be treated with caution.</b>
 109  * <p>
 110  * This class does not store or represent a month, day, time or time-zone.
 111  * For example, the value "2007" can be stored in a {@code Year}.
 112  * <p>
 113  * Years represented by this class follow the ISO-8601 standard and use
 114  * the proleptic numbering system. Year 1 is preceded by year 0, then by year -1.
 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 Year} 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 Year
 144         implements Temporal, TemporalAdjuster, Comparable<Year>, Serializable {
 145 
 146     /**
 147      * The minimum supported year, '-999,999,999'.
 148      */
 149     public static final int MIN_VALUE = -999_999_999;
 150     /**
 151      * The maximum supported year, '+999,999,999'.
 152      */
 153     public static final int MAX_VALUE = 999_999_999;
 154 
 155     /**
 156      * Serialization version.
 157      */
 158     @java.io.Serial
 159     private static final long serialVersionUID = -23038383694477807L;
 160     /**
 161      * Parser.
 162      */
 163     private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder()

1106      * @serialData
1107      * <pre>
1108      *  out.writeByte(11);  // identifies a Year
1109      *  out.writeInt(year);
1110      * </pre>
1111      *
1112      * @return the instance of {@code Ser}, not null
1113      */
1114     @java.io.Serial
1115     private Object writeReplace() {
1116         return new Ser(Ser.YEAR_TYPE, this);
1117     }
1118 
1119     /**
1120      * Defend against malicious streams.
1121      *
1122      * @param s the stream to read
1123      * @throws InvalidObjectException always
1124      */
1125     @java.io.Serial
1126     @SuppressWarnings("serial") // this method is not invoked for value classes
1127     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1128         throw new InvalidObjectException("Deserialization via serialization delegate");
1129     }
1130 
1131     void writeExternal(DataOutput out) throws IOException {
1132         out.writeInt(year);
1133     }
1134 
1135     static Year readExternal(DataInput in) throws IOException {
1136         return Year.of(in.readInt());
1137     }
1138 
1139 }
< prev index next >