< prev index next >

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

Print this page




 117  * to be accurate will find the ISO-8601 approach unsuitable.
 118  *
 119  * <p>
 120  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 121  * class; use of identity-sensitive operations (including reference equality
 122  * ({@code ==}), identity hash code, or synchronization) on instances of
 123  * {@code YearMonth} may have unpredictable results and should be avoided.
 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 public final class YearMonth
 132         implements Temporal, TemporalAdjuster, Comparable<YearMonth>, Serializable {
 133 
 134     /**
 135      * Serialization version.
 136      */

 137     private static final long serialVersionUID = 4183400860270640070L;
 138     /**
 139      * Parser.
 140      */
 141     private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder()
 142         .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
 143         .appendLiteral('-')
 144         .appendValue(MONTH_OF_YEAR, 2)
 145         .toFormatter();
 146 
 147     /**
 148      * The year.
 149      */
 150     private final int year;
 151     /**
 152      * The month-of-year, not null.
 153      */
 154     private final int month;
 155 
 156     //-----------------------------------------------------------------------


1205             buf.append(year);
1206         }
1207         return buf.append(month < 10 ? "-0" : "-")
1208             .append(month)
1209             .toString();
1210     }
1211 
1212     //-----------------------------------------------------------------------
1213     /**
1214      * Writes the object using a
1215      * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1216      * @serialData
1217      * <pre>
1218      *  out.writeByte(12);  // identifies a YearMonth
1219      *  out.writeInt(year);
1220      *  out.writeByte(month);
1221      * </pre>
1222      *
1223      * @return the instance of {@code Ser}, not null
1224      */

1225     private Object writeReplace() {
1226         return new Ser(Ser.YEAR_MONTH_TYPE, this);
1227     }
1228 
1229     /**
1230      * Defend against malicious streams.
1231      *
1232      * @param s the stream to read
1233      * @throws InvalidObjectException always
1234      */

1235     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1236         throw new InvalidObjectException("Deserialization via serialization delegate");
1237     }
1238 
1239     void writeExternal(DataOutput out) throws IOException {
1240         out.writeInt(year);
1241         out.writeByte(month);
1242     }
1243 
1244     static YearMonth readExternal(DataInput in) throws IOException {
1245         int year = in.readInt();
1246         byte month = in.readByte();
1247         return YearMonth.of(year, month);
1248     }
1249 
1250 }


 117  * to be accurate will find the ISO-8601 approach unsuitable.
 118  *
 119  * <p>
 120  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 121  * class; use of identity-sensitive operations (including reference equality
 122  * ({@code ==}), identity hash code, or synchronization) on instances of
 123  * {@code YearMonth} may have unpredictable results and should be avoided.
 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 public final class YearMonth
 132         implements Temporal, TemporalAdjuster, Comparable<YearMonth>, Serializable {
 133 
 134     /**
 135      * Serialization version.
 136      */
 137     @java.io.Serial
 138     private static final long serialVersionUID = 4183400860270640070L;
 139     /**
 140      * Parser.
 141      */
 142     private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder()
 143         .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
 144         .appendLiteral('-')
 145         .appendValue(MONTH_OF_YEAR, 2)
 146         .toFormatter();
 147 
 148     /**
 149      * The year.
 150      */
 151     private final int year;
 152     /**
 153      * The month-of-year, not null.
 154      */
 155     private final int month;
 156 
 157     //-----------------------------------------------------------------------


1206             buf.append(year);
1207         }
1208         return buf.append(month < 10 ? "-0" : "-")
1209             .append(month)
1210             .toString();
1211     }
1212 
1213     //-----------------------------------------------------------------------
1214     /**
1215      * Writes the object using a
1216      * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1217      * @serialData
1218      * <pre>
1219      *  out.writeByte(12);  // identifies a YearMonth
1220      *  out.writeInt(year);
1221      *  out.writeByte(month);
1222      * </pre>
1223      *
1224      * @return the instance of {@code Ser}, not null
1225      */
1226     @java.io.Serial
1227     private Object writeReplace() {
1228         return new Ser(Ser.YEAR_MONTH_TYPE, this);
1229     }
1230 
1231     /**
1232      * Defend against malicious streams.
1233      *
1234      * @param s the stream to read
1235      * @throws InvalidObjectException always
1236      */
1237     @java.io.Serial
1238     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1239         throw new InvalidObjectException("Deserialization via serialization delegate");
1240     }
1241 
1242     void writeExternal(DataOutput out) throws IOException {
1243         out.writeInt(year);
1244         out.writeByte(month);
1245     }
1246 
1247     static YearMonth readExternal(DataInput in) throws IOException {
1248         int year = in.readInt();
1249         byte month = in.readByte();
1250         return YearMonth.of(year, month);
1251     }
1252 
1253 }
< prev index next >