1 /*
   2  * Copyright (c) 2012, 2013, 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.DAY_OF_MONTH;
  60 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  61 import static java.time.temporal.ChronoField.YEAR;
  62 
  63 import java.io.DataInput;
  64 import java.io.DataOutput;
  65 import java.io.IOException;
  66 import java.io.Serializable;
  67 import java.time.Clock;
  68 import java.time.DateTimeException;
  69 import java.time.LocalDate;
  70 import java.time.LocalTime;
  71 import java.time.Period;
  72 import java.time.Year;
  73 import java.time.ZoneId;
  74 import java.time.temporal.ChronoField;
  75 import java.time.temporal.TemporalAccessor;
  76 import java.time.temporal.TemporalAdjuster;
  77 import java.time.temporal.TemporalAmount;
  78 import java.time.temporal.TemporalField;
  79 import java.time.temporal.TemporalQuery;
  80 import java.time.temporal.TemporalUnit;
  81 import java.time.temporal.UnsupportedTemporalTypeException;
  82 import java.time.temporal.ValueRange;
  83 import java.util.Calendar;
  84 import java.util.Objects;
  85 
  86 import sun.util.calendar.LocalGregorianCalendar;
  87 
  88 /**
  89  * A date in the Japanese Imperial calendar system.
  90  * <p>
  91  * This date operates using the {@linkplain JapaneseChronology Japanese Imperial calendar}.
  92  * This calendar system is primarily used in Japan.
  93  * <p>
  94  * The Japanese Imperial calendar system is the same as the ISO calendar system
  95  * apart from the era-based year numbering. The proleptic-year is defined to be
  96  * equal to the ISO proleptic-year.
  97  * <p>
  98  * For example, the Japanese year "Heisei 24" corresponds to ISO year "2012".<br>
  99  * Calling {@code japaneseDate.get(YEAR_OF_ERA)} will return 24.<br>
 100  * Calling {@code japaneseDate.get(YEAR)} will return 2012.<br>
 101  * Calling {@code japaneseDate.get(ERA)} will return 2, corresponding to
 102  * {@code JapaneseChronology.ERA_HEISEI}.<br>
 103  *
 104  * <h3>Specification for implementors</h3>
 105  * This class is immutable and thread-safe.
 106  *
 107  * @since 1.8
 108  */
 109 public final class JapaneseDate
 110         extends ChronoDateImpl<JapaneseDate>
 111         implements ChronoLocalDate<JapaneseDate>, Serializable {
 112 
 113     /**
 114      * Serialization version.
 115      */
 116     private static final long serialVersionUID = -305327627230580483L;
 117 
 118     /**
 119      * The underlying ISO local date.
 120      */
 121     private transient final LocalDate isoDate;
 122     /**
 123      * The JapaneseEra of this date.
 124      */
 125     private transient JapaneseEra era;
 126     /**
 127      * The Japanese imperial calendar year of this date.
 128      */
 129     private transient int yearOfEra;
 130 
 131     //-----------------------------------------------------------------------
 132     /**
 133      * Obtains the current {@code JapaneseDate} from the system clock in the default time-zone.
 134      * <p>
 135      * This will query the {@link Clock#systemDefaultZone() system clock} in the default
 136      * time-zone to obtain the current date.
 137      * <p>
 138      * Using this method will prevent the ability to use an alternate clock for testing
 139      * because the clock is hard-coded.
 140      *
 141      * @return the current date using the system clock and default time-zone, not null
 142      */
 143     public static JapaneseDate now() {
 144         return now(Clock.systemDefaultZone());
 145     }
 146 
 147     /**
 148      * Obtains the current {@code JapaneseDate} from the system clock in the specified time-zone.
 149      * <p>
 150      * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date.
 151      * Specifying the time-zone avoids dependence on the default time-zone.
 152      * <p>
 153      * Using this method will prevent the ability to use an alternate clock for testing
 154      * because the clock is hard-coded.
 155      *
 156      * @param zone  the zone ID to use, not null
 157      * @return the current date using the system clock, not null
 158      */
 159     public static JapaneseDate now(ZoneId zone) {
 160         return now(Clock.system(zone));
 161     }
 162 
 163     /**
 164      * Obtains the current {@code JapaneseDate} from the specified clock.
 165      * <p>
 166      * This will query the specified clock to obtain the current date - today.
 167      * Using this method allows the use of an alternate clock for testing.
 168      * The alternate clock may be introduced using {@linkplain Clock dependency injection}.
 169      *
 170      * @param clock  the clock to use, not null
 171      * @return the current date, not null
 172      * @throws DateTimeException if the current date cannot be obtained
 173      */
 174     public static JapaneseDate now(Clock clock) {
 175         return JapaneseChronology.INSTANCE.date(LocalDate.now(clock));
 176     }
 177 
 178     /**
 179      * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar
 180      * system from the era, year-of-era, month-of-year and day-of-month fields.
 181      * <p>
 182      * This returns a {@code JapaneseDate} with the specified fields.
 183      * The day must be valid for the year and month, otherwise an exception will be thrown.
 184      *
 185      * @param era  the Japanese era, not null
 186      * @param yearOfEra  the Japanese year-of-era
 187      * @param month  the Japanese month-of-year, from 1 to 12
 188      * @param dayOfMonth  the Japanese day-of-month, from 1 to 31
 189      * @return the date in Japanese calendar system, not null
 190      * @throws DateTimeException if the value of any field is out of range,
 191      *  or if the day-of-month is invalid for the month-year,
 192      *  or if the date is not a Japanese era
 193      */
 194     public static JapaneseDate of(Era era, int yearOfEra, int month, int dayOfMonth) {
 195         if (era instanceof JapaneseEra == false) {
 196             throw new ClassCastException("Era must be JapaneseEra");
 197         }
 198         return JapaneseDate.of((JapaneseEra) era, yearOfEra, month, dayOfMonth);
 199     }
 200 
 201     /**
 202      * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar
 203      * system from the proleptic-year, month-of-year and day-of-month fields.
 204      * <p>
 205      * This returns a {@code JapaneseDate} with the specified fields.
 206      * The day must be valid for the year and month, otherwise an exception will be thrown.
 207      *
 208      * @param prolepticYear  the Japanese proleptic-year
 209      * @param month  the Japanese month-of-year, from 1 to 12
 210      * @param dayOfMonth  the Japanese day-of-month, from 1 to 31
 211      * @return the date in Japanese calendar system, not null
 212      * @throws DateTimeException if the value of any field is out of range,
 213      *  or if the day-of-month is invalid for the month-year
 214      */
 215     public static JapaneseDate of(int prolepticYear, int month, int dayOfMonth) {
 216         return new JapaneseDate(LocalDate.of(prolepticYear, month, dayOfMonth));
 217     }
 218 
 219     /**
 220      * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar
 221      * system from the proleptic-year and day-of-year fields.
 222      * <p>
 223      * This returns a {@code JapaneseDate} with the specified fields.
 224      * The day must be valid for the year, otherwise an exception will be thrown.
 225      *
 226      * @param prolepticYear  the chronology proleptic-year
 227      * @param dayOfYear  the chronology day-of-year, from 1 to 366
 228      * @return the date in Japanese calendar system, not null
 229      * @throws DateTimeException if the value of any field is out of range,
 230      *  or if the day-of-year is invalid for the year
 231      */
 232     public static JapaneseDate ofYearDay(int prolepticYear, int dayOfYear) {
 233         LocalDate date = LocalDate.ofYearDay(prolepticYear, dayOfYear);
 234         return of(prolepticYear, date.getMonthValue(), date.getDayOfMonth());
 235     }
 236 
 237     /**
 238      * Obtains a {@code JapaneseDate} representing a date in the Japanese calendar
 239      * system from the era, year-of-era, month-of-year and day-of-month fields.
 240      * <p>
 241      * This returns a {@code JapaneseDate} with the specified fields.
 242      * The day must be valid for the year and month, otherwise an exception will be thrown.
 243      *
 244      * @param era  the Japanese era, not null
 245      * @param yearOfEra  the Japanese year-of-era
 246      * @param month  the Japanese month-of-year, from 1 to 12
 247      * @param dayOfMonth  the Japanese day-of-month, from 1 to 31
 248      * @return the date in Japanese calendar system, not null
 249      * @throws DateTimeException if the value of any field is out of range,
 250      *  or if the day-of-month is invalid for the month-year
 251      */
 252     static JapaneseDate of(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) {
 253         Objects.requireNonNull(era, "era");
 254         LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);
 255         jdate.setEra(era.getPrivateEra()).setDate(yearOfEra, month, dayOfMonth);
 256         if (!JapaneseChronology.JCAL.validate(jdate)) {
 257             throw new DateTimeException("year, month, and day not valid for Era");
 258         }
 259         LocalDate date = LocalDate.of(jdate.getNormalizedYear(), month, dayOfMonth);
 260         return new JapaneseDate(era, yearOfEra, date);
 261     }
 262 
 263     /**
 264      * Obtains a {@code JapaneseDate} from a temporal object.
 265      * <p>
 266      * This obtains a date in the Japanese calendar system based on the specified temporal.
 267      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 268      * which this factory converts to an instance of {@code JapaneseDate}.
 269      * <p>
 270      * The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY}
 271      * field, which is standardized across calendar systems.
 272      * <p>
 273      * This method matches the signature of the functional interface {@link TemporalQuery}
 274      * allowing it to be used as a query via method reference, {@code JapaneseDate::from}.
 275      *
 276      * @param temporal  the temporal object to convert, not null
 277      * @return the date in Japanese calendar system, not null
 278      * @throws DateTimeException if unable to convert to a {@code JapaneseDate}
 279      */
 280     public static JapaneseDate from(TemporalAccessor temporal) {
 281         return JapaneseChronology.INSTANCE.date(temporal);
 282     }
 283 
 284     //-----------------------------------------------------------------------
 285     /**
 286      * Creates an instance from an ISO date.
 287      *
 288      * @param isoDate  the standard local date, validated not null
 289      */
 290     JapaneseDate(LocalDate isoDate) {
 291         LocalGregorianCalendar.Date jdate = toPrivateJapaneseDate(isoDate);
 292         this.era = JapaneseEra.toJapaneseEra(jdate.getEra());
 293         this.yearOfEra = jdate.getYear();
 294         this.isoDate = isoDate;
 295     }
 296 
 297     /**
 298      * Constructs a {@code JapaneseDate}. This constructor does NOT validate the given parameters,
 299      * and {@code era} and {@code year} must agree with {@code isoDate}.
 300      *
 301      * @param era  the era, validated not null
 302      * @param year  the year-of-era, validated
 303      * @param isoDate  the standard local date, validated not null
 304      */
 305     JapaneseDate(JapaneseEra era, int year, LocalDate isoDate) {
 306         this.era = era;
 307         this.yearOfEra = year;
 308         this.isoDate = isoDate;
 309     }
 310 
 311     //-----------------------------------------------------------------------
 312     /**
 313      * Gets the chronology of this date, which is the Japanese calendar system.
 314      * <p>
 315      * The {@code Chronology} represents the calendar system in use.
 316      * The era and other fields in {@link ChronoField} are defined by the chronology.
 317      *
 318      * @return the Japanese chronology, not null
 319      */
 320     @Override
 321     public JapaneseChronology getChronology() {
 322         return JapaneseChronology.INSTANCE;
 323     }
 324 
 325     /**
 326      * Gets the era applicable at this date.
 327      * <p>
 328      * The Japanese calendar system has multiple eras defined by {@link JapaneseEra}.
 329      *
 330      * @return the era applicable at this date, not null
 331      */
 332     @Override
 333     public JapaneseEra getEra() {
 334         return era;
 335     }
 336 
 337     /**
 338      * Returns the length of the month represented by this date.
 339      * <p>
 340      * This returns the length of the month in days.
 341      * Month lengths match those of the ISO calendar system.
 342      *
 343      * @return the length of the month in days
 344      */
 345     @Override
 346     public int lengthOfMonth() {
 347         return isoDate.lengthOfMonth();
 348     }
 349 
 350     //-----------------------------------------------------------------------
 351     @Override
 352     public ValueRange range(TemporalField field) {
 353         if (field instanceof ChronoField) {
 354             if (isSupported(field)) {
 355                 ChronoField f = (ChronoField) field;
 356                 switch (f) {
 357                     case DAY_OF_MONTH:
 358                     case ALIGNED_WEEK_OF_MONTH:
 359                         return isoDate.range(field);
 360                     case DAY_OF_YEAR:
 361                         return actualRange(Calendar.DAY_OF_YEAR);
 362                     case YEAR_OF_ERA:
 363                         return actualRange(Calendar.YEAR);
 364                 }
 365                 return getChronology().range(f);
 366             }
 367             throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName());
 368         }
 369         return field.rangeRefinedBy(this);
 370     }
 371 
 372     private ValueRange actualRange(int calendarField) {
 373         Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE);
 374         jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);  // TODO: cannot calculate this way for SEIREKI
 375         jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());
 376         return ValueRange.of(jcal.getActualMinimum(calendarField),
 377                 jcal.getActualMaximum(calendarField));
 378     }
 379 
 380     @Override
 381     public long getLong(TemporalField field) {
 382         if (field instanceof ChronoField) {
 383             // same as ISO:
 384             // DAY_OF_WEEK, ALIGNED_DAY_OF_WEEK_IN_MONTH, DAY_OF_MONTH, EPOCH_DAY,
 385             // ALIGNED_WEEK_OF_MONTH, MONTH_OF_YEAR, PROLEPTIC_MONTH, YEAR
 386             //
 387             // calendar specific fields
 388             // ALIGNED_DAY_OF_WEEK_IN_YEAR, DAY_OF_YEAR, ALIGNED_WEEK_OF_YEAR, YEAR_OF_ERA, ERA
 389             switch ((ChronoField) field) {
 390                 case YEAR_OF_ERA:
 391                     return yearOfEra;
 392                 case ERA:
 393                     return era.getValue();
 394                 case DAY_OF_YEAR: {
 395                     LocalGregorianCalendar.Date jdate = toPrivateJapaneseDate(isoDate);
 396                     return JapaneseChronology.JCAL.getDayOfYear(jdate);
 397                 }
 398                 // TODO: ALIGNED_DAY_OF_WEEK_IN_YEAR and ALIGNED_WEEK_OF_YEAR ???
 399             }
 400             return isoDate.getLong(field);
 401         }
 402         return field.getFrom(this);
 403     }
 404 
 405     /**
 406      * Returns a {@code LocalGregorianCalendar.Date} converted from the given {@code isoDate}.
 407      *
 408      * @param isoDate  the local date, not null
 409      * @return a {@code LocalGregorianCalendar.Date}, not null
 410      */
 411     private static LocalGregorianCalendar.Date toPrivateJapaneseDate(LocalDate isoDate) {
 412         LocalGregorianCalendar.Date jdate = JapaneseChronology.JCAL.newCalendarDate(null);
 413         sun.util.calendar.Era sunEra = JapaneseEra.privateEraFrom(isoDate);
 414         int year = isoDate.getYear();
 415         if (sunEra != null) {
 416             year -= sunEra.getSinceDate().getYear() - 1;
 417         }
 418         jdate.setEra(sunEra).setYear(year).setMonth(isoDate.getMonthValue()).setDayOfMonth(isoDate.getDayOfMonth());
 419         JapaneseChronology.JCAL.normalize(jdate);
 420         return jdate;
 421     }
 422 
 423     //-----------------------------------------------------------------------
 424     @Override
 425     public JapaneseDate with(TemporalField field, long newValue) {
 426         if (field instanceof ChronoField) {
 427             ChronoField f = (ChronoField) field;
 428             if (getLong(f) == newValue) {
 429                 return this;
 430             }
 431             switch (f) {
 432                 case YEAR_OF_ERA:
 433                 case YEAR:
 434                 case ERA: {
 435                     int nvalue = getChronology().range(f).checkValidIntValue(newValue, f);
 436                     switch (f) {
 437                         case YEAR_OF_ERA:
 438                             return this.withYear(nvalue);
 439                         case YEAR:
 440                             return with(isoDate.withYear(nvalue));
 441                         case ERA: {
 442                             return this.withYear(JapaneseEra.of(nvalue), yearOfEra);
 443                         }
 444                     }
 445                 }
 446             }
 447             // YEAR, PROLEPTIC_MONTH and others are same as ISO
 448             // TODO: review other fields, such as WEEK_OF_YEAR
 449             return with(isoDate.with(field, newValue));
 450         }
 451         return ChronoLocalDate.super.with(field, newValue);
 452     }
 453 
 454     /**
 455      * {@inheritDoc}
 456      * @throws DateTimeException {@inheritDoc}
 457      * @throws ArithmeticException {@inheritDoc}
 458      */
 459     @Override
 460     public  JapaneseDate with(TemporalAdjuster adjuster) {
 461         return super.with(adjuster);
 462     }
 463 
 464     /**
 465      * {@inheritDoc}
 466      * @throws DateTimeException {@inheritDoc}
 467      * @throws ArithmeticException {@inheritDoc}
 468      */
 469     @Override
 470     public JapaneseDate plus(TemporalAmount amount) {
 471         return super.plus(amount);
 472     }
 473 
 474     /**
 475      * {@inheritDoc}
 476      * @throws DateTimeException {@inheritDoc}
 477      * @throws ArithmeticException {@inheritDoc}
 478      */
 479     @Override
 480     public JapaneseDate minus(TemporalAmount amount) {
 481         return super.minus(amount);
 482     }
 483     //-----------------------------------------------------------------------
 484     /**
 485      * Returns a copy of this date with the year altered.
 486      * <p>
 487      * This method changes the year of the date.
 488      * If the month-day is invalid for the year, then the previous valid day
 489      * will be selected instead.
 490      * <p>
 491      * This instance is immutable and unaffected by this method call.
 492      *
 493      * @param era  the era to set in the result, not null
 494      * @param yearOfEra  the year-of-era to set in the returned date
 495      * @return a {@code JapaneseDate} based on this date with the requested year, never null
 496      * @throws DateTimeException if {@code year} is invalid
 497      */
 498     private JapaneseDate withYear(JapaneseEra era, int yearOfEra) {
 499         int year = JapaneseChronology.INSTANCE.prolepticYear(era, yearOfEra);
 500         return with(isoDate.withYear(year));
 501     }
 502 
 503     /**
 504      * Returns a copy of this date with the year-of-era altered.
 505      * <p>
 506      * This method changes the year-of-era of the date.
 507      * If the month-day is invalid for the year, then the previous valid day
 508      * will be selected instead.
 509      * <p>
 510      * This instance is immutable and unaffected by this method call.
 511      *
 512      * @param year  the year to set in the returned date
 513      * @return a {@code JapaneseDate} based on this date with the requested year-of-era, never null
 514      * @throws DateTimeException if {@code year} is invalid
 515      */
 516     private JapaneseDate withYear(int year) {
 517         return withYear(getEra(), year);
 518     }
 519 
 520     //-----------------------------------------------------------------------
 521     @Override
 522     JapaneseDate plusYears(long years) {
 523         return with(isoDate.plusYears(years));
 524     }
 525 
 526     @Override
 527     JapaneseDate plusMonths(long months) {
 528         return with(isoDate.plusMonths(months));
 529     }
 530 
 531     @Override
 532     JapaneseDate plusWeeks(long weeksToAdd) {
 533         return with(isoDate.plusWeeks(weeksToAdd));
 534     }
 535 
 536     @Override
 537     JapaneseDate plusDays(long days) {
 538         return with(isoDate.plusDays(days));
 539     }
 540 
 541     @Override
 542     public JapaneseDate plus(long amountToAdd, TemporalUnit unit) {
 543         return super.plus(amountToAdd, unit);
 544     }
 545 
 546     @Override
 547     public JapaneseDate minus(long amountToAdd, TemporalUnit unit) {
 548         return super.minus(amountToAdd, unit);
 549     }
 550 
 551     @Override
 552     JapaneseDate minusYears(long yearsToSubtract) {
 553         return super.minusYears(yearsToSubtract);
 554     }
 555 
 556     @Override
 557     JapaneseDate minusMonths(long monthsToSubtract) {
 558         return super.minusMonths(monthsToSubtract);
 559     }
 560 
 561     @Override
 562     JapaneseDate minusWeeks(long weeksToSubtract) {
 563         return super.minusWeeks(weeksToSubtract);
 564     }
 565 
 566     @Override
 567     JapaneseDate minusDays(long daysToSubtract) {
 568         return super.minusDays(daysToSubtract);
 569     }
 570 
 571     private JapaneseDate with(LocalDate newDate) {
 572         return (newDate.equals(isoDate) ? this : new JapaneseDate(newDate));
 573     }
 574 
 575     @Override        // for javadoc and covariant return type
 576     public final ChronoLocalDateTime<JapaneseDate> atTime(LocalTime localTime) {
 577         return super.atTime(localTime);
 578     }
 579 
 580     @Override
 581     public Period periodUntil(ChronoLocalDate<?> endDate) {
 582         return isoDate.periodUntil(endDate);
 583     }
 584 
 585     @Override  // override for performance
 586     public long toEpochDay() {
 587         return isoDate.toEpochDay();
 588     }
 589 
 590     //-------------------------------------------------------------------------
 591     @Override  // override for performance
 592     public boolean equals(Object obj) {
 593         if (this == obj) {
 594             return true;
 595         }
 596         if (obj instanceof JapaneseDate) {
 597             JapaneseDate otherDate = (JapaneseDate) obj;
 598             return this.isoDate.equals(otherDate.isoDate);
 599         }
 600         return false;
 601     }
 602 
 603     @Override  // override for performance
 604     public int hashCode() {
 605         return getChronology().getId().hashCode() ^ isoDate.hashCode();
 606     }
 607 
 608     @Override
 609     public String toString() {
 610         if (era == JapaneseEra.SEIREKI) {
 611             return getChronology().getId() + " " + isoDate.toString();
 612         }
 613         return super.toString();
 614     }
 615 
 616     //-----------------------------------------------------------------------
 617     private Object writeReplace() {
 618         return new Ser(Ser.JAPANESE_DATE_TYPE, this);
 619     }
 620 
 621     void writeExternal(DataOutput out) throws IOException {
 622         // JapaneseChronology is implicit in the JAPANESE_DATE_TYPE
 623         out.writeInt(get(YEAR));
 624         out.writeByte(get(MONTH_OF_YEAR));
 625         out.writeByte(get(DAY_OF_MONTH));
 626     }
 627 
 628     static JapaneseDate readExternal(DataInput in) throws IOException {
 629         int year = in.readInt();
 630         int month = in.readByte();
 631         int dayOfMonth = in.readByte();
 632         return JapaneseChronology.INSTANCE.date(year, month, dayOfMonth);
 633     }
 634 
 635 }