1 /*
  2  * Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 /*
 27  * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
 28  *
 29  * All rights reserved.
 30  *
 31  * Redistribution and use in source and binary forms, with or without
 32  * modification, are permitted provided that the following conditions are met:
 33  *
 34  *  * Redistributions of source code must retain the above copyright notice,
 35  *    this list of conditions and the following disclaimer.
 36  *
 37  *  * Redistributions in binary form must reproduce the above copyright notice,
 38  *    this list of conditions and the following disclaimer in the documentation
 39  *    and/or other materials provided with the distribution.
 40  *
 41  *  * Neither the name of JSR-310 nor the names of its contributors
 42  *    may be used to endorse or promote products derived from this software
 43  *    without specific prior written permission.
 44  *
 45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 56  */
 57 package java.time.chrono;
 58 
 59 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;
 60 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;
 61 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;
 62 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
 63 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
 64 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
 65 import static java.time.temporal.ChronoField.YEAR;
 66 
 67 import java.io.DataInput;
 68 import java.io.DataOutput;
 69 import java.io.IOException;
 70 import java.io.InvalidObjectException;
 71 import java.io.ObjectInputStream;
 72 import java.io.Serializable;
 73 import java.time.Clock;
 74 import java.time.DateTimeException;
 75 import java.time.LocalDate;
 76 import java.time.LocalTime;
 77 import java.time.Period;
 78 import java.time.ZoneId;
 79 import java.time.temporal.ChronoField;
 80 import java.time.temporal.TemporalAccessor;
 81 import java.time.temporal.TemporalAdjuster;
 82 import java.time.temporal.TemporalAmount;
 83 import java.time.temporal.TemporalField;
 84 import java.time.temporal.TemporalQuery;
 85 import java.time.temporal.TemporalUnit;
 86 import java.time.temporal.UnsupportedTemporalTypeException;
 87 import java.time.temporal.ValueRange;
 88 import java.util.Calendar;
 89 import java.util.Objects;
 90 
 91 import sun.util.calendar.CalendarDate;
 92 import sun.util.calendar.LocalGregorianCalendar;
 93 
 94 /**
 95  * A date in the Japanese Imperial calendar system.
 96  * <p>
 97  * This date operates using the {@linkplain JapaneseChronology Japanese Imperial calendar}.
 98  * This calendar system is primarily used in Japan.
 99  * <p>
100  * The Japanese Imperial calendar system is the same as the ISO calendar system
101  * apart from the era-based year numbering. The proleptic-year is defined to be
102  * equal to the ISO proleptic-year.
103  * <p>
104  * Japan introduced the Gregorian calendar starting with Meiji 6.
105  * Only Meiji and later eras are supported;
106  * dates before Meiji 6, January 1 are not supported.
107  * <p>
108  * For example, the Japanese year "Heisei 24" corresponds to ISO year "2012".<br>
109  * Calling {@code japaneseDate.get(YEAR_OF_ERA)} will return 24.<br>
110  * Calling {@code japaneseDate.get(YEAR)} will return 2012.<br>
111  * Calling {@code japaneseDate.get(ERA)} will return 2, corresponding to
112  * {@code JapaneseChronology.ERA_HEISEI}.<br>
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 are
116  * {@linkplain #equals(Object) equal} as interchangeable and should not
117  * use instances for synchronization, or unpredictable behavior may
118  * occur. For example, in a future release, synchronization may fail.
119  * The {@code equals} method should be used for comparisons.
120  *
121  * @implSpec
122  * This class is immutable and thread-safe.
123  *
124  * @since 1.8
125  */
126 @jdk.internal.ValueBased
127 @jdk.internal.MigratedValueClass
128 public final class JapaneseDate
129         extends ChronoLocalDateImpl<JapaneseDate>
130         implements ChronoLocalDate, Serializable {
131 
132     /**
133      * Serialization version.
134      */
135     @java.io.Serial
136     private static final long serialVersionUID = -305327627230580483L;
137 
138     /**
139      * The underlying ISO local date.
140      */
141     private final transient LocalDate isoDate;
142     /**
143      * The JapaneseEra of this date.
144      */
145     private final transient JapaneseEra era;
146     /**
147      * The Japanese imperial calendar year of this date.
148      */
149     private final transient int yearOfEra;
150 
151     /**
152      * The first day supported by the JapaneseChronology is Meiji 6, January 1st.
153      */
154     static final LocalDate MEIJI_6_ISODATE = LocalDate.of(1873, 1, 1);
155 
156     //-----------------------------------------------------------------------
157     /**
158      * Obtains the current {@code JapaneseDate} from the system clock in the default time-zone.
159      * <p>
160      * This will query the {@link Clock#systemDefaultZone() system clock} in the default
161      * time-zone to obtain the current date.
162      * <p>
163      * Using this method will prevent the ability to use an alternate clock for testing
164      * because the clock is hard-coded.
165      *
166      * @return the current date using the system clock and default time-zone, not null
167      */
168     public static JapaneseDate now() {
169         return now(Clock.systemDefaultZone());
170     }
171 
172     /**
173      * Obtains the current {@code JapaneseDate} from the system clock in the specified time-zone.
174      * <p>
175      * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date.
176      * Specifying the time-zone avoids dependence on the default time-zone.
177      * <p>
178      * Using this method will prevent the ability to use an alternate clock for testing
179      * because the clock is hard-coded.
180      *
181      * @param zone  the zone ID to use, not null
182      * @return the current date using the system clock, not null
183      */
184     public static JapaneseDate now(ZoneId zone) {
185         return now(Clock.system(zone));
186     }
187 
188     /**
189      * Obtains the current {@code JapaneseDate} from the specified clock.
190      * <p>
191      * This will query the specified clock to obtain the current date - today.
192      * Using this method allows the use of an alternate clock for testing.
193      * The alternate clock may be introduced using {@linkplain Clock dependency injection}.
194      *
195      * @param clock  the clock to use, not null
196      * @return the current date, not null
197      * @throws DateTimeException if the current date cannot be obtained
198      */
199     public static JapaneseDate now(Clock clock) {
200         return new JapaneseDate(LocalDate.now(clock));
201     }
202 
203     /**
204      * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar
205      * system from the era, year-of-era, month-of-year and day-of-month fields.
206      * <p>
207      * This returns a {@code JapaneseDate} with the specified fields.
208      * The day must be valid for the year and month, otherwise an exception will be thrown.
209      * <p>
210      * The Japanese month and day-of-month are the same as those in the
211      * ISO calendar system. They are not reset when the era changes.
212      * For example:
213      * <pre>
214      *  6th Jan Showa 64 = ISO 1989-01-06
215      *  7th Jan Showa 64 = ISO 1989-01-07
216      *  8th Jan Heisei 1 = ISO 1989-01-08
217      *  9th Jan Heisei 1 = ISO 1989-01-09
218      * </pre>
219      *
220      * @param era  the Japanese era, not null
221      * @param yearOfEra  the Japanese year-of-era
222      * @param month  the Japanese month-of-year, from 1 to 12
223      * @param dayOfMonth  the Japanese day-of-month, from 1 to 31
224      * @return the date in Japanese calendar system, not null
225      * @throws DateTimeException if the value of any field is out of range,
226      *  or if the day-of-month is invalid for the month-year,
227      *  or if the date is not a Japanese era
228      */
229     public static JapaneseDate of(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) {
230         Objects.requireNonNull(era, "era");
231         LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);
232         jdate.setEra(era.getPrivateEra()).setDate(yearOfEra, month, dayOfMonth);
233         if (!JapaneseChronology.JCAL.validate(jdate)) {
234             throw new DateTimeException("year, month, and day not valid for Era");
235         }
236         LocalDate date = LocalDate.of(jdate.getNormalizedYear(), month, dayOfMonth);
237         return new JapaneseDate(era, yearOfEra, date);
238     }
239 
240     /**
241      * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar
242      * system from the proleptic-year, month-of-year and day-of-month fields.
243      * <p>
244      * This returns a {@code JapaneseDate} with the specified fields.
245      * The day must be valid for the year and month, otherwise an exception will be thrown.
246      * <p>
247      * The Japanese proleptic year, month and day-of-month are the same as those
248      * in the ISO calendar system. They are not reset when the era changes.
249      *
250      * @param prolepticYear  the Japanese proleptic-year
251      * @param month  the Japanese month-of-year, from 1 to 12
252      * @param dayOfMonth  the Japanese day-of-month, from 1 to 31
253      * @return the date in Japanese calendar system, not null
254      * @throws DateTimeException if the value of any field is out of range,
255      *  or if the day-of-month is invalid for the month-year
256      */
257     public static JapaneseDate of(int prolepticYear, int month, int dayOfMonth) {
258         return new JapaneseDate(LocalDate.of(prolepticYear, month, dayOfMonth));
259     }
260 
261     /**
262      * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar
263      * system from the era, year-of-era and day-of-year fields.
264      * <p>
265      * This returns a {@code JapaneseDate} with the specified fields.
266      * The day must be valid for the year, otherwise an exception will be thrown.
267      * <p>
268      * The day-of-year in this factory is expressed relative to the start of the year-of-era.
269      * This definition changes the normal meaning of day-of-year only in those years
270      * where the year-of-era is reset to one due to a change in the era.
271      * For example:
272      * <pre>
273      *  6th Jan Showa 64 = day-of-year 6
274      *  7th Jan Showa 64 = day-of-year 7
275      *  8th Jan Heisei 1 = day-of-year 1
276      *  9th Jan Heisei 1 = day-of-year 2
277      * </pre>
278      *
279      * @param era  the Japanese era, not null
280      * @param yearOfEra  the Japanese year-of-era
281      * @param dayOfYear  the chronology day-of-year, from 1 to 366
282      * @return the date in Japanese calendar system, not null
283      * @throws DateTimeException if the value of any field is out of range,
284      *  or if the day-of-year is invalid for the year
285      */
286     static JapaneseDate ofYearDay(JapaneseEra era, int yearOfEra, int dayOfYear) {
287         Objects.requireNonNull(era, "era");
288         CalendarDate firstDay = era.getPrivateEra().getSinceDate();
289         LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);
290         jdate.setEra(era.getPrivateEra());
291         if (yearOfEra == 1) {
292             jdate.setDate(yearOfEra, firstDay.getMonth(), firstDay.getDayOfMonth() + dayOfYear - 1);
293         } else {
294             jdate.setDate(yearOfEra, 1, dayOfYear);
295         }
296         JapaneseChronology.JCAL.normalize(jdate);
297         if (era.getPrivateEra() != jdate.getEra() || yearOfEra != jdate.getYear()) {
298             throw new DateTimeException("Invalid parameters");
299         }
300         LocalDate localdate = LocalDate.of(jdate.getNormalizedYear(),
301                                       jdate.getMonth(), jdate.getDayOfMonth());
302         return new JapaneseDate(era, yearOfEra, localdate);
303     }
304 
305     /**
306      * Obtains a {@code JapaneseDate} from a temporal object.
307      * <p>
308      * This obtains a date in the Japanese calendar system based on the specified temporal.
309      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
310      * which this factory converts to an instance of {@code JapaneseDate}.
311      * <p>
312      * The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY}
313      * field, which is standardized across calendar systems.
314      * <p>
315      * This method matches the signature of the functional interface {@link TemporalQuery}
316      * allowing it to be used as a query via method reference, {@code JapaneseDate::from}.
317      *
318      * @param temporal  the temporal object to convert, not null
319      * @return the date in Japanese calendar system, not null
320      * @throws DateTimeException if unable to convert to a {@code JapaneseDate}
321      */
322     public static JapaneseDate from(TemporalAccessor temporal) {
323         return JapaneseChronology.INSTANCE.date(temporal);
324     }
325 
326     //-----------------------------------------------------------------------
327     /**
328      * Creates an instance from an ISO date.
329      *
330      * @param isoDate  the standard local date, validated not null
331      */
332     JapaneseDate(LocalDate isoDate) {
333         if (isoDate.isBefore(MEIJI_6_ISODATE)) {
334             throw new DateTimeException("JapaneseDate before Meiji 6 is not supported");
335         }
336         LocalGregorianCalendar.Date jdate = toPrivateJapaneseDate(isoDate);
337         this.era = JapaneseEra.toJapaneseEra(jdate.getEra());
338         this.yearOfEra = jdate.getYear();
339         this.isoDate = isoDate;
340     }
341 
342     /**
343      * Constructs a {@code JapaneseDate}. This constructor does NOT validate the given parameters,
344      * and {@code era} and {@code year} must agree with {@code isoDate}.
345      *
346      * @param era  the era, validated not null
347      * @param year  the year-of-era, validated
348      * @param isoDate  the standard local date, validated not null
349      */
350     JapaneseDate(JapaneseEra era, int year, LocalDate isoDate) {
351         if (isoDate.isBefore(MEIJI_6_ISODATE)) {
352             throw new DateTimeException("JapaneseDate before Meiji 6 is not supported");
353         }
354         this.era = era;
355         this.yearOfEra = year;
356         this.isoDate = isoDate;
357     }
358 
359     //-----------------------------------------------------------------------
360     /**
361      * Gets the chronology of this date, which is the Japanese calendar system.
362      * <p>
363      * The {@code Chronology} represents the calendar system in use.
364      * The era and other fields in {@link ChronoField} are defined by the chronology.
365      *
366      * @return the Japanese chronology, not null
367      */
368     @Override
369     public JapaneseChronology getChronology() {
370         return JapaneseChronology.INSTANCE;
371     }
372 
373     /**
374      * Gets the era applicable at this date.
375      * <p>
376      * The Japanese calendar system has multiple eras defined by {@link JapaneseEra}.
377      *
378      * @return the era applicable at this date, not null
379      */
380     @Override
381     public JapaneseEra getEra() {
382         return era;
383     }
384 
385     /**
386      * Returns the length of the month represented by this date.
387      * <p>
388      * This returns the length of the month in days.
389      * Month lengths match those of the ISO calendar system.
390      *
391      * @return the length of the month in days
392      */
393     @Override
394     public int lengthOfMonth() {
395         return isoDate.lengthOfMonth();
396     }
397 
398     @Override
399     public int lengthOfYear() {
400         Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE);
401         jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);
402         jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());
403         return  jcal.getActualMaximum(Calendar.DAY_OF_YEAR);
404     }
405 
406     //-----------------------------------------------------------------------
407     /**
408      * Checks if the specified field is supported.
409      * <p>
410      * This checks if this date can be queried for the specified field.
411      * If false, then calling the {@link #range(TemporalField) range} and
412      * {@link #get(TemporalField) get} methods will throw an exception.
413      * <p>
414      * If the field is a {@link ChronoField} then the query is implemented here.
415      * The supported fields are:
416      * <ul>
417      * <li>{@code DAY_OF_WEEK}
418      * <li>{@code DAY_OF_MONTH}
419      * <li>{@code DAY_OF_YEAR}
420      * <li>{@code EPOCH_DAY}
421      * <li>{@code MONTH_OF_YEAR}
422      * <li>{@code PROLEPTIC_MONTH}
423      * <li>{@code YEAR_OF_ERA}
424      * <li>{@code YEAR}
425      * <li>{@code ERA}
426      * </ul>
427      * All other {@code ChronoField} instances will return false.
428      * <p>
429      * If the field is not a {@code ChronoField}, then the result of this method
430      * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
431      * passing {@code this} as the argument.
432      * Whether the field is supported is determined by the field.
433      *
434      * @param field  the field to check, null returns false
435      * @return true if the field is supported on this date, false if not
436      */
437     @Override
438     public boolean isSupported(TemporalField field) {
439         if (field == ALIGNED_DAY_OF_WEEK_IN_MONTH || field == ALIGNED_DAY_OF_WEEK_IN_YEAR ||
440                 field == ALIGNED_WEEK_OF_MONTH || field == ALIGNED_WEEK_OF_YEAR) {
441             return false;
442         }
443         return super.isSupported(field);
444     }
445 
446     @Override
447     public ValueRange range(TemporalField field) {
448         if (field instanceof ChronoField chronoField) {
449             if (isSupported(field)) {
450                 return switch (chronoField) {
451                     case DAY_OF_MONTH -> ValueRange.of(1, lengthOfMonth());
452                     case DAY_OF_YEAR -> ValueRange.of(1, lengthOfYear());
453                     case YEAR_OF_ERA -> {
454                         Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE);
455                         jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);
456                         jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());
457                         yield ValueRange.of(1, jcal.getActualMaximum(Calendar.YEAR));
458                     }
459                     default -> getChronology().range(chronoField);
460                 };
461             }
462             throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
463         }
464         return field.rangeRefinedBy(this);
465     }
466 
467     @Override
468     public long getLong(TemporalField field) {
469         if (field instanceof ChronoField cf) {
470             // same as ISO:
471             // DAY_OF_WEEK, DAY_OF_MONTH, EPOCH_DAY, MONTH_OF_YEAR, PROLEPTIC_MONTH, YEAR
472             //
473             // calendar specific fields
474             // DAY_OF_YEAR, YEAR_OF_ERA, ERA
475             switch (cf) {
476                 case ALIGNED_DAY_OF_WEEK_IN_MONTH:
477                 case ALIGNED_DAY_OF_WEEK_IN_YEAR:
478                 case ALIGNED_WEEK_OF_MONTH:
479                 case ALIGNED_WEEK_OF_YEAR:
480                     throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
481                 case YEAR_OF_ERA:
482                     return yearOfEra;
483                 case ERA:
484                     return era.getValue();
485                 case DAY_OF_YEAR:
486                     Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE);
487                     jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);
488                     jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());
489                     return jcal.get(Calendar.DAY_OF_YEAR);
490             }
491             return isoDate.getLong(field);
492         }
493         return field.getFrom(this);
494     }
495 
496     /**
497      * Returns a {@code LocalGregorianCalendar.Date} converted from the given {@code isoDate}.
498      *
499      * @param isoDate  the local date, not null
500      * @return a {@code LocalGregorianCalendar.Date}, not null
501      */
502     private static LocalGregorianCalendar.Date toPrivateJapaneseDate(LocalDate isoDate) {
503         LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);
504         sun.util.calendar.Era sunEra = JapaneseEra.privateEraFrom(isoDate);
505         int year = isoDate.getYear();
506         if (sunEra != null) {
507             year -= sunEra.getSinceDate().getYear() - 1;
508         }
509         jdate.setEra(sunEra).setYear(year).setMonth(isoDate.getMonthValue()).setDayOfMonth(isoDate.getDayOfMonth());
510         JapaneseChronology.JCAL.normalize(jdate);
511         return jdate;
512     }
513 
514     //-----------------------------------------------------------------------
515     @Override
516     public JapaneseDate with(TemporalField field, long newValue) {
517         if (field instanceof ChronoField chronoField) {
518             if (getLong(chronoField) == newValue) {  // getLong() validates for supported fields
519                 return this;
520             }
521             switch (chronoField) {
522                 case YEAR_OF_ERA:
523                 case YEAR:
524                 case ERA: {
525                     int nvalue = getChronology().range(chronoField).checkValidIntValue(newValue, chronoField);
526                     switch (chronoField) {
527                         case YEAR_OF_ERA:
528                             return this.withYear(nvalue);
529                         case YEAR:
530                             return with(isoDate.withYear(nvalue));
531                         case ERA: {
532                             return this.withYear(JapaneseEra.of(nvalue), yearOfEra);
533                         }
534                     }
535                 }
536             }
537             // YEAR, PROLEPTIC_MONTH and others are same as ISO
538             return with(isoDate.with(field, newValue));
539         }
540         return super.with(field, newValue);
541     }
542 
543     /**
544      * {@inheritDoc}
545      * @throws DateTimeException {@inheritDoc}
546      * @throws ArithmeticException {@inheritDoc}
547      */
548     @Override
549     public  JapaneseDate with(TemporalAdjuster adjuster) {
550         return super.with(adjuster);
551     }
552 
553     /**
554      * {@inheritDoc}
555      * @throws DateTimeException {@inheritDoc}
556      * @throws ArithmeticException {@inheritDoc}
557      */
558     @Override
559     public JapaneseDate plus(TemporalAmount amount) {
560         return super.plus(amount);
561     }
562 
563     /**
564      * {@inheritDoc}
565      * @throws DateTimeException {@inheritDoc}
566      * @throws ArithmeticException {@inheritDoc}
567      */
568     @Override
569     public JapaneseDate minus(TemporalAmount amount) {
570         return super.minus(amount);
571     }
572     //-----------------------------------------------------------------------
573     /**
574      * Returns a copy of this date with the year altered.
575      * <p>
576      * This method changes the year of the date.
577      * If the month-day is invalid for the year, then the previous valid day
578      * will be selected instead.
579      * <p>
580      * This instance is immutable and unaffected by this method call.
581      *
582      * @param era  the era to set in the result, not null
583      * @param yearOfEra  the year-of-era to set in the returned date
584      * @return a {@code JapaneseDate} based on this date with the requested year, never null
585      * @throws DateTimeException if {@code year} is invalid
586      */
587     private JapaneseDate withYear(JapaneseEra era, int yearOfEra) {
588         int year = JapaneseChronology.INSTANCE.prolepticYear(era, yearOfEra);
589         return with(isoDate.withYear(year));
590     }
591 
592     /**
593      * Returns a copy of this date with the year-of-era altered.
594      * <p>
595      * This method changes the year-of-era of the date.
596      * If the month-day is invalid for the year, then the previous valid day
597      * will be selected instead.
598      * <p>
599      * This instance is immutable and unaffected by this method call.
600      *
601      * @param year  the year to set in the returned date
602      * @return a {@code JapaneseDate} based on this date with the requested year-of-era, never null
603      * @throws DateTimeException if {@code year} is invalid
604      */
605     private JapaneseDate withYear(int year) {
606         return withYear(getEra(), year);
607     }
608 
609     //-----------------------------------------------------------------------
610     @Override
611     JapaneseDate plusYears(long years) {
612         return with(isoDate.plusYears(years));
613     }
614 
615     @Override
616     JapaneseDate plusMonths(long months) {
617         return with(isoDate.plusMonths(months));
618     }
619 
620     @Override
621     JapaneseDate plusWeeks(long weeksToAdd) {
622         return with(isoDate.plusWeeks(weeksToAdd));
623     }
624 
625     @Override
626     JapaneseDate plusDays(long days) {
627         return with(isoDate.plusDays(days));
628     }
629 
630     @Override
631     public JapaneseDate plus(long amountToAdd, TemporalUnit unit) {
632         return super.plus(amountToAdd, unit);
633     }
634 
635     @Override
636     public JapaneseDate minus(long amountToSubtract, TemporalUnit unit) {
637         return super.minus(amountToSubtract, unit);
638     }
639 
640     @Override
641     JapaneseDate minusYears(long yearsToSubtract) {
642         return super.minusYears(yearsToSubtract);
643     }
644 
645     @Override
646     JapaneseDate minusMonths(long monthsToSubtract) {
647         return super.minusMonths(monthsToSubtract);
648     }
649 
650     @Override
651     JapaneseDate minusWeeks(long weeksToSubtract) {
652         return super.minusWeeks(weeksToSubtract);
653     }
654 
655     @Override
656     JapaneseDate minusDays(long daysToSubtract) {
657         return super.minusDays(daysToSubtract);
658     }
659 
660     private JapaneseDate with(LocalDate newDate) {
661         return (newDate.equals(isoDate) ? this : new JapaneseDate(newDate));
662     }
663 
664     @Override        // for javadoc and covariant return type
665     @SuppressWarnings("unchecked")
666     public final ChronoLocalDateTime<JapaneseDate> atTime(LocalTime localTime) {
667         return (ChronoLocalDateTime<JapaneseDate>)super.atTime(localTime);
668     }
669 
670     @Override
671     public ChronoPeriod until(ChronoLocalDate endDate) {
672         Period period = isoDate.until(endDate);
673         return getChronology().period(period.getYears(), period.getMonths(), period.getDays());
674     }
675 
676     @Override  // override for performance
677     public long toEpochDay() {
678         return isoDate.toEpochDay();
679     }
680 
681     //-------------------------------------------------------------------------
682     /**
683      * Compares this date to another date, including the chronology.
684      * <p>
685      * Compares this {@code JapaneseDate} with another ensuring that the date is the same.
686      * <p>
687      * Only objects of type {@code JapaneseDate} are compared, other types return false.
688      * To compare the dates of two {@code TemporalAccessor} instances, including dates
689      * in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.
690      *
691      * @param obj  the object to check, null returns false
692      * @return true if this is equal to the other date
693      */
694     @Override  // override for performance
695     public boolean equals(Object obj) {
696         if (this == obj) {
697             return true;
698         }
699         return (obj instanceof JapaneseDate otherDate)
700             && this.isoDate.equals(otherDate.isoDate);
701     }
702 
703     /**
704      * A hash code for this date.
705      *
706      * @return a suitable hash code based only on the Chronology and the date
707      */
708     @Override  // override for performance
709     public int hashCode() {
710         return getChronology().getId().hashCode() ^ isoDate.hashCode();
711     }
712 
713     //-----------------------------------------------------------------------
714     /**
715      * Defend against malicious streams.
716      *
717      * @param s the stream to read
718      * @throws InvalidObjectException always
719      */
720     @java.io.Serial
721     private void readObject(ObjectInputStream s) throws InvalidObjectException {
722         throw new InvalidObjectException("Deserialization via serialization delegate");
723     }
724 
725     /**
726      * Writes the object using a
727      * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
728      * @serialData
729      * <pre>
730      *  out.writeByte(4);                 // identifies a JapaneseDate
731      *  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 null
737      */
738     @java.io.Serial
739     private Object writeReplace() {
740         return new Ser(Ser.JAPANESE_DATE_TYPE, this);
741     }
742 
743     void writeExternal(DataOutput out) throws IOException {
744         // JapaneseChronology is implicit in the JAPANESE_DATE_TYPE
745         out.writeInt(get(YEAR));
746         out.writeByte(get(MONTH_OF_YEAR));
747         out.writeByte(get(DAY_OF_MONTH));
748     }
749 
750     static JapaneseDate readExternal(DataInput in) throws IOException {
751         int year = in.readInt();
752         int month = in.readByte();
753         int dayOfMonth = in.readByte();
754         return JapaneseChronology.INSTANCE.date(year, month, dayOfMonth);
755     }
756 
757 }