Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/time/chrono/JapaneseDate.java
38918 views
/*1* Copyright (c) 2012, 2013, 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*113* <p>114* This is a <a href="{@docRoot}/java/lang/doc-files/ValueBased.html">value-based</a>115* class; use of identity-sensitive operations (including reference equality116* ({@code ==}), identity hash code, or synchronization) on instances of117* {@code JapaneseDate} may have unpredictable results and should be avoided.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*/125public final class JapaneseDate126extends ChronoLocalDateImpl<JapaneseDate>127implements ChronoLocalDate, Serializable {128129/**130* Serialization version.131*/132private static final long serialVersionUID = -305327627230580483L;133134/**135* The underlying ISO local date.136*/137private final transient LocalDate isoDate;138/**139* The JapaneseEra of this date.140*/141private transient JapaneseEra era;142/**143* The Japanese imperial calendar year of this date.144*/145private transient int yearOfEra;146147/**148* The first day supported by the JapaneseChronology is Meiji 6, January 1st.149*/150static final LocalDate MEIJI_6_ISODATE = LocalDate.of(1873, 1, 1);151152//-----------------------------------------------------------------------153/**154* Obtains the current {@code JapaneseDate} from the system clock in the default time-zone.155* <p>156* This will query the {@link Clock#systemDefaultZone() system clock} in the default157* time-zone to obtain the current date.158* <p>159* Using this method will prevent the ability to use an alternate clock for testing160* because the clock is hard-coded.161*162* @return the current date using the system clock and default time-zone, not null163*/164public static JapaneseDate now() {165return now(Clock.systemDefaultZone());166}167168/**169* Obtains the current {@code JapaneseDate} from the system clock in the specified time-zone.170* <p>171* This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date.172* Specifying the time-zone avoids dependence on the default time-zone.173* <p>174* Using this method will prevent the ability to use an alternate clock for testing175* because the clock is hard-coded.176*177* @param zone the zone ID to use, not null178* @return the current date using the system clock, not null179*/180public static JapaneseDate now(ZoneId zone) {181return now(Clock.system(zone));182}183184/**185* Obtains the current {@code JapaneseDate} from the specified clock.186* <p>187* This will query the specified clock to obtain the current date - today.188* Using this method allows the use of an alternate clock for testing.189* The alternate clock may be introduced using {@linkplain Clock dependency injection}.190*191* @param clock the clock to use, not null192* @return the current date, not null193* @throws DateTimeException if the current date cannot be obtained194*/195public static JapaneseDate now(Clock clock) {196return new JapaneseDate(LocalDate.now(clock));197}198199/**200* Obtains a {@code JapaneseDate} representing a date in the Japanese calendar201* system from the era, year-of-era, month-of-year and day-of-month fields.202* <p>203* This returns a {@code JapaneseDate} with the specified fields.204* The day must be valid for the year and month, otherwise an exception will be thrown.205* <p>206* The Japanese month and day-of-month are the same as those in the207* ISO calendar system. They are not reset when the era changes.208* For example:209* <pre>210* 6th Jan Showa 64 = ISO 1989-01-06211* 7th Jan Showa 64 = ISO 1989-01-07212* 8th Jan Heisei 1 = ISO 1989-01-08213* 9th Jan Heisei 1 = ISO 1989-01-09214* </pre>215*216* @param era the Japanese era, not null217* @param yearOfEra the Japanese year-of-era218* @param month the Japanese month-of-year, from 1 to 12219* @param dayOfMonth the Japanese day-of-month, from 1 to 31220* @return the date in Japanese calendar system, not null221* @throws DateTimeException if the value of any field is out of range,222* or if the day-of-month is invalid for the month-year,223* or if the date is not a Japanese era224*/225public static JapaneseDate of(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) {226Objects.requireNonNull(era, "era");227LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);228jdate.setEra(era.getPrivateEra()).setDate(yearOfEra, month, dayOfMonth);229if (!JapaneseChronology.JCAL.validate(jdate)) {230throw new DateTimeException("year, month, and day not valid for Era");231}232LocalDate date = LocalDate.of(jdate.getNormalizedYear(), month, dayOfMonth);233return new JapaneseDate(era, yearOfEra, date);234}235236/**237* Obtains a {@code JapaneseDate} representing a date in the Japanese calendar238* system from the proleptic-year, month-of-year and day-of-month fields.239* <p>240* This returns a {@code JapaneseDate} with the specified fields.241* The day must be valid for the year and month, otherwise an exception will be thrown.242* <p>243* The Japanese proleptic year, month and day-of-month are the same as those244* in the ISO calendar system. They are not reset when the era changes.245*246* @param prolepticYear the Japanese proleptic-year247* @param month the Japanese month-of-year, from 1 to 12248* @param dayOfMonth the Japanese day-of-month, from 1 to 31249* @return the date in Japanese calendar system, not null250* @throws DateTimeException if the value of any field is out of range,251* or if the day-of-month is invalid for the month-year252*/253public static JapaneseDate of(int prolepticYear, int month, int dayOfMonth) {254return new JapaneseDate(LocalDate.of(prolepticYear, month, dayOfMonth));255}256257/**258* Obtains a {@code JapaneseDate} representing a date in the Japanese calendar259* system from the era, year-of-era and day-of-year fields.260* <p>261* This returns a {@code JapaneseDate} with the specified fields.262* The day must be valid for the year, otherwise an exception will be thrown.263* <p>264* The day-of-year in this factory is expressed relative to the start of the year-of-era.265* This definition changes the normal meaning of day-of-year only in those years266* where the year-of-era is reset to one due to a change in the era.267* For example:268* <pre>269* 6th Jan Showa 64 = day-of-year 6270* 7th Jan Showa 64 = day-of-year 7271* 8th Jan Heisei 1 = day-of-year 1272* 9th Jan Heisei 1 = day-of-year 2273* </pre>274*275* @param era the Japanese era, not null276* @param yearOfEra the Japanese year-of-era277* @param dayOfYear the chronology day-of-year, from 1 to 366278* @return the date in Japanese calendar system, not null279* @throws DateTimeException if the value of any field is out of range,280* or if the day-of-year is invalid for the year281*/282static JapaneseDate ofYearDay(JapaneseEra era, int yearOfEra, int dayOfYear) {283Objects.requireNonNull(era, "era");284CalendarDate firstDay = era.getPrivateEra().getSinceDate();285LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);286jdate.setEra(era.getPrivateEra());287if (yearOfEra == 1) {288jdate.setDate(yearOfEra, firstDay.getMonth(), firstDay.getDayOfMonth() + dayOfYear - 1);289} else {290jdate.setDate(yearOfEra, 1, dayOfYear);291}292JapaneseChronology.JCAL.normalize(jdate);293if (era.getPrivateEra() != jdate.getEra() || yearOfEra != jdate.getYear()) {294throw new DateTimeException("Invalid parameters");295}296LocalDate localdate = LocalDate.of(jdate.getNormalizedYear(),297jdate.getMonth(), jdate.getDayOfMonth());298return new JapaneseDate(era, yearOfEra, localdate);299}300301/**302* Obtains a {@code JapaneseDate} from a temporal object.303* <p>304* This obtains a date in the Japanese calendar system based on the specified temporal.305* A {@code TemporalAccessor} represents an arbitrary set of date and time information,306* which this factory converts to an instance of {@code JapaneseDate}.307* <p>308* The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY}309* field, which is standardized across calendar systems.310* <p>311* This method matches the signature of the functional interface {@link TemporalQuery}312* allowing it to be used as a query via method reference, {@code JapaneseDate::from}.313*314* @param temporal the temporal object to convert, not null315* @return the date in Japanese calendar system, not null316* @throws DateTimeException if unable to convert to a {@code JapaneseDate}317*/318public static JapaneseDate from(TemporalAccessor temporal) {319return JapaneseChronology.INSTANCE.date(temporal);320}321322//-----------------------------------------------------------------------323/**324* Creates an instance from an ISO date.325*326* @param isoDate the standard local date, validated not null327*/328JapaneseDate(LocalDate isoDate) {329if (isoDate.isBefore(MEIJI_6_ISODATE)) {330throw new DateTimeException("JapaneseDate before Meiji 6 is not supported");331}332LocalGregorianCalendar.Date jdate = toPrivateJapaneseDate(isoDate);333this.era = JapaneseEra.toJapaneseEra(jdate.getEra());334this.yearOfEra = jdate.getYear();335this.isoDate = isoDate;336}337338/**339* Constructs a {@code JapaneseDate}. This constructor does NOT validate the given parameters,340* and {@code era} and {@code year} must agree with {@code isoDate}.341*342* @param era the era, validated not null343* @param year the year-of-era, validated344* @param isoDate the standard local date, validated not null345*/346JapaneseDate(JapaneseEra era, int year, LocalDate isoDate) {347if (isoDate.isBefore(MEIJI_6_ISODATE)) {348throw new DateTimeException("JapaneseDate before Meiji 6 is not supported");349}350this.era = era;351this.yearOfEra = year;352this.isoDate = isoDate;353}354355//-----------------------------------------------------------------------356/**357* Gets the chronology of this date, which is the Japanese calendar system.358* <p>359* The {@code Chronology} represents the calendar system in use.360* The era and other fields in {@link ChronoField} are defined by the chronology.361*362* @return the Japanese chronology, not null363*/364@Override365public JapaneseChronology getChronology() {366return JapaneseChronology.INSTANCE;367}368369/**370* Gets the era applicable at this date.371* <p>372* The Japanese calendar system has multiple eras defined by {@link JapaneseEra}.373*374* @return the era applicable at this date, not null375*/376@Override377public JapaneseEra getEra() {378return era;379}380381/**382* Returns the length of the month represented by this date.383* <p>384* This returns the length of the month in days.385* Month lengths match those of the ISO calendar system.386*387* @return the length of the month in days388*/389@Override390public int lengthOfMonth() {391return isoDate.lengthOfMonth();392}393394@Override395public int lengthOfYear() {396Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE);397jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);398jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());399return jcal.getActualMaximum(Calendar.DAY_OF_YEAR);400}401402//-----------------------------------------------------------------------403/**404* Checks if the specified field is supported.405* <p>406* This checks if this date can be queried for the specified field.407* If false, then calling the {@link #range(TemporalField) range} and408* {@link #get(TemporalField) get} methods will throw an exception.409* <p>410* If the field is a {@link ChronoField} then the query is implemented here.411* The supported fields are:412* <ul>413* <li>{@code DAY_OF_WEEK}414* <li>{@code DAY_OF_MONTH}415* <li>{@code DAY_OF_YEAR}416* <li>{@code EPOCH_DAY}417* <li>{@code MONTH_OF_YEAR}418* <li>{@code PROLEPTIC_MONTH}419* <li>{@code YEAR_OF_ERA}420* <li>{@code YEAR}421* <li>{@code ERA}422* </ul>423* All other {@code ChronoField} instances will return false.424* <p>425* If the field is not a {@code ChronoField}, then the result of this method426* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}427* passing {@code this} as the argument.428* Whether the field is supported is determined by the field.429*430* @param field the field to check, null returns false431* @return true if the field is supported on this date, false if not432*/433@Override434public boolean isSupported(TemporalField field) {435if (field == ALIGNED_DAY_OF_WEEK_IN_MONTH || field == ALIGNED_DAY_OF_WEEK_IN_YEAR ||436field == ALIGNED_WEEK_OF_MONTH || field == ALIGNED_WEEK_OF_YEAR) {437return false;438}439return ChronoLocalDate.super.isSupported(field);440}441442@Override443public ValueRange range(TemporalField field) {444if (field instanceof ChronoField) {445if (isSupported(field)) {446ChronoField f = (ChronoField) field;447switch (f) {448case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());449case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());450case YEAR_OF_ERA: {451Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE);452jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);453jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());454return ValueRange.of(1, jcal.getActualMaximum(Calendar.YEAR));455}456}457return getChronology().range(f);458}459throw new UnsupportedTemporalTypeException("Unsupported field: " + field);460}461return field.rangeRefinedBy(this);462}463464@Override465public long getLong(TemporalField field) {466if (field instanceof ChronoField) {467// same as ISO:468// DAY_OF_WEEK, DAY_OF_MONTH, EPOCH_DAY, MONTH_OF_YEAR, PROLEPTIC_MONTH, YEAR469//470// calendar specific fields471// DAY_OF_YEAR, YEAR_OF_ERA, ERA472switch ((ChronoField) field) {473case ALIGNED_DAY_OF_WEEK_IN_MONTH:474case ALIGNED_DAY_OF_WEEK_IN_YEAR:475case ALIGNED_WEEK_OF_MONTH:476case ALIGNED_WEEK_OF_YEAR:477throw new UnsupportedTemporalTypeException("Unsupported field: " + field);478case YEAR_OF_ERA:479return yearOfEra;480case ERA:481return era.getValue();482case DAY_OF_YEAR:483Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE);484jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);485jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());486return jcal.get(Calendar.DAY_OF_YEAR);487}488return isoDate.getLong(field);489}490return field.getFrom(this);491}492493/**494* Returns a {@code LocalGregorianCalendar.Date} converted from the given {@code isoDate}.495*496* @param isoDate the local date, not null497* @return a {@code LocalGregorianCalendar.Date}, not null498*/499private static LocalGregorianCalendar.Date toPrivateJapaneseDate(LocalDate isoDate) {500LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);501sun.util.calendar.Era sunEra = JapaneseEra.privateEraFrom(isoDate);502int year = isoDate.getYear();503if (sunEra != null) {504year -= sunEra.getSinceDate().getYear() - 1;505}506jdate.setEra(sunEra).setYear(year).setMonth(isoDate.getMonthValue()).setDayOfMonth(isoDate.getDayOfMonth());507JapaneseChronology.JCAL.normalize(jdate);508return jdate;509}510511//-----------------------------------------------------------------------512@Override513public JapaneseDate with(TemporalField field, long newValue) {514if (field instanceof ChronoField) {515ChronoField f = (ChronoField) field;516if (getLong(f) == newValue) { // getLong() validates for supported fields517return this;518}519switch (f) {520case YEAR_OF_ERA:521case YEAR:522case ERA: {523int nvalue = getChronology().range(f).checkValidIntValue(newValue, f);524switch (f) {525case YEAR_OF_ERA:526return this.withYear(nvalue);527case YEAR:528return with(isoDate.withYear(nvalue));529case ERA: {530return this.withYear(JapaneseEra.of(nvalue), yearOfEra);531}532}533}534}535// YEAR, PROLEPTIC_MONTH and others are same as ISO536return with(isoDate.with(field, newValue));537}538return super.with(field, newValue);539}540541/**542* {@inheritDoc}543* @throws DateTimeException {@inheritDoc}544* @throws ArithmeticException {@inheritDoc}545*/546@Override547public JapaneseDate with(TemporalAdjuster adjuster) {548return super.with(adjuster);549}550551/**552* {@inheritDoc}553* @throws DateTimeException {@inheritDoc}554* @throws ArithmeticException {@inheritDoc}555*/556@Override557public JapaneseDate plus(TemporalAmount amount) {558return super.plus(amount);559}560561/**562* {@inheritDoc}563* @throws DateTimeException {@inheritDoc}564* @throws ArithmeticException {@inheritDoc}565*/566@Override567public JapaneseDate minus(TemporalAmount amount) {568return super.minus(amount);569}570//-----------------------------------------------------------------------571/**572* Returns a copy of this date with the year altered.573* <p>574* This method changes the year of the date.575* If the month-day is invalid for the year, then the previous valid day576* will be selected instead.577* <p>578* This instance is immutable and unaffected by this method call.579*580* @param era the era to set in the result, not null581* @param yearOfEra the year-of-era to set in the returned date582* @return a {@code JapaneseDate} based on this date with the requested year, never null583* @throws DateTimeException if {@code year} is invalid584*/585private JapaneseDate withYear(JapaneseEra era, int yearOfEra) {586int year = JapaneseChronology.INSTANCE.prolepticYear(era, yearOfEra);587return with(isoDate.withYear(year));588}589590/**591* Returns a copy of this date with the year-of-era altered.592* <p>593* This method changes the year-of-era of the date.594* If the month-day is invalid for the year, then the previous valid day595* will be selected instead.596* <p>597* This instance is immutable and unaffected by this method call.598*599* @param year the year to set in the returned date600* @return a {@code JapaneseDate} based on this date with the requested year-of-era, never null601* @throws DateTimeException if {@code year} is invalid602*/603private JapaneseDate withYear(int year) {604return withYear(getEra(), year);605}606607//-----------------------------------------------------------------------608@Override609JapaneseDate plusYears(long years) {610return with(isoDate.plusYears(years));611}612613@Override614JapaneseDate plusMonths(long months) {615return with(isoDate.plusMonths(months));616}617618@Override619JapaneseDate plusWeeks(long weeksToAdd) {620return with(isoDate.plusWeeks(weeksToAdd));621}622623@Override624JapaneseDate plusDays(long days) {625return with(isoDate.plusDays(days));626}627628@Override629public JapaneseDate plus(long amountToAdd, TemporalUnit unit) {630return super.plus(amountToAdd, unit);631}632633@Override634public JapaneseDate minus(long amountToAdd, TemporalUnit unit) {635return super.minus(amountToAdd, unit);636}637638@Override639JapaneseDate minusYears(long yearsToSubtract) {640return super.minusYears(yearsToSubtract);641}642643@Override644JapaneseDate minusMonths(long monthsToSubtract) {645return super.minusMonths(monthsToSubtract);646}647648@Override649JapaneseDate minusWeeks(long weeksToSubtract) {650return super.minusWeeks(weeksToSubtract);651}652653@Override654JapaneseDate minusDays(long daysToSubtract) {655return super.minusDays(daysToSubtract);656}657658private JapaneseDate with(LocalDate newDate) {659return (newDate.equals(isoDate) ? this : new JapaneseDate(newDate));660}661662@Override // for javadoc and covariant return type663@SuppressWarnings("unchecked")664public final ChronoLocalDateTime<JapaneseDate> atTime(LocalTime localTime) {665return (ChronoLocalDateTime<JapaneseDate>)super.atTime(localTime);666}667668@Override669public ChronoPeriod until(ChronoLocalDate endDate) {670Period period = isoDate.until(endDate);671return getChronology().period(period.getYears(), period.getMonths(), period.getDays());672}673674@Override // override for performance675public long toEpochDay() {676return isoDate.toEpochDay();677}678679//-------------------------------------------------------------------------680/**681* Compares this date to another date, including the chronology.682* <p>683* Compares this {@code JapaneseDate} with another ensuring that the date is the same.684* <p>685* Only objects of type {@code JapaneseDate} are compared, other types return false.686* To compare the dates of two {@code TemporalAccessor} instances, including dates687* in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.688*689* @param obj the object to check, null returns false690* @return true if this is equal to the other date691*/692@Override // override for performance693public boolean equals(Object obj) {694if (this == obj) {695return true;696}697if (obj instanceof JapaneseDate) {698JapaneseDate otherDate = (JapaneseDate) obj;699return this.isoDate.equals(otherDate.isoDate);700}701return false;702}703704/**705* A hash code for this date.706*707* @return a suitable hash code based only on the Chronology and the date708*/709@Override // override for performance710public int hashCode() {711return getChronology().getId().hashCode() ^ isoDate.hashCode();712}713714//-----------------------------------------------------------------------715/**716* Defend against malicious streams.717*718* @param s the stream to read719* @throws InvalidObjectException always720*/721private void readObject(ObjectInputStream s) throws InvalidObjectException {722throw new InvalidObjectException("Deserialization via serialization delegate");723}724725/**726* Writes the object using a727* <a href="../../../serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.728* @serialData729* <pre>730* out.writeByte(4); // identifies a JapaneseDate731* out.writeInt(get(YEAR));732* out.writeByte(get(MONTH_OF_YEAR));733* out.writeByte(get(DAY_OF_MONTH));734* </pre>735*736* @return the instance of {@code Ser}, not null737*/738private 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