Path: blob/master/src/java.base/share/classes/java/time/MonthDay.java
41152 views
/*1* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos32*33* All rights reserved.34*35* Redistribution and use in source and binary forms, with or without36* modification, are permitted provided that the following conditions are met:37*38* * Redistributions of source code must retain the above copyright notice,39* this list of conditions and the following disclaimer.40*41* * Redistributions in binary form must reproduce the above copyright notice,42* this list of conditions and the following disclaimer in the documentation43* and/or other materials provided with the distribution.44*45* * Neither the name of JSR-310 nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS50* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT51* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR52* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR53* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,54* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,55* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR56* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF57* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING58* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS59* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.60*/61package java.time;6263import static java.time.temporal.ChronoField.DAY_OF_MONTH;64import static java.time.temporal.ChronoField.MONTH_OF_YEAR;6566import java.io.DataInput;67import java.io.DataOutput;68import java.io.IOException;69import java.io.InvalidObjectException;70import java.io.ObjectInputStream;71import java.io.Serializable;72import java.time.chrono.Chronology;73import java.time.chrono.IsoChronology;74import java.time.format.DateTimeFormatter;75import java.time.format.DateTimeFormatterBuilder;76import java.time.format.DateTimeParseException;77import java.time.temporal.ChronoField;78import java.time.temporal.Temporal;79import java.time.temporal.TemporalAccessor;80import java.time.temporal.TemporalAdjuster;81import java.time.temporal.TemporalField;82import java.time.temporal.TemporalQueries;83import java.time.temporal.TemporalQuery;84import java.time.temporal.UnsupportedTemporalTypeException;85import java.time.temporal.ValueRange;86import java.util.Objects;8788/**89* A month-day in the ISO-8601 calendar system, such as {@code --12-03}.90* <p>91* {@code MonthDay} is an immutable date-time object that represents the combination92* of a month and day-of-month. Any field that can be derived from a month and day,93* such as quarter-of-year, can be obtained.94* <p>95* This class does not store or represent a year, time or time-zone.96* For example, the value "December 3rd" can be stored in a {@code MonthDay}.97* <p>98* Since a {@code MonthDay} does not possess a year, the leap day of99* February 29th is considered valid.100* <p>101* This class implements {@link TemporalAccessor} rather than {@link Temporal}.102* This is because it is not possible to define whether February 29th is valid or not103* without external information, preventing the implementation of plus/minus.104* Related to this, {@code MonthDay} only provides access to query and set the fields105* {@code MONTH_OF_YEAR} and {@code DAY_OF_MONTH}.106* <p>107* The ISO-8601 calendar system is the modern civil calendar system used today108* in most of the world. It is equivalent to the proleptic Gregorian calendar109* system, in which today's rules for leap years are applied for all time.110* For most applications written today, the ISO-8601 rules are entirely suitable.111* However, any application that makes use of historical dates, and requires them112* to be accurate will find the ISO-8601 approach unsuitable.113* <p>114* This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>115* class; programmers should treat instances that are116* {@linkplain #equals(Object) equal} as interchangeable and should not117* use instances for synchronization, or unpredictable behavior may118* occur. For example, in a future release, synchronization may fail.119* The {@code equals} method should be used for comparisons.120*121* @implSpec122* This class is immutable and thread-safe.123*124* @since 1.8125*/126@jdk.internal.ValueBased127public final class MonthDay128implements TemporalAccessor, TemporalAdjuster, Comparable<MonthDay>, Serializable {129130/**131* Serialization version.132*/133@java.io.Serial134private static final long serialVersionUID = -939150713474957432L;135/**136* Parser.137*/138private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder()139.appendLiteral("--")140.appendValue(MONTH_OF_YEAR, 2)141.appendLiteral('-')142.appendValue(DAY_OF_MONTH, 2)143.toFormatter();144145/**146* The month-of-year, not null.147*/148private final int month;149/**150* The day-of-month.151*/152private final int day;153154//-----------------------------------------------------------------------155/**156* Obtains the current month-day from the system clock in the default time-zone.157* <p>158* This will query the {@link Clock#systemDefaultZone() system clock} in the default159* time-zone to obtain the current month-day.160* <p>161* Using this method will prevent the ability to use an alternate clock for testing162* because the clock is hard-coded.163*164* @return the current month-day using the system clock and default time-zone, not null165*/166public static MonthDay now() {167return now(Clock.systemDefaultZone());168}169170/**171* Obtains the current month-day from the system clock in the specified time-zone.172* <p>173* This will query the {@link Clock#system(ZoneId) system clock} to obtain the current month-day.174* Specifying the time-zone avoids dependence on the default time-zone.175* <p>176* Using this method will prevent the ability to use an alternate clock for testing177* because the clock is hard-coded.178*179* @param zone the zone ID to use, not null180* @return the current month-day using the system clock, not null181*/182public static MonthDay now(ZoneId zone) {183return now(Clock.system(zone));184}185186/**187* Obtains the current month-day from the specified clock.188* <p>189* This will query the specified clock to obtain the current month-day.190* Using this method allows the use of an alternate clock for testing.191* The alternate clock may be introduced using {@link Clock dependency injection}.192*193* @param clock the clock to use, not null194* @return the current month-day, not null195*/196public static MonthDay now(Clock clock) {197final LocalDate now = LocalDate.now(clock); // called once198return MonthDay.of(now.getMonth(), now.getDayOfMonth());199}200201//-----------------------------------------------------------------------202/**203* Obtains an instance of {@code MonthDay}.204* <p>205* The day-of-month must be valid for the month within a leap year.206* Hence, for February, day 29 is valid.207* <p>208* For example, passing in April and day 31 will throw an exception, as209* there can never be April 31st in any year. By contrast, passing in210* February 29th is permitted, as that month-day can sometimes be valid.211*212* @param month the month-of-year to represent, not null213* @param dayOfMonth the day-of-month to represent, from 1 to 31214* @return the month-day, not null215* @throws DateTimeException if the value of any field is out of range,216* or if the day-of-month is invalid for the month217*/218public static MonthDay of(Month month, int dayOfMonth) {219Objects.requireNonNull(month, "month");220DAY_OF_MONTH.checkValidValue(dayOfMonth);221if (dayOfMonth > month.maxLength()) {222throw new DateTimeException("Illegal value for DayOfMonth field, value " + dayOfMonth +223" is not valid for month " + month.name());224}225return new MonthDay(month.getValue(), dayOfMonth);226}227228/**229* Obtains an instance of {@code MonthDay}.230* <p>231* The day-of-month must be valid for the month within a leap year.232* Hence, for month 2 (February), day 29 is valid.233* <p>234* For example, passing in month 4 (April) and day 31 will throw an exception, as235* there can never be April 31st in any year. By contrast, passing in236* February 29th is permitted, as that month-day can sometimes be valid.237*238* @param month the month-of-year to represent, from 1 (January) to 12 (December)239* @param dayOfMonth the day-of-month to represent, from 1 to 31240* @return the month-day, not null241* @throws DateTimeException if the value of any field is out of range,242* or if the day-of-month is invalid for the month243*/244public static MonthDay of(int month, int dayOfMonth) {245return of(Month.of(month), dayOfMonth);246}247248//-----------------------------------------------------------------------249/**250* Obtains an instance of {@code MonthDay} from a temporal object.251* <p>252* This obtains a month-day based on the specified temporal.253* A {@code TemporalAccessor} represents an arbitrary set of date and time information,254* which this factory converts to an instance of {@code MonthDay}.255* <p>256* The conversion extracts the {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} and257* {@link ChronoField#DAY_OF_MONTH DAY_OF_MONTH} fields.258* The extraction is only permitted if the temporal object has an ISO259* chronology, or can be converted to a {@code LocalDate}.260* <p>261* This method matches the signature of the functional interface {@link TemporalQuery}262* allowing it to be used as a query via method reference, {@code MonthDay::from}.263*264* @param temporal the temporal object to convert, not null265* @return the month-day, not null266* @throws DateTimeException if unable to convert to a {@code MonthDay}267*/268public static MonthDay from(TemporalAccessor temporal) {269if (temporal instanceof MonthDay) {270return (MonthDay) temporal;271}272try {273if (IsoChronology.INSTANCE.equals(Chronology.from(temporal)) == false) {274temporal = LocalDate.from(temporal);275}276return of(temporal.get(MONTH_OF_YEAR), temporal.get(DAY_OF_MONTH));277} catch (DateTimeException ex) {278throw new DateTimeException("Unable to obtain MonthDay from TemporalAccessor: " +279temporal + " of type " + temporal.getClass().getName(), ex);280}281}282283//-----------------------------------------------------------------------284/**285* Obtains an instance of {@code MonthDay} from a text string such as {@code --12-03}.286* <p>287* The string must represent a valid month-day.288* The format is {@code --MM-dd}.289*290* @param text the text to parse such as "--12-03", not null291* @return the parsed month-day, not null292* @throws DateTimeParseException if the text cannot be parsed293*/294public static MonthDay parse(CharSequence text) {295return parse(text, PARSER);296}297298/**299* Obtains an instance of {@code MonthDay} from a text string using a specific formatter.300* <p>301* The text is parsed using the formatter, returning a month-day.302*303* @param text the text to parse, not null304* @param formatter the formatter to use, not null305* @return the parsed month-day, not null306* @throws DateTimeParseException if the text cannot be parsed307*/308public static MonthDay parse(CharSequence text, DateTimeFormatter formatter) {309Objects.requireNonNull(formatter, "formatter");310return formatter.parse(text, MonthDay::from);311}312313//-----------------------------------------------------------------------314/**315* Constructor, previously validated.316*317* @param month the month-of-year to represent, validated from 1 to 12318* @param dayOfMonth the day-of-month to represent, validated from 1 to 29-31319*/320private MonthDay(int month, int dayOfMonth) {321this.month = month;322this.day = dayOfMonth;323}324325//-----------------------------------------------------------------------326/**327* Checks if the specified field is supported.328* <p>329* This checks if this month-day can be queried for the specified field.330* If false, then calling the {@link #range(TemporalField) range} and331* {@link #get(TemporalField) get} methods will throw an exception.332* <p>333* If the field is a {@link ChronoField} then the query is implemented here.334* The supported fields are:335* <ul>336* <li>{@code MONTH_OF_YEAR}337* <li>{@code YEAR}338* </ul>339* All other {@code ChronoField} instances will return false.340* <p>341* If the field is not a {@code ChronoField}, then the result of this method342* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}343* passing {@code this} as the argument.344* Whether the field is supported is determined by the field.345*346* @param field the field to check, null returns false347* @return true if the field is supported on this month-day, false if not348*/349@Override350public boolean isSupported(TemporalField field) {351if (field instanceof ChronoField) {352return field == MONTH_OF_YEAR || field == DAY_OF_MONTH;353}354return field != null && field.isSupportedBy(this);355}356357/**358* Gets the range of valid values for the specified field.359* <p>360* The range object expresses the minimum and maximum valid values for a field.361* This month-day is used to enhance the accuracy of the returned range.362* If it is not possible to return the range, because the field is not supported363* or for some other reason, an exception is thrown.364* <p>365* If the field is a {@link ChronoField} then the query is implemented here.366* The {@link #isSupported(TemporalField) supported fields} will return367* appropriate range instances.368* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.369* <p>370* If the field is not a {@code ChronoField}, then the result of this method371* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}372* passing {@code this} as the argument.373* Whether the range can be obtained is determined by the field.374*375* @param field the field to query the range for, not null376* @return the range of valid values for the field, not null377* @throws DateTimeException if the range for the field cannot be obtained378* @throws UnsupportedTemporalTypeException if the field is not supported379*/380@Override381public ValueRange range(TemporalField field) {382if (field == MONTH_OF_YEAR) {383return field.range();384} else if (field == DAY_OF_MONTH) {385return ValueRange.of(1, getMonth().minLength(), getMonth().maxLength());386}387return TemporalAccessor.super.range(field);388}389390/**391* Gets the value of the specified field from this month-day as an {@code int}.392* <p>393* This queries this month-day for the value of the specified field.394* The returned value will always be within the valid range of values for the field.395* If it is not possible to return the value, because the field is not supported396* or for some other reason, an exception is thrown.397* <p>398* If the field is a {@link ChronoField} then the query is implemented here.399* The {@link #isSupported(TemporalField) supported fields} will return valid400* values based on this month-day.401* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.402* <p>403* If the field is not a {@code ChronoField}, then the result of this method404* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}405* passing {@code this} as the argument. Whether the value can be obtained,406* and what the value represents, is determined by the field.407*408* @param field the field to get, not null409* @return the value for the field410* @throws DateTimeException if a value for the field cannot be obtained or411* the value is outside the range of valid values for the field412* @throws UnsupportedTemporalTypeException if the field is not supported or413* the range of values exceeds an {@code int}414* @throws ArithmeticException if numeric overflow occurs415*/416@Override // override for Javadoc417public int get(TemporalField field) {418return range(field).checkValidIntValue(getLong(field), field);419}420421/**422* Gets the value of the specified field from this month-day as a {@code long}.423* <p>424* This queries this month-day for the value of the specified field.425* If it is not possible to return the value, because the field is not supported426* or for some other reason, an exception is thrown.427* <p>428* If the field is a {@link ChronoField} then the query is implemented here.429* The {@link #isSupported(TemporalField) supported fields} will return valid430* values based on this month-day.431* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.432* <p>433* If the field is not a {@code ChronoField}, then the result of this method434* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}435* passing {@code this} as the argument. Whether the value can be obtained,436* and what the value represents, is determined by the field.437*438* @param field the field to get, not null439* @return the value for the field440* @throws DateTimeException if a value for the field cannot be obtained441* @throws UnsupportedTemporalTypeException if the field is not supported442* @throws ArithmeticException if numeric overflow occurs443*/444@Override445public long getLong(TemporalField field) {446if (field instanceof ChronoField chronoField) {447switch (chronoField) {448// alignedDOW and alignedWOM not supported because they cannot be set in with()449case DAY_OF_MONTH: return day;450case MONTH_OF_YEAR: return month;451}452throw new UnsupportedTemporalTypeException("Unsupported field: " + field);453}454return field.getFrom(this);455}456457//-----------------------------------------------------------------------458/**459* Gets the month-of-year field from 1 to 12.460* <p>461* This method returns the month as an {@code int} from 1 to 12.462* Application code is frequently clearer if the enum {@link Month}463* is used by calling {@link #getMonth()}.464*465* @return the month-of-year, from 1 to 12466* @see #getMonth()467*/468public int getMonthValue() {469return month;470}471472/**473* Gets the month-of-year field using the {@code Month} enum.474* <p>475* This method returns the enum {@link Month} for the month.476* This avoids confusion as to what {@code int} values mean.477* If you need access to the primitive {@code int} value then the enum478* provides the {@link Month#getValue() int value}.479*480* @return the month-of-year, not null481* @see #getMonthValue()482*/483public Month getMonth() {484return Month.of(month);485}486487/**488* Gets the day-of-month field.489* <p>490* This method returns the primitive {@code int} value for the day-of-month.491*492* @return the day-of-month, from 1 to 31493*/494public int getDayOfMonth() {495return day;496}497498//-----------------------------------------------------------------------499/**500* Checks if the year is valid for this month-day.501* <p>502* This method checks whether this month and day and the input year form503* a valid date. This can only return false for February 29th.504*505* @param year the year to validate506* @return true if the year is valid for this month-day507* @see Year#isValidMonthDay(MonthDay)508*/509public boolean isValidYear(int year) {510return (day == 29 && month == 2 && Year.isLeap(year) == false) == false;511}512513//-----------------------------------------------------------------------514/**515* Returns a copy of this {@code MonthDay} with the month-of-year altered.516* <p>517* This returns a month-day with the specified month.518* If the day-of-month is invalid for the specified month, the day will519* be adjusted to the last valid day-of-month.520* <p>521* This instance is immutable and unaffected by this method call.522*523* @param month the month-of-year to set in the returned month-day, from 1 (January) to 12 (December)524* @return a {@code MonthDay} based on this month-day with the requested month, not null525* @throws DateTimeException if the month-of-year value is invalid526*/527public MonthDay withMonth(int month) {528return with(Month.of(month));529}530531/**532* Returns a copy of this {@code MonthDay} with the month-of-year altered.533* <p>534* This returns a month-day with the specified month.535* If the day-of-month is invalid for the specified month, the day will536* be adjusted to the last valid day-of-month.537* <p>538* This instance is immutable and unaffected by this method call.539*540* @param month the month-of-year to set in the returned month-day, not null541* @return a {@code MonthDay} based on this month-day with the requested month, not null542*/543public MonthDay with(Month month) {544Objects.requireNonNull(month, "month");545if (month.getValue() == this.month) {546return this;547}548int day = Math.min(this.day, month.maxLength());549return new MonthDay(month.getValue(), day);550}551552/**553* Returns a copy of this {@code MonthDay} with the day-of-month altered.554* <p>555* This returns a month-day with the specified day-of-month.556* If the day-of-month is invalid for the month, an exception is thrown.557* <p>558* This instance is immutable and unaffected by this method call.559*560* @param dayOfMonth the day-of-month to set in the return month-day, from 1 to 31561* @return a {@code MonthDay} based on this month-day with the requested day, not null562* @throws DateTimeException if the day-of-month value is invalid,563* or if the day-of-month is invalid for the month564*/565public MonthDay withDayOfMonth(int dayOfMonth) {566if (dayOfMonth == this.day) {567return this;568}569return of(month, dayOfMonth);570}571572//-----------------------------------------------------------------------573/**574* Queries this month-day using the specified query.575* <p>576* This queries this month-day using the specified query strategy object.577* The {@code TemporalQuery} object defines the logic to be used to578* obtain the result. Read the documentation of the query to understand579* what the result of this method will be.580* <p>581* The result of this method is obtained by invoking the582* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the583* specified query passing {@code this} as the argument.584*585* @param <R> the type of the result586* @param query the query to invoke, not null587* @return the query result, null may be returned (defined by the query)588* @throws DateTimeException if unable to query (defined by the query)589* @throws ArithmeticException if numeric overflow occurs (defined by the query)590*/591@SuppressWarnings("unchecked")592@Override593public <R> R query(TemporalQuery<R> query) {594if (query == TemporalQueries.chronology()) {595return (R) IsoChronology.INSTANCE;596}597return TemporalAccessor.super.query(query);598}599600/**601* Adjusts the specified temporal object to have this month-day.602* <p>603* This returns a temporal object of the same observable type as the input604* with the month and day-of-month changed to be the same as this.605* <p>606* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}607* twice, passing {@link ChronoField#MONTH_OF_YEAR} and608* {@link ChronoField#DAY_OF_MONTH} as the fields.609* If the specified temporal object does not use the ISO calendar system then610* a {@code DateTimeException} is thrown.611* <p>612* In most cases, it is clearer to reverse the calling pattern by using613* {@link Temporal#with(TemporalAdjuster)}:614* <pre>615* // these two lines are equivalent, but the second approach is recommended616* temporal = thisMonthDay.adjustInto(temporal);617* temporal = temporal.with(thisMonthDay);618* </pre>619* <p>620* This instance is immutable and unaffected by this method call.621*622* @param temporal the target object to be adjusted, not null623* @return the adjusted object, not null624* @throws DateTimeException if unable to make the adjustment625* @throws ArithmeticException if numeric overflow occurs626*/627@Override628public Temporal adjustInto(Temporal temporal) {629if (Chronology.from(temporal).equals(IsoChronology.INSTANCE) == false) {630throw new DateTimeException("Adjustment only supported on ISO date-time");631}632temporal = temporal.with(MONTH_OF_YEAR, month);633return temporal.with(DAY_OF_MONTH, Math.min(temporal.range(DAY_OF_MONTH).getMaximum(), day));634}635636/**637* Formats this month-day using the specified formatter.638* <p>639* This month-day will be passed to the formatter to produce a string.640*641* @param formatter the formatter to use, not null642* @return the formatted month-day string, not null643* @throws DateTimeException if an error occurs during printing644*/645public String format(DateTimeFormatter formatter) {646Objects.requireNonNull(formatter, "formatter");647return formatter.format(this);648}649650//-----------------------------------------------------------------------651/**652* Combines this month-day with a year to create a {@code LocalDate}.653* <p>654* This returns a {@code LocalDate} formed from this month-day and the specified year.655* <p>656* A month-day of February 29th will be adjusted to February 28th in the resulting657* date if the year is not a leap year.658* <p>659* This instance is immutable and unaffected by this method call.660*661* @param year the year to use, from MIN_YEAR to MAX_YEAR662* @return the local date formed from this month-day and the specified year, not null663* @throws DateTimeException if the year is outside the valid range of years664*/665public LocalDate atYear(int year) {666return LocalDate.of(year, month, isValidYear(year) ? day : 28);667}668669//-----------------------------------------------------------------------670/**671* Compares this month-day to another month-day.672* <p>673* The comparison is based first on value of the month, then on the value of the day.674* It is "consistent with equals", as defined by {@link Comparable}.675*676* @param other the other month-day to compare to, not null677* @return the comparator value, negative if less, positive if greater678*/679@Override680public int compareTo(MonthDay other) {681int cmp = (month - other.month);682if (cmp == 0) {683cmp = (day - other.day);684}685return cmp;686}687688/**689* Checks if this month-day is after the specified month-day.690*691* @param other the other month-day to compare to, not null692* @return true if this is after the specified month-day693*/694public boolean isAfter(MonthDay other) {695return compareTo(other) > 0;696}697698/**699* Checks if this month-day is before the specified month-day.700*701* @param other the other month-day to compare to, not null702* @return true if this point is before the specified month-day703*/704public boolean isBefore(MonthDay other) {705return compareTo(other) < 0;706}707708//-----------------------------------------------------------------------709/**710* Checks if this month-day is equal to another month-day.711* <p>712* The comparison is based on the time-line position of the month-day within a year.713*714* @param obj the object to check, null returns false715* @return true if this is equal to the other month-day716*/717@Override718public boolean equals(Object obj) {719if (this == obj) {720return true;721}722return (obj instanceof MonthDay other)723&& month == other.month724&& day == other.day;725}726727/**728* A hash code for this month-day.729*730* @return a suitable hash code731*/732@Override733public int hashCode() {734return (month << 6) + day;735}736737//-----------------------------------------------------------------------738/**739* Outputs this month-day as a {@code String}, such as {@code --12-03}.740* <p>741* The output will be in the format {@code --MM-dd}:742*743* @return a string representation of this month-day, not null744*/745@Override746public String toString() {747return new StringBuilder(10).append("--")748.append(month < 10 ? "0" : "").append(month)749.append(day < 10 ? "-0" : "-").append(day)750.toString();751}752753//-----------------------------------------------------------------------754/**755* Writes the object using a756* <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.757* @serialData758* <pre>759* out.writeByte(13); // identifies a MonthDay760* out.writeByte(month);761* out.writeByte(day);762* </pre>763*764* @return the instance of {@code Ser}, not null765*/766@java.io.Serial767private Object writeReplace() {768return new Ser(Ser.MONTH_DAY_TYPE, this);769}770771/**772* Defend against malicious streams.773*774* @param s the stream to read775* @throws InvalidObjectException always776*/777@java.io.Serial778private void readObject(ObjectInputStream s) throws InvalidObjectException {779throw new InvalidObjectException("Deserialization via serialization delegate");780}781782void writeExternal(DataOutput out) throws IOException {783out.writeByte(month);784out.writeByte(day);785}786787static MonthDay readExternal(DataInput in) throws IOException {788byte month = in.readByte();789byte day = in.readByte();790return MonthDay.of(month, day);791}792793}794795796