Path: blob/master/src/java.base/share/classes/java/time/chrono/JapaneseDate.java
41159 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* Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos27*28* All rights reserved.29*30* Redistribution and use in source and binary forms, with or without31* modification, are permitted provided that the following conditions are met:32*33* * Redistributions of source code must retain the above copyright notice,34* this list of conditions and the following disclaimer.35*36* * Redistributions in binary form must reproduce the above copyright notice,37* this list of conditions and the following disclaimer in the documentation38* and/or other materials provided with the distribution.39*40* * Neither the name of JSR-310 nor the names of its contributors41* may be used to endorse or promote products derived from this software42* without specific prior written permission.43*44* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS45* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT46* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR47* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR48* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,49* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,50* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR51* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF52* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING53* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS54* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.55*/56package java.time.chrono;5758import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;59import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;60import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;61import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;62import static java.time.temporal.ChronoField.DAY_OF_MONTH;63import static java.time.temporal.ChronoField.MONTH_OF_YEAR;64import static java.time.temporal.ChronoField.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.Clock;73import java.time.DateTimeException;74import java.time.LocalDate;75import java.time.LocalTime;76import java.time.Period;77import java.time.ZoneId;78import java.time.temporal.ChronoField;79import java.time.temporal.TemporalAccessor;80import java.time.temporal.TemporalAdjuster;81import java.time.temporal.TemporalAmount;82import java.time.temporal.TemporalField;83import java.time.temporal.TemporalQuery;84import java.time.temporal.TemporalUnit;85import java.time.temporal.UnsupportedTemporalTypeException;86import java.time.temporal.ValueRange;87import java.util.Calendar;88import java.util.Objects;8990import sun.util.calendar.CalendarDate;91import sun.util.calendar.LocalGregorianCalendar;9293/**94* A date in the Japanese Imperial calendar system.95* <p>96* This date operates using the {@linkplain JapaneseChronology Japanese Imperial calendar}.97* This calendar system is primarily used in Japan.98* <p>99* The Japanese Imperial calendar system is the same as the ISO calendar system100* apart from the era-based year numbering. The proleptic-year is defined to be101* equal to the ISO proleptic-year.102* <p>103* Japan introduced the Gregorian calendar starting with Meiji 6.104* Only Meiji and later eras are supported;105* dates before Meiji 6, January 1 are not supported.106* <p>107* For example, the Japanese year "Heisei 24" corresponds to ISO year "2012".<br>108* Calling {@code japaneseDate.get(YEAR_OF_ERA)} will return 24.<br>109* Calling {@code japaneseDate.get(YEAR)} will return 2012.<br>110* Calling {@code japaneseDate.get(ERA)} will return 2, corresponding to111* {@code JapaneseChronology.ERA_HEISEI}.<br>112* <p>113* This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>114* class; programmers should treat instances that are115* {@linkplain #equals(Object) equal} as interchangeable and should not116* use instances for synchronization, or unpredictable behavior may117* occur. For example, in a future release, synchronization may fail.118* The {@code equals} method should be used for comparisons.119*120* @implSpec121* This class is immutable and thread-safe.122*123* @since 1.8124*/125@jdk.internal.ValueBased126public final class JapaneseDate127extends ChronoLocalDateImpl<JapaneseDate>128implements ChronoLocalDate, Serializable {129130/**131* Serialization version.132*/133@java.io.Serial134private static final long serialVersionUID = -305327627230580483L;135136/**137* The underlying ISO local date.138*/139private final transient LocalDate isoDate;140/**141* The JapaneseEra of this date.142*/143private final transient JapaneseEra era;144/**145* The Japanese imperial calendar year of this date.146*/147private final transient int yearOfEra;148149/**150* The first day supported by the JapaneseChronology is Meiji 6, January 1st.151*/152static final LocalDate MEIJI_6_ISODATE = LocalDate.of(1873, 1, 1);153154//-----------------------------------------------------------------------155/**156* Obtains the current {@code JapaneseDate} 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 date.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 date using the system clock and default time-zone, not null165*/166public static JapaneseDate now() {167return now(Clock.systemDefaultZone());168}169170/**171* Obtains the current {@code JapaneseDate} 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 date.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 date using the system clock, not null181*/182public static JapaneseDate now(ZoneId zone) {183return now(Clock.system(zone));184}185186/**187* Obtains the current {@code JapaneseDate} from the specified clock.188* <p>189* This will query the specified clock to obtain the current date - today.190* Using this method allows the use of an alternate clock for testing.191* The alternate clock may be introduced using {@linkplain Clock dependency injection}.192*193* @param clock the clock to use, not null194* @return the current date, not null195* @throws DateTimeException if the current date cannot be obtained196*/197public static JapaneseDate now(Clock clock) {198return new JapaneseDate(LocalDate.now(clock));199}200201/**202* Obtains a {@code JapaneseDate} representing a date in the Japanese calendar203* system from the era, year-of-era, month-of-year and day-of-month fields.204* <p>205* This returns a {@code JapaneseDate} with the specified fields.206* The day must be valid for the year and month, otherwise an exception will be thrown.207* <p>208* The Japanese month and day-of-month are the same as those in the209* ISO calendar system. They are not reset when the era changes.210* For example:211* <pre>212* 6th Jan Showa 64 = ISO 1989-01-06213* 7th Jan Showa 64 = ISO 1989-01-07214* 8th Jan Heisei 1 = ISO 1989-01-08215* 9th Jan Heisei 1 = ISO 1989-01-09216* </pre>217*218* @param era the Japanese era, not null219* @param yearOfEra the Japanese year-of-era220* @param month the Japanese month-of-year, from 1 to 12221* @param dayOfMonth the Japanese day-of-month, from 1 to 31222* @return the date in Japanese calendar system, not null223* @throws DateTimeException if the value of any field is out of range,224* or if the day-of-month is invalid for the month-year,225* or if the date is not a Japanese era226*/227public static JapaneseDate of(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) {228Objects.requireNonNull(era, "era");229LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);230jdate.setEra(era.getPrivateEra()).setDate(yearOfEra, month, dayOfMonth);231if (!JapaneseChronology.JCAL.validate(jdate)) {232throw new DateTimeException("year, month, and day not valid for Era");233}234LocalDate date = LocalDate.of(jdate.getNormalizedYear(), month, dayOfMonth);235return new JapaneseDate(era, yearOfEra, date);236}237238/**239* Obtains a {@code JapaneseDate} representing a date in the Japanese calendar240* system from the proleptic-year, month-of-year and day-of-month fields.241* <p>242* This returns a {@code JapaneseDate} with the specified fields.243* The day must be valid for the year and month, otherwise an exception will be thrown.244* <p>245* The Japanese proleptic year, month and day-of-month are the same as those246* in the ISO calendar system. They are not reset when the era changes.247*248* @param prolepticYear the Japanese proleptic-year249* @param month the Japanese month-of-year, from 1 to 12250* @param dayOfMonth the Japanese day-of-month, from 1 to 31251* @return the date in Japanese calendar system, not null252* @throws DateTimeException if the value of any field is out of range,253* or if the day-of-month is invalid for the month-year254*/255public static JapaneseDate of(int prolepticYear, int month, int dayOfMonth) {256return new JapaneseDate(LocalDate.of(prolepticYear, month, dayOfMonth));257}258259/**260* Obtains a {@code JapaneseDate} representing a date in the Japanese calendar261* system from the era, year-of-era and day-of-year fields.262* <p>263* This returns a {@code JapaneseDate} with the specified fields.264* The day must be valid for the year, otherwise an exception will be thrown.265* <p>266* The day-of-year in this factory is expressed relative to the start of the year-of-era.267* This definition changes the normal meaning of day-of-year only in those years268* where the year-of-era is reset to one due to a change in the era.269* For example:270* <pre>271* 6th Jan Showa 64 = day-of-year 6272* 7th Jan Showa 64 = day-of-year 7273* 8th Jan Heisei 1 = day-of-year 1274* 9th Jan Heisei 1 = day-of-year 2275* </pre>276*277* @param era the Japanese era, not null278* @param yearOfEra the Japanese year-of-era279* @param dayOfYear the chronology day-of-year, from 1 to 366280* @return the date in Japanese calendar system, not null281* @throws DateTimeException if the value of any field is out of range,282* or if the day-of-year is invalid for the year283*/284static JapaneseDate ofYearDay(JapaneseEra era, int yearOfEra, int dayOfYear) {285Objects.requireNonNull(era, "era");286CalendarDate firstDay = era.getPrivateEra().getSinceDate();287LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);288jdate.setEra(era.getPrivateEra());289if (yearOfEra == 1) {290jdate.setDate(yearOfEra, firstDay.getMonth(), firstDay.getDayOfMonth() + dayOfYear - 1);291} else {292jdate.setDate(yearOfEra, 1, dayOfYear);293}294JapaneseChronology.JCAL.normalize(jdate);295if (era.getPrivateEra() != jdate.getEra() || yearOfEra != jdate.getYear()) {296throw new DateTimeException("Invalid parameters");297}298LocalDate localdate = LocalDate.of(jdate.getNormalizedYear(),299jdate.getMonth(), jdate.getDayOfMonth());300return new JapaneseDate(era, yearOfEra, localdate);301}302303/**304* Obtains a {@code JapaneseDate} from a temporal object.305* <p>306* This obtains a date in the Japanese calendar system based on the specified temporal.307* A {@code TemporalAccessor} represents an arbitrary set of date and time information,308* which this factory converts to an instance of {@code JapaneseDate}.309* <p>310* The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY}311* field, which is standardized across calendar systems.312* <p>313* This method matches the signature of the functional interface {@link TemporalQuery}314* allowing it to be used as a query via method reference, {@code JapaneseDate::from}.315*316* @param temporal the temporal object to convert, not null317* @return the date in Japanese calendar system, not null318* @throws DateTimeException if unable to convert to a {@code JapaneseDate}319*/320public static JapaneseDate from(TemporalAccessor temporal) {321return JapaneseChronology.INSTANCE.date(temporal);322}323324//-----------------------------------------------------------------------325/**326* Creates an instance from an ISO date.327*328* @param isoDate the standard local date, validated not null329*/330JapaneseDate(LocalDate isoDate) {331if (isoDate.isBefore(MEIJI_6_ISODATE)) {332throw new DateTimeException("JapaneseDate before Meiji 6 is not supported");333}334LocalGregorianCalendar.Date jdate = toPrivateJapaneseDate(isoDate);335this.era = JapaneseEra.toJapaneseEra(jdate.getEra());336this.yearOfEra = jdate.getYear();337this.isoDate = isoDate;338}339340/**341* Constructs a {@code JapaneseDate}. This constructor does NOT validate the given parameters,342* and {@code era} and {@code year} must agree with {@code isoDate}.343*344* @param era the era, validated not null345* @param year the year-of-era, validated346* @param isoDate the standard local date, validated not null347*/348JapaneseDate(JapaneseEra era, int year, LocalDate isoDate) {349if (isoDate.isBefore(MEIJI_6_ISODATE)) {350throw new DateTimeException("JapaneseDate before Meiji 6 is not supported");351}352this.era = era;353this.yearOfEra = year;354this.isoDate = isoDate;355}356357//-----------------------------------------------------------------------358/**359* Gets the chronology of this date, which is the Japanese calendar system.360* <p>361* The {@code Chronology} represents the calendar system in use.362* The era and other fields in {@link ChronoField} are defined by the chronology.363*364* @return the Japanese chronology, not null365*/366@Override367public JapaneseChronology getChronology() {368return JapaneseChronology.INSTANCE;369}370371/**372* Gets the era applicable at this date.373* <p>374* The Japanese calendar system has multiple eras defined by {@link JapaneseEra}.375*376* @return the era applicable at this date, not null377*/378@Override379public JapaneseEra getEra() {380return era;381}382383/**384* Returns the length of the month represented by this date.385* <p>386* This returns the length of the month in days.387* Month lengths match those of the ISO calendar system.388*389* @return the length of the month in days390*/391@Override392public int lengthOfMonth() {393return isoDate.lengthOfMonth();394}395396@Override397public int lengthOfYear() {398Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE);399jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);400jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());401return jcal.getActualMaximum(Calendar.DAY_OF_YEAR);402}403404//-----------------------------------------------------------------------405/**406* Checks if the specified field is supported.407* <p>408* This checks if this date can be queried for the specified field.409* If false, then calling the {@link #range(TemporalField) range} and410* {@link #get(TemporalField) get} methods will throw an exception.411* <p>412* If the field is a {@link ChronoField} then the query is implemented here.413* The supported fields are:414* <ul>415* <li>{@code DAY_OF_WEEK}416* <li>{@code DAY_OF_MONTH}417* <li>{@code DAY_OF_YEAR}418* <li>{@code EPOCH_DAY}419* <li>{@code MONTH_OF_YEAR}420* <li>{@code PROLEPTIC_MONTH}421* <li>{@code YEAR_OF_ERA}422* <li>{@code YEAR}423* <li>{@code ERA}424* </ul>425* All other {@code ChronoField} instances will return false.426* <p>427* If the field is not a {@code ChronoField}, then the result of this method428* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}429* passing {@code this} as the argument.430* Whether the field is supported is determined by the field.431*432* @param field the field to check, null returns false433* @return true if the field is supported on this date, false if not434*/435@Override436public boolean isSupported(TemporalField field) {437if (field == ALIGNED_DAY_OF_WEEK_IN_MONTH || field == ALIGNED_DAY_OF_WEEK_IN_YEAR ||438field == ALIGNED_WEEK_OF_MONTH || field == ALIGNED_WEEK_OF_YEAR) {439return false;440}441return super.isSupported(field);442}443444@Override445public ValueRange range(TemporalField field) {446if (field instanceof ChronoField) {447if (isSupported(field)) {448ChronoField f = (ChronoField) field;449switch (f) {450case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());451case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());452case YEAR_OF_ERA: {453Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE);454jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);455jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());456return ValueRange.of(1, jcal.getActualMaximum(Calendar.YEAR));457}458}459return getChronology().range(f);460}461throw new UnsupportedTemporalTypeException("Unsupported field: " + field);462}463return field.rangeRefinedBy(this);464}465466@Override467public long getLong(TemporalField field) {468if (field instanceof ChronoField) {469// same as ISO:470// DAY_OF_WEEK, DAY_OF_MONTH, EPOCH_DAY, MONTH_OF_YEAR, PROLEPTIC_MONTH, YEAR471//472// calendar specific fields473// DAY_OF_YEAR, YEAR_OF_ERA, ERA474switch ((ChronoField) field) {475case ALIGNED_DAY_OF_WEEK_IN_MONTH:476case ALIGNED_DAY_OF_WEEK_IN_YEAR:477case ALIGNED_WEEK_OF_MONTH:478case ALIGNED_WEEK_OF_YEAR:479throw new UnsupportedTemporalTypeException("Unsupported field: " + field);480case YEAR_OF_ERA:481return yearOfEra;482case ERA:483return era.getValue();484case DAY_OF_YEAR:485Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE);486jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);487jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());488return jcal.get(Calendar.DAY_OF_YEAR);489}490return isoDate.getLong(field);491}492return field.getFrom(this);493}494495/**496* Returns a {@code LocalGregorianCalendar.Date} converted from the given {@code isoDate}.497*498* @param isoDate the local date, not null499* @return a {@code LocalGregorianCalendar.Date}, not null500*/501private static LocalGregorianCalendar.Date toPrivateJapaneseDate(LocalDate isoDate) {502LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);503sun.util.calendar.Era sunEra = JapaneseEra.privateEraFrom(isoDate);504int year = isoDate.getYear();505if (sunEra != null) {506year -= sunEra.getSinceDate().getYear() - 1;507}508jdate.setEra(sunEra).setYear(year).setMonth(isoDate.getMonthValue()).setDayOfMonth(isoDate.getDayOfMonth());509JapaneseChronology.JCAL.normalize(jdate);510return jdate;511}512513//-----------------------------------------------------------------------514@Override515public JapaneseDate with(TemporalField field, long newValue) {516if (field instanceof ChronoField chronoField) {517if (getLong(chronoField) == newValue) { // getLong() validates for supported fields518return this;519}520switch (chronoField) {521case YEAR_OF_ERA:522case YEAR:523case ERA: {524int nvalue = getChronology().range(chronoField).checkValidIntValue(newValue, chronoField);525switch (chronoField) {526case YEAR_OF_ERA:527return this.withYear(nvalue);528case YEAR:529return with(isoDate.withYear(nvalue));530case ERA: {531return this.withYear(JapaneseEra.of(nvalue), yearOfEra);532}533}534}535}536// YEAR, PROLEPTIC_MONTH and others are same as ISO537return with(isoDate.with(field, newValue));538}539return super.with(field, newValue);540}541542/**543* {@inheritDoc}544* @throws DateTimeException {@inheritDoc}545* @throws ArithmeticException {@inheritDoc}546*/547@Override548public JapaneseDate with(TemporalAdjuster adjuster) {549return super.with(adjuster);550}551552/**553* {@inheritDoc}554* @throws DateTimeException {@inheritDoc}555* @throws ArithmeticException {@inheritDoc}556*/557@Override558public JapaneseDate plus(TemporalAmount amount) {559return super.plus(amount);560}561562/**563* {@inheritDoc}564* @throws DateTimeException {@inheritDoc}565* @throws ArithmeticException {@inheritDoc}566*/567@Override568public JapaneseDate minus(TemporalAmount amount) {569return super.minus(amount);570}571//-----------------------------------------------------------------------572/**573* Returns a copy of this date with the year altered.574* <p>575* This method changes the year of the date.576* If the month-day is invalid for the year, then the previous valid day577* will be selected instead.578* <p>579* This instance is immutable and unaffected by this method call.580*581* @param era the era to set in the result, not null582* @param yearOfEra the year-of-era to set in the returned date583* @return a {@code JapaneseDate} based on this date with the requested year, never null584* @throws DateTimeException if {@code year} is invalid585*/586private JapaneseDate withYear(JapaneseEra era, int yearOfEra) {587int year = JapaneseChronology.INSTANCE.prolepticYear(era, yearOfEra);588return with(isoDate.withYear(year));589}590591/**592* Returns a copy of this date with the year-of-era altered.593* <p>594* This method changes the year-of-era of the date.595* If the month-day is invalid for the year, then the previous valid day596* will be selected instead.597* <p>598* This instance is immutable and unaffected by this method call.599*600* @param year the year to set in the returned date601* @return a {@code JapaneseDate} based on this date with the requested year-of-era, never null602* @throws DateTimeException if {@code year} is invalid603*/604private JapaneseDate withYear(int year) {605return withYear(getEra(), year);606}607608//-----------------------------------------------------------------------609@Override610JapaneseDate plusYears(long years) {611return with(isoDate.plusYears(years));612}613614@Override615JapaneseDate plusMonths(long months) {616return with(isoDate.plusMonths(months));617}618619@Override620JapaneseDate plusWeeks(long weeksToAdd) {621return with(isoDate.plusWeeks(weeksToAdd));622}623624@Override625JapaneseDate plusDays(long days) {626return with(isoDate.plusDays(days));627}628629@Override630public JapaneseDate plus(long amountToAdd, TemporalUnit unit) {631return super.plus(amountToAdd, unit);632}633634@Override635public JapaneseDate minus(long amountToAdd, TemporalUnit unit) {636return super.minus(amountToAdd, unit);637}638639@Override640JapaneseDate minusYears(long yearsToSubtract) {641return super.minusYears(yearsToSubtract);642}643644@Override645JapaneseDate minusMonths(long monthsToSubtract) {646return super.minusMonths(monthsToSubtract);647}648649@Override650JapaneseDate minusWeeks(long weeksToSubtract) {651return super.minusWeeks(weeksToSubtract);652}653654@Override655JapaneseDate minusDays(long daysToSubtract) {656return super.minusDays(daysToSubtract);657}658659private JapaneseDate with(LocalDate newDate) {660return (newDate.equals(isoDate) ? this : new JapaneseDate(newDate));661}662663@Override // for javadoc and covariant return type664@SuppressWarnings("unchecked")665public final ChronoLocalDateTime<JapaneseDate> atTime(LocalTime localTime) {666return (ChronoLocalDateTime<JapaneseDate>)super.atTime(localTime);667}668669@Override670public ChronoPeriod until(ChronoLocalDate endDate) {671Period period = isoDate.until(endDate);672return getChronology().period(period.getYears(), period.getMonths(), period.getDays());673}674675@Override // override for performance676public long toEpochDay() {677return isoDate.toEpochDay();678}679680//-------------------------------------------------------------------------681/**682* Compares this date to another date, including the chronology.683* <p>684* Compares this {@code JapaneseDate} with another ensuring that the date is the same.685* <p>686* Only objects of type {@code JapaneseDate} are compared, other types return false.687* To compare the dates of two {@code TemporalAccessor} instances, including dates688* in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.689*690* @param obj the object to check, null returns false691* @return true if this is equal to the other date692*/693@Override // override for performance694public boolean equals(Object obj) {695if (this == obj) {696return true;697}698return (obj instanceof JapaneseDate otherDate)699&& this.isoDate.equals(otherDate.isoDate);700}701702/**703* A hash code for this date.704*705* @return a suitable hash code based only on the Chronology and the date706*/707@Override // override for performance708public int hashCode() {709return getChronology().getId().hashCode() ^ isoDate.hashCode();710}711712//-----------------------------------------------------------------------713/**714* Defend against malicious streams.715*716* @param s the stream to read717* @throws InvalidObjectException always718*/719@java.io.Serial720private void readObject(ObjectInputStream s) throws InvalidObjectException {721throw new InvalidObjectException("Deserialization via serialization delegate");722}723724/**725* Writes the object using a726* <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.727* @serialData728* <pre>729* out.writeByte(4); // identifies a JapaneseDate730* out.writeInt(get(YEAR));731* out.writeByte(get(MONTH_OF_YEAR));732* out.writeByte(get(DAY_OF_MONTH));733* </pre>734*735* @return the instance of {@code Ser}, not null736*/737@java.io.Serial738private Object writeReplace() {739return new Ser(Ser.JAPANESE_DATE_TYPE, this);740}741742void writeExternal(DataOutput out) throws IOException {743// JapaneseChronology is implicit in the JAPANESE_DATE_TYPE744out.writeInt(get(YEAR));745out.writeByte(get(MONTH_OF_YEAR));746out.writeByte(get(DAY_OF_MONTH));747}748749static JapaneseDate readExternal(DataInput in) throws IOException {750int year = in.readInt();751int month = in.readByte();752int dayOfMonth = in.readByte();753return JapaneseChronology.INSTANCE.date(year, month, dayOfMonth);754}755756}757758759