src/share/classes/java/time/LocalDate.java

Print this page




  63 
  64 import static java.time.LocalTime.SECONDS_PER_DAY;
  65 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;
  66 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;
  67 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;
  68 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
  69 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
  70 import static java.time.temporal.ChronoField.DAY_OF_YEAR;
  71 import static java.time.temporal.ChronoField.EPOCH_DAY;
  72 import static java.time.temporal.ChronoField.EPOCH_MONTH;
  73 import static java.time.temporal.ChronoField.ERA;
  74 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  75 import static java.time.temporal.ChronoField.YEAR;
  76 
  77 import java.io.DataInput;
  78 import java.io.DataOutput;
  79 import java.io.IOException;
  80 import java.io.InvalidObjectException;
  81 import java.io.ObjectStreamException;
  82 import java.io.Serializable;
  83 import java.time.format.DateTimeBuilder;


  84 import java.time.format.DateTimeFormatter;
  85 import java.time.format.DateTimeFormatters;
  86 import java.time.format.DateTimeParseException;
  87 import java.time.temporal.ChronoField;
  88 import java.time.temporal.ChronoLocalDate;
  89 import java.time.temporal.ChronoUnit;
  90 import java.time.temporal.Era;
  91 import java.time.temporal.ISOChrono;
  92 import java.time.temporal.OffsetDate;
  93 import java.time.temporal.Temporal;
  94 import java.time.temporal.TemporalAccessor;
  95 import java.time.temporal.TemporalAdder;
  96 import java.time.temporal.TemporalAdjuster;

  97 import java.time.temporal.TemporalField;
  98 import java.time.temporal.TemporalQuery;
  99 import java.time.temporal.TemporalSubtractor;
 100 import java.time.temporal.TemporalUnit;
 101 import java.time.temporal.ValueRange;
 102 import java.time.temporal.Year;
 103 import java.time.zone.ZoneOffsetTransition;
 104 import java.time.zone.ZoneRules;
 105 import java.util.Objects;
 106 
 107 /**
 108  * A date without a time-zone in the ISO-8601 calendar system,
 109  * such as {@code 2007-12-03}.
 110  * <p>
 111  * {@code LocalDate} is an immutable date-time object that represents a date,
 112  * often viewed as year-month-day. Other date fields, such as day-of-year,
 113  * day-of-week and week-of-year, can also be accessed.
 114  * For example, the value "2nd October 2007" can be stored in a {@code LocalDate}.
 115  * <p>
 116  * This class does not store or represent a time or time-zone.
 117  * Instead, it is a description of the date, as used for birthdays.
 118  * It cannot represent an instant on the time-line without additional information
 119  * such as an offset or time-zone.
 120  * <p>
 121  * The ISO-8601 calendar system is the modern civil calendar system used today
 122  * in most of the world. It is equivalent to the proleptic Gregorian calendar
 123  * system, in which today's rules for leap years are applied for all time.
 124  * For most applications written today, the ISO-8601 rules are entirely suitable.
 125  * However, any application that makes use of historical dates, and requires them
 126  * to be accurate will find the ISO-8601 approach unsuitable.
 127  *
 128  * <h3>Specification for implementors</h3>
 129  * This class is immutable and thread-safe.
 130  *
 131  * @since 1.8
 132  */
 133 public final class LocalDate
 134         implements Temporal, TemporalAdjuster, ChronoLocalDate<ISOChrono>, Serializable {
 135 
 136     /**
 137      * The minimum supported {@code LocalDate}, '-999999999-01-01'.
 138      * This could be used by an application as a "far past" date.
 139      */
 140     public static final LocalDate MIN = LocalDate.of(Year.MIN_VALUE, 1, 1);
 141     /**
 142      * The maximum supported {@code LocalDate}, '+999999999-12-31'.
 143      * This could be used by an application as a "far future" date.
 144      */
 145     public static final LocalDate MAX = LocalDate.of(Year.MAX_VALUE, 12, 31);
 146 
 147     /**
 148      * Serialization version.
 149      */
 150     private static final long serialVersionUID = 2942565459149668126L;
 151     /**
 152      * The number of days in a 400 year cycle.
 153      */
 154     private static final int DAYS_PER_CYCLE = 146097;


 199      *
 200      * @param zone  the zone ID to use, not null
 201      * @return the current date using the system clock, not null
 202      */
 203     public static LocalDate now(ZoneId zone) {
 204         return now(Clock.system(zone));
 205     }
 206 
 207     /**
 208      * Obtains the current date from the specified clock.
 209      * <p>
 210      * This will query the specified clock to obtain the current date - today.
 211      * Using this method allows the use of an alternate clock for testing.
 212      * The alternate clock may be introduced using {@link Clock dependency injection}.
 213      *
 214      * @param clock  the clock to use, not null
 215      * @return the current date, not null
 216      */
 217     public static LocalDate now(Clock clock) {
 218         Objects.requireNonNull(clock, "clock");
 219         // inline OffsetDate factory to avoid creating object and InstantProvider checks
 220         final Instant now = clock.instant();  // called once
 221         ZoneOffset offset = clock.getZone().getRules().getOffset(now);
 222         long epochSec = now.getEpochSecond() + offset.getTotalSeconds();  // overflow caught later
 223         long epochDay = Math.floorDiv(epochSec, SECONDS_PER_DAY);
 224         return LocalDate.ofEpochDay(epochDay);
 225     }
 226 
 227     //-----------------------------------------------------------------------
 228     /**
 229      * Obtains an instance of {@code LocalDate} from a year, month and day.
 230      * <p>

 231      * The day must be valid for the year and month, otherwise an exception will be thrown.
 232      *
 233      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 234      * @param month  the month-of-year to represent, not null
 235      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 236      * @return the local date, not null
 237      * @throws DateTimeException if the value of any field is out of range
 238      * @throws DateTimeException if the day-of-month is invalid for the month-year
 239      */
 240     public static LocalDate of(int year, Month month, int dayOfMonth) {
 241         YEAR.checkValidValue(year);
 242         Objects.requireNonNull(month, "month");
 243         DAY_OF_MONTH.checkValidValue(dayOfMonth);
 244         return create(year, month, dayOfMonth);
 245     }
 246 
 247     /**
 248      * Obtains an instance of {@code LocalDate} from a year, month and day.
 249      * <p>

 250      * The day must be valid for the year and month, otherwise an exception will be thrown.
 251      *
 252      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 253      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 254      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 255      * @return the local date, not null
 256      * @throws DateTimeException if the value of any field is out of range
 257      * @throws DateTimeException if the day-of-month is invalid for the month-year
 258      */
 259     public static LocalDate of(int year, int month, int dayOfMonth) {
 260         YEAR.checkValidValue(year);
 261         MONTH_OF_YEAR.checkValidValue(month);
 262         DAY_OF_MONTH.checkValidValue(dayOfMonth);
 263         return create(year, Month.of(month), dayOfMonth);
 264     }
 265 
 266     //-----------------------------------------------------------------------
 267     /**
 268      * Obtains an instance of {@code LocalDate} from a year and day-of-year.
 269      * <p>

 270      * The day-of-year must be valid for the year, otherwise an exception will be thrown.
 271      *
 272      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 273      * @param dayOfYear  the day-of-year to represent, from 1 to 366
 274      * @return the local date, not null
 275      * @throws DateTimeException if the value of any field is out of range
 276      * @throws DateTimeException if the day-of-year is invalid for the month-year
 277      */
 278     public static LocalDate ofYearDay(int year, int dayOfYear) {
 279         YEAR.checkValidValue(year);
 280         DAY_OF_YEAR.checkValidValue(dayOfYear);
 281         boolean leap = ISOChrono.INSTANCE.isLeapYear(year);
 282         if (dayOfYear == 366 && leap == false) {
 283             throw new DateTimeException("Invalid date 'DayOfYear 366' as '" + year + "' is not a leap year");
 284         }
 285         Month moy = Month.of((dayOfYear - 1) / 31 + 1);
 286         int monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1;
 287         if (dayOfYear > monthEnd) {
 288             moy = moy.plus(1);
 289         }
 290         int dom = dayOfYear - moy.firstDayOfYear(leap) + 1;
 291         return create(year, moy, dom);
 292     }
 293 
 294     //-----------------------------------------------------------------------
 295     /**
 296      * Obtains an instance of {@code LocalDate} from the epoch day count.
 297      * <p>
 298      * The Epoch Day count is a simple incrementing count of days
 299      * where day 0 is 1970-01-01. Negative numbers represent earlier days.

 300      *
 301      * @param epochDay  the Epoch Day to convert, based on the epoch 1970-01-01
 302      * @return the local date, not null
 303      * @throws DateTimeException if the epoch days exceeds the supported date range
 304      */
 305     public static LocalDate ofEpochDay(long epochDay) {
 306         long zeroDay = epochDay + DAYS_0000_TO_1970;
 307         // find the march-based year
 308         zeroDay -= 60;  // adjust to 0000-03-01 so leap day is at end of four year cycle
 309         long adjust = 0;
 310         if (zeroDay < 0) {
 311             // adjust negative years to positive for calculation
 312             long adjustCycles = (zeroDay + 1) / DAYS_PER_CYCLE - 1;
 313             adjust = adjustCycles * 400;
 314             zeroDay += -adjustCycles * DAYS_PER_CYCLE;
 315         }
 316         long yearEst = (400 * zeroDay + 591) / DAYS_PER_CYCLE;
 317         long doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
 318         if (doyEst < 0) {
 319             // fix estimate


 321             doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
 322         }
 323         yearEst += adjust;  // reset any negative year
 324         int marchDoy0 = (int) doyEst;
 325 
 326         // convert march-based values back to january-based
 327         int marchMonth0 = (marchDoy0 * 5 + 2) / 153;
 328         int month = (marchMonth0 + 2) % 12 + 1;
 329         int dom = marchDoy0 - (marchMonth0 * 306 + 5) / 10 + 1;
 330         yearEst += marchMonth0 / 10;
 331 
 332         // check year now we are certain it is correct
 333         int year = YEAR.checkValidIntValue(yearEst);
 334         return new LocalDate(year, month, dom);
 335     }
 336 
 337     //-----------------------------------------------------------------------
 338     /**
 339      * Obtains an instance of {@code LocalDate} from a temporal object.
 340      * <p>
 341      * A {@code TemporalAccessor} represents some form of date and time information.
 342      * This factory converts the arbitrary temporal object to an instance of {@code LocalDate}.

 343      * <p>
 344      * The conversion extracts the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field.

 345      * <p>
 346      * This method matches the signature of the functional interface {@link TemporalQuery}
 347      * allowing it to be used as a query via method reference, {@code LocalDate::from}.
 348      *
 349      * @param temporal  the temporal object to convert, not null
 350      * @return the local date, not null
 351      * @throws DateTimeException if unable to convert to a {@code LocalDate}
 352      */
 353     public static LocalDate from(TemporalAccessor temporal) {
 354         if (temporal instanceof LocalDate) {
 355             return (LocalDate) temporal;
 356         } else if (temporal instanceof LocalDateTime) {
 357             return ((LocalDateTime) temporal).getDate();
 358         } else if (temporal instanceof ZonedDateTime) {
 359             return ((ZonedDateTime) temporal).getDate();
 360         }
 361         // handle builder as a special case
 362         if (temporal instanceof DateTimeBuilder) {
 363             DateTimeBuilder builder = (DateTimeBuilder) temporal;
 364             LocalDate date = builder.extract(LocalDate.class);
 365             if (date != null) {
 366                 return date;
 367             }
 368         }
 369         try {
 370             return ofEpochDay(temporal.getLong(EPOCH_DAY));
 371         } catch (DateTimeException ex) {
 372             throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " + temporal.getClass(), ex);
 373         }

 374     }
 375 
 376     //-----------------------------------------------------------------------
 377     /**
 378      * Obtains an instance of {@code LocalDate} from a text string such as {@code 2007-12-03}.
 379      * <p>
 380      * The string must represent a valid date and is parsed using
 381      * {@link java.time.format.DateTimeFormatters#isoLocalDate()}.
 382      *
 383      * @param text  the text to parse such as "2007-12-03", not null
 384      * @return the parsed local date, not null
 385      * @throws DateTimeParseException if the text cannot be parsed
 386      */
 387     public static LocalDate parse(CharSequence text) {
 388         return parse(text, DateTimeFormatters.isoLocalDate());
 389     }
 390 
 391     /**
 392      * Obtains an instance of {@code LocalDate} from a text string using a specific formatter.
 393      * <p>
 394      * The text is parsed using the formatter, returning a date.
 395      *
 396      * @param text  the text to parse, not null
 397      * @param formatter  the formatter to use, not null
 398      * @return the parsed local date, not null
 399      * @throws DateTimeParseException if the text cannot be parsed
 400      */
 401     public static LocalDate parse(CharSequence text, DateTimeFormatter formatter) {
 402         Objects.requireNonNull(formatter, "formatter");
 403         return formatter.parse(text, LocalDate::from);
 404     }
 405 
 406     //-----------------------------------------------------------------------
 407     /**
 408      * Creates a local date from the year, month and day fields.
 409      *
 410      * @param year  the year to represent, validated from MIN_YEAR to MAX_YEAR
 411      * @param month  the month-of-year to represent, validated not null
 412      * @param dayOfMonth  the day-of-month to represent, validated from 1 to 31
 413      * @return the local date, not null
 414      * @throws DateTimeException if the day-of-month is invalid for the month-year
 415      */
 416     private static LocalDate create(int year, Month month, int dayOfMonth) {
 417         if (dayOfMonth > 28 && dayOfMonth > month.length(ISOChrono.INSTANCE.isLeapYear(year))) {
 418             if (dayOfMonth == 29) {
 419                 throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year");
 420             } else {
 421                 throw new DateTimeException("Invalid date '" + month.name() + " " + dayOfMonth + "'");
 422             }
 423         }
 424         return new LocalDate(year, month.getValue(), dayOfMonth);
 425     }
 426 
 427     /**
 428      * Resolves the date, resolving days past the end of month.
 429      *
 430      * @param year  the year to represent, validated from MIN_YEAR to MAX_YEAR
 431      * @param month  the month-of-year to represent, validated from 1 to 12
 432      * @param day  the day-of-month to represent, validated from 1 to 31
 433      * @return the resolved date, not null
 434      */
 435     private static LocalDate resolvePreviousValid(int year, int month, int day) {
 436         switch (month) {
 437             case 2:
 438                 day = Math.min(day, ISOChrono.INSTANCE.isLeapYear(year) ? 29 : 28);
 439                 break;
 440             case 4:
 441             case 6:
 442             case 9:
 443             case 11:
 444                 day = Math.min(day, 30);
 445                 break;
 446         }
 447         return LocalDate.of(year, month, day);
 448     }
 449 
 450     /**
 451      * Constructor, previously validated.
 452      *
 453      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 454      * @param month  the month-of-year to represent, not null
 455      * @param dayOfMonth  the day-of-month to represent, valid for year-month, from 1 to 31
 456      */
 457     private LocalDate(int year, int month, int dayOfMonth) {
 458         this.year = year;
 459         this.month = (short) month;
 460         this.day = (short) dayOfMonth;
 461     }
 462 
 463     //-----------------------------------------------------------------------
 464     /**
 465      * Checks if the specified field is supported.
 466      * <p>
 467      * This checks if this date can be queried for the specified field.
 468      * If false, then calling the {@link #range(TemporalField) range} and
 469      * {@link #get(TemporalField) get} methods will throw an exception.
 470      * <p>
 471      * If the field is a {@link ChronoField} then the query is implemented here.
 472      * The {@link #isSupported(TemporalField) supported fields} will return valid
 473      * values based on this date-time.
 474      * The supported fields are:
 475      * <ul>
 476      * <li>{@code DAY_OF_WEEK}
 477      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
 478      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
 479      * <li>{@code DAY_OF_MONTH}
 480      * <li>{@code DAY_OF_YEAR}
 481      * <li>{@code EPOCH_DAY}
 482      * <li>{@code ALIGNED_WEEK_OF_MONTH}
 483      * <li>{@code ALIGNED_WEEK_OF_YEAR}
 484      * <li>{@code MONTH_OF_YEAR}
 485      * <li>{@code EPOCH_MONTH}
 486      * <li>{@code YEAR_OF_ERA}
 487      * <li>{@code YEAR}
 488      * <li>{@code ERA}
 489      * </ul>
 490      * All other {@code ChronoField} instances will return false.
 491      * <p>
 492      * If the field is not a {@code ChronoField}, then the result of this method
 493      * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)}
 494      * passing {@code this} as the argument.
 495      * Whether the field is supported is determined by the field.
 496      *
 497      * @param field  the field to check, null returns false
 498      * @return true if the field is supported on this date, false if not
 499      */
 500     @Override  // override for Javadoc
 501     public boolean isSupported(TemporalField field) {
 502         return ChronoLocalDate.super.isSupported(field);
 503     }
 504 
 505     /**
 506      * Gets the range of valid values for the specified field.
 507      * <p>
 508      * The range object expresses the minimum and maximum valid values for a field.
 509      * This date is used to enhance the accuracy of the returned range.
 510      * If it is not possible to return the range, because the field is not supported
 511      * or for some other reason, an exception is thrown.
 512      * <p>
 513      * If the field is a {@link ChronoField} then the query is implemented here.
 514      * The {@link #isSupported(TemporalField) supported fields} will return
 515      * appropriate range instances.
 516      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 517      * <p>
 518      * If the field is not a {@code ChronoField}, then the result of this method
 519      * is obtained by invoking {@code TemporalField.doRange(TemporalAccessor)}
 520      * passing {@code this} as the argument.
 521      * Whether the range can be obtained is determined by the field.
 522      *
 523      * @param field  the field to query the range for, not null
 524      * @return the range of valid values for the field, not null
 525      * @throws DateTimeException if the range for the field cannot be obtained
 526      */
 527     @Override
 528     public ValueRange range(TemporalField field) {
 529         if (field instanceof ChronoField) {
 530             ChronoField f = (ChronoField) field;
 531             if (f.isDateField()) {
 532                 switch (f) {
 533                     case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
 534                     case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
 535                     case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, getMonth() == Month.FEBRUARY && isLeapYear() == false ? 4 : 5);
 536                     case YEAR_OF_ERA:
 537                         return (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE));
 538                 }
 539                 return field.range();
 540             }
 541             throw new DateTimeException("Unsupported field: " + field.getName());
 542         }
 543         return field.doRange(this);
 544     }
 545 
 546     /**
 547      * Gets the value of the specified field from this date as an {@code int}.
 548      * <p>
 549      * This queries this date for the value for the specified field.
 550      * The returned value will always be within the valid range of values for the field.
 551      * If it is not possible to return the value, because the field is not supported
 552      * or for some other reason, an exception is thrown.
 553      * <p>
 554      * If the field is a {@link ChronoField} then the query is implemented here.
 555      * The {@link #isSupported(TemporalField) supported fields} will return valid
 556      * values based on this date, except {@code EPOCH_DAY} and {@code EPOCH_MONTH}
 557      * which are too large to fit in an {@code int} and throw a {@code DateTimeException}.
 558      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 559      * <p>
 560      * If the field is not a {@code ChronoField}, then the result of this method
 561      * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
 562      * passing {@code this} as the argument. Whether the value can be obtained,
 563      * and what the value represents, is determined by the field.
 564      *
 565      * @param field  the field to get, not null
 566      * @return the value for the field
 567      * @throws DateTimeException if a value for the field cannot be obtained
 568      * @throws ArithmeticException if numeric overflow occurs
 569      */
 570     @Override  // override for Javadoc and performance
 571     public int get(TemporalField field) {
 572         if (field instanceof ChronoField) {
 573             return get0(field);
 574         }
 575         return ChronoLocalDate.super.get(field);
 576     }
 577 
 578     /**
 579      * Gets the value of the specified field from this date as a {@code long}.
 580      * <p>
 581      * This queries this date for the value for the specified field.
 582      * If it is not possible to return the value, because the field is not supported
 583      * or for some other reason, an exception is thrown.
 584      * <p>
 585      * If the field is a {@link ChronoField} then the query is implemented here.
 586      * The {@link #isSupported(TemporalField) supported fields} will return valid
 587      * values based on this date.
 588      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 589      * <p>
 590      * If the field is not a {@code ChronoField}, then the result of this method
 591      * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
 592      * passing {@code this} as the argument. Whether the value can be obtained,
 593      * and what the value represents, is determined by the field.
 594      *
 595      * @param field  the field to get, not null
 596      * @return the value for the field
 597      * @throws DateTimeException if a value for the field cannot be obtained
 598      * @throws ArithmeticException if numeric overflow occurs
 599      */
 600     @Override
 601     public long getLong(TemporalField field) {
 602         if (field instanceof ChronoField) {
 603             if (field == EPOCH_DAY) {
 604                 return toEpochDay();
 605             }
 606             if (field == EPOCH_MONTH) {
 607                 return getEpochMonth();
 608             }
 609             return get0(field);
 610         }
 611         return field.doGet(this);
 612     }
 613 
 614     private int get0(TemporalField field) {
 615         switch ((ChronoField) field) {
 616             case DAY_OF_WEEK: return getDayOfWeek().getValue();
 617             case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1;
 618             case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1;
 619             case DAY_OF_MONTH: return day;
 620             case DAY_OF_YEAR: return getDayOfYear();
 621             case EPOCH_DAY: throw new DateTimeException("Field too large for an int: " + field);
 622             case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1;
 623             case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1;
 624             case MONTH_OF_YEAR: return month;
 625             case EPOCH_MONTH: throw new DateTimeException("Field too large for an int: " + field);
 626             case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year);
 627             case YEAR: return year;
 628             case ERA: return (year >= 1 ? 1 : 0);
 629         }
 630         throw new DateTimeException("Unsupported field: " + field.getName());
 631     }
 632 
 633     private long getEpochMonth() {
 634         return ((year - 1970) * 12L) + (month - 1);
 635     }
 636 
 637     //-----------------------------------------------------------------------
 638     /**
 639      * Gets the chronology of this date, which is the ISO calendar system.
 640      * <p>
 641      * The {@code Chrono} represents the calendar system in use.
 642      * The ISO-8601 calendar system is the modern civil calendar system used today
 643      * in most of the world. It is equivalent to the proleptic Gregorian calendar
 644      * system, in which todays's rules for leap years are applied for all time.
 645      *
 646      * @return the ISO chronology, not null
 647      */
 648     @Override
 649     public ISOChrono getChrono() {
 650         return ISOChrono.INSTANCE;
 651     }
 652 
 653     /**
 654      * Gets the era applicable at this date.
 655      * <p>
 656      * The official ISO-8601 standard does not define eras, however {@code ISOChrono} does.
 657      * It defines two eras, 'CE' from year one onwards and 'BCE' from year zero backwards.
 658      * Since dates before the Julian-Gregorian cutover are not in line with history,
 659      * the cutover between 'BCE' and 'CE' is also not aligned with the commonly used
 660      * eras, often referred to using 'BC' and 'AD'.
 661      * <p>
 662      * Users of this class should typically ignore this method as it exists primarily
 663      * to fulfill the {@link ChronoLocalDate} contract where it is necessary to support
 664      * the Japanese calendar system.
 665      * <p>
 666      * The returned era will be a singleton capable of being compared with the constants
 667      * in {@link ISOChrono} using the {@code ==} operator.
 668      *
 669      * @return the {@code ISOChrono} era constant applicable at this date, not null
 670      */
 671     @Override // override for Javadoc
 672     public Era<ISOChrono> getEra() {
 673         return ChronoLocalDate.super.getEra();
 674     }
 675 
 676     /**
 677      * Gets the year field.
 678      * <p>
 679      * This method returns the primitive {@code int} value for the year.
 680      * <p>
 681      * The year returned by this method is proleptic as per {@code get(YEAR)}.
 682      * To obtain the year-of-era, use {@code get(YEAR_OF_ERA}.
 683      *
 684      * @return the year, from MIN_YEAR to MAX_YEAR
 685      */
 686     public int getYear() {
 687         return year;
 688     }
 689 
 690     /**
 691      * Gets the month-of-year field from 1 to 12.
 692      * <p>
 693      * This method returns the month as an {@code int} from 1 to 12.
 694      * Application code is frequently clearer if the enum {@link Month}
 695      * is used by calling {@link #getMonth()}.
 696      *
 697      * @return the month-of-year, from 1 to 12
 698      * @see #getMonth()
 699      */
 700     public int getMonthValue() {
 701         return month;
 702     }


 760     /**
 761      * Checks if the year is a leap year, according to the ISO proleptic
 762      * calendar system rules.
 763      * <p>
 764      * This method applies the current rules for leap years across the whole time-line.
 765      * In general, a year is a leap year if it is divisible by four without
 766      * remainder. However, years divisible by 100, are not leap years, with
 767      * the exception of years divisible by 400 which are.
 768      * <p>
 769      * For example, 1904 is a leap year it is divisible by 4.
 770      * 1900 was not a leap year as it is divisible by 100, however 2000 was a
 771      * leap year as it is divisible by 400.
 772      * <p>
 773      * The calculation is proleptic - applying the same rules into the far future and far past.
 774      * This is historically inaccurate, but is correct for the ISO-8601 standard.
 775      *
 776      * @return true if the year is leap, false otherwise
 777      */
 778     @Override // override for Javadoc and performance
 779     public boolean isLeapYear() {
 780         return ISOChrono.INSTANCE.isLeapYear(year);
 781     }
 782 
 783     /**
 784      * Returns the length of the month represented by this date.
 785      * <p>
 786      * This returns the length of the month in days.
 787      * For example, a date in January would return 31.
 788      *
 789      * @return the length of the month in days
 790      */
 791     @Override
 792     public int lengthOfMonth() {
 793         switch (month) {
 794             case 2:
 795                 return (isLeapYear() ? 29 : 28);
 796             case 4:
 797             case 6:
 798             case 9:
 799             case 11:
 800                 return 30;


 802                 return 31;
 803         }
 804     }
 805 
 806     /**
 807      * Returns the length of the year represented by this date.
 808      * <p>
 809      * This returns the length of the year in days, either 365 or 366.
 810      *
 811      * @return 366 if the year is leap, 365 otherwise
 812      */
 813     @Override // override for Javadoc and performance
 814     public int lengthOfYear() {
 815         return (isLeapYear() ? 366 : 365);
 816     }
 817 
 818     //-----------------------------------------------------------------------
 819     /**
 820      * Returns an adjusted copy of this date.
 821      * <p>
 822      * This returns a new {@code LocalDate}, based on this one, with the date adjusted.
 823      * The adjustment takes place using the specified adjuster strategy object.
 824      * Read the documentation of the adjuster to understand what adjustment will be made.
 825      * <p>
 826      * A simple adjuster might simply set the one of the fields, such as the year field.
 827      * A more complex adjuster might set the date to the last day of the month.
 828      * A selection of common adjustments is provided in {@link java.time.temporal.Adjusters}.
 829      * These include finding the "last day of the month" and "next Wednesday".
 830      * Key date-time classes also implement the {@code TemporalAdjuster} interface,
 831      * such as {@link Month} and {@link java.time.temporal.MonthDay MonthDay}.
 832      * The adjuster is responsible for handling special cases, such as the varying
 833      * lengths of month and leap years.
 834      * <p>
 835      * For example this code returns a date on the last day of July:
 836      * <pre>
 837      *  import static java.time.Month.*;
 838      *  import static java.time.temporal.Adjusters.*;
 839      *
 840      *  result = localDate.with(JULY).with(lastDayOfMonth());
 841      * </pre>
 842      * <p>
 843      * The result of this method is obtained by invoking the
 844      * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
 845      * specified adjuster passing {@code this} as the argument.
 846      * <p>
 847      * This instance is immutable and unaffected by this method call.
 848      *
 849      * @param adjuster the adjuster to use, not null
 850      * @return a {@code LocalDate} based on {@code this} with the adjustment made, not null
 851      * @throws DateTimeException if the adjustment cannot be made
 852      * @throws ArithmeticException if numeric overflow occurs
 853      */
 854     @Override
 855     public LocalDate with(TemporalAdjuster adjuster) {
 856         // optimizations
 857         if (adjuster instanceof LocalDate) {
 858             return (LocalDate) adjuster;
 859         }
 860         return (LocalDate) adjuster.adjustInto(this);
 861     }
 862 
 863     /**
 864      * Returns a copy of this date with the specified field set to a new value.
 865      * <p>
 866      * This returns a new {@code LocalDate}, based on this one, with the value
 867      * for the specified field changed.
 868      * This can be used to change any supported field, such as the year, month or day-of-month.
 869      * If it is not possible to set the value, because the field is not supported or for
 870      * some other reason, an exception is thrown.
 871      * <p>
 872      * In some cases, changing the specified field can cause the resulting date to become invalid,
 873      * such as changing the month from 31st January to February would make the day-of-month invalid.
 874      * In cases like this, the field is responsible for resolving the date. Typically it will choose
 875      * the previous valid date, which would be the last valid day of February in this example.
 876      * <p>
 877      * If the field is a {@link ChronoField} then the adjustment is implemented here.
 878      * The supported fields behave as follows:
 879      * <ul>
 880      * <li>{@code DAY_OF_WEEK} -
 881      *  Returns a {@code LocalDate} with the specified day-of-week.
 882      *  The date is adjusted up to 6 days forward or backward within the boundary
 883      *  of a Monday to Sunday week.
 884      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} -
 885      *  Returns a {@code LocalDate} with the specified aligned-day-of-week.
 886      *  The date is adjusted to the specified month-based aligned-day-of-week.


 934      *  unless it would be invalid for the new month and year. In that case, the
 935      *  day-of-month is adjusted to the maximum valid value for the new month and year.
 936      * <li>{@code YEAR} -
 937      *  Returns a {@code LocalDate} with the specified year.
 938      *  The month will be unchanged. The day-of-month will also be unchanged,
 939      *  unless it would be invalid for the new month and year. In that case, the
 940      *  day-of-month is adjusted to the maximum valid value for the new month and year.
 941      * <li>{@code ERA} -
 942      *  Returns a {@code LocalDate} with the specified era.
 943      *  The year-of-era and month will be unchanged. The day-of-month will also be unchanged,
 944      *  unless it would be invalid for the new month and year. In that case, the
 945      *  day-of-month is adjusted to the maximum valid value for the new month and year.
 946      * </ul>
 947      * <p>
 948      * In all cases, if the new value is outside the valid range of values for the field
 949      * then a {@code DateTimeException} will be thrown.
 950      * <p>
 951      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 952      * <p>
 953      * If the field is not a {@code ChronoField}, then the result of this method
 954      * is obtained by invoking {@code TemporalField.doWith(Temporal, long)}
 955      * passing {@code this} as the argument. In this case, the field determines
 956      * whether and how to adjust the instant.
 957      * <p>
 958      * This instance is immutable and unaffected by this method call.
 959      *
 960      * @param field  the field to set in the result, not null
 961      * @param newValue  the new value of the field in the result
 962      * @return a {@code LocalDate} based on {@code this} with the specified field set, not null
 963      * @throws DateTimeException if the field cannot be set
 964      * @throws ArithmeticException if numeric overflow occurs
 965      */
 966     @Override
 967     public LocalDate with(TemporalField field, long newValue) {
 968         if (field instanceof ChronoField) {
 969             ChronoField f = (ChronoField) field;
 970             f.checkValidValue(newValue);
 971             switch (f) {
 972                 case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek().getValue());
 973                 case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH));
 974                 case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR));
 975                 case DAY_OF_MONTH: return withDayOfMonth((int) newValue);
 976                 case DAY_OF_YEAR: return withDayOfYear((int) newValue);
 977                 case EPOCH_DAY: return LocalDate.ofEpochDay(newValue);
 978                 case ALIGNED_WEEK_OF_MONTH: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_MONTH));
 979                 case ALIGNED_WEEK_OF_YEAR: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_YEAR));
 980                 case MONTH_OF_YEAR: return withMonth((int) newValue);
 981                 case EPOCH_MONTH: return plusMonths(newValue - getLong(EPOCH_MONTH));
 982                 case YEAR_OF_ERA: return withYear((int) (year >= 1 ? newValue : 1 - newValue));
 983                 case YEAR: return withYear((int) newValue);
 984                 case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year));
 985             }
 986             throw new DateTimeException("Unsupported field: " + field.getName());
 987         }
 988         return field.doWith(this, newValue);
 989     }
 990 
 991     //-----------------------------------------------------------------------
 992     /**
 993      * Returns a copy of this date with the year altered.
 994      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
 995      * <p>
 996      * This instance is immutable and unaffected by this method call.
 997      *
 998      * @param year  the year to set in the result, from MIN_YEAR to MAX_YEAR
 999      * @return a {@code LocalDate} based on this date with the requested year, not null
1000      * @throws DateTimeException if the year value is invalid
1001      */
1002     public LocalDate withYear(int year) {
1003         if (this.year == year) {
1004             return this;
1005         }
1006         YEAR.checkValidValue(year);
1007         return resolvePreviousValid(year, month, day);
1008     }


1016      * @param month  the month-of-year to set in the result, from 1 (January) to 12 (December)
1017      * @return a {@code LocalDate} based on this date with the requested month, not null
1018      * @throws DateTimeException if the month-of-year value is invalid
1019      */
1020     public LocalDate withMonth(int month) {
1021         if (this.month == month) {
1022             return this;
1023         }
1024         MONTH_OF_YEAR.checkValidValue(month);
1025         return resolvePreviousValid(year, month, day);
1026     }
1027 
1028     /**
1029      * Returns a copy of this date with the day-of-month altered.
1030      * If the resulting date is invalid, an exception is thrown.
1031      * <p>
1032      * This instance is immutable and unaffected by this method call.
1033      *
1034      * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
1035      * @return a {@code LocalDate} based on this date with the requested day, not null
1036      * @throws DateTimeException if the day-of-month value is invalid
1037      * @throws DateTimeException if the day-of-month is invalid for the month-year
1038      */
1039     public LocalDate withDayOfMonth(int dayOfMonth) {
1040         if (this.day == dayOfMonth) {
1041             return this;
1042         }
1043         return of(year, month, dayOfMonth);
1044     }
1045 
1046     /**
1047      * Returns a copy of this date with the day-of-year altered.
1048      * If the resulting date is invalid, an exception is thrown.
1049      * <p>
1050      * This instance is immutable and unaffected by this method call.
1051      *
1052      * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
1053      * @return a {@code LocalDate} based on this date with the requested day, not null
1054      * @throws DateTimeException if the day-of-year value is invalid
1055      * @throws DateTimeException if the day-of-year is invalid for the year
1056      */
1057     public LocalDate withDayOfYear(int dayOfYear) {
1058         if (this.getDayOfYear() == dayOfYear) {
1059             return this;
1060         }
1061         return ofYearDay(year, dayOfYear);
1062     }
1063 
1064     //-----------------------------------------------------------------------
1065     /**
1066      * Returns a copy of this date with the specified period added.




1067      * <p>
1068      * This method returns a new date based on this date with the specified period added.
1069      * The adder is typically {@link Period} but may be any other type implementing
1070      * the {@link TemporalAdder} interface.
1071      * The calculation is delegated to the specified adjuster, which typically calls
1072      * back to {@link #plus(long, TemporalUnit)}.
1073      * <p>
1074      * This instance is immutable and unaffected by this method call.
1075      *
1076      * @param adder  the adder to use, not null
1077      * @return a {@code LocalDate} based on this date with the addition made, not null
1078      * @throws DateTimeException if the addition cannot be made
1079      * @throws ArithmeticException if numeric overflow occurs
1080      */
1081     @Override
1082     public LocalDate plus(TemporalAdder adder) {
1083         return (LocalDate) adder.addTo(this);
1084     }
1085 
1086     /**
1087      * Returns a copy of this date with the specified period added.
































































1088      * <p>
1089      * This method returns a new date based on this date with the specified period added.
1090      * This can be used to add any period that is defined by a unit, for example to add years, months or days.
1091      * The unit is responsible for the details of the calculation, including the resolution
1092      * of any edge cases in the calculation.
1093      * <p>
1094      * This instance is immutable and unaffected by this method call.
1095      *
1096      * @param amountToAdd  the amount of the unit to add to the result, may be negative
1097      * @param unit  the unit of the period to add, not null
1098      * @return a {@code LocalDate} based on this date with the specified period added, not null
1099      * @throws DateTimeException if the unit cannot be added to this type

1100      */
1101     @Override
1102     public LocalDate plus(long amountToAdd, TemporalUnit unit) {
1103         if (unit instanceof ChronoUnit) {
1104             ChronoUnit f = (ChronoUnit) unit;
1105             switch (f) {
1106                 case DAYS: return plusDays(amountToAdd);
1107                 case WEEKS: return plusWeeks(amountToAdd);
1108                 case MONTHS: return plusMonths(amountToAdd);
1109                 case YEARS: return plusYears(amountToAdd);
1110                 case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
1111                 case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
1112                 case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
1113                 case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
1114             }
1115             throw new DateTimeException("Unsupported unit: " + unit.getName());
1116         }
1117         return unit.doPlus(this, amountToAdd);
1118     }
1119 
1120     //-----------------------------------------------------------------------
1121     /**
1122      * Returns a copy of this {@code LocalDate} with the specified period in years added.
1123      * <p>
1124      * This method adds the specified amount to the years field in three steps:
1125      * <ol>
1126      * <li>Add the input years to the year field</li>
1127      * <li>Check if the resulting date would be invalid</li>
1128      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1129      * </ol>
1130      * <p>
1131      * For example, 2008-02-29 (leap year) plus one year would result in the
1132      * invalid date 2009-02-29 (standard year). Instead of returning an invalid
1133      * result, the last valid day of the month, 2009-02-28, is selected instead.
1134      * <p>
1135      * This instance is immutable and unaffected by this method call.
1136      *
1137      * @param yearsToAdd  the years to add, may be negative


1204      * The result is only invalid if the maximum/minimum year is exceeded.
1205      * <p>
1206      * For example, 2008-12-31 plus one day would result in 2009-01-01.
1207      * <p>
1208      * This instance is immutable and unaffected by this method call.
1209      *
1210      * @param daysToAdd  the days to add, may be negative
1211      * @return a {@code LocalDate} based on this date with the days added, not null
1212      * @throws DateTimeException if the result exceeds the supported date range
1213      */
1214     public LocalDate plusDays(long daysToAdd) {
1215         if (daysToAdd == 0) {
1216             return this;
1217         }
1218         long mjDay = Math.addExact(toEpochDay(), daysToAdd);
1219         return LocalDate.ofEpochDay(mjDay);
1220     }
1221 
1222     //-----------------------------------------------------------------------
1223     /**
1224      * Returns a copy of this date with the specified period subtracted.
1225      * <p>
1226      * This method returns a new date based on this date with the specified period subtracted.
1227      * The subtractor is typically {@link Period} but may be any other type implementing
1228      * the {@link TemporalSubtractor} interface.
1229      * The calculation is delegated to the specified adjuster, which typically calls
1230      * back to {@link #minus(long, TemporalUnit)}.




1231      * <p>
1232      * This instance is immutable and unaffected by this method call.
1233      *
1234      * @param subtractor  the subtractor to use, not null
1235      * @return a {@code LocalDate} based on this date with the subtraction made, not null
1236      * @throws DateTimeException if the subtraction cannot be made
1237      * @throws ArithmeticException if numeric overflow occurs
1238      */
1239     @Override
1240     public LocalDate minus(TemporalSubtractor subtractor) {
1241         return (LocalDate) subtractor.subtractFrom(this);
1242     }
1243 
1244     /**
1245      * Returns a copy of this date with the specified period subtracted.




1246      * <p>
1247      * This method returns a new date based on this date with the specified period subtracted.
1248      * This can be used to subtract any period that is defined by a unit, for example to subtract years, months or days.
1249      * The unit is responsible for the details of the calculation, including the resolution
1250      * of any edge cases in the calculation.
1251      * <p>
1252      * This instance is immutable and unaffected by this method call.
1253      *
1254      * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
1255      * @param unit  the unit of the period to subtract, not null
1256      * @return a {@code LocalDate} based on this date with the specified period subtracted, not null
1257      * @throws DateTimeException if the unit cannot be added to this type

1258      */
1259     @Override
1260     public LocalDate minus(long amountToSubtract, TemporalUnit unit) {
1261         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
1262     }
1263 
1264     //-----------------------------------------------------------------------
1265     /**
1266      * Returns a copy of this {@code LocalDate} with the specified period in years subtracted.
1267      * <p>
1268      * This method subtracts the specified amount from the years field in three steps:
1269      * <ol>
1270      * <li>Subtract the input years to the year field</li>
1271      * <li>Check if the resulting date would be invalid</li>
1272      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1273      * </ol>
1274      * <p>
1275      * For example, 2008-02-29 (leap year) minus one year would result in the
1276      * invalid date 2007-02-29 (standard year). Instead of returning an invalid
1277      * result, the last valid day of the month, 2007-02-28, is selected instead.


1350 
1351     //-----------------------------------------------------------------------
1352     /**
1353      * Queries this date using the specified query.
1354      * <p>
1355      * This queries this date using the specified query strategy object.
1356      * The {@code TemporalQuery} object defines the logic to be used to
1357      * obtain the result. Read the documentation of the query to understand
1358      * what the result of this method will be.
1359      * <p>
1360      * The result of this method is obtained by invoking the
1361      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
1362      * specified query passing {@code this} as the argument.
1363      *
1364      * @param <R> the type of the result
1365      * @param query  the query to invoke, not null
1366      * @return the query result, null may be returned (defined by the query)
1367      * @throws DateTimeException if unable to query (defined by the query)
1368      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
1369      */
1370     @Override  // override for Javadoc

1371     public <R> R query(TemporalQuery<R> query) {



1372         return ChronoLocalDate.super.query(query);
1373     }
1374 
1375     /**
1376      * Adjusts the specified temporal object to have the same date as this object.
1377      * <p>
1378      * This returns a temporal object of the same observable type as the input
1379      * with the date changed to be the same as this.
1380      * <p>
1381      * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
1382      * passing {@link ChronoField#EPOCH_DAY} as the field.
1383      * <p>
1384      * In most cases, it is clearer to reverse the calling pattern by using
1385      * {@link Temporal#with(TemporalAdjuster)}:
1386      * <pre>
1387      *   // these two lines are equivalent, but the second approach is recommended
1388      *   temporal = thisLocalDate.adjustInto(temporal);
1389      *   temporal = temporal.with(thisLocalDate);
1390      * </pre>
1391      * <p>


1400     public Temporal adjustInto(Temporal temporal) {
1401         return ChronoLocalDate.super.adjustInto(temporal);
1402     }
1403 
1404     /**
1405      * Calculates the period between this date and another date in
1406      * terms of the specified unit.
1407      * <p>
1408      * This calculates the period between two dates in terms of a single unit.
1409      * The start and end points are {@code this} and the specified date.
1410      * The result will be negative if the end is before the start.
1411      * The {@code Temporal} passed to this method must be a {@code LocalDate}.
1412      * For example, the period in days between two dates can be calculated
1413      * using {@code startDate.periodUntil(endDate, DAYS)}.
1414      * <p>
1415      * The calculation returns a whole number, representing the number of
1416      * complete units between the two dates.
1417      * For example, the period in months between 2012-06-15 and 2012-08-14
1418      * will only be one month as it is one day short of two months.
1419      * <p>
1420      * This method operates in association with {@link TemporalUnit#between}.
1421      * The result of this method is a {@code long} representing the amount of
1422      * the specified unit. By contrast, the result of {@code between} is an
1423      * object that can be used directly in addition/subtraction:
1424      * <pre>
1425      *   long period = start.periodUntil(end, MONTHS);   // this method
1426      *   dateTime.plus(MONTHS.between(start, end));      // use in plus/minus

1427      * </pre>

1428      * <p>
1429      * The calculation is implemented in this method for {@link ChronoUnit}.
1430      * The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS},
1431      * {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS}
1432      * are supported. Other {@code ChronoUnit} values will throw an exception.
1433      * <p>
1434      * If the unit is not a {@code ChronoUnit}, then the result of this method
1435      * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
1436      * passing {@code this} as the first argument and the input temporal as
1437      * the second argument.
1438      * <p>
1439      * This instance is immutable and unaffected by this method call.
1440      *
1441      * @param endDate  the end date, which must be a {@code LocalDate}, not null
1442      * @param unit  the unit to measure the period in, not null
1443      * @return the amount of the period between this date and the end date
1444      * @throws DateTimeException if the period cannot be calculated
1445      * @throws ArithmeticException if numeric overflow occurs
1446      */
1447     @Override
1448     public long periodUntil(Temporal endDate, TemporalUnit unit) {
1449         if (endDate instanceof LocalDate == false) {
1450             Objects.requireNonNull(endDate, "endDate");
1451             throw new DateTimeException("Unable to calculate period between objects of two different types");
1452         }
1453         LocalDate end = (LocalDate) endDate;
1454         if (unit instanceof ChronoUnit) {
1455             switch ((ChronoUnit) unit) {
1456                 case DAYS: return daysUntil(end);
1457                 case WEEKS: return daysUntil(end) / 7;
1458                 case MONTHS: return monthsUntil(end);
1459                 case YEARS: return monthsUntil(end) / 12;
1460                 case DECADES: return monthsUntil(end) / 120;
1461                 case CENTURIES: return monthsUntil(end) / 1200;
1462                 case MILLENNIA: return monthsUntil(end) / 12000;
1463                 case ERAS: return end.getLong(ERA) - getLong(ERA);
1464             }
1465             throw new DateTimeException("Unsupported unit: " + unit.getName());
1466         }
1467         return unit.between(this, endDate).getAmount();
1468     }
1469 
1470     long daysUntil(LocalDate end) {
1471         return end.toEpochDay() - toEpochDay();  // no overflow
1472     }
1473 
1474     private long monthsUntil(LocalDate end) {
1475         long packed1 = getEpochMonth() * 32L + getDayOfMonth();  // no overflow
1476         long packed2 = end.getEpochMonth() * 32L + end.getDayOfMonth();  // no overflow
1477         return (packed2 - packed1) / 32;
1478     }
1479 




















































1480     //-----------------------------------------------------------------------
1481     /**
1482      * Returns a local date-time formed from this date at the specified time.
1483      * <p>
1484      * This combines this date with the specified time to form a {@code LocalDateTime}.
1485      * All possible combinations of date and time are valid.
1486      * <p>
1487      * This instance is immutable and unaffected by this method call.
1488      *
1489      * @param time  the time to combine with, not null
1490      * @return the local date-time formed from this date and the specified time, not null
1491      */
1492     @Override
1493     public LocalDateTime atTime(LocalTime time) {
1494         return LocalDateTime.of(this, time);
1495     }
1496 
1497     /**
1498      * Returns a local date-time formed from this date at the specified time.
1499      * <p>
1500      * This combines this date with the specified time to form a {@code LocalDateTime}.


1501      * The individual time fields must be within their valid range.
1502      * All possible combinations of date and time are valid.
1503      * <p>
1504      * This instance is immutable and unaffected by this method call.
1505      *
1506      * @param hour  the hour-of-day to use, from 0 to 23
1507      * @param minute  the minute-of-hour to use, from 0 to 59
1508      * @return the local date-time formed from this date and the specified time, not null
1509      * @throws DateTimeException if the value of any field is out of range
1510      */
1511     public LocalDateTime atTime(int hour, int minute) {
1512         return atTime(LocalTime.of(hour, minute));
1513     }
1514 
1515     /**
1516      * Returns a local date-time formed from this date at the specified time.
1517      * <p>
1518      * This combines this date with the specified time to form a {@code LocalDateTime}.


1519      * The individual time fields must be within their valid range.
1520      * All possible combinations of date and time are valid.
1521      * <p>
1522      * This instance is immutable and unaffected by this method call.
1523      *
1524      * @param hour  the hour-of-day to use, from 0 to 23
1525      * @param minute  the minute-of-hour to use, from 0 to 59
1526      * @param second  the second-of-minute to represent, from 0 to 59
1527      * @return the local date-time formed from this date and the specified time, not null
1528      * @throws DateTimeException if the value of any field is out of range
1529      */
1530     public LocalDateTime atTime(int hour, int minute, int second) {
1531         return atTime(LocalTime.of(hour, minute, second));
1532     }
1533 
1534     /**
1535      * Returns a local date-time formed from this date at the specified time.
1536      * <p>
1537      * This combines this date with the specified time to form a {@code LocalDateTime}.

1538      * The individual time fields must be within their valid range.
1539      * All possible combinations of date and time are valid.
1540      * <p>
1541      * This instance is immutable and unaffected by this method call.
1542      *
1543      * @param hour  the hour-of-day to use, from 0 to 23
1544      * @param minute  the minute-of-hour to use, from 0 to 59
1545      * @param second  the second-of-minute to represent, from 0 to 59
1546      * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
1547      * @return the local date-time formed from this date and the specified time, not null
1548      * @throws DateTimeException if the value of any field is out of range
1549      */
1550     public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond) {
1551         return atTime(LocalTime.of(hour, minute, second, nanoOfSecond));
1552     }
1553 
1554     /**
1555      * Returns an offset date formed from this date and the specified offset.
1556      * <p>
1557      * This combines this date with the specified offset to form an {@code OffsetDate}.
1558      * All possible combinations of date and offset are valid.











1559      * <p>
1560      * This instance is immutable and unaffected by this method call.

1561      *
1562      * @param offset  the offset to combine with, not null
1563      * @return the offset date formed from this date and the specified offset, not null
1564      */
1565     public OffsetDate atOffset(ZoneOffset offset) {
1566         return OffsetDate.of(this, offset);
1567     }
1568 
1569     /**
1570      * Returns a zoned date-time from this date at the earliest valid time according
1571      * to the rules in the time-zone.
1572      * <p>
1573      * Time-zone rules, such as daylight savings, mean that not every local date-time
1574      * is valid for the specified zone, thus the local date-time may not be midnight.
1575      * <p>
1576      * In most cases, there is only one valid offset for a local date-time.
1577      * In the case of an overlap, there are two valid offsets, and the earlier one is used,
1578      * corresponding to the first occurrence of midnight on the date.
1579      * In the case of a gap, the zoned date-time will represent the instant just after the gap.
1580      * <p>
1581      * If the zone ID is a {@link ZoneOffset}, then the result always has a time of midnight.
1582      * <p>
1583      * To convert to a specific time in a given time-zone call {@link #atTime(LocalTime)}
1584      * followed by {@link LocalDateTime#atZone(ZoneId)}.
1585      * <p>
1586      * This instance is immutable and unaffected by this method call.
1587      *
1588      * @param zone  the zone ID to use, not null
1589      * @return the zoned date-time formed from this date and the earliest valid time for the zone, not null
1590      */
1591     public ZonedDateTime atStartOfDay(ZoneId zone) {
1592         Objects.requireNonNull(zone, "zone");
1593         // need to handle case where there is a gap from 11:30 to 00:30
1594         // standard ZDT factory would result in 01:00 rather than 00:30
1595         LocalDateTime ldt = atTime(LocalTime.MIDNIGHT);
1596         if (zone instanceof ZoneOffset == false) {
1597             ZoneRules rules = zone.getRules();
1598             ZoneOffsetTransition trans = rules.getTransition(ldt);
1599             if (trans != null && trans.isGap()) {
1600                 ldt = trans.getDateTimeAfter();
1601             }
1602         }
1603         return ZonedDateTime.of(ldt, zone);
1604     }
1605 
1606     //-----------------------------------------------------------------------


1619         total += day - 1;
1620         if (m > 2) {
1621             total--;
1622             if (isLeapYear() == false) {
1623                 total--;
1624             }
1625         }
1626         return total - DAYS_0000_TO_1970;
1627     }
1628 
1629     //-----------------------------------------------------------------------
1630     /**
1631      * Compares this date to another date.
1632      * <p>
1633      * The comparison is primarily based on the date, from earliest to latest.
1634      * It is "consistent with equals", as defined by {@link Comparable}.
1635      * <p>
1636      * If all the dates being compared are instances of {@code LocalDate},
1637      * then the comparison will be entirely based on the date.
1638      * If some dates being compared are in different chronologies, then the
1639      * chronology is also considered, see {@link java.time.temporal.ChronoLocalDate#compareTo}.
1640      *
1641      * @param other  the other date to compare to, not null
1642      * @return the comparator value, negative if less, positive if greater
1643      */
1644     @Override  // override for Javadoc and performance
1645     public int compareTo(ChronoLocalDate<?> other) {
1646         if (other instanceof LocalDate) {
1647             return compareTo0((LocalDate) other);
1648         }
1649         return ChronoLocalDate.super.compareTo(other);
1650     }
1651 
1652     int compareTo0(LocalDate otherDate) {
1653         int cmp = (year - otherDate.year);
1654         if (cmp == 0) {
1655             cmp = (month - otherDate.month);
1656             if (cmp == 0) {
1657                 cmp = (day - otherDate.day);
1658             }
1659         }


1805             } else {
1806                 buf.append(yearValue + 10000).deleteCharAt(0);
1807             }
1808         } else {
1809             if (yearValue > 9999) {
1810                 buf.append('+');
1811             }
1812             buf.append(yearValue);
1813         }
1814         return buf.append(monthValue < 10 ? "-0" : "-")
1815             .append(monthValue)
1816             .append(dayValue < 10 ? "-0" : "-")
1817             .append(dayValue)
1818             .toString();
1819     }
1820 
1821     /**
1822      * Outputs this date as a {@code String} using the formatter.
1823      * <p>
1824      * This date will be passed to the formatter
1825      * {@link DateTimeFormatter#print(TemporalAccessor) print method}.
1826      *
1827      * @param formatter  the formatter to use, not null
1828      * @return the formatted date string, not null
1829      * @throws DateTimeException if an error occurs during printing
1830      */
1831     @Override  // override for Javadoc
1832     public String toString(DateTimeFormatter formatter) {
1833         return ChronoLocalDate.super.toString(formatter);
1834     }
1835 
1836     //-----------------------------------------------------------------------
1837     /**
1838      * Writes the object using a
1839      * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1840      * <pre>
1841      *  out.writeByte(3);  // identifies this as a LocalDate
1842      *  out.writeInt(year);
1843      *  out.writeByte(month);
1844      *  out.writeByte(day);
1845      * </pre>




  63 
  64 import static java.time.LocalTime.SECONDS_PER_DAY;
  65 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;
  66 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;
  67 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;
  68 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
  69 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
  70 import static java.time.temporal.ChronoField.DAY_OF_YEAR;
  71 import static java.time.temporal.ChronoField.EPOCH_DAY;
  72 import static java.time.temporal.ChronoField.EPOCH_MONTH;
  73 import static java.time.temporal.ChronoField.ERA;
  74 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  75 import static java.time.temporal.ChronoField.YEAR;
  76 
  77 import java.io.DataInput;
  78 import java.io.DataOutput;
  79 import java.io.IOException;
  80 import java.io.InvalidObjectException;
  81 import java.io.ObjectStreamException;
  82 import java.io.Serializable;
  83 import java.time.chrono.ChronoLocalDate;
  84 import java.time.chrono.Era;
  85 import java.time.chrono.IsoChronology;
  86 import java.time.format.DateTimeFormatter;

  87 import java.time.format.DateTimeParseException;
  88 import java.time.temporal.ChronoField;

  89 import java.time.temporal.ChronoUnit;
  90 import java.time.temporal.Queries;


  91 import java.time.temporal.Temporal;
  92 import java.time.temporal.TemporalAccessor;

  93 import java.time.temporal.TemporalAdjuster;
  94 import java.time.temporal.TemporalAmount;
  95 import java.time.temporal.TemporalField;
  96 import java.time.temporal.TemporalQuery;

  97 import java.time.temporal.TemporalUnit;
  98 import java.time.temporal.ValueRange;

  99 import java.time.zone.ZoneOffsetTransition;
 100 import java.time.zone.ZoneRules;
 101 import java.util.Objects;
 102 
 103 /**
 104  * A date without a time-zone in the ISO-8601 calendar system,
 105  * such as {@code 2007-12-03}.
 106  * <p>
 107  * {@code LocalDate} is an immutable date-time object that represents a date,
 108  * often viewed as year-month-day. Other date fields, such as day-of-year,
 109  * day-of-week and week-of-year, can also be accessed.
 110  * For example, the value "2nd October 2007" can be stored in a {@code LocalDate}.
 111  * <p>
 112  * This class does not store or represent a time or time-zone.
 113  * Instead, it is a description of the date, as used for birthdays.
 114  * It cannot represent an instant on the time-line without additional information
 115  * such as an offset or time-zone.
 116  * <p>
 117  * The ISO-8601 calendar system is the modern civil calendar system used today
 118  * in most of the world. It is equivalent to the proleptic Gregorian calendar
 119  * system, in which today's rules for leap years are applied for all time.
 120  * For most applications written today, the ISO-8601 rules are entirely suitable.
 121  * However, any application that makes use of historical dates, and requires them
 122  * to be accurate will find the ISO-8601 approach unsuitable.
 123  *
 124  * <h3>Specification for implementors</h3>
 125  * This class is immutable and thread-safe.
 126  *
 127  * @since 1.8
 128  */
 129 public final class LocalDate
 130         implements Temporal, TemporalAdjuster, ChronoLocalDate<LocalDate>, Serializable {
 131 
 132     /**
 133      * The minimum supported {@code LocalDate}, '-999999999-01-01'.
 134      * This could be used by an application as a "far past" date.
 135      */
 136     public static final LocalDate MIN = LocalDate.of(Year.MIN_VALUE, 1, 1);
 137     /**
 138      * The maximum supported {@code LocalDate}, '+999999999-12-31'.
 139      * This could be used by an application as a "far future" date.
 140      */
 141     public static final LocalDate MAX = LocalDate.of(Year.MAX_VALUE, 12, 31);
 142 
 143     /**
 144      * Serialization version.
 145      */
 146     private static final long serialVersionUID = 2942565459149668126L;
 147     /**
 148      * The number of days in a 400 year cycle.
 149      */
 150     private static final int DAYS_PER_CYCLE = 146097;


 195      *
 196      * @param zone  the zone ID to use, not null
 197      * @return the current date using the system clock, not null
 198      */
 199     public static LocalDate now(ZoneId zone) {
 200         return now(Clock.system(zone));
 201     }
 202 
 203     /**
 204      * Obtains the current date from the specified clock.
 205      * <p>
 206      * This will query the specified clock to obtain the current date - today.
 207      * Using this method allows the use of an alternate clock for testing.
 208      * The alternate clock may be introduced using {@link Clock dependency injection}.
 209      *
 210      * @param clock  the clock to use, not null
 211      * @return the current date, not null
 212      */
 213     public static LocalDate now(Clock clock) {
 214         Objects.requireNonNull(clock, "clock");
 215         // inline to avoid creating object and Instant checks
 216         final Instant now = clock.instant();  // called once
 217         ZoneOffset offset = clock.getZone().getRules().getOffset(now);
 218         long epochSec = now.getEpochSecond() + offset.getTotalSeconds();  // overflow caught later
 219         long epochDay = Math.floorDiv(epochSec, SECONDS_PER_DAY);
 220         return LocalDate.ofEpochDay(epochDay);
 221     }
 222 
 223     //-----------------------------------------------------------------------
 224     /**
 225      * Obtains an instance of {@code LocalDate} from a year, month and day.
 226      * <p>
 227      * This returns a {@code LocalDate} with the specified year, month and day-of-month.
 228      * The day must be valid for the year and month, otherwise an exception will be thrown.
 229      *
 230      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 231      * @param month  the month-of-year to represent, not null
 232      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 233      * @return the local date, not null
 234      * @throws DateTimeException if the value of any field is out of range,
 235      *  or if the day-of-month is invalid for the month-year
 236      */
 237     public static LocalDate of(int year, Month month, int dayOfMonth) {
 238         YEAR.checkValidValue(year);
 239         Objects.requireNonNull(month, "month");
 240         DAY_OF_MONTH.checkValidValue(dayOfMonth);
 241         return create(year, month, dayOfMonth);
 242     }
 243 
 244     /**
 245      * Obtains an instance of {@code LocalDate} from a year, month and day.
 246      * <p>
 247      * This returns a {@code LocalDate} with the specified year, month and day-of-month.
 248      * The day must be valid for the year and month, otherwise an exception will be thrown.
 249      *
 250      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 251      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 252      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 253      * @return the local date, 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 LocalDate of(int year, int month, int dayOfMonth) {
 258         YEAR.checkValidValue(year);
 259         MONTH_OF_YEAR.checkValidValue(month);
 260         DAY_OF_MONTH.checkValidValue(dayOfMonth);
 261         return create(year, Month.of(month), dayOfMonth);
 262     }
 263 
 264     //-----------------------------------------------------------------------
 265     /**
 266      * Obtains an instance of {@code LocalDate} from a year and day-of-year.
 267      * <p>
 268      * This returns a {@code LocalDate} with the specified year and day-of-year.
 269      * The day-of-year must be valid for the year, otherwise an exception will be thrown.
 270      *
 271      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 272      * @param dayOfYear  the day-of-year to represent, from 1 to 366
 273      * @return the local date, not null
 274      * @throws DateTimeException if the value of any field is out of range,
 275      *  or if the day-of-year is invalid for the month-year
 276      */
 277     public static LocalDate ofYearDay(int year, int dayOfYear) {
 278         YEAR.checkValidValue(year);
 279         DAY_OF_YEAR.checkValidValue(dayOfYear);
 280         boolean leap = IsoChronology.INSTANCE.isLeapYear(year);
 281         if (dayOfYear == 366 && leap == false) {
 282             throw new DateTimeException("Invalid date 'DayOfYear 366' as '" + year + "' is not a leap year");
 283         }
 284         Month moy = Month.of((dayOfYear - 1) / 31 + 1);
 285         int monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1;
 286         if (dayOfYear > monthEnd) {
 287             moy = moy.plus(1);
 288         }
 289         int dom = dayOfYear - moy.firstDayOfYear(leap) + 1;
 290         return create(year, moy, dom);
 291     }
 292 
 293     //-----------------------------------------------------------------------
 294     /**
 295      * Obtains an instance of {@code LocalDate} from the epoch day count.
 296      * <p>
 297      * This returns a {@code LocalDate} with the specified epoch-day.
 298      * The {@link ChronoField#EPOCH_DAY EPOCH_DAY} is a simple incrementing count
 299      * of days where day 0 is 1970-01-01. Negative numbers represent earlier days.
 300      *
 301      * @param epochDay  the Epoch Day to convert, based on the epoch 1970-01-01
 302      * @return the local date, not null
 303      * @throws DateTimeException if the epoch days exceeds the supported date range
 304      */
 305     public static LocalDate ofEpochDay(long epochDay) {
 306         long zeroDay = epochDay + DAYS_0000_TO_1970;
 307         // find the march-based year
 308         zeroDay -= 60;  // adjust to 0000-03-01 so leap day is at end of four year cycle
 309         long adjust = 0;
 310         if (zeroDay < 0) {
 311             // adjust negative years to positive for calculation
 312             long adjustCycles = (zeroDay + 1) / DAYS_PER_CYCLE - 1;
 313             adjust = adjustCycles * 400;
 314             zeroDay += -adjustCycles * DAYS_PER_CYCLE;
 315         }
 316         long yearEst = (400 * zeroDay + 591) / DAYS_PER_CYCLE;
 317         long doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
 318         if (doyEst < 0) {
 319             // fix estimate


 321             doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
 322         }
 323         yearEst += adjust;  // reset any negative year
 324         int marchDoy0 = (int) doyEst;
 325 
 326         // convert march-based values back to january-based
 327         int marchMonth0 = (marchDoy0 * 5 + 2) / 153;
 328         int month = (marchMonth0 + 2) % 12 + 1;
 329         int dom = marchDoy0 - (marchMonth0 * 306 + 5) / 10 + 1;
 330         yearEst += marchMonth0 / 10;
 331 
 332         // check year now we are certain it is correct
 333         int year = YEAR.checkValidIntValue(yearEst);
 334         return new LocalDate(year, month, dom);
 335     }
 336 
 337     //-----------------------------------------------------------------------
 338     /**
 339      * Obtains an instance of {@code LocalDate} from a temporal object.
 340      * <p>
 341      * This obtains a local date based on the specified temporal.
 342      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 343      * which this factory converts to an instance of {@code LocalDate}.
 344      * <p>
 345      * The conversion uses the {@link Queries#localDate()} query, which relies
 346      * on extracting the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field.
 347      * <p>
 348      * This method matches the signature of the functional interface {@link TemporalQuery}
 349      * allowing it to be used as a query via method reference, {@code LocalDate::from}.
 350      *
 351      * @param temporal  the temporal object to convert, not null
 352      * @return the local date, not null
 353      * @throws DateTimeException if unable to convert to a {@code LocalDate}
 354      */
 355     public static LocalDate from(TemporalAccessor temporal) {
 356         LocalDate date = temporal.query(Queries.localDate());
 357         if (date == null) {
 358             throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " + temporal.getClass());
















 359         }
 360         return date;
 361     }
 362 
 363     //-----------------------------------------------------------------------
 364     /**
 365      * Obtains an instance of {@code LocalDate} from a text string such as {@code 2007-12-03}.
 366      * <p>
 367      * The string must represent a valid date and is parsed using
 368      * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE}.
 369      *
 370      * @param text  the text to parse such as "2007-12-03", not null
 371      * @return the parsed local date, not null
 372      * @throws DateTimeParseException if the text cannot be parsed
 373      */
 374     public static LocalDate parse(CharSequence text) {
 375         return parse(text, DateTimeFormatter.ISO_LOCAL_DATE);
 376     }
 377 
 378     /**
 379      * Obtains an instance of {@code LocalDate} from a text string using a specific formatter.
 380      * <p>
 381      * The text is parsed using the formatter, returning a date.
 382      *
 383      * @param text  the text to parse, not null
 384      * @param formatter  the formatter to use, not null
 385      * @return the parsed local date, not null
 386      * @throws DateTimeParseException if the text cannot be parsed
 387      */
 388     public static LocalDate parse(CharSequence text, DateTimeFormatter formatter) {
 389         Objects.requireNonNull(formatter, "formatter");
 390         return formatter.parse(text, LocalDate::from);
 391     }
 392 
 393     //-----------------------------------------------------------------------
 394     /**
 395      * Creates a local date from the year, month and day fields.
 396      *
 397      * @param year  the year to represent, validated from MIN_YEAR to MAX_YEAR
 398      * @param month  the month-of-year to represent, validated not null
 399      * @param dayOfMonth  the day-of-month to represent, validated from 1 to 31
 400      * @return the local date, not null
 401      * @throws DateTimeException if the day-of-month is invalid for the month-year
 402      */
 403     private static LocalDate create(int year, Month month, int dayOfMonth) {
 404         if (dayOfMonth > 28 && dayOfMonth > month.length(IsoChronology.INSTANCE.isLeapYear(year))) {
 405             if (dayOfMonth == 29) {
 406                 throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year");
 407             } else {
 408                 throw new DateTimeException("Invalid date '" + month.name() + " " + dayOfMonth + "'");
 409             }
 410         }
 411         return new LocalDate(year, month.getValue(), dayOfMonth);
 412     }
 413 
 414     /**
 415      * Resolves the date, resolving days past the end of month.
 416      *
 417      * @param year  the year to represent, validated from MIN_YEAR to MAX_YEAR
 418      * @param month  the month-of-year to represent, validated from 1 to 12
 419      * @param day  the day-of-month to represent, validated from 1 to 31
 420      * @return the resolved date, not null
 421      */
 422     private static LocalDate resolvePreviousValid(int year, int month, int day) {
 423         switch (month) {
 424             case 2:
 425                 day = Math.min(day, IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
 426                 break;
 427             case 4:
 428             case 6:
 429             case 9:
 430             case 11:
 431                 day = Math.min(day, 30);
 432                 break;
 433         }
 434         return LocalDate.of(year, month, day);
 435     }
 436 
 437     /**
 438      * Constructor, previously validated.
 439      *
 440      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 441      * @param month  the month-of-year to represent, not null
 442      * @param dayOfMonth  the day-of-month to represent, valid for year-month, from 1 to 31
 443      */
 444     private LocalDate(int year, int month, int dayOfMonth) {
 445         this.year = year;
 446         this.month = (short) month;
 447         this.day = (short) dayOfMonth;
 448     }
 449 
 450     //-----------------------------------------------------------------------
 451     /**
 452      * Checks if the specified field is supported.
 453      * <p>
 454      * This checks if this date can be queried for the specified field.
 455      * If false, then calling the {@link #range(TemporalField) range} and
 456      * {@link #get(TemporalField) get} methods will throw an exception.
 457      * <p>
 458      * If the field is a {@link ChronoField} then the query is implemented here.


 459      * The supported fields are:
 460      * <ul>
 461      * <li>{@code DAY_OF_WEEK}
 462      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
 463      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
 464      * <li>{@code DAY_OF_MONTH}
 465      * <li>{@code DAY_OF_YEAR}
 466      * <li>{@code EPOCH_DAY}
 467      * <li>{@code ALIGNED_WEEK_OF_MONTH}
 468      * <li>{@code ALIGNED_WEEK_OF_YEAR}
 469      * <li>{@code MONTH_OF_YEAR}
 470      * <li>{@code EPOCH_MONTH}
 471      * <li>{@code YEAR_OF_ERA}
 472      * <li>{@code YEAR}
 473      * <li>{@code ERA}
 474      * </ul>
 475      * All other {@code ChronoField} instances will return false.
 476      * <p>
 477      * If the field is not a {@code ChronoField}, then the result of this method
 478      * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
 479      * passing {@code this} as the argument.
 480      * Whether the field is supported is determined by the field.
 481      *
 482      * @param field  the field to check, null returns false
 483      * @return true if the field is supported on this date, false if not
 484      */
 485     @Override  // override for Javadoc
 486     public boolean isSupported(TemporalField field) {
 487         return ChronoLocalDate.super.isSupported(field);
 488     }
 489 
 490     /**
 491      * Gets the range of valid values for the specified field.
 492      * <p>
 493      * The range object expresses the minimum and maximum valid values for a field.
 494      * This date is used to enhance the accuracy of the returned range.
 495      * If it is not possible to return the range, because the field is not supported
 496      * or for some other reason, an exception is thrown.
 497      * <p>
 498      * If the field is a {@link ChronoField} then the query is implemented here.
 499      * The {@link #isSupported(TemporalField) supported fields} will return
 500      * appropriate range instances.
 501      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 502      * <p>
 503      * If the field is not a {@code ChronoField}, then the result of this method
 504      * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
 505      * passing {@code this} as the argument.
 506      * Whether the range can be obtained is determined by the field.
 507      *
 508      * @param field  the field to query the range for, not null
 509      * @return the range of valid values for the field, not null
 510      * @throws DateTimeException if the range for the field cannot be obtained
 511      */
 512     @Override
 513     public ValueRange range(TemporalField field) {
 514         if (field instanceof ChronoField) {
 515             ChronoField f = (ChronoField) field;
 516             if (f.isDateField()) {
 517                 switch (f) {
 518                     case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
 519                     case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
 520                     case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, getMonth() == Month.FEBRUARY && isLeapYear() == false ? 4 : 5);
 521                     case YEAR_OF_ERA:
 522                         return (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE));
 523                 }
 524                 return field.range();
 525             }
 526             throw new DateTimeException("Unsupported field: " + field.getName());
 527         }
 528         return field.rangeRefinedBy(this);
 529     }
 530 
 531     /**
 532      * Gets the value of the specified field from this date as an {@code int}.
 533      * <p>
 534      * This queries this date for the value for the specified field.
 535      * The returned value will always be within the valid range of values for the field.
 536      * If it is not possible to return the value, because the field is not supported
 537      * or for some other reason, an exception is thrown.
 538      * <p>
 539      * If the field is a {@link ChronoField} then the query is implemented here.
 540      * The {@link #isSupported(TemporalField) supported fields} will return valid
 541      * values based on this date, except {@code EPOCH_DAY} and {@code EPOCH_MONTH}
 542      * which are too large to fit in an {@code int} and throw a {@code DateTimeException}.
 543      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 544      * <p>
 545      * If the field is not a {@code ChronoField}, then the result of this method
 546      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 547      * passing {@code this} as the argument. Whether the value can be obtained,
 548      * and what the value represents, is determined by the field.
 549      *
 550      * @param field  the field to get, not null
 551      * @return the value for the field
 552      * @throws DateTimeException if a value for the field cannot be obtained
 553      * @throws ArithmeticException if numeric overflow occurs
 554      */
 555     @Override  // override for Javadoc and performance
 556     public int get(TemporalField field) {
 557         if (field instanceof ChronoField) {
 558             return get0(field);
 559         }
 560         return ChronoLocalDate.super.get(field);
 561     }
 562 
 563     /**
 564      * Gets the value of the specified field from this date as a {@code long}.
 565      * <p>
 566      * This queries this date for the value for the specified field.
 567      * If it is not possible to return the value, because the field is not supported
 568      * or for some other reason, an exception is thrown.
 569      * <p>
 570      * If the field is a {@link ChronoField} then the query is implemented here.
 571      * The {@link #isSupported(TemporalField) supported fields} will return valid
 572      * values based on this date.
 573      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 574      * <p>
 575      * If the field is not a {@code ChronoField}, then the result of this method
 576      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 577      * passing {@code this} as the argument. Whether the value can be obtained,
 578      * and what the value represents, is determined by the field.
 579      *
 580      * @param field  the field to get, not null
 581      * @return the value for the field
 582      * @throws DateTimeException if a value for the field cannot be obtained
 583      * @throws ArithmeticException if numeric overflow occurs
 584      */
 585     @Override
 586     public long getLong(TemporalField field) {
 587         if (field instanceof ChronoField) {
 588             if (field == EPOCH_DAY) {
 589                 return toEpochDay();
 590             }
 591             if (field == EPOCH_MONTH) {
 592                 return getEpochMonth();
 593             }
 594             return get0(field);
 595         }
 596         return field.getFrom(this);
 597     }
 598 
 599     private int get0(TemporalField field) {
 600         switch ((ChronoField) field) {
 601             case DAY_OF_WEEK: return getDayOfWeek().getValue();
 602             case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1;
 603             case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1;
 604             case DAY_OF_MONTH: return day;
 605             case DAY_OF_YEAR: return getDayOfYear();
 606             case EPOCH_DAY: throw new DateTimeException("Field too large for an int: " + field);
 607             case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1;
 608             case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1;
 609             case MONTH_OF_YEAR: return month;
 610             case EPOCH_MONTH: throw new DateTimeException("Field too large for an int: " + field);
 611             case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year);
 612             case YEAR: return year;
 613             case ERA: return (year >= 1 ? 1 : 0);
 614         }
 615         throw new DateTimeException("Unsupported field: " + field.getName());
 616     }
 617 
 618     private long getEpochMonth() {
 619         return ((year - 1970) * 12L) + (month - 1);
 620     }
 621 
 622     //-----------------------------------------------------------------------
 623     /**
 624      * Gets the chronology of this date, which is the ISO calendar system.
 625      * <p>
 626      * The {@code Chronology} represents the calendar system in use.
 627      * The ISO-8601 calendar system is the modern civil calendar system used today
 628      * in most of the world. It is equivalent to the proleptic Gregorian calendar
 629      * system, in which todays's rules for leap years are applied for all time.
 630      *
 631      * @return the ISO chronology, not null
 632      */
 633     @Override
 634     public IsoChronology getChronology() {
 635         return IsoChronology.INSTANCE;
 636     }
 637 
 638     /**
 639      * Gets the era applicable at this date.
 640      * <p>
 641      * The official ISO-8601 standard does not define eras, however {@code IsoChronology} does.
 642      * It defines two eras, 'CE' from year one onwards and 'BCE' from year zero backwards.
 643      * Since dates before the Julian-Gregorian cutover are not in line with history,
 644      * the cutover between 'BCE' and 'CE' is also not aligned with the commonly used
 645      * eras, often referred to using 'BC' and 'AD'.
 646      * <p>
 647      * Users of this class should typically ignore this method as it exists primarily
 648      * to fulfill the {@link ChronoLocalDate} contract where it is necessary to support
 649      * the Japanese calendar system.
 650      * <p>
 651      * The returned era will be a singleton capable of being compared with the constants
 652      * in {@link IsoChronology} using the {@code ==} operator.
 653      *
 654      * @return the {@code IsoChronology} era constant applicable at this date, not null
 655      */
 656     @Override // override for Javadoc
 657     public Era getEra() {
 658         return ChronoLocalDate.super.getEra();
 659     }
 660 
 661     /**
 662      * Gets the year field.
 663      * <p>
 664      * This method returns the primitive {@code int} value for the year.
 665      * <p>
 666      * The year returned by this method is proleptic as per {@code get(YEAR)}.
 667      * To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}.
 668      *
 669      * @return the year, from MIN_YEAR to MAX_YEAR
 670      */
 671     public int getYear() {
 672         return year;
 673     }
 674 
 675     /**
 676      * Gets the month-of-year field from 1 to 12.
 677      * <p>
 678      * This method returns the month as an {@code int} from 1 to 12.
 679      * Application code is frequently clearer if the enum {@link Month}
 680      * is used by calling {@link #getMonth()}.
 681      *
 682      * @return the month-of-year, from 1 to 12
 683      * @see #getMonth()
 684      */
 685     public int getMonthValue() {
 686         return month;
 687     }


 745     /**
 746      * Checks if the year is a leap year, according to the ISO proleptic
 747      * calendar system rules.
 748      * <p>
 749      * This method applies the current rules for leap years across the whole time-line.
 750      * In general, a year is a leap year if it is divisible by four without
 751      * remainder. However, years divisible by 100, are not leap years, with
 752      * the exception of years divisible by 400 which are.
 753      * <p>
 754      * For example, 1904 is a leap year it is divisible by 4.
 755      * 1900 was not a leap year as it is divisible by 100, however 2000 was a
 756      * leap year as it is divisible by 400.
 757      * <p>
 758      * The calculation is proleptic - applying the same rules into the far future and far past.
 759      * This is historically inaccurate, but is correct for the ISO-8601 standard.
 760      *
 761      * @return true if the year is leap, false otherwise
 762      */
 763     @Override // override for Javadoc and performance
 764     public boolean isLeapYear() {
 765         return IsoChronology.INSTANCE.isLeapYear(year);
 766     }
 767 
 768     /**
 769      * Returns the length of the month represented by this date.
 770      * <p>
 771      * This returns the length of the month in days.
 772      * For example, a date in January would return 31.
 773      *
 774      * @return the length of the month in days
 775      */
 776     @Override
 777     public int lengthOfMonth() {
 778         switch (month) {
 779             case 2:
 780                 return (isLeapYear() ? 29 : 28);
 781             case 4:
 782             case 6:
 783             case 9:
 784             case 11:
 785                 return 30;


 787                 return 31;
 788         }
 789     }
 790 
 791     /**
 792      * Returns the length of the year represented by this date.
 793      * <p>
 794      * This returns the length of the year in days, either 365 or 366.
 795      *
 796      * @return 366 if the year is leap, 365 otherwise
 797      */
 798     @Override // override for Javadoc and performance
 799     public int lengthOfYear() {
 800         return (isLeapYear() ? 366 : 365);
 801     }
 802 
 803     //-----------------------------------------------------------------------
 804     /**
 805      * Returns an adjusted copy of this date.
 806      * <p>
 807      * This returns a {@code LocalDate}, based on this one, with the date adjusted.
 808      * The adjustment takes place using the specified adjuster strategy object.
 809      * Read the documentation of the adjuster to understand what adjustment will be made.
 810      * <p>
 811      * A simple adjuster might simply set the one of the fields, such as the year field.
 812      * A more complex adjuster might set the date to the last day of the month.
 813      * A selection of common adjustments is provided in {@link java.time.temporal.Adjusters}.
 814      * These include finding the "last day of the month" and "next Wednesday".
 815      * Key date-time classes also implement the {@code TemporalAdjuster} interface,
 816      * such as {@link Month} and {@link java.time.MonthDay MonthDay}.
 817      * The adjuster is responsible for handling special cases, such as the varying
 818      * lengths of month and leap years.
 819      * <p>
 820      * For example this code returns a date on the last day of July:
 821      * <pre>
 822      *  import static java.time.Month.*;
 823      *  import static java.time.temporal.Adjusters.*;
 824      *
 825      *  result = localDate.with(JULY).with(lastDayOfMonth());
 826      * </pre>
 827      * <p>
 828      * The result of this method is obtained by invoking the
 829      * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
 830      * specified adjuster passing {@code this} as the argument.
 831      * <p>
 832      * This instance is immutable and unaffected by this method call.
 833      *
 834      * @param adjuster the adjuster to use, not null
 835      * @return a {@code LocalDate} based on {@code this} with the adjustment made, not null
 836      * @throws DateTimeException if the adjustment cannot be made
 837      * @throws ArithmeticException if numeric overflow occurs
 838      */
 839     @Override
 840     public LocalDate with(TemporalAdjuster adjuster) {
 841         // optimizations
 842         if (adjuster instanceof LocalDate) {
 843             return (LocalDate) adjuster;
 844         }
 845         return (LocalDate) adjuster.adjustInto(this);
 846     }
 847 
 848     /**
 849      * Returns a copy of this date with the specified field set to a new value.
 850      * <p>
 851      * This returns a {@code LocalDate}, based on this one, with the value
 852      * for the specified field changed.
 853      * This can be used to change any supported field, such as the year, month or day-of-month.
 854      * If it is not possible to set the value, because the field is not supported or for
 855      * some other reason, an exception is thrown.
 856      * <p>
 857      * In some cases, changing the specified field can cause the resulting date to become invalid,
 858      * such as changing the month from 31st January to February would make the day-of-month invalid.
 859      * In cases like this, the field is responsible for resolving the date. Typically it will choose
 860      * the previous valid date, which would be the last valid day of February in this example.
 861      * <p>
 862      * If the field is a {@link ChronoField} then the adjustment is implemented here.
 863      * The supported fields behave as follows:
 864      * <ul>
 865      * <li>{@code DAY_OF_WEEK} -
 866      *  Returns a {@code LocalDate} with the specified day-of-week.
 867      *  The date is adjusted up to 6 days forward or backward within the boundary
 868      *  of a Monday to Sunday week.
 869      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} -
 870      *  Returns a {@code LocalDate} with the specified aligned-day-of-week.
 871      *  The date is adjusted to the specified month-based aligned-day-of-week.


 919      *  unless it would be invalid for the new month and year. In that case, the
 920      *  day-of-month is adjusted to the maximum valid value for the new month and year.
 921      * <li>{@code YEAR} -
 922      *  Returns a {@code LocalDate} with the specified year.
 923      *  The month will be unchanged. The day-of-month will also be unchanged,
 924      *  unless it would be invalid for the new month and year. In that case, the
 925      *  day-of-month is adjusted to the maximum valid value for the new month and year.
 926      * <li>{@code ERA} -
 927      *  Returns a {@code LocalDate} with the specified era.
 928      *  The year-of-era and month will be unchanged. The day-of-month will also be unchanged,
 929      *  unless it would be invalid for the new month and year. In that case, the
 930      *  day-of-month is adjusted to the maximum valid value for the new month and year.
 931      * </ul>
 932      * <p>
 933      * In all cases, if the new value is outside the valid range of values for the field
 934      * then a {@code DateTimeException} will be thrown.
 935      * <p>
 936      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 937      * <p>
 938      * If the field is not a {@code ChronoField}, then the result of this method
 939      * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
 940      * passing {@code this} as the argument. In this case, the field determines
 941      * whether and how to adjust the instant.
 942      * <p>
 943      * This instance is immutable and unaffected by this method call.
 944      *
 945      * @param field  the field to set in the result, not null
 946      * @param newValue  the new value of the field in the result
 947      * @return a {@code LocalDate} based on {@code this} with the specified field set, not null
 948      * @throws DateTimeException if the field cannot be set
 949      * @throws ArithmeticException if numeric overflow occurs
 950      */
 951     @Override
 952     public LocalDate with(TemporalField field, long newValue) {
 953         if (field instanceof ChronoField) {
 954             ChronoField f = (ChronoField) field;
 955             f.checkValidValue(newValue);
 956             switch (f) {
 957                 case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek().getValue());
 958                 case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH));
 959                 case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR));
 960                 case DAY_OF_MONTH: return withDayOfMonth((int) newValue);
 961                 case DAY_OF_YEAR: return withDayOfYear((int) newValue);
 962                 case EPOCH_DAY: return LocalDate.ofEpochDay(newValue);
 963                 case ALIGNED_WEEK_OF_MONTH: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_MONTH));
 964                 case ALIGNED_WEEK_OF_YEAR: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_YEAR));
 965                 case MONTH_OF_YEAR: return withMonth((int) newValue);
 966                 case EPOCH_MONTH: return plusMonths(newValue - getLong(EPOCH_MONTH));
 967                 case YEAR_OF_ERA: return withYear((int) (year >= 1 ? newValue : 1 - newValue));
 968                 case YEAR: return withYear((int) newValue);
 969                 case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year));
 970             }
 971             throw new DateTimeException("Unsupported field: " + field.getName());
 972         }
 973         return field.adjustInto(this, newValue);
 974     }
 975 
 976     //-----------------------------------------------------------------------
 977     /**
 978      * Returns a copy of this date with the year altered.
 979      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
 980      * <p>
 981      * This instance is immutable and unaffected by this method call.
 982      *
 983      * @param year  the year to set in the result, from MIN_YEAR to MAX_YEAR
 984      * @return a {@code LocalDate} based on this date with the requested year, not null
 985      * @throws DateTimeException if the year value is invalid
 986      */
 987     public LocalDate withYear(int year) {
 988         if (this.year == year) {
 989             return this;
 990         }
 991         YEAR.checkValidValue(year);
 992         return resolvePreviousValid(year, month, day);
 993     }


1001      * @param month  the month-of-year to set in the result, from 1 (January) to 12 (December)
1002      * @return a {@code LocalDate} based on this date with the requested month, not null
1003      * @throws DateTimeException if the month-of-year value is invalid
1004      */
1005     public LocalDate withMonth(int month) {
1006         if (this.month == month) {
1007             return this;
1008         }
1009         MONTH_OF_YEAR.checkValidValue(month);
1010         return resolvePreviousValid(year, month, day);
1011     }
1012 
1013     /**
1014      * Returns a copy of this date with the day-of-month altered.
1015      * If the resulting date is invalid, an exception is thrown.
1016      * <p>
1017      * This instance is immutable and unaffected by this method call.
1018      *
1019      * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
1020      * @return a {@code LocalDate} based on this date with the requested day, not null
1021      * @throws DateTimeException if the day-of-month value is invalid,
1022      *  or if the day-of-month is invalid for the month-year
1023      */
1024     public LocalDate withDayOfMonth(int dayOfMonth) {
1025         if (this.day == dayOfMonth) {
1026             return this;
1027         }
1028         return of(year, month, dayOfMonth);
1029     }
1030 
1031     /**
1032      * Returns a copy of this date with the day-of-year altered.
1033      * If the resulting date is invalid, an exception is thrown.
1034      * <p>
1035      * This instance is immutable and unaffected by this method call.
1036      *
1037      * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
1038      * @return a {@code LocalDate} based on this date with the requested day, not null
1039      * @throws DateTimeException if the day-of-year value is invalid,
1040      *  or if the day-of-year is invalid for the year
1041      */
1042     public LocalDate withDayOfYear(int dayOfYear) {
1043         if (this.getDayOfYear() == dayOfYear) {
1044             return this;
1045         }
1046         return ofYearDay(year, dayOfYear);
1047     }
1048 
1049     //-----------------------------------------------------------------------
1050     /**
1051      * Returns a copy of this date with the specified amount added.
1052      * <p>
1053      * This returns a {@code LocalDate}, based on this one, with the specified amount added.
1054      * The amount is typically {@link Period} but may be any other type implementing
1055      * the {@link TemporalAmount} interface.
1056      * <p>
1057      * The calculation is delegated to the amount object by calling
1058      * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
1059      * to implement the addition in any way it wishes, however it typically
1060      * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
1061      * of the amount implementation to determine if it can be successfully added.
1062      * <p>
1063      * This instance is immutable and unaffected by this method call.
1064      *
1065      * @param amountToAdd  the amount to add, not null
1066      * @return a {@code LocalDate} based on this date with the addition made, not null
1067      * @throws DateTimeException if the addition cannot be made
1068      * @throws ArithmeticException if numeric overflow occurs
1069      */
1070     @Override
1071     public LocalDate plus(TemporalAmount amountToAdd) {
1072         return (LocalDate) amountToAdd.addTo(this);
1073     }
1074 
1075     /**
1076      * Returns a copy of this date with the specified amount added.
1077      * <p>
1078      * This returns a {@code LocalDate}, based on this one, with the amount
1079      * in terms of the unit added. If it is not possible to add the amount, because the
1080      * unit is not supported or for some other reason, an exception is thrown.
1081      * <p>
1082      * In some cases, adding the amount can cause the resulting date to become invalid.
1083      * For example, adding one month to 31st January would result in 31st February.
1084      * In cases like this, the unit is responsible for resolving the date.
1085      * Typically it will choose the previous valid date, which would be the last valid
1086      * day of February in this example.
1087      * <p>
1088      * If the field is a {@link ChronoUnit} then the addition is implemented here.
1089      * The supported fields behave as follows:
1090      * <ul>
1091      * <li>{@code DAYS} -
1092      *  Returns a {@code LocalDate} with the specified number of days added.
1093      *  This is equivalent to {@link #plusDays(long)}.
1094      * <li>{@code WEEKS} -
1095      *  Returns a {@code LocalDate} with the specified number of weeks added.
1096      *  This is equivalent to {@link #plusWeeks(long)} and uses a 7 day week.
1097      * <li>{@code MONTHS} -
1098      *  Returns a {@code LocalDate} with the specified number of months added.
1099      *  This is equivalent to {@link #plusMonths(long)}.
1100      *  The day-of-month will be unchanged unless it would be invalid for the new
1101      *  month and year. In that case, the day-of-month is adjusted to the maximum
1102      *  valid value for the new month and year.
1103      * <li>{@code YEARS} -
1104      *  Returns a {@code LocalDate} with the specified number of years added.
1105      *  This is equivalent to {@link #plusYears(long)}.
1106      *  The day-of-month will be unchanged unless it would be invalid for the new
1107      *  month and year. In that case, the day-of-month is adjusted to the maximum
1108      *  valid value for the new month and year.
1109      * <li>{@code DECADES} -
1110      *  Returns a {@code LocalDate} with the specified number of decades added.
1111      *  This is equivalent to calling {@link #plusYears(long)} with the amount
1112      *  multiplied by 10.
1113      *  The day-of-month will be unchanged unless it would be invalid for the new
1114      *  month and year. In that case, the day-of-month is adjusted to the maximum
1115      *  valid value for the new month and year.
1116      * <li>{@code CENTURIES} -
1117      *  Returns a {@code LocalDate} with the specified number of centuries added.
1118      *  This is equivalent to calling {@link #plusYears(long)} with the amount
1119      *  multiplied by 100.
1120      *  The day-of-month will be unchanged unless it would be invalid for the new
1121      *  month and year. In that case, the day-of-month is adjusted to the maximum
1122      *  valid value for the new month and year.
1123      * <li>{@code MILLENNIA} -
1124      *  Returns a {@code LocalDate} with the specified number of millennia added.
1125      *  This is equivalent to calling {@link #plusYears(long)} with the amount
1126      *  multiplied by 1,000.
1127      *  The day-of-month will be unchanged unless it would be invalid for the new
1128      *  month and year. In that case, the day-of-month is adjusted to the maximum
1129      *  valid value for the new month and year.
1130      * <li>{@code ERAS} -
1131      *  Returns a {@code LocalDate} with the specified number of eras added.
1132      *  Only two eras are supported so the amount must be one, zero or minus one.
1133      *  If the amount is non-zero then the year is changed such that the year-of-era
1134      *  is unchanged.
1135      *  The day-of-month will be unchanged unless it would be invalid for the new
1136      *  month and year. In that case, the day-of-month is adjusted to the maximum
1137      *  valid value for the new month and year.
1138      * </ul>
1139      * <p>
1140      * All other {@code ChronoUnit} instances will throw a {@code DateTimeException}.
1141      * <p>
1142      * If the field is not a {@code ChronoUnit}, then the result of this method
1143      * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
1144      * passing {@code this} as the argument. In this case, the unit determines
1145      * whether and how to perform the addition.
1146      * <p>
1147      * This instance is immutable and unaffected by this method call.
1148      *
1149      * @param amountToAdd  the amount of the unit to add to the result, may be negative
1150      * @param unit  the unit of the amount to add, not null
1151      * @return a {@code LocalDate} based on this date with the specified amount added, not null
1152      * @throws DateTimeException if the addition cannot be made
1153      * @throws ArithmeticException if numeric overflow occurs
1154      */
1155     @Override
1156     public LocalDate plus(long amountToAdd, TemporalUnit unit) {
1157         if (unit instanceof ChronoUnit) {
1158             ChronoUnit f = (ChronoUnit) unit;
1159             switch (f) {
1160                 case DAYS: return plusDays(amountToAdd);
1161                 case WEEKS: return plusWeeks(amountToAdd);
1162                 case MONTHS: return plusMonths(amountToAdd);
1163                 case YEARS: return plusYears(amountToAdd);
1164                 case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
1165                 case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
1166                 case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
1167                 case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
1168             }
1169             throw new DateTimeException("Unsupported unit: " + unit.getName());
1170         }
1171         return unit.addTo(this, amountToAdd);
1172     }
1173 
1174     //-----------------------------------------------------------------------
1175     /**
1176      * Returns a copy of this {@code LocalDate} with the specified period in years added.
1177      * <p>
1178      * This method adds the specified amount to the years field in three steps:
1179      * <ol>
1180      * <li>Add the input years to the year field</li>
1181      * <li>Check if the resulting date would be invalid</li>
1182      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1183      * </ol>
1184      * <p>
1185      * For example, 2008-02-29 (leap year) plus one year would result in the
1186      * invalid date 2009-02-29 (standard year). Instead of returning an invalid
1187      * result, the last valid day of the month, 2009-02-28, is selected instead.
1188      * <p>
1189      * This instance is immutable and unaffected by this method call.
1190      *
1191      * @param yearsToAdd  the years to add, may be negative


1258      * The result is only invalid if the maximum/minimum year is exceeded.
1259      * <p>
1260      * For example, 2008-12-31 plus one day would result in 2009-01-01.
1261      * <p>
1262      * This instance is immutable and unaffected by this method call.
1263      *
1264      * @param daysToAdd  the days to add, may be negative
1265      * @return a {@code LocalDate} based on this date with the days added, not null
1266      * @throws DateTimeException if the result exceeds the supported date range
1267      */
1268     public LocalDate plusDays(long daysToAdd) {
1269         if (daysToAdd == 0) {
1270             return this;
1271         }
1272         long mjDay = Math.addExact(toEpochDay(), daysToAdd);
1273         return LocalDate.ofEpochDay(mjDay);
1274     }
1275 
1276     //-----------------------------------------------------------------------
1277     /**
1278      * Returns a copy of this date with the specified amount subtracted.
1279      * <p>
1280      * This returns a {@code LocalDate}, based on this one, with the specified amount subtracted.
1281      * The amount is typically {@link Period} but may be any other type implementing
1282      * the {@link TemporalAmount} interface.
1283      * <p>
1284      * The calculation is delegated to the amount object by calling
1285      * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
1286      * to implement the subtraction in any way it wishes, however it typically
1287      * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
1288      * of the amount implementation to determine if it can be successfully subtracted.
1289      * <p>
1290      * This instance is immutable and unaffected by this method call.
1291      *
1292      * @param amountToSubtract  the amount to subtract, not null
1293      * @return a {@code LocalDate} based on this date with the subtraction made, not null
1294      * @throws DateTimeException if the subtraction cannot be made
1295      * @throws ArithmeticException if numeric overflow occurs
1296      */
1297     @Override
1298     public LocalDate minus(TemporalAmount amountToSubtract) {
1299         return (LocalDate) amountToSubtract.subtractFrom(this);
1300     }
1301 
1302     /**
1303      * Returns a copy of this date with the specified amount subtracted.
1304      * <p>
1305      * This returns a {@code LocalDate}, based on this one, with the amount
1306      * in terms of the unit subtracted. If it is not possible to subtract the amount,
1307      * because the unit is not supported or for some other reason, an exception is thrown.
1308      * <p>
1309      * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
1310      * See that method for a full description of how addition, and thus subtraction, works.


1311      * <p>
1312      * This instance is immutable and unaffected by this method call.
1313      *
1314      * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
1315      * @param unit  the unit of the amount to subtract, not null
1316      * @return a {@code LocalDate} based on this date with the specified amount subtracted, not null
1317      * @throws DateTimeException if the subtraction cannot be made
1318      * @throws ArithmeticException if numeric overflow occurs
1319      */
1320     @Override
1321     public LocalDate minus(long amountToSubtract, TemporalUnit unit) {
1322         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
1323     }
1324 
1325     //-----------------------------------------------------------------------
1326     /**
1327      * Returns a copy of this {@code LocalDate} with the specified period in years subtracted.
1328      * <p>
1329      * This method subtracts the specified amount from the years field in three steps:
1330      * <ol>
1331      * <li>Subtract the input years to the year field</li>
1332      * <li>Check if the resulting date would be invalid</li>
1333      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1334      * </ol>
1335      * <p>
1336      * For example, 2008-02-29 (leap year) minus one year would result in the
1337      * invalid date 2007-02-29 (standard year). Instead of returning an invalid
1338      * result, the last valid day of the month, 2007-02-28, is selected instead.


1411 
1412     //-----------------------------------------------------------------------
1413     /**
1414      * Queries this date using the specified query.
1415      * <p>
1416      * This queries this date using the specified query strategy object.
1417      * The {@code TemporalQuery} object defines the logic to be used to
1418      * obtain the result. Read the documentation of the query to understand
1419      * what the result of this method will be.
1420      * <p>
1421      * The result of this method is obtained by invoking the
1422      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
1423      * specified query passing {@code this} as the argument.
1424      *
1425      * @param <R> the type of the result
1426      * @param query  the query to invoke, not null
1427      * @return the query result, null may be returned (defined by the query)
1428      * @throws DateTimeException if unable to query (defined by the query)
1429      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
1430      */
1431     @SuppressWarnings("unchecked")
1432     @Override
1433     public <R> R query(TemporalQuery<R> query) {
1434         if (query == Queries.localDate()) {
1435             return (R) this;
1436         }
1437         return ChronoLocalDate.super.query(query);
1438     }
1439 
1440     /**
1441      * Adjusts the specified temporal object to have the same date as this object.
1442      * <p>
1443      * This returns a temporal object of the same observable type as the input
1444      * with the date changed to be the same as this.
1445      * <p>
1446      * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
1447      * passing {@link ChronoField#EPOCH_DAY} as the field.
1448      * <p>
1449      * In most cases, it is clearer to reverse the calling pattern by using
1450      * {@link Temporal#with(TemporalAdjuster)}:
1451      * <pre>
1452      *   // these two lines are equivalent, but the second approach is recommended
1453      *   temporal = thisLocalDate.adjustInto(temporal);
1454      *   temporal = temporal.with(thisLocalDate);
1455      * </pre>
1456      * <p>


1465     public Temporal adjustInto(Temporal temporal) {
1466         return ChronoLocalDate.super.adjustInto(temporal);
1467     }
1468 
1469     /**
1470      * Calculates the period between this date and another date in
1471      * terms of the specified unit.
1472      * <p>
1473      * This calculates the period between two dates in terms of a single unit.
1474      * The start and end points are {@code this} and the specified date.
1475      * The result will be negative if the end is before the start.
1476      * The {@code Temporal} passed to this method must be a {@code LocalDate}.
1477      * For example, the period in days between two dates can be calculated
1478      * using {@code startDate.periodUntil(endDate, DAYS)}.
1479      * <p>
1480      * The calculation returns a whole number, representing the number of
1481      * complete units between the two dates.
1482      * For example, the period in months between 2012-06-15 and 2012-08-14
1483      * will only be one month as it is one day short of two months.
1484      * <p>
1485      * There are two equivalent ways of using this method.
1486      * The first is to invoke this method.
1487      * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:

1488      * <pre>
1489      *   // these two lines are equivalent
1490      *   amount = start.periodUntil(end, MONTHS);
1491      *   amount = MONTHS.between(start, end);
1492      * </pre>
1493      * The choice should be made based on which makes the code more readable.
1494      * <p>
1495      * The calculation is implemented in this method for {@link ChronoUnit}.
1496      * The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS},
1497      * {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS}
1498      * are supported. Other {@code ChronoUnit} values will throw an exception.
1499      * <p>
1500      * If the unit is not a {@code ChronoUnit}, then the result of this method
1501      * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
1502      * passing {@code this} as the first argument and the input temporal as
1503      * the second argument.
1504      * <p>
1505      * This instance is immutable and unaffected by this method call.
1506      *
1507      * @param endDate  the end date, which must be a {@code LocalDate}, not null
1508      * @param unit  the unit to measure the period in, not null
1509      * @return the amount of the period between this date and the end date
1510      * @throws DateTimeException if the period cannot be calculated
1511      * @throws ArithmeticException if numeric overflow occurs
1512      */
1513     @Override
1514     public long periodUntil(Temporal endDate, TemporalUnit unit) {
1515         if (endDate instanceof LocalDate == false) {
1516             Objects.requireNonNull(endDate, "endDate");
1517             throw new DateTimeException("Unable to calculate period between objects of two different types");
1518         }
1519         LocalDate end = (LocalDate) endDate;
1520         if (unit instanceof ChronoUnit) {
1521             switch ((ChronoUnit) unit) {
1522                 case DAYS: return daysUntil(end);
1523                 case WEEKS: return daysUntil(end) / 7;
1524                 case MONTHS: return monthsUntil(end);
1525                 case YEARS: return monthsUntil(end) / 12;
1526                 case DECADES: return monthsUntil(end) / 120;
1527                 case CENTURIES: return monthsUntil(end) / 1200;
1528                 case MILLENNIA: return monthsUntil(end) / 12000;
1529                 case ERAS: return end.getLong(ERA) - getLong(ERA);
1530             }
1531             throw new DateTimeException("Unsupported unit: " + unit.getName());
1532         }
1533         return unit.between(this, endDate);
1534     }
1535 
1536     long daysUntil(LocalDate end) {
1537         return end.toEpochDay() - toEpochDay();  // no overflow
1538     }
1539 
1540     private long monthsUntil(LocalDate end) {
1541         long packed1 = getEpochMonth() * 32L + getDayOfMonth();  // no overflow
1542         long packed2 = end.getEpochMonth() * 32L + end.getDayOfMonth();  // no overflow
1543         return (packed2 - packed1) / 32;
1544     }
1545 
1546     /**
1547      * Calculates the period between this date and another date as a {@code Period}.
1548      * <p>
1549      * This calculates the period between two dates in terms of years, months and days.
1550      * The start and end points are {@code this} and the specified date.
1551      * The result will be negative if the end is before the start.
1552      * <p>
1553      * The calculation is performed using the ISO calendar system.
1554      * If necessary, the input date will be converted to ISO.
1555      * <p>
1556      * The start date is included, but the end date is not.
1557      * The period is calculated by removing complete months, then calculating
1558      * the remaining number of days, adjusting to ensure that both have the same sign.
1559      * The number of months is then normalized into years and months based on a 12 month year.
1560      * A month is considered to be complete if the end day-of-month is greater
1561      * than or equal to the start day-of-month.
1562      * For example, from {@code 2010-01-15} to {@code 2011-03-18} is "1 year, 2 months and 3 days".
1563      * <p>
1564      * The result of this method can be a negative period if the end is before the start.
1565      * The negative sign will be the same in each of year, month and day.
1566      * <p>
1567      * There are two equivalent ways of using this method.
1568      * The first is to invoke this method.
1569      * The second is to use {@link Period#between(LocalDate, LocalDate)}:
1570      * <pre>
1571      *   // these two lines are equivalent
1572      *   period = start.periodUntil(end);
1573      *   period = Period.between(start, end);
1574      * </pre>
1575      * The choice should be made based on which makes the code more readable.
1576      *
1577      * @param endDate  the end date, exclusive, which may be in any chronology, not null
1578      * @return the period between this date and the end date, not null
1579      */
1580     @Override
1581     public Period periodUntil(ChronoLocalDate<?> endDate) {
1582         LocalDate end = LocalDate.from(endDate);
1583         long totalMonths = end.getEpochMonth() - this.getEpochMonth();  // safe
1584         int days = end.day - this.day;
1585         if (totalMonths > 0 && days < 0) {
1586             totalMonths--;
1587             LocalDate calcDate = this.plusMonths(totalMonths);
1588             days = (int) (end.toEpochDay() - calcDate.toEpochDay());  // safe
1589         } else if (totalMonths < 0 && days > 0) {
1590             totalMonths++;
1591             days -= end.lengthOfMonth();
1592         }
1593         long years = totalMonths / 12;  // safe
1594         int months = (int) (totalMonths % 12);  // safe
1595         return Period.of(Math.toIntExact(years), months, days);
1596     }
1597 
1598     //-----------------------------------------------------------------------
1599     /**
1600      * Combines this date with a time to create a {@code LocalDateTime}.
1601      * <p>
1602      * This returns a {@code LocalDateTime} formed from this date at the specified time.
1603      * All possible combinations of date and time are valid.


1604      *
1605      * @param time  the time to combine with, not null
1606      * @return the local date-time formed from this date and the specified time, not null
1607      */
1608     @Override
1609     public LocalDateTime atTime(LocalTime time) {
1610         return LocalDateTime.of(this, time);
1611     }
1612 
1613     /**
1614      * Combines this date with a time to create a {@code LocalDateTime}.
1615      * <p>
1616      * This returns a {@code LocalDateTime} formed from this date at the
1617      * specified hour and minute.
1618      * The seconds and nanosecond fields will be set to zero.
1619      * The individual time fields must be within their valid range.
1620      * All possible combinations of date and time are valid.


1621      *
1622      * @param hour  the hour-of-day to use, from 0 to 23
1623      * @param minute  the minute-of-hour to use, from 0 to 59
1624      * @return the local date-time formed from this date and the specified time, not null
1625      * @throws DateTimeException if the value of any field is out of range
1626      */
1627     public LocalDateTime atTime(int hour, int minute) {
1628         return atTime(LocalTime.of(hour, minute));
1629     }
1630 
1631     /**
1632      * Combines this date with a time to create a {@code LocalDateTime}.
1633      * <p>
1634      * This returns a {@code LocalDateTime} formed from this date at the
1635      * specified hour, minute and second.
1636      * The nanosecond field will be set to zero.
1637      * The individual time fields must be within their valid range.
1638      * All possible combinations of date and time are valid.


1639      *
1640      * @param hour  the hour-of-day to use, from 0 to 23
1641      * @param minute  the minute-of-hour to use, from 0 to 59
1642      * @param second  the second-of-minute to represent, from 0 to 59
1643      * @return the local date-time formed from this date and the specified time, not null
1644      * @throws DateTimeException if the value of any field is out of range
1645      */
1646     public LocalDateTime atTime(int hour, int minute, int second) {
1647         return atTime(LocalTime.of(hour, minute, second));
1648     }
1649 
1650     /**
1651      * Combines this date with a time to create a {@code LocalDateTime}.
1652      * <p>
1653      * This returns a {@code LocalDateTime} formed from this date at the
1654      * specified hour, minute, second and nanosecond.
1655      * The individual time fields must be within their valid range.
1656      * All possible combinations of date and time are valid.


1657      *
1658      * @param hour  the hour-of-day to use, from 0 to 23
1659      * @param minute  the minute-of-hour to use, from 0 to 59
1660      * @param second  the second-of-minute to represent, from 0 to 59
1661      * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
1662      * @return the local date-time formed from this date and the specified time, not null
1663      * @throws DateTimeException if the value of any field is out of range
1664      */
1665     public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond) {
1666         return atTime(LocalTime.of(hour, minute, second, nanoOfSecond));
1667     }
1668 
1669     /**
1670      * Combines this date with an offset time to create an {@code OffsetDateTime}.
1671      * <p>
1672      * This returns an {@code OffsetDateTime} formed from this date at the specified time.
1673      * All possible combinations of date and time are valid.
1674      *
1675      * @param time  the time to combine with, not null
1676      * @return the offset date-time formed from this date and the specified time, not null
1677      */
1678     public OffsetDateTime atTime(OffsetTime time) {
1679         return OffsetDateTime.of(LocalDateTime.of(this, time.toLocalTime()), time.getOffset());
1680     }
1681 
1682     /**
1683      * Combines this date with the time of midnight to create a {@code LocalDateTime}
1684      * at the start of this date.
1685      * <p>
1686      * This returns a {@code LocalDateTime} formed from this date at the time of
1687      * midnight, 00:00, at the start of this date.
1688      *
1689      * @return the local date-time of midnight at the start of this date, not null

1690      */
1691     public LocalDateTime atStartOfDay() {
1692         return LocalDateTime.of(this, LocalTime.MIDNIGHT);
1693     }
1694 
1695     /**
1696      * Returns a zoned date-time from this date at the earliest valid time according
1697      * to the rules in the time-zone.
1698      * <p>
1699      * Time-zone rules, such as daylight savings, mean that not every local date-time
1700      * is valid for the specified zone, thus the local date-time may not be midnight.
1701      * <p>
1702      * In most cases, there is only one valid offset for a local date-time.
1703      * In the case of an overlap, there are two valid offsets, and the earlier one is used,
1704      * corresponding to the first occurrence of midnight on the date.
1705      * In the case of a gap, the zoned date-time will represent the instant just after the gap.
1706      * <p>
1707      * If the zone ID is a {@link ZoneOffset}, then the result always has a time of midnight.
1708      * <p>
1709      * To convert to a specific time in a given time-zone call {@link #atTime(LocalTime)}
1710      * followed by {@link LocalDateTime#atZone(ZoneId)}.


1711      *
1712      * @param zone  the zone ID to use, not null
1713      * @return the zoned date-time formed from this date and the earliest valid time for the zone, not null
1714      */
1715     public ZonedDateTime atStartOfDay(ZoneId zone) {
1716         Objects.requireNonNull(zone, "zone");
1717         // need to handle case where there is a gap from 11:30 to 00:30
1718         // standard ZDT factory would result in 01:00 rather than 00:30
1719         LocalDateTime ldt = atTime(LocalTime.MIDNIGHT);
1720         if (zone instanceof ZoneOffset == false) {
1721             ZoneRules rules = zone.getRules();
1722             ZoneOffsetTransition trans = rules.getTransition(ldt);
1723             if (trans != null && trans.isGap()) {
1724                 ldt = trans.getDateTimeAfter();
1725             }
1726         }
1727         return ZonedDateTime.of(ldt, zone);
1728     }
1729 
1730     //-----------------------------------------------------------------------


1743         total += day - 1;
1744         if (m > 2) {
1745             total--;
1746             if (isLeapYear() == false) {
1747                 total--;
1748             }
1749         }
1750         return total - DAYS_0000_TO_1970;
1751     }
1752 
1753     //-----------------------------------------------------------------------
1754     /**
1755      * Compares this date to another date.
1756      * <p>
1757      * The comparison is primarily based on the date, from earliest to latest.
1758      * It is "consistent with equals", as defined by {@link Comparable}.
1759      * <p>
1760      * If all the dates being compared are instances of {@code LocalDate},
1761      * then the comparison will be entirely based on the date.
1762      * If some dates being compared are in different chronologies, then the
1763      * chronology is also considered, see {@link java.time.chrono.ChronoLocalDate#compareTo}.
1764      *
1765      * @param other  the other date to compare to, not null
1766      * @return the comparator value, negative if less, positive if greater
1767      */
1768     @Override  // override for Javadoc and performance
1769     public int compareTo(ChronoLocalDate<?> other) {
1770         if (other instanceof LocalDate) {
1771             return compareTo0((LocalDate) other);
1772         }
1773         return ChronoLocalDate.super.compareTo(other);
1774     }
1775 
1776     int compareTo0(LocalDate otherDate) {
1777         int cmp = (year - otherDate.year);
1778         if (cmp == 0) {
1779             cmp = (month - otherDate.month);
1780             if (cmp == 0) {
1781                 cmp = (day - otherDate.day);
1782             }
1783         }


1929             } else {
1930                 buf.append(yearValue + 10000).deleteCharAt(0);
1931             }
1932         } else {
1933             if (yearValue > 9999) {
1934                 buf.append('+');
1935             }
1936             buf.append(yearValue);
1937         }
1938         return buf.append(monthValue < 10 ? "-0" : "-")
1939             .append(monthValue)
1940             .append(dayValue < 10 ? "-0" : "-")
1941             .append(dayValue)
1942             .toString();
1943     }
1944 
1945     /**
1946      * Outputs this date as a {@code String} using the formatter.
1947      * <p>
1948      * This date will be passed to the formatter
1949      * {@link DateTimeFormatter#format(TemporalAccessor) format method}.
1950      *
1951      * @param formatter  the formatter to use, not null
1952      * @return the formatted date string, not null
1953      * @throws DateTimeException if an error occurs during printing
1954      */
1955     @Override  // override for Javadoc
1956     public String toString(DateTimeFormatter formatter) {
1957         return ChronoLocalDate.super.toString(formatter);
1958     }
1959 
1960     //-----------------------------------------------------------------------
1961     /**
1962      * Writes the object using a
1963      * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1964      * <pre>
1965      *  out.writeByte(3);  // identifies this as a LocalDate
1966      *  out.writeInt(year);
1967      *  out.writeByte(month);
1968      *  out.writeByte(day);
1969      * </pre>