< prev index next >

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

Print this page




 113  * to be accurate will find the ISO-8601 approach unsuitable.
 114  *
 115  * <p>
 116  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 117  * class; use of identity-sensitive operations (including reference equality
 118  * ({@code ==}), identity hash code, or synchronization) on instances of
 119  * {@code MonthDay} may have unpredictable results and should be avoided.
 120  * The {@code equals} method should be used for comparisons.
 121  *
 122  * @implSpec
 123  * This class is immutable and thread-safe.
 124  *
 125  * @since 1.8
 126  */
 127 public final class MonthDay
 128         implements TemporalAccessor, TemporalAdjuster, Comparable<MonthDay>, Serializable {
 129 
 130     /**
 131      * Serialization version.
 132      */

 133     private static final long serialVersionUID = -939150713474957432L;
 134     /**
 135      * Parser.
 136      */
 137     private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder()
 138         .appendLiteral("--")
 139         .appendValue(MONTH_OF_YEAR, 2)
 140         .appendLiteral('-')
 141         .appendValue(DAY_OF_MONTH, 2)
 142         .toFormatter();
 143 
 144     /**
 145      * The month-of-year, not null.
 146      */
 147     private final int month;
 148     /**
 149      * The day-of-month.
 150      */
 151     private final int day;
 152 


 747     public String toString() {
 748         return new StringBuilder(10).append("--")
 749             .append(month < 10 ? "0" : "").append(month)
 750             .append(day < 10 ? "-0" : "-").append(day)
 751             .toString();
 752     }
 753 
 754     //-----------------------------------------------------------------------
 755     /**
 756      * Writes the object using a
 757      * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
 758      * @serialData
 759      * <pre>
 760      *  out.writeByte(13);  // identifies a MonthDay
 761      *  out.writeByte(month);
 762      *  out.writeByte(day);
 763      * </pre>
 764      *
 765      * @return the instance of {@code Ser}, not null
 766      */

 767     private Object writeReplace() {
 768         return new Ser(Ser.MONTH_DAY_TYPE, this);
 769     }
 770 
 771     /**
 772      * Defend against malicious streams.
 773      *
 774      * @param s the stream to read
 775      * @throws InvalidObjectException always
 776      */

 777     private void readObject(ObjectInputStream s) throws InvalidObjectException {
 778         throw new InvalidObjectException("Deserialization via serialization delegate");
 779     }
 780 
 781     void writeExternal(DataOutput out) throws IOException {
 782         out.writeByte(month);
 783         out.writeByte(day);
 784     }
 785 
 786     static MonthDay readExternal(DataInput in) throws IOException {
 787         byte month = in.readByte();
 788         byte day = in.readByte();
 789         return MonthDay.of(month, day);
 790     }
 791 
 792 }


 113  * to be accurate will find the ISO-8601 approach unsuitable.
 114  *
 115  * <p>
 116  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 117  * class; use of identity-sensitive operations (including reference equality
 118  * ({@code ==}), identity hash code, or synchronization) on instances of
 119  * {@code MonthDay} may have unpredictable results and should be avoided.
 120  * The {@code equals} method should be used for comparisons.
 121  *
 122  * @implSpec
 123  * This class is immutable and thread-safe.
 124  *
 125  * @since 1.8
 126  */
 127 public final class MonthDay
 128         implements TemporalAccessor, TemporalAdjuster, Comparable<MonthDay>, Serializable {
 129 
 130     /**
 131      * Serialization version.
 132      */
 133     @java.io.Serial
 134     private static final long serialVersionUID = -939150713474957432L;
 135     /**
 136      * Parser.
 137      */
 138     private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder()
 139         .appendLiteral("--")
 140         .appendValue(MONTH_OF_YEAR, 2)
 141         .appendLiteral('-')
 142         .appendValue(DAY_OF_MONTH, 2)
 143         .toFormatter();
 144 
 145     /**
 146      * The month-of-year, not null.
 147      */
 148     private final int month;
 149     /**
 150      * The day-of-month.
 151      */
 152     private final int day;
 153 


 748     public String toString() {
 749         return new StringBuilder(10).append("--")
 750             .append(month < 10 ? "0" : "").append(month)
 751             .append(day < 10 ? "-0" : "-").append(day)
 752             .toString();
 753     }
 754 
 755     //-----------------------------------------------------------------------
 756     /**
 757      * Writes the object using a
 758      * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
 759      * @serialData
 760      * <pre>
 761      *  out.writeByte(13);  // identifies a MonthDay
 762      *  out.writeByte(month);
 763      *  out.writeByte(day);
 764      * </pre>
 765      *
 766      * @return the instance of {@code Ser}, not null
 767      */
 768     @java.io.Serial
 769     private Object writeReplace() {
 770         return new Ser(Ser.MONTH_DAY_TYPE, this);
 771     }
 772 
 773     /**
 774      * Defend against malicious streams.
 775      *
 776      * @param s the stream to read
 777      * @throws InvalidObjectException always
 778      */
 779     @java.io.Serial
 780     private void readObject(ObjectInputStream s) throws InvalidObjectException {
 781         throw new InvalidObjectException("Deserialization via serialization delegate");
 782     }
 783 
 784     void writeExternal(DataOutput out) throws IOException {
 785         out.writeByte(month);
 786         out.writeByte(day);
 787     }
 788 
 789     static MonthDay readExternal(DataInput in) throws IOException {
 790         byte month = in.readByte();
 791         byte day = in.readByte();
 792         return MonthDay.of(month, day);
 793     }
 794 
 795 }
< prev index next >