1 /*
   2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * This file is available under and governed by the GNU General Public
  28  * License version 2 only, as published by the Free Software Foundation.
  29  * However, the following notice accompanied the original version of this
  30  * file:
  31  *
  32  * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
  33  *
  34  * All rights reserved.
  35  *
  36  * Redistribution and use in source and binary forms, with or without
  37  * modification, are permitted provided that the following conditions are met:
  38  *
  39  *  * Redistributions of source code must retain the above copyright notice,
  40  *    this list of conditions and the following disclaimer.
  41  *
  42  *  * Redistributions in binary form must reproduce the above copyright notice,
  43  *    this list of conditions and the following disclaimer in the documentation
  44  *    and/or other materials provided with the distribution.
  45  *
  46  *  * Neither the name of JSR-310 nor the names of its contributors
  47  *    may be used to endorse or promote products derived from this software
  48  *    without specific prior written permission.
  49  *
  50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61  */
  62 package java.time;
  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.ERA;
  73 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  74 import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
  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.ObjectInputStream;
  82 import java.io.Serializable;
  83 import java.time.chrono.ChronoLocalDate;
  84 import java.time.chrono.IsoEra;
  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.Temporal;
  91 import java.time.temporal.TemporalAccessor;
  92 import java.time.temporal.TemporalAdjuster;
  93 import java.time.temporal.TemporalAmount;
  94 import java.time.temporal.TemporalField;
  95 import java.time.temporal.TemporalQueries;
  96 import java.time.temporal.TemporalQuery;
  97 import java.time.temporal.TemporalUnit;
  98 import java.time.temporal.UnsupportedTemporalTypeException;
  99 import java.time.temporal.ValueRange;
 100 import java.time.zone.ZoneOffsetTransition;
 101 import java.time.zone.ZoneRules;
 102 import java.util.Objects;
 103 
 104 /**
 105  * A date without a time-zone in the ISO-8601 calendar system,
 106  * such as {@code 2007-12-03}.
 107  * <p>
 108  * {@code LocalDate} is an immutable date-time object that represents a date,
 109  * often viewed as year-month-day. Other date fields, such as day-of-year,
 110  * day-of-week and week-of-year, can also be accessed.
 111  * For example, the value "2nd October 2007" can be stored in a {@code LocalDate}.
 112  * <p>
 113  * This class does not store or represent a time or time-zone.
 114  * Instead, it is a description of the date, as used for birthdays.
 115  * It cannot represent an instant on the time-line without additional information
 116  * such as an offset or time-zone.
 117  * <p>
 118  * The ISO-8601 calendar system is the modern civil calendar system used today
 119  * in most of the world. It is equivalent to the proleptic Gregorian calendar
 120  * system, in which today's rules for leap years are applied for all time.
 121  * For most applications written today, the ISO-8601 rules are entirely suitable.
 122  * However, any application that makes use of historical dates, and requires them
 123  * to be accurate will find the ISO-8601 approach unsuitable.
 124  *
 125  * <p>
 126  * This is a <a href="{@docRoot}/java/lang/doc-files/ValueBased.html">value-based</a>
 127  * class; use of identity-sensitive operations (including reference equality
 128  * ({@code ==}), identity hash code, or synchronization) on instances of
 129  * {@code LocalDate} may have unpredictable results and should be avoided.
 130  * The {@code equals} method should be used for comparisons.
 131  *
 132  * @implSpec
 133  * This class is immutable and thread-safe.
 134  *
 135  * @since 1.8
 136  */
 137 public final class LocalDate
 138         implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable {
 139 
 140     /**
 141      * The minimum supported {@code LocalDate}, '-999999999-01-01'.
 142      * This could be used by an application as a "far past" date.
 143      */
 144     public static final LocalDate MIN = LocalDate.of(Year.MIN_VALUE, 1, 1);
 145     /**
 146      * The maximum supported {@code LocalDate}, '+999999999-12-31'.
 147      * This could be used by an application as a "far future" date.
 148      */
 149     public static final LocalDate MAX = LocalDate.of(Year.MAX_VALUE, 12, 31);
 150     /**
 151      * The epoch year {@code LocalDate}, '1970-01-01'.
 152      */
 153     public static final LocalDate EPOCH = LocalDate.of(1970, 1, 1);
 154 
 155     /**
 156      * Serialization version.
 157      */
 158     private static final long serialVersionUID = 2942565459149668126L;
 159     /**
 160      * The number of days in a 400 year cycle.
 161      */
 162     private static final int DAYS_PER_CYCLE = 146097;
 163     /**
 164      * The number of days from year zero to year 1970.
 165      * There are five 400 year cycles from year zero to 2000.
 166      * There are 7 leap years from 1970 to 2000.
 167      */
 168     static final long DAYS_0000_TO_1970 = (DAYS_PER_CYCLE * 5L) - (30L * 365L + 7L);
 169 
 170     /**
 171      * The year.
 172      */
 173     private final int year;
 174     /**
 175      * The month-of-year.
 176      */
 177     private final short month;
 178     /**
 179      * The day-of-month.
 180      */
 181     private final short day;
 182 
 183     //-----------------------------------------------------------------------
 184     /**
 185      * Obtains the current date from the system clock in the default time-zone.
 186      * <p>
 187      * This will query the {@link Clock#systemDefaultZone() system clock} in the default
 188      * time-zone to obtain the current date.
 189      * <p>
 190      * Using this method will prevent the ability to use an alternate clock for testing
 191      * because the clock is hard-coded.
 192      *
 193      * @return the current date using the system clock and default time-zone, not null
 194      */
 195     public static LocalDate now() {
 196         return now(Clock.systemDefaultZone());
 197     }
 198 
 199     /**
 200      * Obtains the current date from the system clock in the specified time-zone.
 201      * <p>
 202      * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date.
 203      * Specifying the time-zone avoids dependence on the default time-zone.
 204      * <p>
 205      * Using this method will prevent the ability to use an alternate clock for testing
 206      * because the clock is hard-coded.
 207      *
 208      * @param zone  the zone ID to use, not null
 209      * @return the current date using the system clock, not null
 210      */
 211     public static LocalDate now(ZoneId zone) {
 212         return now(Clock.system(zone));
 213     }
 214 
 215     /**
 216      * Obtains the current date from the specified clock.
 217      * <p>
 218      * This will query the specified clock to obtain the current date - today.
 219      * Using this method allows the use of an alternate clock for testing.
 220      * The alternate clock may be introduced using {@link Clock dependency injection}.
 221      *
 222      * @param clock  the clock to use, not null
 223      * @return the current date, not null
 224      */
 225     public static LocalDate now(Clock clock) {
 226         Objects.requireNonNull(clock, "clock");
 227         final Instant now = clock.instant();  // called once
 228         return ofInstant(now, clock.getZone());
 229     }
 230 
 231     //-----------------------------------------------------------------------
 232     /**
 233      * Obtains an instance of {@code LocalDate} from a year, month and day.
 234      * <p>
 235      * This returns a {@code LocalDate} with the specified year, month and day-of-month.
 236      * The day must be valid for the year and month, otherwise an exception will be thrown.
 237      *
 238      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 239      * @param month  the month-of-year to represent, not null
 240      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 241      * @return the local date, not null
 242      * @throws DateTimeException if the value of any field is out of range,
 243      *  or if the day-of-month is invalid for the month-year
 244      */
 245     public static LocalDate of(int year, Month month, int dayOfMonth) {
 246         YEAR.checkValidValue(year);
 247         Objects.requireNonNull(month, "month");
 248         DAY_OF_MONTH.checkValidValue(dayOfMonth);
 249         return create(year, month.getValue(), dayOfMonth);
 250     }
 251 
 252     /**
 253      * Obtains an instance of {@code LocalDate} from a year, month and day.
 254      * <p>
 255      * This returns a {@code LocalDate} with the specified year, month and day-of-month.
 256      * The day must be valid for the year and month, otherwise an exception will be thrown.
 257      *
 258      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 259      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 260      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 261      * @return the local date, not null
 262      * @throws DateTimeException if the value of any field is out of range,
 263      *  or if the day-of-month is invalid for the month-year
 264      */
 265     public static LocalDate of(int year, int month, int dayOfMonth) {
 266         YEAR.checkValidValue(year);
 267         MONTH_OF_YEAR.checkValidValue(month);
 268         DAY_OF_MONTH.checkValidValue(dayOfMonth);
 269         return create(year, month, dayOfMonth);
 270     }
 271 
 272     //-----------------------------------------------------------------------
 273     /**
 274      * Obtains an instance of {@code LocalDate} from a year and day-of-year.
 275      * <p>
 276      * This returns a {@code LocalDate} with the specified year and day-of-year.
 277      * The day-of-year must be valid for the year, otherwise an exception will be thrown.
 278      *
 279      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 280      * @param dayOfYear  the day-of-year to represent, from 1 to 366
 281      * @return the local date, not null
 282      * @throws DateTimeException if the value of any field is out of range,
 283      *  or if the day-of-year is invalid for the year
 284      */
 285     public static LocalDate ofYearDay(int year, int dayOfYear) {
 286         YEAR.checkValidValue(year);
 287         DAY_OF_YEAR.checkValidValue(dayOfYear);
 288         boolean leap = IsoChronology.INSTANCE.isLeapYear(year);
 289         if (dayOfYear == 366 && leap == false) {
 290             throw new DateTimeException("Invalid date 'DayOfYear 366' as '" + year + "' is not a leap year");
 291         }
 292         Month moy = Month.of((dayOfYear - 1) / 31 + 1);
 293         int monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1;
 294         if (dayOfYear > monthEnd) {
 295             moy = moy.plus(1);
 296         }
 297         int dom = dayOfYear - moy.firstDayOfYear(leap) + 1;
 298         return new LocalDate(year, moy.getValue(), dom);
 299     }
 300 
 301     //-----------------------------------------------------------------------
 302     /**
 303      * Obtains an instance of {@code LocalDate} from an {@code Instant} and zone ID.
 304      * <p>
 305      * This creates a local date based on the specified instant.
 306      * First, the offset from UTC/Greenwich is obtained using the zone ID and instant,
 307      * which is simple as there is only one valid offset for each instant.
 308      * Then, the instant and offset are used to calculate the local date.
 309      *
 310      * @param instant  the instant to create the date from, not null
 311      * @param zone  the time-zone, which may be an offset, not null
 312      * @return the local date, not null
 313      * @throws DateTimeException if the result exceeds the supported range
 314      * @since 9
 315      */
 316     public static LocalDate ofInstant(Instant instant, ZoneId zone) {
 317         Objects.requireNonNull(instant, "instant");
 318         Objects.requireNonNull(zone, "zone");
 319         ZoneRules rules = zone.getRules();
 320         ZoneOffset offset = rules.getOffset(instant);
 321         long localSecond = instant.getEpochSecond() + offset.getTotalSeconds();
 322         long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY);
 323         return ofEpochDay(localEpochDay);
 324     }
 325 
 326     //-----------------------------------------------------------------------
 327     /**
 328      * Obtains an instance of {@code LocalDate} from the epoch day count.
 329      * <p>
 330      * This returns a {@code LocalDate} with the specified epoch-day.
 331      * The {@link ChronoField#EPOCH_DAY EPOCH_DAY} is a simple incrementing count
 332      * of days where day 0 is 1970-01-01. Negative numbers represent earlier days.
 333      *
 334      * @param epochDay  the Epoch Day to convert, based on the epoch 1970-01-01
 335      * @return the local date, not null
 336      * @throws DateTimeException if the epoch day exceeds the supported date range
 337      */
 338     public static LocalDate ofEpochDay(long epochDay) {
 339         long zeroDay = epochDay + DAYS_0000_TO_1970;
 340         // find the march-based year
 341         zeroDay -= 60;  // adjust to 0000-03-01 so leap day is at end of four year cycle
 342         long adjust = 0;
 343         if (zeroDay < 0) {
 344             // adjust negative years to positive for calculation
 345             long adjustCycles = (zeroDay + 1) / DAYS_PER_CYCLE - 1;
 346             adjust = adjustCycles * 400;
 347             zeroDay += -adjustCycles * DAYS_PER_CYCLE;
 348         }
 349         long yearEst = (400 * zeroDay + 591) / DAYS_PER_CYCLE;
 350         long doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
 351         if (doyEst < 0) {
 352             // fix estimate
 353             yearEst--;
 354             doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
 355         }
 356         yearEst += adjust;  // reset any negative year
 357         int marchDoy0 = (int) doyEst;
 358 
 359         // convert march-based values back to january-based
 360         int marchMonth0 = (marchDoy0 * 5 + 2) / 153;
 361         int month = (marchMonth0 + 2) % 12 + 1;
 362         int dom = marchDoy0 - (marchMonth0 * 306 + 5) / 10 + 1;
 363         yearEst += marchMonth0 / 10;
 364 
 365         // check year now we are certain it is correct
 366         int year = YEAR.checkValidIntValue(yearEst);
 367         return new LocalDate(year, month, dom);
 368     }
 369 
 370     //-----------------------------------------------------------------------
 371     /**
 372      * Obtains an instance of {@code LocalDate} from a temporal object.
 373      * <p>
 374      * This obtains a local date based on the specified temporal.
 375      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 376      * which this factory converts to an instance of {@code LocalDate}.
 377      * <p>
 378      * The conversion uses the {@link TemporalQueries#localDate()} query, which relies
 379      * on extracting the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field.
 380      * <p>
 381      * This method matches the signature of the functional interface {@link TemporalQuery}
 382      * allowing it to be used as a query via method reference, {@code LocalDate::from}.
 383      *
 384      * @param temporal  the temporal object to convert, not null
 385      * @return the local date, not null
 386      * @throws DateTimeException if unable to convert to a {@code LocalDate}
 387      */
 388     public static LocalDate from(TemporalAccessor temporal) {
 389         Objects.requireNonNull(temporal, "temporal");
 390         LocalDate date = temporal.query(TemporalQueries.localDate());
 391         if (date == null) {
 392             throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " +
 393                     temporal + " of type " + temporal.getClass().getName());
 394         }
 395         return date;
 396     }
 397 
 398     //-----------------------------------------------------------------------
 399     /**
 400      * Obtains an instance of {@code LocalDate} from a text string such as {@code 2007-12-03}.
 401      * <p>
 402      * The string must represent a valid date and is parsed using
 403      * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE}.
 404      *
 405      * @param text  the text to parse such as "2007-12-03", not null
 406      * @return the parsed local date, not null
 407      * @throws DateTimeParseException if the text cannot be parsed
 408      */
 409     public static LocalDate parse(CharSequence text) {
 410         return parse(text, DateTimeFormatter.ISO_LOCAL_DATE);
 411     }
 412 
 413     /**
 414      * Obtains an instance of {@code LocalDate} from a text string using a specific formatter.
 415      * <p>
 416      * The text is parsed using the formatter, returning a date.
 417      *
 418      * @param text  the text to parse, not null
 419      * @param formatter  the formatter to use, not null
 420      * @return the parsed local date, not null
 421      * @throws DateTimeParseException if the text cannot be parsed
 422      */
 423     public static LocalDate parse(CharSequence text, DateTimeFormatter formatter) {
 424         Objects.requireNonNull(formatter, "formatter");
 425         return formatter.parse(text, LocalDate::from);
 426     }
 427 
 428     //-----------------------------------------------------------------------
 429     /**
 430      * Creates a local date from the year, month and day fields.
 431      *
 432      * @param year  the year to represent, validated from MIN_YEAR to MAX_YEAR
 433      * @param month  the month-of-year to represent, from 1 to 12, validated
 434      * @param dayOfMonth  the day-of-month to represent, validated from 1 to 31
 435      * @return the local date, not null
 436      * @throws DateTimeException if the day-of-month is invalid for the month-year
 437      */
 438     private static LocalDate create(int year, int month, int dayOfMonth) {
 439         if (dayOfMonth > 28) {
 440             int dom = 31;
 441             switch (month) {
 442                 case 2:
 443                     dom = (IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
 444                     break;
 445                 case 4:
 446                 case 6:
 447                 case 9:
 448                 case 11:
 449                     dom = 30;
 450                     break;
 451             }
 452             if (dayOfMonth > dom) {
 453                 if (dayOfMonth == 29) {
 454                     throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year");
 455                 } else {
 456                     throw new DateTimeException("Invalid date '" + Month.of(month).name() + " " + dayOfMonth + "'");
 457                 }
 458             }
 459         }
 460         return new LocalDate(year, month, dayOfMonth);
 461     }
 462 
 463     /**
 464      * Resolves the date, resolving days past the end of month.
 465      *
 466      * @param year  the year to represent, validated from MIN_YEAR to MAX_YEAR
 467      * @param month  the month-of-year to represent, validated from 1 to 12
 468      * @param day  the day-of-month to represent, validated from 1 to 31
 469      * @return the resolved date, not null
 470      */
 471     private static LocalDate resolvePreviousValid(int year, int month, int day) {
 472         switch (month) {
 473             case 2:
 474                 day = Math.min(day, IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
 475                 break;
 476             case 4:
 477             case 6:
 478             case 9:
 479             case 11:
 480                 day = Math.min(day, 30);
 481                 break;
 482         }
 483         return new LocalDate(year, month, day);
 484     }
 485 
 486     /**
 487      * Constructor, previously validated.
 488      *
 489      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 490      * @param month  the month-of-year to represent, not null
 491      * @param dayOfMonth  the day-of-month to represent, valid for year-month, from 1 to 31
 492      */
 493     private LocalDate(int year, int month, int dayOfMonth) {
 494         this.year = year;
 495         this.month = (short) month;
 496         this.day = (short) dayOfMonth;
 497     }
 498 
 499     //-----------------------------------------------------------------------
 500     /**
 501      * Checks if the specified field is supported.
 502      * <p>
 503      * This checks if this date can be queried for the specified field.
 504      * If false, then calling the {@link #range(TemporalField) range},
 505      * {@link #get(TemporalField) get} and {@link #with(TemporalField, long)}
 506      * methods will throw an exception.
 507      * <p>
 508      * If the field is a {@link ChronoField} then the query is implemented here.
 509      * The supported fields are:
 510      * <ul>
 511      * <li>{@code DAY_OF_WEEK}
 512      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
 513      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
 514      * <li>{@code DAY_OF_MONTH}
 515      * <li>{@code DAY_OF_YEAR}
 516      * <li>{@code EPOCH_DAY}
 517      * <li>{@code ALIGNED_WEEK_OF_MONTH}
 518      * <li>{@code ALIGNED_WEEK_OF_YEAR}
 519      * <li>{@code MONTH_OF_YEAR}
 520      * <li>{@code PROLEPTIC_MONTH}
 521      * <li>{@code YEAR_OF_ERA}
 522      * <li>{@code YEAR}
 523      * <li>{@code ERA}
 524      * </ul>
 525      * All other {@code ChronoField} instances will return false.
 526      * <p>
 527      * If the field is not a {@code ChronoField}, then the result of this method
 528      * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
 529      * passing {@code this} as the argument.
 530      * Whether the field is supported is determined by the field.
 531      *
 532      * @param field  the field to check, null returns false
 533      * @return true if the field is supported on this date, false if not
 534      */
 535     @Override  // override for Javadoc
 536     public boolean isSupported(TemporalField field) {
 537         return ChronoLocalDate.super.isSupported(field);
 538     }
 539 
 540     /**
 541      * Checks if the specified unit is supported.
 542      * <p>
 543      * This checks if the specified unit can be added to, or subtracted from, this date.
 544      * If false, then calling the {@link #plus(long, TemporalUnit)} and
 545      * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
 546      * <p>
 547      * If the unit is a {@link ChronoUnit} then the query is implemented here.
 548      * The supported units are:
 549      * <ul>
 550      * <li>{@code DAYS}
 551      * <li>{@code WEEKS}
 552      * <li>{@code MONTHS}
 553      * <li>{@code YEARS}
 554      * <li>{@code DECADES}
 555      * <li>{@code CENTURIES}
 556      * <li>{@code MILLENNIA}
 557      * <li>{@code ERAS}
 558      * </ul>
 559      * All other {@code ChronoUnit} instances will return false.
 560      * <p>
 561      * If the unit is not a {@code ChronoUnit}, then the result of this method
 562      * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
 563      * passing {@code this} as the argument.
 564      * Whether the unit is supported is determined by the unit.
 565      *
 566      * @param unit  the unit to check, null returns false
 567      * @return true if the unit can be added/subtracted, false if not
 568      */
 569     @Override  // override for Javadoc
 570     public boolean isSupported(TemporalUnit unit) {
 571         return ChronoLocalDate.super.isSupported(unit);
 572     }
 573 
 574     //-----------------------------------------------------------------------
 575     /**
 576      * Gets the range of valid values for the specified field.
 577      * <p>
 578      * The range object expresses the minimum and maximum valid values for a field.
 579      * This date is used to enhance the accuracy of the returned range.
 580      * If it is not possible to return the range, because the field is not supported
 581      * or for some other reason, an exception is thrown.
 582      * <p>
 583      * If the field is a {@link ChronoField} then the query is implemented here.
 584      * The {@link #isSupported(TemporalField) supported fields} will return
 585      * appropriate range instances.
 586      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 587      * <p>
 588      * If the field is not a {@code ChronoField}, then the result of this method
 589      * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
 590      * passing {@code this} as the argument.
 591      * Whether the range can be obtained is determined by the field.
 592      *
 593      * @param field  the field to query the range for, not null
 594      * @return the range of valid values for the field, not null
 595      * @throws DateTimeException if the range for the field cannot be obtained
 596      * @throws UnsupportedTemporalTypeException if the field is not supported
 597      */
 598     @Override
 599     public ValueRange range(TemporalField field) {
 600         if (field instanceof ChronoField) {
 601             ChronoField f = (ChronoField) field;
 602             if (f.isDateBased()) {
 603                 switch (f) {
 604                     case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
 605                     case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
 606                     case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, getMonth() == Month.FEBRUARY && isLeapYear() == false ? 4 : 5);
 607                     case YEAR_OF_ERA:
 608                         return (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE));
 609                 }
 610                 return field.range();
 611             }
 612             throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
 613         }
 614         return field.rangeRefinedBy(this);
 615     }
 616 
 617     /**
 618      * Gets the value of the specified field from this date as an {@code int}.
 619      * <p>
 620      * This queries this date for the value of the specified field.
 621      * The returned value will always be within the valid range of values for the field.
 622      * If it is not possible to return the value, because the field is not supported
 623      * or for some other reason, an exception is thrown.
 624      * <p>
 625      * If the field is a {@link ChronoField} then the query is implemented here.
 626      * The {@link #isSupported(TemporalField) supported fields} will return valid
 627      * values based on this date, except {@code EPOCH_DAY} and {@code PROLEPTIC_MONTH}
 628      * which are too large to fit in an {@code int} and throw an {@code UnsupportedTemporalTypeException}.
 629      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 630      * <p>
 631      * If the field is not a {@code ChronoField}, then the result of this method
 632      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 633      * passing {@code this} as the argument. Whether the value can be obtained,
 634      * and what the value represents, is determined by the field.
 635      *
 636      * @param field  the field to get, not null
 637      * @return the value for the field
 638      * @throws DateTimeException if a value for the field cannot be obtained or
 639      *         the value is outside the range of valid values for the field
 640      * @throws UnsupportedTemporalTypeException if the field is not supported or
 641      *         the range of values exceeds an {@code int}
 642      * @throws ArithmeticException if numeric overflow occurs
 643      */
 644     @Override  // override for Javadoc and performance
 645     public int get(TemporalField field) {
 646         if (field instanceof ChronoField) {
 647             return get0(field);
 648         }
 649         return ChronoLocalDate.super.get(field);
 650     }
 651 
 652     /**
 653      * Gets the value of the specified field from this date as a {@code long}.
 654      * <p>
 655      * This queries this date for the value of the specified field.
 656      * If it is not possible to return the value, because the field is not supported
 657      * or for some other reason, an exception is thrown.
 658      * <p>
 659      * If the field is a {@link ChronoField} then the query is implemented here.
 660      * The {@link #isSupported(TemporalField) supported fields} will return valid
 661      * values based on this date.
 662      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 663      * <p>
 664      * If the field is not a {@code ChronoField}, then the result of this method
 665      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 666      * passing {@code this} as the argument. Whether the value can be obtained,
 667      * and what the value represents, is determined by the field.
 668      *
 669      * @param field  the field to get, not null
 670      * @return the value for the field
 671      * @throws DateTimeException if a value for the field cannot be obtained
 672      * @throws UnsupportedTemporalTypeException if the field is not supported
 673      * @throws ArithmeticException if numeric overflow occurs
 674      */
 675     @Override
 676     public long getLong(TemporalField field) {
 677         if (field instanceof ChronoField) {
 678             if (field == EPOCH_DAY) {
 679                 return toEpochDay();
 680             }
 681             if (field == PROLEPTIC_MONTH) {
 682                 return getProlepticMonth();
 683             }
 684             return get0(field);
 685         }
 686         return field.getFrom(this);
 687     }
 688 
 689     private int get0(TemporalField field) {
 690         switch ((ChronoField) field) {
 691             case DAY_OF_WEEK: return getDayOfWeek().getValue();
 692             case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1;
 693             case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1;
 694             case DAY_OF_MONTH: return day;
 695             case DAY_OF_YEAR: return getDayOfYear();
 696             case EPOCH_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'EpochDay' for get() method, use getLong() instead");
 697             case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1;
 698             case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1;
 699             case MONTH_OF_YEAR: return month;
 700             case PROLEPTIC_MONTH: throw new UnsupportedTemporalTypeException("Invalid field 'ProlepticMonth' for get() method, use getLong() instead");
 701             case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year);
 702             case YEAR: return year;
 703             case ERA: return (year >= 1 ? 1 : 0);
 704         }
 705         throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
 706     }
 707 
 708     private long getProlepticMonth() {
 709         return (year * 12L + month - 1);
 710     }
 711 
 712     //-----------------------------------------------------------------------
 713     /**
 714      * Gets the chronology of this date, which is the ISO calendar system.
 715      * <p>
 716      * The {@code Chronology} represents the calendar system in use.
 717      * The ISO-8601 calendar system is the modern civil calendar system used today
 718      * in most of the world. It is equivalent to the proleptic Gregorian calendar
 719      * system, in which today's rules for leap years are applied for all time.
 720      *
 721      * @return the ISO chronology, not null
 722      */
 723     @Override
 724     public IsoChronology getChronology() {
 725         return IsoChronology.INSTANCE;
 726     }
 727 
 728     /**
 729      * Gets the era applicable at this date.
 730      * <p>
 731      * The official ISO-8601 standard does not define eras, however {@code IsoChronology} does.
 732      * It defines two eras, 'CE' from year one onwards and 'BCE' from year zero backwards.
 733      * Since dates before the Julian-Gregorian cutover are not in line with history,
 734      * the cutover between 'BCE' and 'CE' is also not aligned with the commonly used
 735      * eras, often referred to using 'BC' and 'AD'.
 736      * <p>
 737      * Users of this class should typically ignore this method as it exists primarily
 738      * to fulfill the {@link ChronoLocalDate} contract where it is necessary to support
 739      * the Japanese calendar system.
 740      *
 741      * @return the IsoEra applicable at this date, not null
 742      */
 743     @Override // override for Javadoc
 744     public IsoEra getEra() {
 745         return (getYear() >= 1 ? IsoEra.CE : IsoEra.BCE);
 746     }
 747 
 748     /**
 749      * Gets the year field.
 750      * <p>
 751      * This method returns the primitive {@code int} value for the year.
 752      * <p>
 753      * The year returned by this method is proleptic as per {@code get(YEAR)}.
 754      * To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}.
 755      *
 756      * @return the year, from MIN_YEAR to MAX_YEAR
 757      */
 758     public int getYear() {
 759         return year;
 760     }
 761 
 762     /**
 763      * Gets the month-of-year field from 1 to 12.
 764      * <p>
 765      * This method returns the month as an {@code int} from 1 to 12.
 766      * Application code is frequently clearer if the enum {@link Month}
 767      * is used by calling {@link #getMonth()}.
 768      *
 769      * @return the month-of-year, from 1 to 12
 770      * @see #getMonth()
 771      */
 772     public int getMonthValue() {
 773         return month;
 774     }
 775 
 776     /**
 777      * Gets the month-of-year field using the {@code Month} enum.
 778      * <p>
 779      * This method returns the enum {@link Month} for the month.
 780      * This avoids confusion as to what {@code int} values mean.
 781      * If you need access to the primitive {@code int} value then the enum
 782      * provides the {@link Month#getValue() int value}.
 783      *
 784      * @return the month-of-year, not null
 785      * @see #getMonthValue()
 786      */
 787     public Month getMonth() {
 788         return Month.of(month);
 789     }
 790 
 791     /**
 792      * Gets the day-of-month field.
 793      * <p>
 794      * This method returns the primitive {@code int} value for the day-of-month.
 795      *
 796      * @return the day-of-month, from 1 to 31
 797      */
 798     public int getDayOfMonth() {
 799         return day;
 800     }
 801 
 802     /**
 803      * Gets the day-of-year field.
 804      * <p>
 805      * This method returns the primitive {@code int} value for the day-of-year.
 806      *
 807      * @return the day-of-year, from 1 to 365, or 366 in a leap year
 808      */
 809     public int getDayOfYear() {
 810         return getMonth().firstDayOfYear(isLeapYear()) + day - 1;
 811     }
 812 
 813     /**
 814      * Gets the day-of-week field, which is an enum {@code DayOfWeek}.
 815      * <p>
 816      * This method returns the enum {@link DayOfWeek} for the day-of-week.
 817      * This avoids confusion as to what {@code int} values mean.
 818      * If you need access to the primitive {@code int} value then the enum
 819      * provides the {@link DayOfWeek#getValue() int value}.
 820      * <p>
 821      * Additional information can be obtained from the {@code DayOfWeek}.
 822      * This includes textual names of the values.
 823      *
 824      * @return the day-of-week, not null
 825      */
 826     public DayOfWeek getDayOfWeek() {
 827         int dow0 = (int)Math.floorMod(toEpochDay() + 3, 7);
 828         return DayOfWeek.of(dow0 + 1);
 829     }
 830 
 831     //-----------------------------------------------------------------------
 832     /**
 833      * Checks if the year is a leap year, according to the ISO proleptic
 834      * calendar system rules.
 835      * <p>
 836      * This method applies the current rules for leap years across the whole time-line.
 837      * In general, a year is a leap year if it is divisible by four without
 838      * remainder. However, years divisible by 100, are not leap years, with
 839      * the exception of years divisible by 400 which are.
 840      * <p>
 841      * For example, 1904 is a leap year it is divisible by 4.
 842      * 1900 was not a leap year as it is divisible by 100, however 2000 was a
 843      * leap year as it is divisible by 400.
 844      * <p>
 845      * The calculation is proleptic - applying the same rules into the far future and far past.
 846      * This is historically inaccurate, but is correct for the ISO-8601 standard.
 847      *
 848      * @return true if the year is leap, false otherwise
 849      */
 850     @Override // override for Javadoc and performance
 851     public boolean isLeapYear() {
 852         return IsoChronology.INSTANCE.isLeapYear(year);
 853     }
 854 
 855     /**
 856      * Returns the length of the month represented by this date.
 857      * <p>
 858      * This returns the length of the month in days.
 859      * For example, a date in January would return 31.
 860      *
 861      * @return the length of the month in days
 862      */
 863     @Override
 864     public int lengthOfMonth() {
 865         switch (month) {
 866             case 2:
 867                 return (isLeapYear() ? 29 : 28);
 868             case 4:
 869             case 6:
 870             case 9:
 871             case 11:
 872                 return 30;
 873             default:
 874                 return 31;
 875         }
 876     }
 877 
 878     /**
 879      * Returns the length of the year represented by this date.
 880      * <p>
 881      * This returns the length of the year in days, either 365 or 366.
 882      *
 883      * @return 366 if the year is leap, 365 otherwise
 884      */
 885     @Override // override for Javadoc and performance
 886     public int lengthOfYear() {
 887         return (isLeapYear() ? 366 : 365);
 888     }
 889 
 890     //-----------------------------------------------------------------------
 891     /**
 892      * Returns an adjusted copy of this date.
 893      * <p>
 894      * This returns a {@code LocalDate}, based on this one, with the date adjusted.
 895      * The adjustment takes place using the specified adjuster strategy object.
 896      * Read the documentation of the adjuster to understand what adjustment will be made.
 897      * <p>
 898      * A simple adjuster might simply set the one of the fields, such as the year field.
 899      * A more complex adjuster might set the date to the last day of the month.
 900      * <p>
 901      * A selection of common adjustments is provided in
 902      * {@link java.time.temporal.TemporalAdjusters TemporalAdjusters}.
 903      * These include finding the "last day of the month" and "next Wednesday".
 904      * Key date-time classes also implement the {@code TemporalAdjuster} interface,
 905      * such as {@link Month} and {@link java.time.MonthDay MonthDay}.
 906      * The adjuster is responsible for handling special cases, such as the varying
 907      * lengths of month and leap years.
 908      * <p>
 909      * For example this code returns a date on the last day of July:
 910      * <pre>
 911      *  import static java.time.Month.*;
 912      *  import static java.time.temporal.TemporalAdjusters.*;
 913      *
 914      *  result = localDate.with(JULY).with(lastDayOfMonth());
 915      * </pre>
 916      * <p>
 917      * The result of this method is obtained by invoking the
 918      * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
 919      * specified adjuster passing {@code this} as the argument.
 920      * <p>
 921      * This instance is immutable and unaffected by this method call.
 922      *
 923      * @param adjuster the adjuster to use, not null
 924      * @return a {@code LocalDate} based on {@code this} with the adjustment made, not null
 925      * @throws DateTimeException if the adjustment cannot be made
 926      * @throws ArithmeticException if numeric overflow occurs
 927      */
 928     @Override
 929     public LocalDate with(TemporalAdjuster adjuster) {
 930         // optimizations
 931         if (adjuster instanceof LocalDate) {
 932             return (LocalDate) adjuster;
 933         }
 934         return (LocalDate) adjuster.adjustInto(this);
 935     }
 936 
 937     /**
 938      * Returns a copy of this date with the specified field set to a new value.
 939      * <p>
 940      * This returns a {@code LocalDate}, based on this one, with the value
 941      * for the specified field changed.
 942      * This can be used to change any supported field, such as the year, month or day-of-month.
 943      * If it is not possible to set the value, because the field is not supported or for
 944      * some other reason, an exception is thrown.
 945      * <p>
 946      * In some cases, changing the specified field can cause the resulting date to become invalid,
 947      * such as changing the month from 31st January to February would make the day-of-month invalid.
 948      * In cases like this, the field is responsible for resolving the date. Typically it will choose
 949      * the previous valid date, which would be the last valid day of February in this example.
 950      * <p>
 951      * If the field is a {@link ChronoField} then the adjustment is implemented here.
 952      * The supported fields behave as follows:
 953      * <ul>
 954      * <li>{@code DAY_OF_WEEK} -
 955      *  Returns a {@code LocalDate} with the specified day-of-week.
 956      *  The date is adjusted up to 6 days forward or backward within the boundary
 957      *  of a Monday to Sunday week.
 958      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} -
 959      *  Returns a {@code LocalDate} with the specified aligned-day-of-week.
 960      *  The date is adjusted to the specified month-based aligned-day-of-week.
 961      *  Aligned weeks are counted such that the first week of a given month starts
 962      *  on the first day of that month.
 963      *  This may cause the date to be moved up to 6 days into the following month.
 964      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR} -
 965      *  Returns a {@code LocalDate} with the specified aligned-day-of-week.
 966      *  The date is adjusted to the specified year-based aligned-day-of-week.
 967      *  Aligned weeks are counted such that the first week of a given year starts
 968      *  on the first day of that year.
 969      *  This may cause the date to be moved up to 6 days into the following year.
 970      * <li>{@code DAY_OF_MONTH} -
 971      *  Returns a {@code LocalDate} with the specified day-of-month.
 972      *  The month and year will be unchanged. If the day-of-month is invalid for the
 973      *  year and month, then a {@code DateTimeException} is thrown.
 974      * <li>{@code DAY_OF_YEAR} -
 975      *  Returns a {@code LocalDate} with the specified day-of-year.
 976      *  The year will be unchanged. If the day-of-year is invalid for the
 977      *  year, then a {@code DateTimeException} is thrown.
 978      * <li>{@code EPOCH_DAY} -
 979      *  Returns a {@code LocalDate} with the specified epoch-day.
 980      *  This completely replaces the date and is equivalent to {@link #ofEpochDay(long)}.
 981      * <li>{@code ALIGNED_WEEK_OF_MONTH} -
 982      *  Returns a {@code LocalDate} with the specified aligned-week-of-month.
 983      *  Aligned weeks are counted such that the first week of a given month starts
 984      *  on the first day of that month.
 985      *  This adjustment moves the date in whole week chunks to match the specified week.
 986      *  The result will have the same day-of-week as this date.
 987      *  This may cause the date to be moved into the following month.
 988      * <li>{@code ALIGNED_WEEK_OF_YEAR} -
 989      *  Returns a {@code LocalDate} with the specified aligned-week-of-year.
 990      *  Aligned weeks are counted such that the first week of a given year starts
 991      *  on the first day of that year.
 992      *  This adjustment moves the date in whole week chunks to match the specified week.
 993      *  The result will have the same day-of-week as this date.
 994      *  This may cause the date to be moved into the following year.
 995      * <li>{@code MONTH_OF_YEAR} -
 996      *  Returns a {@code LocalDate} with the specified month-of-year.
 997      *  The year will be unchanged. The day-of-month will also be unchanged,
 998      *  unless it would be invalid for the new month and year. In that case, the
 999      *  day-of-month is adjusted to the maximum valid value for the new month and year.
1000      * <li>{@code PROLEPTIC_MONTH} -
1001      *  Returns a {@code LocalDate} with the specified proleptic-month.
1002      *  The day-of-month will be unchanged, unless it would be invalid for the new month
1003      *  and year. In that case, the day-of-month is adjusted to the maximum valid value
1004      *  for the new month and year.
1005      * <li>{@code YEAR_OF_ERA} -
1006      *  Returns a {@code LocalDate} with the specified year-of-era.
1007      *  The era and month will be unchanged. The day-of-month will also be unchanged,
1008      *  unless it would be invalid for the new month and year. In that case, the
1009      *  day-of-month is adjusted to the maximum valid value for the new month and year.
1010      * <li>{@code YEAR} -
1011      *  Returns a {@code LocalDate} with the specified year.
1012      *  The month will be unchanged. The day-of-month will also be unchanged,
1013      *  unless it would be invalid for the new month and year. In that case, the
1014      *  day-of-month is adjusted to the maximum valid value for the new month and year.
1015      * <li>{@code ERA} -
1016      *  Returns a {@code LocalDate} with the specified era.
1017      *  The year-of-era and month will be unchanged. The day-of-month will also be unchanged,
1018      *  unless it would be invalid for the new month and year. In that case, the
1019      *  day-of-month is adjusted to the maximum valid value for the new month and year.
1020      * </ul>
1021      * <p>
1022      * In all cases, if the new value is outside the valid range of values for the field
1023      * then a {@code DateTimeException} will be thrown.
1024      * <p>
1025      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
1026      * <p>
1027      * If the field is not a {@code ChronoField}, then the result of this method
1028      * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
1029      * passing {@code this} as the argument. In this case, the field determines
1030      * whether and how to adjust the instant.
1031      * <p>
1032      * This instance is immutable and unaffected by this method call.
1033      *
1034      * @param field  the field to set in the result, not null
1035      * @param newValue  the new value of the field in the result
1036      * @return a {@code LocalDate} based on {@code this} with the specified field set, not null
1037      * @throws DateTimeException if the field cannot be set
1038      * @throws UnsupportedTemporalTypeException if the field is not supported
1039      * @throws ArithmeticException if numeric overflow occurs
1040      */
1041     @Override
1042     public LocalDate with(TemporalField field, long newValue) {
1043         if (field instanceof ChronoField) {
1044             ChronoField f = (ChronoField) field;
1045             f.checkValidValue(newValue);
1046             switch (f) {
1047                 case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek().getValue());
1048                 case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH));
1049                 case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR));
1050                 case DAY_OF_MONTH: return withDayOfMonth((int) newValue);
1051                 case DAY_OF_YEAR: return withDayOfYear((int) newValue);
1052                 case EPOCH_DAY: return LocalDate.ofEpochDay(newValue);
1053                 case ALIGNED_WEEK_OF_MONTH: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_MONTH));
1054                 case ALIGNED_WEEK_OF_YEAR: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_YEAR));
1055                 case MONTH_OF_YEAR: return withMonth((int) newValue);
1056                 case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth());
1057                 case YEAR_OF_ERA: return withYear((int) (year >= 1 ? newValue : 1 - newValue));
1058                 case YEAR: return withYear((int) newValue);
1059                 case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year));
1060             }
1061             throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
1062         }
1063         return field.adjustInto(this, newValue);
1064     }
1065 
1066     //-----------------------------------------------------------------------
1067     /**
1068      * Returns a copy of this {@code LocalDate} with the year altered.
1069      * <p>
1070      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
1071      * <p>
1072      * This instance is immutable and unaffected by this method call.
1073      *
1074      * @param year  the year to set in the result, from MIN_YEAR to MAX_YEAR
1075      * @return a {@code LocalDate} based on this date with the requested year, not null
1076      * @throws DateTimeException if the year value is invalid
1077      */
1078     public LocalDate withYear(int year) {
1079         if (this.year == year) {
1080             return this;
1081         }
1082         YEAR.checkValidValue(year);
1083         return resolvePreviousValid(year, month, day);
1084     }
1085 
1086     /**
1087      * Returns a copy of this {@code LocalDate} with the month-of-year altered.
1088      * <p>
1089      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
1090      * <p>
1091      * This instance is immutable and unaffected by this method call.
1092      *
1093      * @param month  the month-of-year to set in the result, from 1 (January) to 12 (December)
1094      * @return a {@code LocalDate} based on this date with the requested month, not null
1095      * @throws DateTimeException if the month-of-year value is invalid
1096      */
1097     public LocalDate withMonth(int month) {
1098         if (this.month == month) {
1099             return this;
1100         }
1101         MONTH_OF_YEAR.checkValidValue(month);
1102         return resolvePreviousValid(year, month, day);
1103     }
1104 
1105     /**
1106      * Returns a copy of this {@code LocalDate} with the day-of-month altered.
1107      * <p>
1108      * If the resulting date is invalid, an exception is thrown.
1109      * <p>
1110      * This instance is immutable and unaffected by this method call.
1111      *
1112      * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
1113      * @return a {@code LocalDate} based on this date with the requested day, not null
1114      * @throws DateTimeException if the day-of-month value is invalid,
1115      *  or if the day-of-month is invalid for the month-year
1116      */
1117     public LocalDate withDayOfMonth(int dayOfMonth) {
1118         if (this.day == dayOfMonth) {
1119             return this;
1120         }
1121         return of(year, month, dayOfMonth);
1122     }
1123 
1124     /**
1125      * Returns a copy of this {@code LocalDate} with the day-of-year altered.
1126      * <p>
1127      * If the resulting date is invalid, an exception is thrown.
1128      * <p>
1129      * This instance is immutable and unaffected by this method call.
1130      *
1131      * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
1132      * @return a {@code LocalDate} based on this date with the requested day, not null
1133      * @throws DateTimeException if the day-of-year value is invalid,
1134      *  or if the day-of-year is invalid for the year
1135      */
1136     public LocalDate withDayOfYear(int dayOfYear) {
1137         if (this.getDayOfYear() == dayOfYear) {
1138             return this;
1139         }
1140         return ofYearDay(year, dayOfYear);
1141     }
1142 
1143     //-----------------------------------------------------------------------
1144     /**
1145      * Returns a copy of this date with the specified amount added.
1146      * <p>
1147      * This returns a {@code LocalDate}, based on this one, with the specified amount added.
1148      * The amount is typically {@link Period} but may be any other type implementing
1149      * the {@link TemporalAmount} interface.
1150      * <p>
1151      * The calculation is delegated to the amount object by calling
1152      * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
1153      * to implement the addition in any way it wishes, however it typically
1154      * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
1155      * of the amount implementation to determine if it can be successfully added.
1156      * <p>
1157      * This instance is immutable and unaffected by this method call.
1158      *
1159      * @param amountToAdd  the amount to add, not null
1160      * @return a {@code LocalDate} based on this date with the addition made, not null
1161      * @throws DateTimeException if the addition cannot be made
1162      * @throws ArithmeticException if numeric overflow occurs
1163      */
1164     @Override
1165     public LocalDate plus(TemporalAmount amountToAdd) {
1166         if (amountToAdd instanceof Period) {
1167             Period periodToAdd = (Period) amountToAdd;
1168             return plusMonths(periodToAdd.toTotalMonths()).plusDays(periodToAdd.getDays());
1169         }
1170         Objects.requireNonNull(amountToAdd, "amountToAdd");
1171         return (LocalDate) amountToAdd.addTo(this);
1172     }
1173 
1174     /**
1175      * Returns a copy of this date with the specified amount added.
1176      * <p>
1177      * This returns a {@code LocalDate}, based on this one, with the amount
1178      * in terms of the unit added. If it is not possible to add the amount, because the
1179      * unit is not supported or for some other reason, an exception is thrown.
1180      * <p>
1181      * In some cases, adding the amount can cause the resulting date to become invalid.
1182      * For example, adding one month to 31st January would result in 31st February.
1183      * In cases like this, the unit is responsible for resolving the date.
1184      * Typically it will choose the previous valid date, which would be the last valid
1185      * day of February in this example.
1186      * <p>
1187      * If the field is a {@link ChronoUnit} then the addition is implemented here.
1188      * The supported fields behave as follows:
1189      * <ul>
1190      * <li>{@code DAYS} -
1191      *  Returns a {@code LocalDate} with the specified number of days added.
1192      *  This is equivalent to {@link #plusDays(long)}.
1193      * <li>{@code WEEKS} -
1194      *  Returns a {@code LocalDate} with the specified number of weeks added.
1195      *  This is equivalent to {@link #plusWeeks(long)} and uses a 7 day week.
1196      * <li>{@code MONTHS} -
1197      *  Returns a {@code LocalDate} with the specified number of months added.
1198      *  This is equivalent to {@link #plusMonths(long)}.
1199      *  The day-of-month will be unchanged unless it would be invalid for the new
1200      *  month and year. In that case, the day-of-month is adjusted to the maximum
1201      *  valid value for the new month and year.
1202      * <li>{@code YEARS} -
1203      *  Returns a {@code LocalDate} with the specified number of years added.
1204      *  This is equivalent to {@link #plusYears(long)}.
1205      *  The day-of-month will be unchanged unless it would be invalid for the new
1206      *  month and year. In that case, the day-of-month is adjusted to the maximum
1207      *  valid value for the new month and year.
1208      * <li>{@code DECADES} -
1209      *  Returns a {@code LocalDate} with the specified number of decades added.
1210      *  This is equivalent to calling {@link #plusYears(long)} with the amount
1211      *  multiplied by 10.
1212      *  The day-of-month will be unchanged unless it would be invalid for the new
1213      *  month and year. In that case, the day-of-month is adjusted to the maximum
1214      *  valid value for the new month and year.
1215      * <li>{@code CENTURIES} -
1216      *  Returns a {@code LocalDate} with the specified number of centuries added.
1217      *  This is equivalent to calling {@link #plusYears(long)} with the amount
1218      *  multiplied by 100.
1219      *  The day-of-month will be unchanged unless it would be invalid for the new
1220      *  month and year. In that case, the day-of-month is adjusted to the maximum
1221      *  valid value for the new month and year.
1222      * <li>{@code MILLENNIA} -
1223      *  Returns a {@code LocalDate} with the specified number of millennia added.
1224      *  This is equivalent to calling {@link #plusYears(long)} with the amount
1225      *  multiplied by 1,000.
1226      *  The day-of-month will be unchanged unless it would be invalid for the new
1227      *  month and year. In that case, the day-of-month is adjusted to the maximum
1228      *  valid value for the new month and year.
1229      * <li>{@code ERAS} -
1230      *  Returns a {@code LocalDate} with the specified number of eras added.
1231      *  Only two eras are supported so the amount must be one, zero or minus one.
1232      *  If the amount is non-zero then the year is changed such that the year-of-era
1233      *  is unchanged.
1234      *  The day-of-month will be unchanged unless it would be invalid for the new
1235      *  month and year. In that case, the day-of-month is adjusted to the maximum
1236      *  valid value for the new month and year.
1237      * </ul>
1238      * <p>
1239      * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}.
1240      * <p>
1241      * If the field is not a {@code ChronoUnit}, then the result of this method
1242      * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
1243      * passing {@code this} as the argument. In this case, the unit determines
1244      * whether and how to perform the addition.
1245      * <p>
1246      * This instance is immutable and unaffected by this method call.
1247      *
1248      * @param amountToAdd  the amount of the unit to add to the result, may be negative
1249      * @param unit  the unit of the amount to add, not null
1250      * @return a {@code LocalDate} based on this date with the specified amount added, not null
1251      * @throws DateTimeException if the addition cannot be made
1252      * @throws UnsupportedTemporalTypeException if the unit is not supported
1253      * @throws ArithmeticException if numeric overflow occurs
1254      */
1255     @Override
1256     public LocalDate plus(long amountToAdd, TemporalUnit unit) {
1257         if (unit instanceof ChronoUnit) {
1258             ChronoUnit f = (ChronoUnit) unit;
1259             switch (f) {
1260                 case DAYS: return plusDays(amountToAdd);
1261                 case WEEKS: return plusWeeks(amountToAdd);
1262                 case MONTHS: return plusMonths(amountToAdd);
1263                 case YEARS: return plusYears(amountToAdd);
1264                 case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10));
1265                 case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100));
1266                 case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
1267                 case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
1268             }
1269             throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
1270         }
1271         return unit.addTo(this, amountToAdd);
1272     }
1273 
1274     //-----------------------------------------------------------------------
1275     /**
1276      * Returns a copy of this {@code LocalDate} with the specified number of years added.
1277      * <p>
1278      * This method adds the specified amount to the years field in three steps:
1279      * <ol>
1280      * <li>Add the input years to the year field</li>
1281      * <li>Check if the resulting date would be invalid</li>
1282      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1283      * </ol>
1284      * <p>
1285      * For example, 2008-02-29 (leap year) plus one year would result in the
1286      * invalid date 2009-02-29 (standard year). Instead of returning an invalid
1287      * result, the last valid day of the month, 2009-02-28, is selected instead.
1288      * <p>
1289      * This instance is immutable and unaffected by this method call.
1290      *
1291      * @param yearsToAdd  the years to add, may be negative
1292      * @return a {@code LocalDate} based on this date with the years added, not null
1293      * @throws DateTimeException if the result exceeds the supported date range
1294      */
1295     public LocalDate plusYears(long yearsToAdd) {
1296         if (yearsToAdd == 0) {
1297             return this;
1298         }
1299         int newYear = YEAR.checkValidIntValue(year + yearsToAdd);  // safe overflow
1300         return resolvePreviousValid(newYear, month, day);
1301     }
1302 
1303     /**
1304      * Returns a copy of this {@code LocalDate} with the specified number of months added.
1305      * <p>
1306      * This method adds the specified amount to the months field in three steps:
1307      * <ol>
1308      * <li>Add the input months to the month-of-year field</li>
1309      * <li>Check if the resulting date would be invalid</li>
1310      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1311      * </ol>
1312      * <p>
1313      * For example, 2007-03-31 plus one month would result in the invalid date
1314      * 2007-04-31. Instead of returning an invalid result, the last valid day
1315      * of the month, 2007-04-30, is selected instead.
1316      * <p>
1317      * This instance is immutable and unaffected by this method call.
1318      *
1319      * @param monthsToAdd  the months to add, may be negative
1320      * @return a {@code LocalDate} based on this date with the months added, not null
1321      * @throws DateTimeException if the result exceeds the supported date range
1322      */
1323     public LocalDate plusMonths(long monthsToAdd) {
1324         if (monthsToAdd == 0) {
1325             return this;
1326         }
1327         long monthCount = year * 12L + (month - 1);
1328         long calcMonths = monthCount + monthsToAdd;  // safe overflow
1329         int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcMonths, 12));
1330         int newMonth = (int)Math.floorMod(calcMonths, 12) + 1;
1331         return resolvePreviousValid(newYear, newMonth, day);
1332     }
1333 
1334     /**
1335      * Returns a copy of this {@code LocalDate} with the specified number of weeks added.
1336      * <p>
1337      * This method adds the specified amount in weeks to the days field incrementing
1338      * the month and year fields as necessary to ensure the result remains valid.
1339      * The result is only invalid if the maximum/minimum year is exceeded.
1340      * <p>
1341      * For example, 2008-12-31 plus one week would result in 2009-01-07.
1342      * <p>
1343      * This instance is immutable and unaffected by this method call.
1344      *
1345      * @param weeksToAdd  the weeks to add, may be negative
1346      * @return a {@code LocalDate} based on this date with the weeks added, not null
1347      * @throws DateTimeException if the result exceeds the supported date range
1348      */
1349     public LocalDate plusWeeks(long weeksToAdd) {
1350         return plusDays(Math.multiplyExact(weeksToAdd, 7));
1351     }
1352 
1353     /**
1354      * Returns a copy of this {@code LocalDate} with the specified number of days added.
1355      * <p>
1356      * This method adds the specified amount to the days field incrementing the
1357      * month and year fields as necessary to ensure the result remains valid.
1358      * The result is only invalid if the maximum/minimum year is exceeded.
1359      * <p>
1360      * For example, 2008-12-31 plus one day would result in 2009-01-01.
1361      * <p>
1362      * This instance is immutable and unaffected by this method call.
1363      *
1364      * @param daysToAdd  the days to add, may be negative
1365      * @return a {@code LocalDate} based on this date with the days added, not null
1366      * @throws DateTimeException if the result exceeds the supported date range
1367      */
1368     public LocalDate plusDays(long daysToAdd) {
1369         if (daysToAdd == 0) {
1370             return this;
1371         }
1372         long dom = day + daysToAdd;
1373         if (dom > 0 && dom <= 28) {
1374             return LocalDate.of(year, month, (int) dom);
1375         } else if (dom > 28 && dom <= 59) { // 59th Jan is 28th Feb, 59th Feb is 31st Mar
1376             long monthLen = lengthOfMonth();
1377             if (dom <= monthLen) {
1378                 return LocalDate.of(year, month, (int) dom);
1379             } else if (month < 12) {
1380                 return LocalDate.of(year, month + 1, (int) (dom - monthLen));
1381             } else {
1382                 return LocalDate.of(year + 1, 1, (int) (dom - monthLen));
1383             }
1384         }
1385 
1386         long mjDay = Math.addExact(toEpochDay(), daysToAdd);
1387         return LocalDate.ofEpochDay(mjDay);
1388     }
1389 
1390     //-----------------------------------------------------------------------
1391     /**
1392      * Returns a copy of this date with the specified amount subtracted.
1393      * <p>
1394      * This returns a {@code LocalDate}, based on this one, with the specified amount subtracted.
1395      * The amount is typically {@link Period} but may be any other type implementing
1396      * the {@link TemporalAmount} interface.
1397      * <p>
1398      * The calculation is delegated to the amount object by calling
1399      * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
1400      * to implement the subtraction in any way it wishes, however it typically
1401      * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
1402      * of the amount implementation to determine if it can be successfully subtracted.
1403      * <p>
1404      * This instance is immutable and unaffected by this method call.
1405      *
1406      * @param amountToSubtract  the amount to subtract, not null
1407      * @return a {@code LocalDate} based on this date with the subtraction made, not null
1408      * @throws DateTimeException if the subtraction cannot be made
1409      * @throws ArithmeticException if numeric overflow occurs
1410      */
1411     @Override
1412     public LocalDate minus(TemporalAmount amountToSubtract) {
1413         if (amountToSubtract instanceof Period) {
1414             Period periodToSubtract = (Period) amountToSubtract;
1415             return minusMonths(periodToSubtract.toTotalMonths()).minusDays(periodToSubtract.getDays());
1416         }
1417         Objects.requireNonNull(amountToSubtract, "amountToSubtract");
1418         return (LocalDate) amountToSubtract.subtractFrom(this);
1419     }
1420 
1421     /**
1422      * Returns a copy of this date with the specified amount subtracted.
1423      * <p>
1424      * This returns a {@code LocalDate}, based on this one, with the amount
1425      * in terms of the unit subtracted. If it is not possible to subtract the amount,
1426      * because the unit is not supported or for some other reason, an exception is thrown.
1427      * <p>
1428      * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
1429      * See that method for a full description of how addition, and thus subtraction, works.
1430      * <p>
1431      * This instance is immutable and unaffected by this method call.
1432      *
1433      * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
1434      * @param unit  the unit of the amount to subtract, not null
1435      * @return a {@code LocalDate} based on this date with the specified amount subtracted, not null
1436      * @throws DateTimeException if the subtraction cannot be made
1437      * @throws UnsupportedTemporalTypeException if the unit is not supported
1438      * @throws ArithmeticException if numeric overflow occurs
1439      */
1440     @Override
1441     public LocalDate minus(long amountToSubtract, TemporalUnit unit) {
1442         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
1443     }
1444 
1445     //-----------------------------------------------------------------------
1446     /**
1447      * Returns a copy of this {@code LocalDate} with the specified number of years subtracted.
1448      * <p>
1449      * This method subtracts the specified amount from the years field in three steps:
1450      * <ol>
1451      * <li>Subtract the input years from the year field</li>
1452      * <li>Check if the resulting date would be invalid</li>
1453      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1454      * </ol>
1455      * <p>
1456      * For example, 2008-02-29 (leap year) minus one year would result in the
1457      * invalid date 2007-02-29 (standard year). Instead of returning an invalid
1458      * result, the last valid day of the month, 2007-02-28, is selected instead.
1459      * <p>
1460      * This instance is immutable and unaffected by this method call.
1461      *
1462      * @param yearsToSubtract  the years to subtract, may be negative
1463      * @return a {@code LocalDate} based on this date with the years subtracted, not null
1464      * @throws DateTimeException if the result exceeds the supported date range
1465      */
1466     public LocalDate minusYears(long yearsToSubtract) {
1467         return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract));
1468     }
1469 
1470     /**
1471      * Returns a copy of this {@code LocalDate} with the specified number of months subtracted.
1472      * <p>
1473      * This method subtracts the specified amount from the months field in three steps:
1474      * <ol>
1475      * <li>Subtract the input months from the month-of-year field</li>
1476      * <li>Check if the resulting date would be invalid</li>
1477      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1478      * </ol>
1479      * <p>
1480      * For example, 2007-03-31 minus one month would result in the invalid date
1481      * 2007-02-31. Instead of returning an invalid result, the last valid day
1482      * of the month, 2007-02-28, is selected instead.
1483      * <p>
1484      * This instance is immutable and unaffected by this method call.
1485      *
1486      * @param monthsToSubtract  the months to subtract, may be negative
1487      * @return a {@code LocalDate} based on this date with the months subtracted, not null
1488      * @throws DateTimeException if the result exceeds the supported date range
1489      */
1490     public LocalDate minusMonths(long monthsToSubtract) {
1491         return (monthsToSubtract == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-monthsToSubtract));
1492     }
1493 
1494     /**
1495      * Returns a copy of this {@code LocalDate} with the specified number of weeks subtracted.
1496      * <p>
1497      * This method subtracts the specified amount in weeks from the days field decrementing
1498      * the month and year fields as necessary to ensure the result remains valid.
1499      * The result is only invalid if the maximum/minimum year is exceeded.
1500      * <p>
1501      * For example, 2009-01-07 minus one week would result in 2008-12-31.
1502      * <p>
1503      * This instance is immutable and unaffected by this method call.
1504      *
1505      * @param weeksToSubtract  the weeks to subtract, may be negative
1506      * @return a {@code LocalDate} based on this date with the weeks subtracted, not null
1507      * @throws DateTimeException if the result exceeds the supported date range
1508      */
1509     public LocalDate minusWeeks(long weeksToSubtract) {
1510         return (weeksToSubtract == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeksToSubtract));
1511     }
1512 
1513     /**
1514      * Returns a copy of this {@code LocalDate} with the specified number of days subtracted.
1515      * <p>
1516      * This method subtracts the specified amount from the days field decrementing the
1517      * month and year fields as necessary to ensure the result remains valid.
1518      * The result is only invalid if the maximum/minimum year is exceeded.
1519      * <p>
1520      * For example, 2009-01-01 minus one day would result in 2008-12-31.
1521      * <p>
1522      * This instance is immutable and unaffected by this method call.
1523      *
1524      * @param daysToSubtract  the days to subtract, may be negative
1525      * @return a {@code LocalDate} based on this date with the days subtracted, not null
1526      * @throws DateTimeException if the result exceeds the supported date range
1527      */
1528     public LocalDate minusDays(long daysToSubtract) {
1529         return (daysToSubtract == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-daysToSubtract));
1530     }
1531 
1532     //-----------------------------------------------------------------------
1533     /**
1534      * Queries this date using the specified query.
1535      * <p>
1536      * This queries this date using the specified query strategy object.
1537      * The {@code TemporalQuery} object defines the logic to be used to
1538      * obtain the result. Read the documentation of the query to understand
1539      * what the result of this method will be.
1540      * <p>
1541      * The result of this method is obtained by invoking the
1542      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
1543      * specified query passing {@code this} as the argument.
1544      *
1545      * @param <R> the type of the result
1546      * @param query  the query to invoke, not null
1547      * @return the query result, null may be returned (defined by the query)
1548      * @throws DateTimeException if unable to query (defined by the query)
1549      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
1550      */
1551     @SuppressWarnings("unchecked")
1552     @Override
1553     public <R> R query(TemporalQuery<R> query) {
1554         if (query == TemporalQueries.localDate()) {
1555             return (R) this;
1556         }
1557         return ChronoLocalDate.super.query(query);
1558     }
1559 
1560     /**
1561      * Adjusts the specified temporal object to have the same date as this object.
1562      * <p>
1563      * This returns a temporal object of the same observable type as the input
1564      * with the date changed to be the same as this.
1565      * <p>
1566      * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
1567      * passing {@link ChronoField#EPOCH_DAY} as the field.
1568      * <p>
1569      * In most cases, it is clearer to reverse the calling pattern by using
1570      * {@link Temporal#with(TemporalAdjuster)}:
1571      * <pre>
1572      *   // these two lines are equivalent, but the second approach is recommended
1573      *   temporal = thisLocalDate.adjustInto(temporal);
1574      *   temporal = temporal.with(thisLocalDate);
1575      * </pre>
1576      * <p>
1577      * This instance is immutable and unaffected by this method call.
1578      *
1579      * @param temporal  the target object to be adjusted, not null
1580      * @return the adjusted object, not null
1581      * @throws DateTimeException if unable to make the adjustment
1582      * @throws ArithmeticException if numeric overflow occurs
1583      */
1584     @Override  // override for Javadoc
1585     public Temporal adjustInto(Temporal temporal) {
1586         return ChronoLocalDate.super.adjustInto(temporal);
1587     }
1588 
1589     /**
1590      * Calculates the amount of time until another date in terms of the specified unit.
1591      * <p>
1592      * This calculates the amount of time between two {@code LocalDate}
1593      * objects in terms of a single {@code TemporalUnit}.
1594      * The start and end points are {@code this} and the specified date.
1595      * The result will be negative if the end is before the start.
1596      * The {@code Temporal} passed to this method is converted to a
1597      * {@code LocalDate} using {@link #from(TemporalAccessor)}.
1598      * For example, the amount in days between two dates can be calculated
1599      * using {@code startDate.until(endDate, DAYS)}.
1600      * <p>
1601      * The calculation returns a whole number, representing the number of
1602      * complete units between the two dates.
1603      * For example, the amount in months between 2012-06-15 and 2012-08-14
1604      * will only be one month as it is one day short of two months.
1605      * <p>
1606      * There are two equivalent ways of using this method.
1607      * The first is to invoke this method.
1608      * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
1609      * <pre>
1610      *   // these two lines are equivalent
1611      *   amount = start.until(end, MONTHS);
1612      *   amount = MONTHS.between(start, end);
1613      * </pre>
1614      * The choice should be made based on which makes the code more readable.
1615      * <p>
1616      * The calculation is implemented in this method for {@link ChronoUnit}.
1617      * The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS},
1618      * {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS}
1619      * are supported. Other {@code ChronoUnit} values will throw an exception.
1620      * <p>
1621      * If the unit is not a {@code ChronoUnit}, then the result of this method
1622      * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
1623      * passing {@code this} as the first argument and the converted input temporal
1624      * as the second argument.
1625      * <p>
1626      * This instance is immutable and unaffected by this method call.
1627      *
1628      * @param endExclusive  the end date, exclusive, which is converted to a {@code LocalDate}, not null
1629      * @param unit  the unit to measure the amount in, not null
1630      * @return the amount of time between this date and the end date
1631      * @throws DateTimeException if the amount cannot be calculated, or the end
1632      *  temporal cannot be converted to a {@code LocalDate}
1633      * @throws UnsupportedTemporalTypeException if the unit is not supported
1634      * @throws ArithmeticException if numeric overflow occurs
1635      */
1636     @Override
1637     public long until(Temporal endExclusive, TemporalUnit unit) {
1638         LocalDate end = LocalDate.from(endExclusive);
1639         if (unit instanceof ChronoUnit) {
1640             switch ((ChronoUnit) unit) {
1641                 case DAYS: return daysUntil(end);
1642                 case WEEKS: return daysUntil(end) / 7;
1643                 case MONTHS: return monthsUntil(end);
1644                 case YEARS: return monthsUntil(end) / 12;
1645                 case DECADES: return monthsUntil(end) / 120;
1646                 case CENTURIES: return monthsUntil(end) / 1200;
1647                 case MILLENNIA: return monthsUntil(end) / 12000;
1648                 case ERAS: return end.getLong(ERA) - getLong(ERA);
1649             }
1650             throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
1651         }
1652         return unit.between(this, end);
1653     }
1654 
1655     long daysUntil(LocalDate end) {
1656         return end.toEpochDay() - toEpochDay();  // no overflow
1657     }
1658 
1659     private long monthsUntil(LocalDate end) {
1660         long packed1 = getProlepticMonth() * 32L + getDayOfMonth();  // no overflow
1661         long packed2 = end.getProlepticMonth() * 32L + end.getDayOfMonth();  // no overflow
1662         return (packed2 - packed1) / 32;
1663     }
1664 
1665     /**
1666      * Calculates the period between this date and another date as a {@code Period}.
1667      * <p>
1668      * This calculates the period between two dates in terms of years, months and days.
1669      * The start and end points are {@code this} and the specified date.
1670      * The result will be negative if the end is before the start.
1671      * The negative sign will be the same in each of year, month and day.
1672      * <p>
1673      * The calculation is performed using the ISO calendar system.
1674      * If necessary, the input date will be converted to ISO.
1675      * <p>
1676      * The start date is included, but the end date is not.
1677      * The period is calculated by removing complete months, then calculating
1678      * the remaining number of days, adjusting to ensure that both have the same sign.
1679      * The number of months is then normalized into years and months based on a 12 month year.
1680      * A month is considered to be complete if the end day-of-month is greater
1681      * than or equal to the start day-of-month.
1682      * For example, from {@code 2010-01-15} to {@code 2011-03-18} is "1 year, 2 months and 3 days".
1683      * <p>
1684      * There are two equivalent ways of using this method.
1685      * The first is to invoke this method.
1686      * The second is to use {@link Period#between(LocalDate, LocalDate)}:
1687      * <pre>
1688      *   // these two lines are equivalent
1689      *   period = start.until(end);
1690      *   period = Period.between(start, end);
1691      * </pre>
1692      * The choice should be made based on which makes the code more readable.
1693      *
1694      * @param endDateExclusive  the end date, exclusive, which may be in any chronology, not null
1695      * @return the period between this date and the end date, not null
1696      */
1697     @Override
1698     public Period until(ChronoLocalDate endDateExclusive) {
1699         LocalDate end = LocalDate.from(endDateExclusive);
1700         long totalMonths = end.getProlepticMonth() - this.getProlepticMonth();  // safe
1701         int days = end.day - this.day;
1702         if (totalMonths > 0 && days < 0) {
1703             totalMonths--;
1704             LocalDate calcDate = this.plusMonths(totalMonths);
1705             days = (int) (end.toEpochDay() - calcDate.toEpochDay());  // safe
1706         } else if (totalMonths < 0 && days > 0) {
1707             totalMonths++;
1708             days -= end.lengthOfMonth();
1709         }
1710         long years = totalMonths / 12;  // safe
1711         int months = (int) (totalMonths % 12);  // safe
1712         return Period.of(Math.toIntExact(years), months, days);
1713     }
1714 
1715     /**
1716      * Formats this date using the specified formatter.
1717      * <p>
1718      * This date will be passed to the formatter to produce a string.
1719      *
1720      * @param formatter  the formatter to use, not null
1721      * @return the formatted date string, not null
1722      * @throws DateTimeException if an error occurs during printing
1723      */
1724     @Override  // override for Javadoc and performance
1725     public String format(DateTimeFormatter formatter) {
1726         Objects.requireNonNull(formatter, "formatter");
1727         return formatter.format(this);
1728     }
1729 
1730     //-----------------------------------------------------------------------
1731     /**
1732      * Combines this date with a time to create a {@code LocalDateTime}.
1733      * <p>
1734      * This returns a {@code LocalDateTime} formed from this date at the specified time.
1735      * All possible combinations of date and time are valid.
1736      *
1737      * @param time  the time to combine with, not null
1738      * @return the local date-time formed from this date and the specified time, not null
1739      */
1740     @Override
1741     public LocalDateTime atTime(LocalTime time) {
1742         return LocalDateTime.of(this, time);
1743     }
1744 
1745     /**
1746      * Combines this date with a time to create a {@code LocalDateTime}.
1747      * <p>
1748      * This returns a {@code LocalDateTime} formed from this date at the
1749      * specified hour and minute.
1750      * The seconds and nanosecond fields will be set to zero.
1751      * The individual time fields must be within their valid range.
1752      * All possible combinations of date and time are valid.
1753      *
1754      * @param hour  the hour-of-day to use, from 0 to 23
1755      * @param minute  the minute-of-hour to use, from 0 to 59
1756      * @return the local date-time formed from this date and the specified time, not null
1757      * @throws DateTimeException if the value of any field is out of range
1758      */
1759     public LocalDateTime atTime(int hour, int minute) {
1760         return atTime(LocalTime.of(hour, minute));
1761     }
1762 
1763     /**
1764      * Combines this date with a time to create a {@code LocalDateTime}.
1765      * <p>
1766      * This returns a {@code LocalDateTime} formed from this date at the
1767      * specified hour, minute and second.
1768      * The nanosecond field will be set to zero.
1769      * The individual time fields must be within their valid range.
1770      * All possible combinations of date and time are valid.
1771      *
1772      * @param hour  the hour-of-day to use, from 0 to 23
1773      * @param minute  the minute-of-hour to use, from 0 to 59
1774      * @param second  the second-of-minute to represent, from 0 to 59
1775      * @return the local date-time formed from this date and the specified time, not null
1776      * @throws DateTimeException if the value of any field is out of range
1777      */
1778     public LocalDateTime atTime(int hour, int minute, int second) {
1779         return atTime(LocalTime.of(hour, minute, second));
1780     }
1781 
1782     /**
1783      * Combines this date with a time to create a {@code LocalDateTime}.
1784      * <p>
1785      * This returns a {@code LocalDateTime} formed from this date at the
1786      * specified hour, minute, second and nanosecond.
1787      * The individual time fields must be within their valid range.
1788      * All possible combinations of date and time are valid.
1789      *
1790      * @param hour  the hour-of-day to use, from 0 to 23
1791      * @param minute  the minute-of-hour to use, from 0 to 59
1792      * @param second  the second-of-minute to represent, from 0 to 59
1793      * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
1794      * @return the local date-time formed from this date and the specified time, not null
1795      * @throws DateTimeException if the value of any field is out of range
1796      */
1797     public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond) {
1798         return atTime(LocalTime.of(hour, minute, second, nanoOfSecond));
1799     }
1800 
1801     /**
1802      * Combines this date with an offset time to create an {@code OffsetDateTime}.
1803      * <p>
1804      * This returns an {@code OffsetDateTime} formed from this date at the specified time.
1805      * All possible combinations of date and time are valid.
1806      *
1807      * @param time  the time to combine with, not null
1808      * @return the offset date-time formed from this date and the specified time, not null
1809      */
1810     public OffsetDateTime atTime(OffsetTime time) {
1811         return OffsetDateTime.of(LocalDateTime.of(this, time.toLocalTime()), time.getOffset());
1812     }
1813 
1814     /**
1815      * Combines this date with the time of midnight to create a {@code LocalDateTime}
1816      * at the start of this date.
1817      * <p>
1818      * This returns a {@code LocalDateTime} formed from this date at the time of
1819      * midnight, 00:00, at the start of this date.
1820      *
1821      * @return the local date-time of midnight at the start of this date, not null
1822      */
1823     public LocalDateTime atStartOfDay() {
1824         return LocalDateTime.of(this, LocalTime.MIDNIGHT);
1825     }
1826 
1827     /**
1828      * Returns a zoned date-time from this date at the earliest valid time according
1829      * to the rules in the time-zone.
1830      * <p>
1831      * Time-zone rules, such as daylight savings, mean that not every local date-time
1832      * is valid for the specified zone, thus the local date-time may not be midnight.
1833      * <p>
1834      * In most cases, there is only one valid offset for a local date-time.
1835      * In the case of an overlap, there are two valid offsets, and the earlier one is used,
1836      * corresponding to the first occurrence of midnight on the date.
1837      * In the case of a gap, the zoned date-time will represent the instant just after the gap.
1838      * <p>
1839      * If the zone ID is a {@link ZoneOffset}, then the result always has a time of midnight.
1840      * <p>
1841      * To convert to a specific time in a given time-zone call {@link #atTime(LocalTime)}
1842      * followed by {@link LocalDateTime#atZone(ZoneId)}.
1843      *
1844      * @param zone  the zone ID to use, not null
1845      * @return the zoned date-time formed from this date and the earliest valid time for the zone, not null
1846      */
1847     public ZonedDateTime atStartOfDay(ZoneId zone) {
1848         Objects.requireNonNull(zone, "zone");
1849         // need to handle case where there is a gap from 11:30 to 00:30
1850         // standard ZDT factory would result in 01:00 rather than 00:30
1851         LocalDateTime ldt = atTime(LocalTime.MIDNIGHT);
1852         if (zone instanceof ZoneOffset == false) {
1853             ZoneRules rules = zone.getRules();
1854             ZoneOffsetTransition trans = rules.getTransition(ldt);
1855             if (trans != null && trans.isGap()) {
1856                 ldt = trans.getDateTimeAfter();
1857             }
1858         }
1859         return ZonedDateTime.of(ldt, zone);
1860     }
1861 
1862     //-----------------------------------------------------------------------
1863     @Override
1864     public long toEpochDay() {
1865         long y = year;
1866         long m = month;
1867         long total = 0;
1868         total += 365 * y;
1869         if (y >= 0) {
1870             total += (y + 3) / 4 - (y + 99) / 100 + (y + 399) / 400;
1871         } else {
1872             total -= y / -4 - y / -100 + y / -400;
1873         }
1874         total += ((367 * m - 362) / 12);
1875         total += day - 1;
1876         if (m > 2) {
1877             total--;
1878             if (isLeapYear() == false) {
1879                 total--;
1880             }
1881         }
1882         return total - DAYS_0000_TO_1970;
1883     }
1884 
1885     /**
1886      * Converts this {@code LocalDate} to the number of seconds since the epoch
1887      * of 1970-01-01T00:00:00Z.
1888      * <p>
1889      * This combines this local date with the specified time and
1890      * offset to calculate the epoch-second value, which is the
1891      * number of elapsed seconds from 1970-01-01T00:00:00Z.
1892      * Instants on the time-line after the epoch are positive, earlier
1893      * are negative.
1894      *
1895      * @param time the local time, not null
1896      * @param offset the zone offset, not null
1897      * @return the number of seconds since the epoch of 1970-01-01T00:00:00Z, may be negative
1898      * @since 9
1899      */
1900     public long toEpochSecond(LocalTime time, ZoneOffset offset) {
1901         Objects.requireNonNull(time, "time");
1902         Objects.requireNonNull(offset, "offset");
1903         long secs = toEpochDay() * SECONDS_PER_DAY + time.toSecondOfDay();
1904         secs -= offset.getTotalSeconds();
1905         return secs;
1906     }
1907 
1908     //-----------------------------------------------------------------------
1909     /**
1910      * Compares this date to another date.
1911      * <p>
1912      * The comparison is primarily based on the date, from earliest to latest.
1913      * It is "consistent with equals", as defined by {@link Comparable}.
1914      * <p>
1915      * If all the dates being compared are instances of {@code LocalDate},
1916      * then the comparison will be entirely based on the date.
1917      * If some dates being compared are in different chronologies, then the
1918      * chronology is also considered, see {@link java.time.chrono.ChronoLocalDate#compareTo}.
1919      *
1920      * @param other  the other date to compare to, not null
1921      * @return the comparator value, negative if less, positive if greater
1922      */
1923     @Override  // override for Javadoc and performance
1924     public int compareTo(ChronoLocalDate other) {
1925         if (other instanceof LocalDate) {
1926             return compareTo0((LocalDate) other);
1927         }
1928         return ChronoLocalDate.super.compareTo(other);
1929     }
1930 
1931     int compareTo0(LocalDate otherDate) {
1932         int cmp = (year - otherDate.year);
1933         if (cmp == 0) {
1934             cmp = (month - otherDate.month);
1935             if (cmp == 0) {
1936                 cmp = (day - otherDate.day);
1937             }
1938         }
1939         return cmp;
1940     }
1941 
1942     /**
1943      * Checks if this date is after the specified date.
1944      * <p>
1945      * This checks to see if this date represents a point on the
1946      * local time-line after the other date.
1947      * <pre>
1948      *   LocalDate a = LocalDate.of(2012, 6, 30);
1949      *   LocalDate b = LocalDate.of(2012, 7, 1);
1950      *   a.isAfter(b) == false
1951      *   a.isAfter(a) == false
1952      *   b.isAfter(a) == true
1953      * </pre>
1954      * <p>
1955      * This method only considers the position of the two dates on the local time-line.
1956      * It does not take into account the chronology, or calendar system.
1957      * This is different from the comparison in {@link #compareTo(ChronoLocalDate)},
1958      * but is the same approach as {@link ChronoLocalDate#timeLineOrder()}.
1959      *
1960      * @param other  the other date to compare to, not null
1961      * @return true if this date is after the specified date
1962      */
1963     @Override  // override for Javadoc and performance
1964     public boolean isAfter(ChronoLocalDate other) {
1965         if (other instanceof LocalDate) {
1966             return compareTo0((LocalDate) other) > 0;
1967         }
1968         return ChronoLocalDate.super.isAfter(other);
1969     }
1970 
1971     /**
1972      * Checks if this date is before the specified date.
1973      * <p>
1974      * This checks to see if this date represents a point on the
1975      * local time-line before the other date.
1976      * <pre>
1977      *   LocalDate a = LocalDate.of(2012, 6, 30);
1978      *   LocalDate b = LocalDate.of(2012, 7, 1);
1979      *   a.isBefore(b) == true
1980      *   a.isBefore(a) == false
1981      *   b.isBefore(a) == false
1982      * </pre>
1983      * <p>
1984      * This method only considers the position of the two dates on the local time-line.
1985      * It does not take into account the chronology, or calendar system.
1986      * This is different from the comparison in {@link #compareTo(ChronoLocalDate)},
1987      * but is the same approach as {@link ChronoLocalDate#timeLineOrder()}.
1988      *
1989      * @param other  the other date to compare to, not null
1990      * @return true if this date is before the specified date
1991      */
1992     @Override  // override for Javadoc and performance
1993     public boolean isBefore(ChronoLocalDate other) {
1994         if (other instanceof LocalDate) {
1995             return compareTo0((LocalDate) other) < 0;
1996         }
1997         return ChronoLocalDate.super.isBefore(other);
1998     }
1999 
2000     /**
2001      * Checks if this date is equal to the specified date.
2002      * <p>
2003      * This checks to see if this date represents the same point on the
2004      * local time-line as the other date.
2005      * <pre>
2006      *   LocalDate a = LocalDate.of(2012, 6, 30);
2007      *   LocalDate b = LocalDate.of(2012, 7, 1);
2008      *   a.isEqual(b) == false
2009      *   a.isEqual(a) == true
2010      *   b.isEqual(a) == false
2011      * </pre>
2012      * <p>
2013      * This method only considers the position of the two dates on the local time-line.
2014      * It does not take into account the chronology, or calendar system.
2015      * This is different from the comparison in {@link #compareTo(ChronoLocalDate)}
2016      * but is the same approach as {@link ChronoLocalDate#timeLineOrder()}.
2017      *
2018      * @param other  the other date to compare to, not null
2019      * @return true if this date is equal to the specified date
2020      */
2021     @Override  // override for Javadoc and performance
2022     public boolean isEqual(ChronoLocalDate other) {
2023         if (other instanceof LocalDate) {
2024             return compareTo0((LocalDate) other) == 0;
2025         }
2026         return ChronoLocalDate.super.isEqual(other);
2027     }
2028 
2029     //-----------------------------------------------------------------------
2030     /**
2031      * Checks if this date is equal to another date.
2032      * <p>
2033      * Compares this {@code LocalDate} with another ensuring that the date is the same.
2034      * <p>
2035      * Only objects of type {@code LocalDate} are compared, other types return false.
2036      * To compare the dates of two {@code TemporalAccessor} instances, including dates
2037      * in two different chronologies, use {@link ChronoField#EPOCH_DAY} as a comparator.
2038      *
2039      * @param obj  the object to check, null returns false
2040      * @return true if this is equal to the other date
2041      */
2042     @Override
2043     public boolean equals(Object obj) {
2044         if (this == obj) {
2045             return true;
2046         }
2047         if (obj instanceof LocalDate) {
2048             return compareTo0((LocalDate) obj) == 0;
2049         }
2050         return false;
2051     }
2052 
2053     /**
2054      * A hash code for this date.
2055      *
2056      * @return a suitable hash code
2057      */
2058     @Override
2059     public int hashCode() {
2060         int yearValue = year;
2061         int monthValue = month;
2062         int dayValue = day;
2063         return (yearValue & 0xFFFFF800) ^ ((yearValue << 11) + (monthValue << 6) + (dayValue));
2064     }
2065 
2066     //-----------------------------------------------------------------------
2067     /**
2068      * Outputs this date as a {@code String}, such as {@code 2007-12-03}.
2069      * <p>
2070      * The output will be in the ISO-8601 format {@code uuuu-MM-dd}.
2071      *
2072      * @return a string representation of this date, not null
2073      */
2074     @Override
2075     public String toString() {
2076         int yearValue = year;
2077         int monthValue = month;
2078         int dayValue = day;
2079         int absYear = Math.abs(yearValue);
2080         StringBuilder buf = new StringBuilder(10);
2081         if (absYear < 1000) {
2082             if (yearValue < 0) {
2083                 buf.append(yearValue - 10000).deleteCharAt(1);
2084             } else {
2085                 buf.append(yearValue + 10000).deleteCharAt(0);
2086             }
2087         } else {
2088             if (yearValue > 9999) {
2089                 buf.append('+');
2090             }
2091             buf.append(yearValue);
2092         }
2093         return buf.append(monthValue < 10 ? "-0" : "-")
2094             .append(monthValue)
2095             .append(dayValue < 10 ? "-0" : "-")
2096             .append(dayValue)
2097             .toString();
2098     }
2099 
2100     //-----------------------------------------------------------------------
2101     /**
2102      * Writes the object using a
2103      * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
2104      * @serialData
2105      * <pre>
2106      *  out.writeByte(3);  // identifies a LocalDate
2107      *  out.writeInt(year);
2108      *  out.writeByte(month);
2109      *  out.writeByte(day);
2110      * </pre>
2111      *
2112      * @return the instance of {@code Ser}, not null
2113      */
2114     private Object writeReplace() {
2115         return new Ser(Ser.LOCAL_DATE_TYPE, this);
2116     }
2117 
2118     /**
2119      * Defend against malicious streams.
2120      *
2121      * @param s the stream to read
2122      * @throws InvalidObjectException always
2123      */
2124     private void readObject(ObjectInputStream s) throws InvalidObjectException {
2125         throw new InvalidObjectException("Deserialization via serialization delegate");
2126     }
2127 
2128     void writeExternal(DataOutput out) throws IOException {
2129         out.writeInt(year);
2130         out.writeByte(month);
2131         out.writeByte(day);
2132     }
2133 
2134     static LocalDate readExternal(DataInput in) throws IOException {
2135         int year = in.readInt();
2136         int month = in.readByte();
2137         int dayOfMonth = in.readByte();
2138         return LocalDate.of(year, month, dayOfMonth);
2139     }
2140 
2141 }