< prev index next >

src/java.base/share/classes/java/time/Year.java

Print this page




 130  * @implSpec
 131  * This class is immutable and thread-safe.
 132  *
 133  * @since 1.8
 134  */
 135 public final class Year
 136         implements Temporal, TemporalAdjuster, Comparable<Year>, Serializable {
 137 
 138     /**
 139      * The minimum supported year, '-999,999,999'.
 140      */
 141     public static final int MIN_VALUE = -999_999_999;
 142     /**
 143      * The maximum supported year, '+999,999,999'.
 144      */
 145     public static final int MAX_VALUE = 999_999_999;
 146 
 147     /**
 148      * Serialization version.
 149      */

 150     private static final long serialVersionUID = -23038383694477807L;
 151     /**
 152      * Parser.
 153      */
 154     private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder()
 155         .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
 156         .toFormatter();
 157 
 158     /**
 159      * The year being represented.
 160      */
 161     private final int year;
 162 
 163     //-----------------------------------------------------------------------
 164     /**
 165      * Obtains the current year from the system clock in the default time-zone.
 166      * <p>
 167      * This will query the {@link Clock#systemDefaultZone() system clock} in the default
 168      * time-zone to obtain the current year.
 169      * <p>


1080      *
1081      * @return a string representation of this year, not null
1082      */
1083     @Override
1084     public String toString() {
1085         return Integer.toString(year);
1086     }
1087 
1088     //-----------------------------------------------------------------------
1089     /**
1090      * Writes the object using a
1091      * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1092      * @serialData
1093      * <pre>
1094      *  out.writeByte(11);  // identifies a Year
1095      *  out.writeInt(year);
1096      * </pre>
1097      *
1098      * @return the instance of {@code Ser}, not null
1099      */

1100     private Object writeReplace() {
1101         return new Ser(Ser.YEAR_TYPE, this);
1102     }
1103 
1104     /**
1105      * Defend against malicious streams.
1106      *
1107      * @param s the stream to read
1108      * @throws InvalidObjectException always
1109      */

1110     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1111         throw new InvalidObjectException("Deserialization via serialization delegate");
1112     }
1113 
1114     void writeExternal(DataOutput out) throws IOException {
1115         out.writeInt(year);
1116     }
1117 
1118     static Year readExternal(DataInput in) throws IOException {
1119         return Year.of(in.readInt());
1120     }
1121 
1122 }


 130  * @implSpec
 131  * This class is immutable and thread-safe.
 132  *
 133  * @since 1.8
 134  */
 135 public final class Year
 136         implements Temporal, TemporalAdjuster, Comparable<Year>, Serializable {
 137 
 138     /**
 139      * The minimum supported year, '-999,999,999'.
 140      */
 141     public static final int MIN_VALUE = -999_999_999;
 142     /**
 143      * The maximum supported year, '+999,999,999'.
 144      */
 145     public static final int MAX_VALUE = 999_999_999;
 146 
 147     /**
 148      * Serialization version.
 149      */
 150     @java.io.Serial
 151     private static final long serialVersionUID = -23038383694477807L;
 152     /**
 153      * Parser.
 154      */
 155     private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder()
 156         .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
 157         .toFormatter();
 158 
 159     /**
 160      * The year being represented.
 161      */
 162     private final int year;
 163 
 164     //-----------------------------------------------------------------------
 165     /**
 166      * Obtains the current year from the system clock in the default time-zone.
 167      * <p>
 168      * This will query the {@link Clock#systemDefaultZone() system clock} in the default
 169      * time-zone to obtain the current year.
 170      * <p>


1081      *
1082      * @return a string representation of this year, not null
1083      */
1084     @Override
1085     public String toString() {
1086         return Integer.toString(year);
1087     }
1088 
1089     //-----------------------------------------------------------------------
1090     /**
1091      * Writes the object using a
1092      * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1093      * @serialData
1094      * <pre>
1095      *  out.writeByte(11);  // identifies a Year
1096      *  out.writeInt(year);
1097      * </pre>
1098      *
1099      * @return the instance of {@code Ser}, not null
1100      */
1101     @java.io.Serial
1102     private Object writeReplace() {
1103         return new Ser(Ser.YEAR_TYPE, this);
1104     }
1105 
1106     /**
1107      * Defend against malicious streams.
1108      *
1109      * @param s the stream to read
1110      * @throws InvalidObjectException always
1111      */
1112     @java.io.Serial
1113     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1114         throw new InvalidObjectException("Deserialization via serialization delegate");
1115     }
1116 
1117     void writeExternal(DataOutput out) throws IOException {
1118         out.writeInt(year);
1119     }
1120 
1121     static Year readExternal(DataInput in) throws IOException {
1122         return Year.of(in.readInt());
1123     }
1124 
1125 }
< prev index next >