1 /*
2  * Copyright (c) 2012, 2019, 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 // Android-changed: removed ValueBased paragraph.
95 /**
96  * A date in the Japanese Imperial calendar system.
97  * <p>
98  * This date operates using the {@linkplain JapaneseChronology Japanese Imperial calendar}.
99  * This calendar system is primarily used in Japan.
100  * <p>
101  * The Japanese Imperial calendar system is the same as the ISO calendar system
102  * apart from the era-based year numbering. The proleptic-year is defined to be
103  * equal to the ISO proleptic-year.
104  * <p>
105  * Japan introduced the Gregorian calendar starting with Meiji 6.
106  * Only Meiji and later eras are supported;
107  * dates before Meiji 6, January 1 are not supported.
108  * <p>
109  * For example, the Japanese year "Heisei 24" corresponds to ISO year "2012".<br>
110  * Calling {@code japaneseDate.get(YEAR_OF_ERA)} will return 24.<br>
111  * Calling {@code japaneseDate.get(YEAR)} will return 2012.<br>
112  * Calling {@code japaneseDate.get(ERA)} will return 2, corresponding to
113  * {@code JapaneseChronology.ERA_HEISEI}.<br>
114  *
115  * @implSpec
116  * This class is immutable and thread-safe.
117  *
118  * @since 1.8
119  */
120 public final class JapaneseDate
121         extends ChronoLocalDateImpl<JapaneseDate>
122         implements ChronoLocalDate, Serializable {
123 
124     /**
125      * Serialization version.
126      */
127     @java.io.Serial
128     private static final long serialVersionUID = -305327627230580483L;
129 
130     /**
131      * The underlying ISO local date.
132      */
133     private final transient LocalDate isoDate;
134     /**
135      * The JapaneseEra of this date.
136      */
137     private final transient JapaneseEra era;
138     /**
139      * The Japanese imperial calendar year of this date.
140      */
141     private final transient int yearOfEra;
142 
143     /**
144      * The first day supported by the JapaneseChronology is Meiji 6, January 1st.
145      */
146     static final LocalDate MEIJI_6_ISODATE = LocalDate.of(1873, 1, 1);
147 
148     //-----------------------------------------------------------------------
149     /**
150      * Obtains the current {@code JapaneseDate} from the system clock in the default time-zone.
151      * <p>
152      * This will query the {@link Clock#systemDefaultZone() system clock} in the default
153      * time-zone to obtain the current date.
154      * <p>
155      * Using this method will prevent the ability to use an alternate clock for testing
156      * because the clock is hard-coded.
157      *
158      * @return the current date using the system clock and default time-zone, not null
159      */
now()160     public static JapaneseDate now() {
161         return now(Clock.systemDefaultZone());
162     }
163 
164     /**
165      * Obtains the current {@code JapaneseDate} from the system clock in the specified time-zone.
166      * <p>
167      * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date.
168      * Specifying the time-zone avoids dependence on the default time-zone.
169      * <p>
170      * Using this method will prevent the ability to use an alternate clock for testing
171      * because the clock is hard-coded.
172      *
173      * @param zone  the zone ID to use, not null
174      * @return the current date using the system clock, not null
175      */
now(ZoneId zone)176     public static JapaneseDate now(ZoneId zone) {
177         return now(Clock.system(zone));
178     }
179 
180     /**
181      * Obtains the current {@code JapaneseDate} from the specified clock.
182      * <p>
183      * This will query the specified clock to obtain the current date - today.
184      * Using this method allows the use of an alternate clock for testing.
185      * The alternate clock may be introduced using {@linkplain Clock dependency injection}.
186      *
187      * @param clock  the clock to use, not null
188      * @return the current date, not null
189      * @throws DateTimeException if the current date cannot be obtained
190      */
now(Clock clock)191     public static JapaneseDate now(Clock clock) {
192         return new JapaneseDate(LocalDate.now(clock));
193     }
194 
195     /**
196      * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar
197      * system from the era, year-of-era, month-of-year and day-of-month fields.
198      * <p>
199      * This returns a {@code JapaneseDate} with the specified fields.
200      * The day must be valid for the year and month, otherwise an exception will be thrown.
201      * <p>
202      * The Japanese month and day-of-month are the same as those in the
203      * ISO calendar system. They are not reset when the era changes.
204      * For example:
205      * <pre>
206      *  6th Jan Showa 64 = ISO 1989-01-06
207      *  7th Jan Showa 64 = ISO 1989-01-07
208      *  8th Jan Heisei 1 = ISO 1989-01-08
209      *  9th Jan Heisei 1 = ISO 1989-01-09
210      * </pre>
211      *
212      * @param era  the Japanese era, not null
213      * @param yearOfEra  the Japanese year-of-era
214      * @param month  the Japanese month-of-year, from 1 to 12
215      * @param dayOfMonth  the Japanese day-of-month, from 1 to 31
216      * @return the date in Japanese calendar system, not null
217      * @throws DateTimeException if the value of any field is out of range,
218      *  or if the day-of-month is invalid for the month-year,
219      *  or if the date is not a Japanese era
220      */
of(JapaneseEra era, int yearOfEra, int month, int dayOfMonth)221     public static JapaneseDate of(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) {
222         Objects.requireNonNull(era, "era");
223         LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);
224         jdate.setEra(era.getPrivateEra()).setDate(yearOfEra, month, dayOfMonth);
225         if (!JapaneseChronology.JCAL.validate(jdate)) {
226             throw new DateTimeException("year, month, and day not valid for Era");
227         }
228         LocalDate date = LocalDate.of(jdate.getNormalizedYear(), month, dayOfMonth);
229         return new JapaneseDate(era, yearOfEra, date);
230     }
231 
232     /**
233      * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar
234      * system from the proleptic-year, month-of-year and day-of-month fields.
235      * <p>
236      * This returns a {@code JapaneseDate} with the specified fields.
237      * The day must be valid for the year and month, otherwise an exception will be thrown.
238      * <p>
239      * The Japanese proleptic year, month and day-of-month are the same as those
240      * in the ISO calendar system. They are not reset when the era changes.
241      *
242      * @param prolepticYear  the Japanese proleptic-year
243      * @param month  the Japanese month-of-year, from 1 to 12
244      * @param dayOfMonth  the Japanese day-of-month, from 1 to 31
245      * @return the date in Japanese calendar system, not null
246      * @throws DateTimeException if the value of any field is out of range,
247      *  or if the day-of-month is invalid for the month-year
248      */
of(int prolepticYear, int month, int dayOfMonth)249     public static JapaneseDate of(int prolepticYear, int month, int dayOfMonth) {
250         return new JapaneseDate(LocalDate.of(prolepticYear, month, dayOfMonth));
251     }
252 
253     /**
254      * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar
255      * system from the era, year-of-era and day-of-year fields.
256      * <p>
257      * This returns a {@code JapaneseDate} with the specified fields.
258      * The day must be valid for the year, otherwise an exception will be thrown.
259      * <p>
260      * The day-of-year in this factory is expressed relative to the start of the year-of-era.
261      * This definition changes the normal meaning of day-of-year only in those years
262      * where the year-of-era is reset to one due to a change in the era.
263      * For example:
264      * <pre>
265      *  6th Jan Showa 64 = day-of-year 6
266      *  7th Jan Showa 64 = day-of-year 7
267      *  8th Jan Heisei 1 = day-of-year 1
268      *  9th Jan Heisei 1 = day-of-year 2
269      * </pre>
270      *
271      * @param era  the Japanese era, not null
272      * @param yearOfEra  the Japanese year-of-era
273      * @param dayOfYear  the chronology day-of-year, from 1 to 366
274      * @return the date in Japanese calendar system, not null
275      * @throws DateTimeException if the value of any field is out of range,
276      *  or if the day-of-year is invalid for the year
277      */
ofYearDay(JapaneseEra era, int yearOfEra, int dayOfYear)278     static JapaneseDate ofYearDay(JapaneseEra era, int yearOfEra, int dayOfYear) {
279         Objects.requireNonNull(era, "era");
280         CalendarDate firstDay = era.getPrivateEra().getSinceDate();
281         LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);
282         jdate.setEra(era.getPrivateEra());
283         if (yearOfEra == 1) {
284             jdate.setDate(yearOfEra, firstDay.getMonth(), firstDay.getDayOfMonth() + dayOfYear - 1);
285         } else {
286             jdate.setDate(yearOfEra, 1, dayOfYear);
287         }
288         JapaneseChronology.JCAL.normalize(jdate);
289         if (era.getPrivateEra() != jdate.getEra() || yearOfEra != jdate.getYear()) {
290             throw new DateTimeException("Invalid parameters");
291         }
292         LocalDate localdate = LocalDate.of(jdate.getNormalizedYear(),
293                                       jdate.getMonth(), jdate.getDayOfMonth());
294         return new JapaneseDate(era, yearOfEra, localdate);
295     }
296 
297     /**
298      * Obtains a {@code JapaneseDate} from a temporal object.
299      * <p>
300      * This obtains a date in the Japanese calendar system based on the specified temporal.
301      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
302      * which this factory converts to an instance of {@code JapaneseDate}.
303      * <p>
304      * The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY}
305      * field, which is standardized across calendar systems.
306      * <p>
307      * This method matches the signature of the functional interface {@link TemporalQuery}
308      * allowing it to be used as a query via method reference, {@code JapaneseDate::from}.
309      *
310      * @param temporal  the temporal object to convert, not null
311      * @return the date in Japanese calendar system, not null
312      * @throws DateTimeException if unable to convert to a {@code JapaneseDate}
313      */
from(TemporalAccessor temporal)314     public static JapaneseDate from(TemporalAccessor temporal) {
315         return JapaneseChronology.INSTANCE.date(temporal);
316     }
317 
318     //-----------------------------------------------------------------------
319     /**
320      * Creates an instance from an ISO date.
321      *
322      * @param isoDate  the standard local date, validated not null
323      */
JapaneseDate(LocalDate isoDate)324     JapaneseDate(LocalDate isoDate) {
325         if (isoDate.isBefore(MEIJI_6_ISODATE)) {
326             throw new DateTimeException("JapaneseDate before Meiji 6 is not supported");
327         }
328         LocalGregorianCalendar.Date jdate = toPrivateJapaneseDate(isoDate);
329         this.era = JapaneseEra.toJapaneseEra(jdate.getEra());
330         this.yearOfEra = jdate.getYear();
331         this.isoDate = isoDate;
332     }
333 
334     /**
335      * Constructs a {@code JapaneseDate}. This constructor does NOT validate the given parameters,
336      * and {@code era} and {@code year} must agree with {@code isoDate}.
337      *
338      * @param era  the era, validated not null
339      * @param year  the year-of-era, validated
340      * @param isoDate  the standard local date, validated not null
341      */
JapaneseDate(JapaneseEra era, int year, LocalDate isoDate)342     JapaneseDate(JapaneseEra era, int year, LocalDate isoDate) {
343         if (isoDate.isBefore(MEIJI_6_ISODATE)) {
344             throw new DateTimeException("JapaneseDate before Meiji 6 is not supported");
345         }
346         this.era = era;
347         this.yearOfEra = year;
348         this.isoDate = isoDate;
349     }
350 
351     //-----------------------------------------------------------------------
352     /**
353      * Gets the chronology of this date, which is the Japanese calendar system.
354      * <p>
355      * The {@code Chronology} represents the calendar system in use.
356      * The era and other fields in {@link ChronoField} are defined by the chronology.
357      *
358      * @return the Japanese chronology, not null
359      */
360     @Override
getChronology()361     public JapaneseChronology getChronology() {
362         return JapaneseChronology.INSTANCE;
363     }
364 
365     /**
366      * Gets the era applicable at this date.
367      * <p>
368      * The Japanese calendar system has multiple eras defined by {@link JapaneseEra}.
369      *
370      * @return the era applicable at this date, not null
371      */
372     @Override
getEra()373     public JapaneseEra getEra() {
374         return era;
375     }
376 
377     /**
378      * Returns the length of the month represented by this date.
379      * <p>
380      * This returns the length of the month in days.
381      * Month lengths match those of the ISO calendar system.
382      *
383      * @return the length of the month in days
384      */
385     @Override
lengthOfMonth()386     public int lengthOfMonth() {
387         return isoDate.lengthOfMonth();
388     }
389 
390     @Override
lengthOfYear()391     public int lengthOfYear() {
392         // Android-changed: use #createCalendar() to create calendar.
393         Calendar jcal = JapaneseChronology.createCalendar();
394         jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);
395         jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());
396         return  jcal.getActualMaximum(Calendar.DAY_OF_YEAR);
397     }
398 
399     //-----------------------------------------------------------------------
400     /**
401      * Checks if the specified field is supported.
402      * <p>
403      * This checks if this date can be queried for the specified field.
404      * If false, then calling the {@link #range(TemporalField) range} and
405      * {@link #get(TemporalField) get} methods will throw an exception.
406      * <p>
407      * If the field is a {@link ChronoField} then the query is implemented here.
408      * The supported fields are:
409      * <ul>
410      * <li>{@code DAY_OF_WEEK}
411      * <li>{@code DAY_OF_MONTH}
412      * <li>{@code DAY_OF_YEAR}
413      * <li>{@code EPOCH_DAY}
414      * <li>{@code MONTH_OF_YEAR}
415      * <li>{@code PROLEPTIC_MONTH}
416      * <li>{@code YEAR_OF_ERA}
417      * <li>{@code YEAR}
418      * <li>{@code ERA}
419      * </ul>
420      * All other {@code ChronoField} instances will return false.
421      * <p>
422      * If the field is not a {@code ChronoField}, then the result of this method
423      * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
424      * passing {@code this} as the argument.
425      * Whether the field is supported is determined by the field.
426      *
427      * @param field  the field to check, null returns false
428      * @return true if the field is supported on this date, false if not
429      */
430     @Override
isSupported(TemporalField field)431     public boolean isSupported(TemporalField field) {
432         if (field == ALIGNED_DAY_OF_WEEK_IN_MONTH || field == ALIGNED_DAY_OF_WEEK_IN_YEAR ||
433                 field == ALIGNED_WEEK_OF_MONTH || field == ALIGNED_WEEK_OF_YEAR) {
434             return false;
435         }
436         return super.isSupported(field);
437     }
438 
439     @Override
range(TemporalField field)440     public ValueRange range(TemporalField field) {
441         if (field instanceof ChronoField) {
442             if (isSupported(field)) {
443                 ChronoField f = (ChronoField) field;
444                 switch (f) {
445                     case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
446                     case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
447                     case YEAR_OF_ERA: {
448                         // Android-changed: use #createCalendar() to create calendar.
449                         Calendar jcal = JapaneseChronology.createCalendar();
450                         jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);
451                         jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());
452                         return ValueRange.of(1, jcal.getActualMaximum(Calendar.YEAR));
453                     }
454                 }
455                 return getChronology().range(f);
456             }
457             throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
458         }
459         return field.rangeRefinedBy(this);
460     }
461 
462     @Override
getLong(TemporalField field)463     public long getLong(TemporalField field) {
464         if (field instanceof ChronoField) {
465             // same as ISO:
466             // DAY_OF_WEEK, DAY_OF_MONTH, EPOCH_DAY, MONTH_OF_YEAR, PROLEPTIC_MONTH, YEAR
467             //
468             // calendar specific fields
469             // DAY_OF_YEAR, YEAR_OF_ERA, ERA
470             switch ((ChronoField) field) {
471                 case ALIGNED_DAY_OF_WEEK_IN_MONTH:
472                 case ALIGNED_DAY_OF_WEEK_IN_YEAR:
473                 case ALIGNED_WEEK_OF_MONTH:
474                 case ALIGNED_WEEK_OF_YEAR:
475                     throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
476                 case YEAR_OF_ERA:
477                     return yearOfEra;
478                 case ERA:
479                     return era.getValue();
480                 case DAY_OF_YEAR:
481                     // Android-changed: use #createCalendar() to create calendar.
482                     Calendar jcal = JapaneseChronology.createCalendar();
483                     jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);
484                     jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());
485                     return jcal.get(Calendar.DAY_OF_YEAR);
486             }
487             return isoDate.getLong(field);
488         }
489         return field.getFrom(this);
490     }
491 
492     /**
493      * Returns a {@code LocalGregorianCalendar.Date} converted from the given {@code isoDate}.
494      *
495      * @param isoDate  the local date, not null
496      * @return a {@code LocalGregorianCalendar.Date}, not null
497      */
toPrivateJapaneseDate(LocalDate isoDate)498     private static LocalGregorianCalendar.Date toPrivateJapaneseDate(LocalDate isoDate) {
499         LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);
500         sun.util.calendar.Era sunEra = JapaneseEra.privateEraFrom(isoDate);
501         int year = isoDate.getYear();
502         if (sunEra != null) {
503             year -= sunEra.getSinceDate().getYear() - 1;
504         }
505         jdate.setEra(sunEra).setYear(year).setMonth(isoDate.getMonthValue()).setDayOfMonth(isoDate.getDayOfMonth());
506         JapaneseChronology.JCAL.normalize(jdate);
507         return jdate;
508     }
509 
510     //-----------------------------------------------------------------------
511     @Override
with(TemporalField field, long newValue)512     public JapaneseDate with(TemporalField field, long newValue) {
513         if (field instanceof ChronoField chronoField) {
514             if (getLong(chronoField) == newValue) {  // getLong() validates for supported fields
515                 return this;
516             }
517             switch (chronoField) {
518                 case YEAR_OF_ERA:
519                 case YEAR:
520                 case ERA: {
521                     int nvalue = getChronology().range(chronoField).checkValidIntValue(newValue, chronoField);
522                     switch (chronoField) {
523                         case YEAR_OF_ERA:
524                             return this.withYear(nvalue);
525                         case YEAR:
526                             return with(isoDate.withYear(nvalue));
527                         case ERA: {
528                             return this.withYear(JapaneseEra.of(nvalue), yearOfEra);
529                         }
530                     }
531                 }
532             }
533             // YEAR, PROLEPTIC_MONTH and others are same as ISO
534             return with(isoDate.with(field, newValue));
535         }
536         return super.with(field, newValue);
537     }
538 
539     /**
540      * {@inheritDoc}
541      * @throws DateTimeException {@inheritDoc}
542      * @throws ArithmeticException {@inheritDoc}
543      */
544     @Override
with(TemporalAdjuster adjuster)545     public  JapaneseDate with(TemporalAdjuster adjuster) {
546         return super.with(adjuster);
547     }
548 
549     /**
550      * {@inheritDoc}
551      * @throws DateTimeException {@inheritDoc}
552      * @throws ArithmeticException {@inheritDoc}
553      */
554     @Override
plus(TemporalAmount amount)555     public JapaneseDate plus(TemporalAmount amount) {
556         return super.plus(amount);
557     }
558 
559     /**
560      * {@inheritDoc}
561      * @throws DateTimeException {@inheritDoc}
562      * @throws ArithmeticException {@inheritDoc}
563      */
564     @Override
minus(TemporalAmount amount)565     public JapaneseDate minus(TemporalAmount amount) {
566         return super.minus(amount);
567     }
568     //-----------------------------------------------------------------------
569     /**
570      * Returns a copy of this date with the year altered.
571      * <p>
572      * This method changes the year of the date.
573      * If the month-day is invalid for the year, then the previous valid day
574      * will be selected instead.
575      * <p>
576      * This instance is immutable and unaffected by this method call.
577      *
578      * @param era  the era to set in the result, not null
579      * @param yearOfEra  the year-of-era to set in the returned date
580      * @return a {@code JapaneseDate} based on this date with the requested year, never null
581      * @throws DateTimeException if {@code year} is invalid
582      */
withYear(JapaneseEra era, int yearOfEra)583     private JapaneseDate withYear(JapaneseEra era, int yearOfEra) {
584         int year = JapaneseChronology.INSTANCE.prolepticYear(era, yearOfEra);
585         return with(isoDate.withYear(year));
586     }
587 
588     /**
589      * Returns a copy of this date with the year-of-era altered.
590      * <p>
591      * This method changes the year-of-era of the date.
592      * If the month-day is invalid for the year, then the previous valid day
593      * will be selected instead.
594      * <p>
595      * This instance is immutable and unaffected by this method call.
596      *
597      * @param year  the year to set in the returned date
598      * @return a {@code JapaneseDate} based on this date with the requested year-of-era, never null
599      * @throws DateTimeException if {@code year} is invalid
600      */
withYear(int year)601     private JapaneseDate withYear(int year) {
602         return withYear(getEra(), year);
603     }
604 
605     //-----------------------------------------------------------------------
606     @Override
plusYears(long years)607     JapaneseDate plusYears(long years) {
608         return with(isoDate.plusYears(years));
609     }
610 
611     @Override
plusMonths(long months)612     JapaneseDate plusMonths(long months) {
613         return with(isoDate.plusMonths(months));
614     }
615 
616     @Override
plusWeeks(long weeksToAdd)617     JapaneseDate plusWeeks(long weeksToAdd) {
618         return with(isoDate.plusWeeks(weeksToAdd));
619     }
620 
621     @Override
plusDays(long days)622     JapaneseDate plusDays(long days) {
623         return with(isoDate.plusDays(days));
624     }
625 
626     @Override
plus(long amountToAdd, TemporalUnit unit)627     public JapaneseDate plus(long amountToAdd, TemporalUnit unit) {
628         return super.plus(amountToAdd, unit);
629     }
630 
631     @Override
minus(long amountToAdd, TemporalUnit unit)632     public JapaneseDate minus(long amountToAdd, TemporalUnit unit) {
633         return super.minus(amountToAdd, unit);
634     }
635 
636     @Override
minusYears(long yearsToSubtract)637     JapaneseDate minusYears(long yearsToSubtract) {
638         return super.minusYears(yearsToSubtract);
639     }
640 
641     @Override
minusMonths(long monthsToSubtract)642     JapaneseDate minusMonths(long monthsToSubtract) {
643         return super.minusMonths(monthsToSubtract);
644     }
645 
646     @Override
minusWeeks(long weeksToSubtract)647     JapaneseDate minusWeeks(long weeksToSubtract) {
648         return super.minusWeeks(weeksToSubtract);
649     }
650 
651     @Override
minusDays(long daysToSubtract)652     JapaneseDate minusDays(long daysToSubtract) {
653         return super.minusDays(daysToSubtract);
654     }
655 
with(LocalDate newDate)656     private JapaneseDate with(LocalDate newDate) {
657         return (newDate.equals(isoDate) ? this : new JapaneseDate(newDate));
658     }
659 
660     @Override        // for javadoc and covariant return type
661     @SuppressWarnings("unchecked")
atTime(LocalTime localTime)662     public final ChronoLocalDateTime<JapaneseDate> atTime(LocalTime localTime) {
663         return (ChronoLocalDateTime<JapaneseDate>)super.atTime(localTime);
664     }
665 
666     @Override
until(ChronoLocalDate endDate)667     public ChronoPeriod until(ChronoLocalDate endDate) {
668         Period period = isoDate.until(endDate);
669         return getChronology().period(period.getYears(), period.getMonths(), period.getDays());
670     }
671 
672     @Override  // override for performance
toEpochDay()673     public long toEpochDay() {
674         return isoDate.toEpochDay();
675     }
676 
677     //-------------------------------------------------------------------------
678     /**
679      * Compares this date to another date, including the chronology.
680      * <p>
681      * Compares this {@code JapaneseDate} with another ensuring that the date is the same.
682      * <p>
683      * Only objects of type {@code JapaneseDate} are compared, other types return false.
684      * To compare the dates of two {@code TemporalAccessor} instances, including dates
685      * in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.
686      *
687      * @param obj  the object to check, null returns false
688      * @return true if this is equal to the other date
689      */
690     @Override  // override for performance
equals(Object obj)691     public boolean equals(Object obj) {
692         if (this == obj) {
693             return true;
694         }
695         return (obj instanceof JapaneseDate otherDate)
696             && this.isoDate.equals(otherDate.isoDate);
697     }
698 
699     /**
700      * A hash code for this date.
701      *
702      * @return a suitable hash code based only on the Chronology and the date
703      */
704     @Override  // override for performance
hashCode()705     public int hashCode() {
706         return getChronology().getId().hashCode() ^ isoDate.hashCode();
707     }
708 
709     //-----------------------------------------------------------------------
710     /**
711      * Defend against malicious streams.
712      *
713      * @param s the stream to read
714      * @throws InvalidObjectException always
715      */
716     @java.io.Serial
readObject(ObjectInputStream s)717     private void readObject(ObjectInputStream s) throws InvalidObjectException {
718         throw new InvalidObjectException("Deserialization via serialization delegate");
719     }
720 
721     /**
722      * Writes the object using a
723      * <a href="{@docRoot}/serialized-form.html#java.time.chrono.Ser">dedicated serialized form</a>.
724      * @serialData
725      * <pre>
726      *  out.writeByte(4);                 // identifies a JapaneseDate
727      *  out.writeInt(get(YEAR));
728      *  out.writeByte(get(MONTH_OF_YEAR));
729      *  out.writeByte(get(DAY_OF_MONTH));
730      * </pre>
731      *
732      * @return the instance of {@code Ser}, not null
733      */
734     @java.io.Serial
writeReplace()735     private Object writeReplace() {
736         return new Ser(Ser.JAPANESE_DATE_TYPE, this);
737     }
738 
writeExternal(DataOutput out)739     void writeExternal(DataOutput out) throws IOException {
740         // JapaneseChronology is implicit in the JAPANESE_DATE_TYPE
741         out.writeInt(get(YEAR));
742         out.writeByte(get(MONTH_OF_YEAR));
743         out.writeByte(get(DAY_OF_MONTH));
744     }
745 
readExternal(DataInput in)746     static JapaneseDate readExternal(DataInput in) throws IOException {
747         int year = in.readInt();
748         int month = in.readByte();
749         int dayOfMonth = in.readByte();
750         return JapaneseChronology.INSTANCE.date(year, month, dayOfMonth);
751     }
752 
753 }
754