jdk/src/share/classes/java/time/LocalDate.java
changeset 15658 55b829ca2334
parent 15289 3ac550392e43
child 16852 60207b2b4b42
equal deleted inserted replaced
15657:c588664d547e 15658:55b829ca2334
    78 import java.io.DataOutput;
    78 import java.io.DataOutput;
    79 import java.io.IOException;
    79 import java.io.IOException;
    80 import java.io.InvalidObjectException;
    80 import java.io.InvalidObjectException;
    81 import java.io.ObjectStreamException;
    81 import java.io.ObjectStreamException;
    82 import java.io.Serializable;
    82 import java.io.Serializable;
    83 import java.time.format.DateTimeBuilder;
    83 import java.time.chrono.ChronoLocalDate;
       
    84 import java.time.chrono.Era;
       
    85 import java.time.chrono.IsoChronology;
    84 import java.time.format.DateTimeFormatter;
    86 import java.time.format.DateTimeFormatter;
    85 import java.time.format.DateTimeFormatters;
       
    86 import java.time.format.DateTimeParseException;
    87 import java.time.format.DateTimeParseException;
    87 import java.time.temporal.ChronoField;
    88 import java.time.temporal.ChronoField;
    88 import java.time.temporal.ChronoLocalDate;
       
    89 import java.time.temporal.ChronoUnit;
    89 import java.time.temporal.ChronoUnit;
    90 import java.time.temporal.Era;
    90 import java.time.temporal.Queries;
    91 import java.time.temporal.ISOChrono;
       
    92 import java.time.temporal.OffsetDate;
       
    93 import java.time.temporal.Temporal;
    91 import java.time.temporal.Temporal;
    94 import java.time.temporal.TemporalAccessor;
    92 import java.time.temporal.TemporalAccessor;
    95 import java.time.temporal.TemporalAdder;
       
    96 import java.time.temporal.TemporalAdjuster;
    93 import java.time.temporal.TemporalAdjuster;
       
    94 import java.time.temporal.TemporalAmount;
    97 import java.time.temporal.TemporalField;
    95 import java.time.temporal.TemporalField;
    98 import java.time.temporal.TemporalQuery;
    96 import java.time.temporal.TemporalQuery;
    99 import java.time.temporal.TemporalSubtractor;
       
   100 import java.time.temporal.TemporalUnit;
    97 import java.time.temporal.TemporalUnit;
   101 import java.time.temporal.ValueRange;
    98 import java.time.temporal.ValueRange;
   102 import java.time.temporal.Year;
       
   103 import java.time.zone.ZoneOffsetTransition;
    99 import java.time.zone.ZoneOffsetTransition;
   104 import java.time.zone.ZoneRules;
   100 import java.time.zone.ZoneRules;
   105 import java.util.Objects;
   101 import java.util.Objects;
   106 
   102 
   107 /**
   103 /**
   129  * This class is immutable and thread-safe.
   125  * This class is immutable and thread-safe.
   130  *
   126  *
   131  * @since 1.8
   127  * @since 1.8
   132  */
   128  */
   133 public final class LocalDate
   129 public final class LocalDate
   134         implements Temporal, TemporalAdjuster, ChronoLocalDate<ISOChrono>, Serializable {
   130         implements Temporal, TemporalAdjuster, ChronoLocalDate<LocalDate>, Serializable {
   135 
   131 
   136     /**
   132     /**
   137      * The minimum supported {@code LocalDate}, '-999999999-01-01'.
   133      * The minimum supported {@code LocalDate}, '-999999999-01-01'.
   138      * This could be used by an application as a "far past" date.
   134      * This could be used by an application as a "far past" date.
   139      */
   135      */
   214      * @param clock  the clock to use, not null
   210      * @param clock  the clock to use, not null
   215      * @return the current date, not null
   211      * @return the current date, not null
   216      */
   212      */
   217     public static LocalDate now(Clock clock) {
   213     public static LocalDate now(Clock clock) {
   218         Objects.requireNonNull(clock, "clock");
   214         Objects.requireNonNull(clock, "clock");
   219         // inline OffsetDate factory to avoid creating object and InstantProvider checks
   215         // inline to avoid creating object and Instant checks
   220         final Instant now = clock.instant();  // called once
   216         final Instant now = clock.instant();  // called once
   221         ZoneOffset offset = clock.getZone().getRules().getOffset(now);
   217         ZoneOffset offset = clock.getZone().getRules().getOffset(now);
   222         long epochSec = now.getEpochSecond() + offset.getTotalSeconds();  // overflow caught later
   218         long epochSec = now.getEpochSecond() + offset.getTotalSeconds();  // overflow caught later
   223         long epochDay = Math.floorDiv(epochSec, SECONDS_PER_DAY);
   219         long epochDay = Math.floorDiv(epochSec, SECONDS_PER_DAY);
   224         return LocalDate.ofEpochDay(epochDay);
   220         return LocalDate.ofEpochDay(epochDay);
   226 
   222 
   227     //-----------------------------------------------------------------------
   223     //-----------------------------------------------------------------------
   228     /**
   224     /**
   229      * Obtains an instance of {@code LocalDate} from a year, month and day.
   225      * Obtains an instance of {@code LocalDate} from a year, month and day.
   230      * <p>
   226      * <p>
       
   227      * This returns a {@code LocalDate} with the specified year, month and day-of-month.
   231      * The day must be valid for the year and month, otherwise an exception will be thrown.
   228      * The day must be valid for the year and month, otherwise an exception will be thrown.
   232      *
   229      *
   233      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
   230      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
   234      * @param month  the month-of-year to represent, not null
   231      * @param month  the month-of-year to represent, not null
   235      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
   232      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
   236      * @return the local date, not null
   233      * @return the local date, not null
   237      * @throws DateTimeException if the value of any field is out of range
   234      * @throws DateTimeException if the value of any field is out of range,
   238      * @throws DateTimeException if the day-of-month is invalid for the month-year
   235      *  or if the day-of-month is invalid for the month-year
   239      */
   236      */
   240     public static LocalDate of(int year, Month month, int dayOfMonth) {
   237     public static LocalDate of(int year, Month month, int dayOfMonth) {
   241         YEAR.checkValidValue(year);
   238         YEAR.checkValidValue(year);
   242         Objects.requireNonNull(month, "month");
   239         Objects.requireNonNull(month, "month");
   243         DAY_OF_MONTH.checkValidValue(dayOfMonth);
   240         DAY_OF_MONTH.checkValidValue(dayOfMonth);
   245     }
   242     }
   246 
   243 
   247     /**
   244     /**
   248      * Obtains an instance of {@code LocalDate} from a year, month and day.
   245      * Obtains an instance of {@code LocalDate} from a year, month and day.
   249      * <p>
   246      * <p>
       
   247      * This returns a {@code LocalDate} with the specified year, month and day-of-month.
   250      * The day must be valid for the year and month, otherwise an exception will be thrown.
   248      * The day must be valid for the year and month, otherwise an exception will be thrown.
   251      *
   249      *
   252      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
   250      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
   253      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
   251      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
   254      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
   252      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
   255      * @return the local date, not null
   253      * @return the local date, not null
   256      * @throws DateTimeException if the value of any field is out of range
   254      * @throws DateTimeException if the value of any field is out of range,
   257      * @throws DateTimeException if the day-of-month is invalid for the month-year
   255      *  or if the day-of-month is invalid for the month-year
   258      */
   256      */
   259     public static LocalDate of(int year, int month, int dayOfMonth) {
   257     public static LocalDate of(int year, int month, int dayOfMonth) {
   260         YEAR.checkValidValue(year);
   258         YEAR.checkValidValue(year);
   261         MONTH_OF_YEAR.checkValidValue(month);
   259         MONTH_OF_YEAR.checkValidValue(month);
   262         DAY_OF_MONTH.checkValidValue(dayOfMonth);
   260         DAY_OF_MONTH.checkValidValue(dayOfMonth);
   265 
   263 
   266     //-----------------------------------------------------------------------
   264     //-----------------------------------------------------------------------
   267     /**
   265     /**
   268      * Obtains an instance of {@code LocalDate} from a year and day-of-year.
   266      * Obtains an instance of {@code LocalDate} from a year and day-of-year.
   269      * <p>
   267      * <p>
       
   268      * This returns a {@code LocalDate} with the specified year and day-of-year.
   270      * The day-of-year must be valid for the year, otherwise an exception will be thrown.
   269      * The day-of-year must be valid for the year, otherwise an exception will be thrown.
   271      *
   270      *
   272      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
   271      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
   273      * @param dayOfYear  the day-of-year to represent, from 1 to 366
   272      * @param dayOfYear  the day-of-year to represent, from 1 to 366
   274      * @return the local date, not null
   273      * @return the local date, not null
   275      * @throws DateTimeException if the value of any field is out of range
   274      * @throws DateTimeException if the value of any field is out of range,
   276      * @throws DateTimeException if the day-of-year is invalid for the month-year
   275      *  or if the day-of-year is invalid for the month-year
   277      */
   276      */
   278     public static LocalDate ofYearDay(int year, int dayOfYear) {
   277     public static LocalDate ofYearDay(int year, int dayOfYear) {
   279         YEAR.checkValidValue(year);
   278         YEAR.checkValidValue(year);
   280         DAY_OF_YEAR.checkValidValue(dayOfYear);
   279         DAY_OF_YEAR.checkValidValue(dayOfYear);
   281         boolean leap = ISOChrono.INSTANCE.isLeapYear(year);
   280         boolean leap = IsoChronology.INSTANCE.isLeapYear(year);
   282         if (dayOfYear == 366 && leap == false) {
   281         if (dayOfYear == 366 && leap == false) {
   283             throw new DateTimeException("Invalid date 'DayOfYear 366' as '" + year + "' is not a leap year");
   282             throw new DateTimeException("Invalid date 'DayOfYear 366' as '" + year + "' is not a leap year");
   284         }
   283         }
   285         Month moy = Month.of((dayOfYear - 1) / 31 + 1);
   284         Month moy = Month.of((dayOfYear - 1) / 31 + 1);
   286         int monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1;
   285         int monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1;
   293 
   292 
   294     //-----------------------------------------------------------------------
   293     //-----------------------------------------------------------------------
   295     /**
   294     /**
   296      * Obtains an instance of {@code LocalDate} from the epoch day count.
   295      * Obtains an instance of {@code LocalDate} from the epoch day count.
   297      * <p>
   296      * <p>
   298      * The Epoch Day count is a simple incrementing count of days
   297      * This returns a {@code LocalDate} with the specified epoch-day.
   299      * where day 0 is 1970-01-01. Negative numbers represent earlier days.
   298      * The {@link ChronoField#EPOCH_DAY EPOCH_DAY} is a simple incrementing count
       
   299      * of days where day 0 is 1970-01-01. Negative numbers represent earlier days.
   300      *
   300      *
   301      * @param epochDay  the Epoch Day to convert, based on the epoch 1970-01-01
   301      * @param epochDay  the Epoch Day to convert, based on the epoch 1970-01-01
   302      * @return the local date, not null
   302      * @return the local date, not null
   303      * @throws DateTimeException if the epoch days exceeds the supported date range
   303      * @throws DateTimeException if the epoch days exceeds the supported date range
   304      */
   304      */
   336 
   336 
   337     //-----------------------------------------------------------------------
   337     //-----------------------------------------------------------------------
   338     /**
   338     /**
   339      * Obtains an instance of {@code LocalDate} from a temporal object.
   339      * Obtains an instance of {@code LocalDate} from a temporal object.
   340      * <p>
   340      * <p>
   341      * A {@code TemporalAccessor} represents some form of date and time information.
   341      * This obtains a local date based on the specified temporal.
   342      * This factory converts the arbitrary temporal object to an instance of {@code LocalDate}.
   342      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
   343      * <p>
   343      * which this factory converts to an instance of {@code LocalDate}.
   344      * The conversion extracts the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field.
   344      * <p>
       
   345      * The conversion uses the {@link Queries#localDate()} query, which relies
       
   346      * on extracting the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field.
   345      * <p>
   347      * <p>
   346      * This method matches the signature of the functional interface {@link TemporalQuery}
   348      * This method matches the signature of the functional interface {@link TemporalQuery}
   347      * allowing it to be used as a query via method reference, {@code LocalDate::from}.
   349      * allowing it to be used as a query via method reference, {@code LocalDate::from}.
   348      *
   350      *
   349      * @param temporal  the temporal object to convert, not null
   351      * @param temporal  the temporal object to convert, not null
   350      * @return the local date, not null
   352      * @return the local date, not null
   351      * @throws DateTimeException if unable to convert to a {@code LocalDate}
   353      * @throws DateTimeException if unable to convert to a {@code LocalDate}
   352      */
   354      */
   353     public static LocalDate from(TemporalAccessor temporal) {
   355     public static LocalDate from(TemporalAccessor temporal) {
   354         if (temporal instanceof LocalDate) {
   356         LocalDate date = temporal.query(Queries.localDate());
   355             return (LocalDate) temporal;
   357         if (date == null) {
   356         } else if (temporal instanceof LocalDateTime) {
   358             throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " + temporal.getClass());
   357             return ((LocalDateTime) temporal).getDate();
   359         }
   358         } else if (temporal instanceof ZonedDateTime) {
   360         return date;
   359             return ((ZonedDateTime) temporal).getDate();
       
   360         }
       
   361         // handle builder as a special case
       
   362         if (temporal instanceof DateTimeBuilder) {
       
   363             DateTimeBuilder builder = (DateTimeBuilder) temporal;
       
   364             LocalDate date = builder.extract(LocalDate.class);
       
   365             if (date != null) {
       
   366                 return date;
       
   367             }
       
   368         }
       
   369         try {
       
   370             return ofEpochDay(temporal.getLong(EPOCH_DAY));
       
   371         } catch (DateTimeException ex) {
       
   372             throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " + temporal.getClass(), ex);
       
   373         }
       
   374     }
   361     }
   375 
   362 
   376     //-----------------------------------------------------------------------
   363     //-----------------------------------------------------------------------
   377     /**
   364     /**
   378      * Obtains an instance of {@code LocalDate} from a text string such as {@code 2007-12-03}.
   365      * Obtains an instance of {@code LocalDate} from a text string such as {@code 2007-12-03}.
   379      * <p>
   366      * <p>
   380      * The string must represent a valid date and is parsed using
   367      * The string must represent a valid date and is parsed using
   381      * {@link java.time.format.DateTimeFormatters#isoLocalDate()}.
   368      * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE}.
   382      *
   369      *
   383      * @param text  the text to parse such as "2007-12-03", not null
   370      * @param text  the text to parse such as "2007-12-03", not null
   384      * @return the parsed local date, not null
   371      * @return the parsed local date, not null
   385      * @throws DateTimeParseException if the text cannot be parsed
   372      * @throws DateTimeParseException if the text cannot be parsed
   386      */
   373      */
   387     public static LocalDate parse(CharSequence text) {
   374     public static LocalDate parse(CharSequence text) {
   388         return parse(text, DateTimeFormatters.isoLocalDate());
   375         return parse(text, DateTimeFormatter.ISO_LOCAL_DATE);
   389     }
   376     }
   390 
   377 
   391     /**
   378     /**
   392      * Obtains an instance of {@code LocalDate} from a text string using a specific formatter.
   379      * Obtains an instance of {@code LocalDate} from a text string using a specific formatter.
   393      * <p>
   380      * <p>
   412      * @param dayOfMonth  the day-of-month to represent, validated from 1 to 31
   399      * @param dayOfMonth  the day-of-month to represent, validated from 1 to 31
   413      * @return the local date, not null
   400      * @return the local date, not null
   414      * @throws DateTimeException if the day-of-month is invalid for the month-year
   401      * @throws DateTimeException if the day-of-month is invalid for the month-year
   415      */
   402      */
   416     private static LocalDate create(int year, Month month, int dayOfMonth) {
   403     private static LocalDate create(int year, Month month, int dayOfMonth) {
   417         if (dayOfMonth > 28 && dayOfMonth > month.length(ISOChrono.INSTANCE.isLeapYear(year))) {
   404         if (dayOfMonth > 28 && dayOfMonth > month.length(IsoChronology.INSTANCE.isLeapYear(year))) {
   418             if (dayOfMonth == 29) {
   405             if (dayOfMonth == 29) {
   419                 throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year");
   406                 throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year");
   420             } else {
   407             } else {
   421                 throw new DateTimeException("Invalid date '" + month.name() + " " + dayOfMonth + "'");
   408                 throw new DateTimeException("Invalid date '" + month.name() + " " + dayOfMonth + "'");
   422             }
   409             }
   433      * @return the resolved date, not null
   420      * @return the resolved date, not null
   434      */
   421      */
   435     private static LocalDate resolvePreviousValid(int year, int month, int day) {
   422     private static LocalDate resolvePreviousValid(int year, int month, int day) {
   436         switch (month) {
   423         switch (month) {
   437             case 2:
   424             case 2:
   438                 day = Math.min(day, ISOChrono.INSTANCE.isLeapYear(year) ? 29 : 28);
   425                 day = Math.min(day, IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
   439                 break;
   426                 break;
   440             case 4:
   427             case 4:
   441             case 6:
   428             case 6:
   442             case 9:
   429             case 9:
   443             case 11:
   430             case 11:
   467      * This checks if this date can be queried for the specified field.
   454      * This checks if this date can be queried for the specified field.
   468      * If false, then calling the {@link #range(TemporalField) range} and
   455      * If false, then calling the {@link #range(TemporalField) range} and
   469      * {@link #get(TemporalField) get} methods will throw an exception.
   456      * {@link #get(TemporalField) get} methods will throw an exception.
   470      * <p>
   457      * <p>
   471      * If the field is a {@link ChronoField} then the query is implemented here.
   458      * If the field is a {@link ChronoField} then the query is implemented here.
   472      * The {@link #isSupported(TemporalField) supported fields} will return valid
       
   473      * values based on this date-time.
       
   474      * The supported fields are:
   459      * The supported fields are:
   475      * <ul>
   460      * <ul>
   476      * <li>{@code DAY_OF_WEEK}
   461      * <li>{@code DAY_OF_WEEK}
   477      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
   462      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
   478      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
   463      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
   488      * <li>{@code ERA}
   473      * <li>{@code ERA}
   489      * </ul>
   474      * </ul>
   490      * All other {@code ChronoField} instances will return false.
   475      * All other {@code ChronoField} instances will return false.
   491      * <p>
   476      * <p>
   492      * If the field is not a {@code ChronoField}, then the result of this method
   477      * If the field is not a {@code ChronoField}, then the result of this method
   493      * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)}
   478      * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
   494      * passing {@code this} as the argument.
   479      * passing {@code this} as the argument.
   495      * Whether the field is supported is determined by the field.
   480      * Whether the field is supported is determined by the field.
   496      *
   481      *
   497      * @param field  the field to check, null returns false
   482      * @param field  the field to check, null returns false
   498      * @return true if the field is supported on this date, false if not
   483      * @return true if the field is supported on this date, false if not
   514      * The {@link #isSupported(TemporalField) supported fields} will return
   499      * The {@link #isSupported(TemporalField) supported fields} will return
   515      * appropriate range instances.
   500      * appropriate range instances.
   516      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
   501      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
   517      * <p>
   502      * <p>
   518      * If the field is not a {@code ChronoField}, then the result of this method
   503      * If the field is not a {@code ChronoField}, then the result of this method
   519      * is obtained by invoking {@code TemporalField.doRange(TemporalAccessor)}
   504      * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
   520      * passing {@code this} as the argument.
   505      * passing {@code this} as the argument.
   521      * Whether the range can be obtained is determined by the field.
   506      * Whether the range can be obtained is determined by the field.
   522      *
   507      *
   523      * @param field  the field to query the range for, not null
   508      * @param field  the field to query the range for, not null
   524      * @return the range of valid values for the field, not null
   509      * @return the range of valid values for the field, not null
   538                 }
   523                 }
   539                 return field.range();
   524                 return field.range();
   540             }
   525             }
   541             throw new DateTimeException("Unsupported field: " + field.getName());
   526             throw new DateTimeException("Unsupported field: " + field.getName());
   542         }
   527         }
   543         return field.doRange(this);
   528         return field.rangeRefinedBy(this);
   544     }
   529     }
   545 
   530 
   546     /**
   531     /**
   547      * Gets the value of the specified field from this date as an {@code int}.
   532      * Gets the value of the specified field from this date as an {@code int}.
   548      * <p>
   533      * <p>
   556      * values based on this date, except {@code EPOCH_DAY} and {@code EPOCH_MONTH}
   541      * values based on this date, except {@code EPOCH_DAY} and {@code EPOCH_MONTH}
   557      * which are too large to fit in an {@code int} and throw a {@code DateTimeException}.
   542      * which are too large to fit in an {@code int} and throw a {@code DateTimeException}.
   558      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
   543      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
   559      * <p>
   544      * <p>
   560      * If the field is not a {@code ChronoField}, then the result of this method
   545      * If the field is not a {@code ChronoField}, then the result of this method
   561      * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
   546      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
   562      * passing {@code this} as the argument. Whether the value can be obtained,
   547      * passing {@code this} as the argument. Whether the value can be obtained,
   563      * and what the value represents, is determined by the field.
   548      * and what the value represents, is determined by the field.
   564      *
   549      *
   565      * @param field  the field to get, not null
   550      * @param field  the field to get, not null
   566      * @return the value for the field
   551      * @return the value for the field
   586      * The {@link #isSupported(TemporalField) supported fields} will return valid
   571      * The {@link #isSupported(TemporalField) supported fields} will return valid
   587      * values based on this date.
   572      * values based on this date.
   588      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
   573      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
   589      * <p>
   574      * <p>
   590      * If the field is not a {@code ChronoField}, then the result of this method
   575      * If the field is not a {@code ChronoField}, then the result of this method
   591      * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
   576      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
   592      * passing {@code this} as the argument. Whether the value can be obtained,
   577      * passing {@code this} as the argument. Whether the value can be obtained,
   593      * and what the value represents, is determined by the field.
   578      * and what the value represents, is determined by the field.
   594      *
   579      *
   595      * @param field  the field to get, not null
   580      * @param field  the field to get, not null
   596      * @return the value for the field
   581      * @return the value for the field
   606             if (field == EPOCH_MONTH) {
   591             if (field == EPOCH_MONTH) {
   607                 return getEpochMonth();
   592                 return getEpochMonth();
   608             }
   593             }
   609             return get0(field);
   594             return get0(field);
   610         }
   595         }
   611         return field.doGet(this);
   596         return field.getFrom(this);
   612     }
   597     }
   613 
   598 
   614     private int get0(TemporalField field) {
   599     private int get0(TemporalField field) {
   615         switch ((ChronoField) field) {
   600         switch ((ChronoField) field) {
   616             case DAY_OF_WEEK: return getDayOfWeek().getValue();
   601             case DAY_OF_WEEK: return getDayOfWeek().getValue();
   636 
   621 
   637     //-----------------------------------------------------------------------
   622     //-----------------------------------------------------------------------
   638     /**
   623     /**
   639      * Gets the chronology of this date, which is the ISO calendar system.
   624      * Gets the chronology of this date, which is the ISO calendar system.
   640      * <p>
   625      * <p>
   641      * The {@code Chrono} represents the calendar system in use.
   626      * The {@code Chronology} represents the calendar system in use.
   642      * The ISO-8601 calendar system is the modern civil calendar system used today
   627      * The ISO-8601 calendar system is the modern civil calendar system used today
   643      * in most of the world. It is equivalent to the proleptic Gregorian calendar
   628      * in most of the world. It is equivalent to the proleptic Gregorian calendar
   644      * system, in which todays's rules for leap years are applied for all time.
   629      * system, in which todays's rules for leap years are applied for all time.
   645      *
   630      *
   646      * @return the ISO chronology, not null
   631      * @return the ISO chronology, not null
   647      */
   632      */
   648     @Override
   633     @Override
   649     public ISOChrono getChrono() {
   634     public IsoChronology getChronology() {
   650         return ISOChrono.INSTANCE;
   635         return IsoChronology.INSTANCE;
   651     }
   636     }
   652 
   637 
   653     /**
   638     /**
   654      * Gets the era applicable at this date.
   639      * Gets the era applicable at this date.
   655      * <p>
   640      * <p>
   656      * The official ISO-8601 standard does not define eras, however {@code ISOChrono} does.
   641      * The official ISO-8601 standard does not define eras, however {@code IsoChronology} does.
   657      * It defines two eras, 'CE' from year one onwards and 'BCE' from year zero backwards.
   642      * It defines two eras, 'CE' from year one onwards and 'BCE' from year zero backwards.
   658      * Since dates before the Julian-Gregorian cutover are not in line with history,
   643      * Since dates before the Julian-Gregorian cutover are not in line with history,
   659      * the cutover between 'BCE' and 'CE' is also not aligned with the commonly used
   644      * the cutover between 'BCE' and 'CE' is also not aligned with the commonly used
   660      * eras, often referred to using 'BC' and 'AD'.
   645      * eras, often referred to using 'BC' and 'AD'.
   661      * <p>
   646      * <p>
   662      * Users of this class should typically ignore this method as it exists primarily
   647      * Users of this class should typically ignore this method as it exists primarily
   663      * to fulfill the {@link ChronoLocalDate} contract where it is necessary to support
   648      * to fulfill the {@link ChronoLocalDate} contract where it is necessary to support
   664      * the Japanese calendar system.
   649      * the Japanese calendar system.
   665      * <p>
   650      * <p>
   666      * The returned era will be a singleton capable of being compared with the constants
   651      * The returned era will be a singleton capable of being compared with the constants
   667      * in {@link ISOChrono} using the {@code ==} operator.
   652      * in {@link IsoChronology} using the {@code ==} operator.
   668      *
   653      *
   669      * @return the {@code ISOChrono} era constant applicable at this date, not null
   654      * @return the {@code IsoChronology} era constant applicable at this date, not null
   670      */
   655      */
   671     @Override // override for Javadoc
   656     @Override // override for Javadoc
   672     public Era<ISOChrono> getEra() {
   657     public Era getEra() {
   673         return ChronoLocalDate.super.getEra();
   658         return ChronoLocalDate.super.getEra();
   674     }
   659     }
   675 
   660 
   676     /**
   661     /**
   677      * Gets the year field.
   662      * Gets the year field.
   678      * <p>
   663      * <p>
   679      * This method returns the primitive {@code int} value for the year.
   664      * This method returns the primitive {@code int} value for the year.
   680      * <p>
   665      * <p>
   681      * The year returned by this method is proleptic as per {@code get(YEAR)}.
   666      * The year returned by this method is proleptic as per {@code get(YEAR)}.
   682      * To obtain the year-of-era, use {@code get(YEAR_OF_ERA}.
   667      * To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}.
   683      *
   668      *
   684      * @return the year, from MIN_YEAR to MAX_YEAR
   669      * @return the year, from MIN_YEAR to MAX_YEAR
   685      */
   670      */
   686     public int getYear() {
   671     public int getYear() {
   687         return year;
   672         return year;
   775      *
   760      *
   776      * @return true if the year is leap, false otherwise
   761      * @return true if the year is leap, false otherwise
   777      */
   762      */
   778     @Override // override for Javadoc and performance
   763     @Override // override for Javadoc and performance
   779     public boolean isLeapYear() {
   764     public boolean isLeapYear() {
   780         return ISOChrono.INSTANCE.isLeapYear(year);
   765         return IsoChronology.INSTANCE.isLeapYear(year);
   781     }
   766     }
   782 
   767 
   783     /**
   768     /**
   784      * Returns the length of the month represented by this date.
   769      * Returns the length of the month represented by this date.
   785      * <p>
   770      * <p>
   817 
   802 
   818     //-----------------------------------------------------------------------
   803     //-----------------------------------------------------------------------
   819     /**
   804     /**
   820      * Returns an adjusted copy of this date.
   805      * Returns an adjusted copy of this date.
   821      * <p>
   806      * <p>
   822      * This returns a new {@code LocalDate}, based on this one, with the date adjusted.
   807      * This returns a {@code LocalDate}, based on this one, with the date adjusted.
   823      * The adjustment takes place using the specified adjuster strategy object.
   808      * The adjustment takes place using the specified adjuster strategy object.
   824      * Read the documentation of the adjuster to understand what adjustment will be made.
   809      * Read the documentation of the adjuster to understand what adjustment will be made.
   825      * <p>
   810      * <p>
   826      * A simple adjuster might simply set the one of the fields, such as the year field.
   811      * A simple adjuster might simply set the one of the fields, such as the year field.
   827      * A more complex adjuster might set the date to the last day of the month.
   812      * A more complex adjuster might set the date to the last day of the month.
   828      * A selection of common adjustments is provided in {@link java.time.temporal.Adjusters}.
   813      * A selection of common adjustments is provided in {@link java.time.temporal.Adjusters}.
   829      * These include finding the "last day of the month" and "next Wednesday".
   814      * These include finding the "last day of the month" and "next Wednesday".
   830      * Key date-time classes also implement the {@code TemporalAdjuster} interface,
   815      * Key date-time classes also implement the {@code TemporalAdjuster} interface,
   831      * such as {@link Month} and {@link java.time.temporal.MonthDay MonthDay}.
   816      * such as {@link Month} and {@link java.time.MonthDay MonthDay}.
   832      * The adjuster is responsible for handling special cases, such as the varying
   817      * The adjuster is responsible for handling special cases, such as the varying
   833      * lengths of month and leap years.
   818      * lengths of month and leap years.
   834      * <p>
   819      * <p>
   835      * For example this code returns a date on the last day of July:
   820      * For example this code returns a date on the last day of July:
   836      * <pre>
   821      * <pre>
   861     }
   846     }
   862 
   847 
   863     /**
   848     /**
   864      * Returns a copy of this date with the specified field set to a new value.
   849      * Returns a copy of this date with the specified field set to a new value.
   865      * <p>
   850      * <p>
   866      * This returns a new {@code LocalDate}, based on this one, with the value
   851      * This returns a {@code LocalDate}, based on this one, with the value
   867      * for the specified field changed.
   852      * for the specified field changed.
   868      * This can be used to change any supported field, such as the year, month or day-of-month.
   853      * This can be used to change any supported field, such as the year, month or day-of-month.
   869      * If it is not possible to set the value, because the field is not supported or for
   854      * If it is not possible to set the value, because the field is not supported or for
   870      * some other reason, an exception is thrown.
   855      * some other reason, an exception is thrown.
   871      * <p>
   856      * <p>
   949      * then a {@code DateTimeException} will be thrown.
   934      * then a {@code DateTimeException} will be thrown.
   950      * <p>
   935      * <p>
   951      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
   936      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
   952      * <p>
   937      * <p>
   953      * If the field is not a {@code ChronoField}, then the result of this method
   938      * If the field is not a {@code ChronoField}, then the result of this method
   954      * is obtained by invoking {@code TemporalField.doWith(Temporal, long)}
   939      * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
   955      * passing {@code this} as the argument. In this case, the field determines
   940      * passing {@code this} as the argument. In this case, the field determines
   956      * whether and how to adjust the instant.
   941      * whether and how to adjust the instant.
   957      * <p>
   942      * <p>
   958      * This instance is immutable and unaffected by this method call.
   943      * This instance is immutable and unaffected by this method call.
   959      *
   944      *
   983                 case YEAR: return withYear((int) newValue);
   968                 case YEAR: return withYear((int) newValue);
   984                 case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year));
   969                 case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year));
   985             }
   970             }
   986             throw new DateTimeException("Unsupported field: " + field.getName());
   971             throw new DateTimeException("Unsupported field: " + field.getName());
   987         }
   972         }
   988         return field.doWith(this, newValue);
   973         return field.adjustInto(this, newValue);
   989     }
   974     }
   990 
   975 
   991     //-----------------------------------------------------------------------
   976     //-----------------------------------------------------------------------
   992     /**
   977     /**
   993      * Returns a copy of this date with the year altered.
   978      * Returns a copy of this date with the year altered.
  1031      * <p>
  1016      * <p>
  1032      * This instance is immutable and unaffected by this method call.
  1017      * This instance is immutable and unaffected by this method call.
  1033      *
  1018      *
  1034      * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
  1019      * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
  1035      * @return a {@code LocalDate} based on this date with the requested day, not null
  1020      * @return a {@code LocalDate} based on this date with the requested day, not null
  1036      * @throws DateTimeException if the day-of-month value is invalid
  1021      * @throws DateTimeException if the day-of-month value is invalid,
  1037      * @throws DateTimeException if the day-of-month is invalid for the month-year
  1022      *  or if the day-of-month is invalid for the month-year
  1038      */
  1023      */
  1039     public LocalDate withDayOfMonth(int dayOfMonth) {
  1024     public LocalDate withDayOfMonth(int dayOfMonth) {
  1040         if (this.day == dayOfMonth) {
  1025         if (this.day == dayOfMonth) {
  1041             return this;
  1026             return this;
  1042         }
  1027         }
  1049      * <p>
  1034      * <p>
  1050      * This instance is immutable and unaffected by this method call.
  1035      * This instance is immutable and unaffected by this method call.
  1051      *
  1036      *
  1052      * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
  1037      * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
  1053      * @return a {@code LocalDate} based on this date with the requested day, not null
  1038      * @return a {@code LocalDate} based on this date with the requested day, not null
  1054      * @throws DateTimeException if the day-of-year value is invalid
  1039      * @throws DateTimeException if the day-of-year value is invalid,
  1055      * @throws DateTimeException if the day-of-year is invalid for the year
  1040      *  or if the day-of-year is invalid for the year
  1056      */
  1041      */
  1057     public LocalDate withDayOfYear(int dayOfYear) {
  1042     public LocalDate withDayOfYear(int dayOfYear) {
  1058         if (this.getDayOfYear() == dayOfYear) {
  1043         if (this.getDayOfYear() == dayOfYear) {
  1059             return this;
  1044             return this;
  1060         }
  1045         }
  1061         return ofYearDay(year, dayOfYear);
  1046         return ofYearDay(year, dayOfYear);
  1062     }
  1047     }
  1063 
  1048 
  1064     //-----------------------------------------------------------------------
  1049     //-----------------------------------------------------------------------
  1065     /**
  1050     /**
  1066      * Returns a copy of this date with the specified period added.
  1051      * Returns a copy of this date with the specified amount added.
  1067      * <p>
  1052      * <p>
  1068      * This method returns a new date based on this date with the specified period added.
  1053      * This returns a {@code LocalDate}, based on this one, with the specified amount added.
  1069      * The adder is typically {@link Period} but may be any other type implementing
  1054      * The amount is typically {@link Period} but may be any other type implementing
  1070      * the {@link TemporalAdder} interface.
  1055      * the {@link TemporalAmount} interface.
  1071      * The calculation is delegated to the specified adjuster, which typically calls
  1056      * <p>
  1072      * back to {@link #plus(long, TemporalUnit)}.
  1057      * The calculation is delegated to the amount object by calling
       
  1058      * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
       
  1059      * to implement the addition in any way it wishes, however it typically
       
  1060      * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
       
  1061      * of the amount implementation to determine if it can be successfully added.
  1073      * <p>
  1062      * <p>
  1074      * This instance is immutable and unaffected by this method call.
  1063      * This instance is immutable and unaffected by this method call.
  1075      *
  1064      *
  1076      * @param adder  the adder to use, not null
  1065      * @param amountToAdd  the amount to add, not null
  1077      * @return a {@code LocalDate} based on this date with the addition made, not null
  1066      * @return a {@code LocalDate} based on this date with the addition made, not null
  1078      * @throws DateTimeException if the addition cannot be made
  1067      * @throws DateTimeException if the addition cannot be made
  1079      * @throws ArithmeticException if numeric overflow occurs
  1068      * @throws ArithmeticException if numeric overflow occurs
  1080      */
  1069      */
  1081     @Override
  1070     @Override
  1082     public LocalDate plus(TemporalAdder adder) {
  1071     public LocalDate plus(TemporalAmount amountToAdd) {
  1083         return (LocalDate) adder.addTo(this);
  1072         return (LocalDate) amountToAdd.addTo(this);
  1084     }
  1073     }
  1085 
  1074 
  1086     /**
  1075     /**
  1087      * Returns a copy of this date with the specified period added.
  1076      * Returns a copy of this date with the specified amount added.
  1088      * <p>
  1077      * <p>
  1089      * This method returns a new date based on this date with the specified period added.
  1078      * This returns a {@code LocalDate}, based on this one, with the amount
  1090      * This can be used to add any period that is defined by a unit, for example to add years, months or days.
  1079      * in terms of the unit added. If it is not possible to add the amount, because the
  1091      * The unit is responsible for the details of the calculation, including the resolution
  1080      * unit is not supported or for some other reason, an exception is thrown.
  1092      * of any edge cases in the calculation.
  1081      * <p>
       
  1082      * In some cases, adding the amount can cause the resulting date to become invalid.
       
  1083      * For example, adding one month to 31st January would result in 31st February.
       
  1084      * In cases like this, the unit is responsible for resolving the date.
       
  1085      * Typically it will choose the previous valid date, which would be the last valid
       
  1086      * day of February in this example.
       
  1087      * <p>
       
  1088      * If the field is a {@link ChronoUnit} then the addition is implemented here.
       
  1089      * The supported fields behave as follows:
       
  1090      * <ul>
       
  1091      * <li>{@code DAYS} -
       
  1092      *  Returns a {@code LocalDate} with the specified number of days added.
       
  1093      *  This is equivalent to {@link #plusDays(long)}.
       
  1094      * <li>{@code WEEKS} -
       
  1095      *  Returns a {@code LocalDate} with the specified number of weeks added.
       
  1096      *  This is equivalent to {@link #plusWeeks(long)} and uses a 7 day week.
       
  1097      * <li>{@code MONTHS} -
       
  1098      *  Returns a {@code LocalDate} with the specified number of months added.
       
  1099      *  This is equivalent to {@link #plusMonths(long)}.
       
  1100      *  The day-of-month will be unchanged unless it would be invalid for the new
       
  1101      *  month and year. In that case, the day-of-month is adjusted to the maximum
       
  1102      *  valid value for the new month and year.
       
  1103      * <li>{@code YEARS} -
       
  1104      *  Returns a {@code LocalDate} with the specified number of years added.
       
  1105      *  This is equivalent to {@link #plusYears(long)}.
       
  1106      *  The day-of-month will be unchanged unless it would be invalid for the new
       
  1107      *  month and year. In that case, the day-of-month is adjusted to the maximum
       
  1108      *  valid value for the new month and year.
       
  1109      * <li>{@code DECADES} -
       
  1110      *  Returns a {@code LocalDate} with the specified number of decades added.
       
  1111      *  This is equivalent to calling {@link #plusYears(long)} with the amount
       
  1112      *  multiplied by 10.
       
  1113      *  The day-of-month will be unchanged unless it would be invalid for the new
       
  1114      *  month and year. In that case, the day-of-month is adjusted to the maximum
       
  1115      *  valid value for the new month and year.
       
  1116      * <li>{@code CENTURIES} -
       
  1117      *  Returns a {@code LocalDate} with the specified number of centuries added.
       
  1118      *  This is equivalent to calling {@link #plusYears(long)} with the amount
       
  1119      *  multiplied by 100.
       
  1120      *  The day-of-month will be unchanged unless it would be invalid for the new
       
  1121      *  month and year. In that case, the day-of-month is adjusted to the maximum
       
  1122      *  valid value for the new month and year.
       
  1123      * <li>{@code MILLENNIA} -
       
  1124      *  Returns a {@code LocalDate} with the specified number of millennia added.
       
  1125      *  This is equivalent to calling {@link #plusYears(long)} with the amount
       
  1126      *  multiplied by 1,000.
       
  1127      *  The day-of-month will be unchanged unless it would be invalid for the new
       
  1128      *  month and year. In that case, the day-of-month is adjusted to the maximum
       
  1129      *  valid value for the new month and year.
       
  1130      * <li>{@code ERAS} -
       
  1131      *  Returns a {@code LocalDate} with the specified number of eras added.
       
  1132      *  Only two eras are supported so the amount must be one, zero or minus one.
       
  1133      *  If the amount is non-zero then the year is changed such that the year-of-era
       
  1134      *  is unchanged.
       
  1135      *  The day-of-month will be unchanged unless it would be invalid for the new
       
  1136      *  month and year. In that case, the day-of-month is adjusted to the maximum
       
  1137      *  valid value for the new month and year.
       
  1138      * </ul>
       
  1139      * <p>
       
  1140      * All other {@code ChronoUnit} instances will throw a {@code DateTimeException}.
       
  1141      * <p>
       
  1142      * If the field is not a {@code ChronoUnit}, then the result of this method
       
  1143      * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
       
  1144      * passing {@code this} as the argument. In this case, the unit determines
       
  1145      * whether and how to perform the addition.
  1093      * <p>
  1146      * <p>
  1094      * This instance is immutable and unaffected by this method call.
  1147      * This instance is immutable and unaffected by this method call.
  1095      *
  1148      *
  1096      * @param amountToAdd  the amount of the unit to add to the result, may be negative
  1149      * @param amountToAdd  the amount of the unit to add to the result, may be negative
  1097      * @param unit  the unit of the period to add, not null
  1150      * @param unit  the unit of the amount to add, not null
  1098      * @return a {@code LocalDate} based on this date with the specified period added, not null
  1151      * @return a {@code LocalDate} based on this date with the specified amount added, not null
  1099      * @throws DateTimeException if the unit cannot be added to this type
  1152      * @throws DateTimeException if the addition cannot be made
       
  1153      * @throws ArithmeticException if numeric overflow occurs
  1100      */
  1154      */
  1101     @Override
  1155     @Override
  1102     public LocalDate plus(long amountToAdd, TemporalUnit unit) {
  1156     public LocalDate plus(long amountToAdd, TemporalUnit unit) {
  1103         if (unit instanceof ChronoUnit) {
  1157         if (unit instanceof ChronoUnit) {
  1104             ChronoUnit f = (ChronoUnit) unit;
  1158             ChronoUnit f = (ChronoUnit) unit;
  1112                 case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
  1166                 case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000));
  1113                 case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
  1167                 case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd));
  1114             }
  1168             }
  1115             throw new DateTimeException("Unsupported unit: " + unit.getName());
  1169             throw new DateTimeException("Unsupported unit: " + unit.getName());
  1116         }
  1170         }
  1117         return unit.doPlus(this, amountToAdd);
  1171         return unit.addTo(this, amountToAdd);
  1118     }
  1172     }
  1119 
  1173 
  1120     //-----------------------------------------------------------------------
  1174     //-----------------------------------------------------------------------
  1121     /**
  1175     /**
  1122      * Returns a copy of this {@code LocalDate} with the specified period in years added.
  1176      * Returns a copy of this {@code LocalDate} with the specified period in years added.
  1219         return LocalDate.ofEpochDay(mjDay);
  1273         return LocalDate.ofEpochDay(mjDay);
  1220     }
  1274     }
  1221 
  1275 
  1222     //-----------------------------------------------------------------------
  1276     //-----------------------------------------------------------------------
  1223     /**
  1277     /**
  1224      * Returns a copy of this date with the specified period subtracted.
  1278      * Returns a copy of this date with the specified amount subtracted.
  1225      * <p>
  1279      * <p>
  1226      * This method returns a new date based on this date with the specified period subtracted.
  1280      * This returns a {@code LocalDate}, based on this one, with the specified amount subtracted.
  1227      * The subtractor is typically {@link Period} but may be any other type implementing
  1281      * The amount is typically {@link Period} but may be any other type implementing
  1228      * the {@link TemporalSubtractor} interface.
  1282      * the {@link TemporalAmount} interface.
  1229      * The calculation is delegated to the specified adjuster, which typically calls
  1283      * <p>
  1230      * back to {@link #minus(long, TemporalUnit)}.
  1284      * The calculation is delegated to the amount object by calling
       
  1285      * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
       
  1286      * to implement the subtraction in any way it wishes, however it typically
       
  1287      * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
       
  1288      * of the amount implementation to determine if it can be successfully subtracted.
  1231      * <p>
  1289      * <p>
  1232      * This instance is immutable and unaffected by this method call.
  1290      * This instance is immutable and unaffected by this method call.
  1233      *
  1291      *
  1234      * @param subtractor  the subtractor to use, not null
  1292      * @param amountToSubtract  the amount to subtract, not null
  1235      * @return a {@code LocalDate} based on this date with the subtraction made, not null
  1293      * @return a {@code LocalDate} based on this date with the subtraction made, not null
  1236      * @throws DateTimeException if the subtraction cannot be made
  1294      * @throws DateTimeException if the subtraction cannot be made
  1237      * @throws ArithmeticException if numeric overflow occurs
  1295      * @throws ArithmeticException if numeric overflow occurs
  1238      */
  1296      */
  1239     @Override
  1297     @Override
  1240     public LocalDate minus(TemporalSubtractor subtractor) {
  1298     public LocalDate minus(TemporalAmount amountToSubtract) {
  1241         return (LocalDate) subtractor.subtractFrom(this);
  1299         return (LocalDate) amountToSubtract.subtractFrom(this);
  1242     }
  1300     }
  1243 
  1301 
  1244     /**
  1302     /**
  1245      * Returns a copy of this date with the specified period subtracted.
  1303      * Returns a copy of this date with the specified amount subtracted.
  1246      * <p>
  1304      * <p>
  1247      * This method returns a new date based on this date with the specified period subtracted.
  1305      * This returns a {@code LocalDate}, based on this one, with the amount
  1248      * This can be used to subtract any period that is defined by a unit, for example to subtract years, months or days.
  1306      * in terms of the unit subtracted. If it is not possible to subtract the amount,
  1249      * The unit is responsible for the details of the calculation, including the resolution
  1307      * because the unit is not supported or for some other reason, an exception is thrown.
  1250      * of any edge cases in the calculation.
  1308      * <p>
       
  1309      * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
       
  1310      * See that method for a full description of how addition, and thus subtraction, works.
  1251      * <p>
  1311      * <p>
  1252      * This instance is immutable and unaffected by this method call.
  1312      * This instance is immutable and unaffected by this method call.
  1253      *
  1313      *
  1254      * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
  1314      * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
  1255      * @param unit  the unit of the period to subtract, not null
  1315      * @param unit  the unit of the amount to subtract, not null
  1256      * @return a {@code LocalDate} based on this date with the specified period subtracted, not null
  1316      * @return a {@code LocalDate} based on this date with the specified amount subtracted, not null
  1257      * @throws DateTimeException if the unit cannot be added to this type
  1317      * @throws DateTimeException if the subtraction cannot be made
       
  1318      * @throws ArithmeticException if numeric overflow occurs
  1258      */
  1319      */
  1259     @Override
  1320     @Override
  1260     public LocalDate minus(long amountToSubtract, TemporalUnit unit) {
  1321     public LocalDate minus(long amountToSubtract, TemporalUnit unit) {
  1261         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
  1322         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
  1262     }
  1323     }
  1365      * @param query  the query to invoke, not null
  1426      * @param query  the query to invoke, not null
  1366      * @return the query result, null may be returned (defined by the query)
  1427      * @return the query result, null may be returned (defined by the query)
  1367      * @throws DateTimeException if unable to query (defined by the query)
  1428      * @throws DateTimeException if unable to query (defined by the query)
  1368      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
  1429      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
  1369      */
  1430      */
  1370     @Override  // override for Javadoc
  1431     @SuppressWarnings("unchecked")
       
  1432     @Override
  1371     public <R> R query(TemporalQuery<R> query) {
  1433     public <R> R query(TemporalQuery<R> query) {
       
  1434         if (query == Queries.localDate()) {
       
  1435             return (R) this;
       
  1436         }
  1372         return ChronoLocalDate.super.query(query);
  1437         return ChronoLocalDate.super.query(query);
  1373     }
  1438     }
  1374 
  1439 
  1375     /**
  1440     /**
  1376      * Adjusts the specified temporal object to have the same date as this object.
  1441      * Adjusts the specified temporal object to have the same date as this object.
  1415      * The calculation returns a whole number, representing the number of
  1480      * The calculation returns a whole number, representing the number of
  1416      * complete units between the two dates.
  1481      * complete units between the two dates.
  1417      * For example, the period in months between 2012-06-15 and 2012-08-14
  1482      * For example, the period in months between 2012-06-15 and 2012-08-14
  1418      * will only be one month as it is one day short of two months.
  1483      * will only be one month as it is one day short of two months.
  1419      * <p>
  1484      * <p>
  1420      * This method operates in association with {@link TemporalUnit#between}.
  1485      * There are two equivalent ways of using this method.
  1421      * The result of this method is a {@code long} representing the amount of
  1486      * The first is to invoke this method.
  1422      * the specified unit. By contrast, the result of {@code between} is an
  1487      * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
  1423      * object that can be used directly in addition/subtraction:
       
  1424      * <pre>
  1488      * <pre>
  1425      *   long period = start.periodUntil(end, MONTHS);   // this method
  1489      *   // these two lines are equivalent
  1426      *   dateTime.plus(MONTHS.between(start, end));      // use in plus/minus
  1490      *   amount = start.periodUntil(end, MONTHS);
       
  1491      *   amount = MONTHS.between(start, end);
  1427      * </pre>
  1492      * </pre>
       
  1493      * The choice should be made based on which makes the code more readable.
  1428      * <p>
  1494      * <p>
  1429      * The calculation is implemented in this method for {@link ChronoUnit}.
  1495      * The calculation is implemented in this method for {@link ChronoUnit}.
  1430      * The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS},
  1496      * The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS},
  1431      * {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS}
  1497      * {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS}
  1432      * are supported. Other {@code ChronoUnit} values will throw an exception.
  1498      * are supported. Other {@code ChronoUnit} values will throw an exception.
  1462                 case MILLENNIA: return monthsUntil(end) / 12000;
  1528                 case MILLENNIA: return monthsUntil(end) / 12000;
  1463                 case ERAS: return end.getLong(ERA) - getLong(ERA);
  1529                 case ERAS: return end.getLong(ERA) - getLong(ERA);
  1464             }
  1530             }
  1465             throw new DateTimeException("Unsupported unit: " + unit.getName());
  1531             throw new DateTimeException("Unsupported unit: " + unit.getName());
  1466         }
  1532         }
  1467         return unit.between(this, endDate).getAmount();
  1533         return unit.between(this, endDate);
  1468     }
  1534     }
  1469 
  1535 
  1470     long daysUntil(LocalDate end) {
  1536     long daysUntil(LocalDate end) {
  1471         return end.toEpochDay() - toEpochDay();  // no overflow
  1537         return end.toEpochDay() - toEpochDay();  // no overflow
  1472     }
  1538     }
  1475         long packed1 = getEpochMonth() * 32L + getDayOfMonth();  // no overflow
  1541         long packed1 = getEpochMonth() * 32L + getDayOfMonth();  // no overflow
  1476         long packed2 = end.getEpochMonth() * 32L + end.getDayOfMonth();  // no overflow
  1542         long packed2 = end.getEpochMonth() * 32L + end.getDayOfMonth();  // no overflow
  1477         return (packed2 - packed1) / 32;
  1543         return (packed2 - packed1) / 32;
  1478     }
  1544     }
  1479 
  1545 
  1480     //-----------------------------------------------------------------------
  1546     /**
  1481     /**
  1547      * Calculates the period between this date and another date as a {@code Period}.
  1482      * Returns a local date-time formed from this date at the specified time.
  1548      * <p>
  1483      * <p>
  1549      * This calculates the period between two dates in terms of years, months and days.
  1484      * This combines this date with the specified time to form a {@code LocalDateTime}.
  1550      * The start and end points are {@code this} and the specified date.
       
  1551      * The result will be negative if the end is before the start.
       
  1552      * <p>
       
  1553      * The calculation is performed using the ISO calendar system.
       
  1554      * If necessary, the input date will be converted to ISO.
       
  1555      * <p>
       
  1556      * The start date is included, but the end date is not.
       
  1557      * The period is calculated by removing complete months, then calculating
       
  1558      * the remaining number of days, adjusting to ensure that both have the same sign.
       
  1559      * The number of months is then normalized into years and months based on a 12 month year.
       
  1560      * A month is considered to be complete if the end day-of-month is greater
       
  1561      * than or equal to the start day-of-month.
       
  1562      * For example, from {@code 2010-01-15} to {@code 2011-03-18} is "1 year, 2 months and 3 days".
       
  1563      * <p>
       
  1564      * The result of this method can be a negative period if the end is before the start.
       
  1565      * The negative sign will be the same in each of year, month and day.
       
  1566      * <p>
       
  1567      * There are two equivalent ways of using this method.
       
  1568      * The first is to invoke this method.
       
  1569      * The second is to use {@link Period#between(LocalDate, LocalDate)}:
       
  1570      * <pre>
       
  1571      *   // these two lines are equivalent
       
  1572      *   period = start.periodUntil(end);
       
  1573      *   period = Period.between(start, end);
       
  1574      * </pre>
       
  1575      * The choice should be made based on which makes the code more readable.
       
  1576      *
       
  1577      * @param endDate  the end date, exclusive, which may be in any chronology, not null
       
  1578      * @return the period between this date and the end date, not null
       
  1579      */
       
  1580     @Override
       
  1581     public Period periodUntil(ChronoLocalDate<?> endDate) {
       
  1582         LocalDate end = LocalDate.from(endDate);
       
  1583         long totalMonths = end.getEpochMonth() - this.getEpochMonth();  // safe
       
  1584         int days = end.day - this.day;
       
  1585         if (totalMonths > 0 && days < 0) {
       
  1586             totalMonths--;
       
  1587             LocalDate calcDate = this.plusMonths(totalMonths);
       
  1588             days = (int) (end.toEpochDay() - calcDate.toEpochDay());  // safe
       
  1589         } else if (totalMonths < 0 && days > 0) {
       
  1590             totalMonths++;
       
  1591             days -= end.lengthOfMonth();
       
  1592         }
       
  1593         long years = totalMonths / 12;  // safe
       
  1594         int months = (int) (totalMonths % 12);  // safe
       
  1595         return Period.of(Math.toIntExact(years), months, days);
       
  1596     }
       
  1597 
       
  1598     //-----------------------------------------------------------------------
       
  1599     /**
       
  1600      * Combines this date with a time to create a {@code LocalDateTime}.
       
  1601      * <p>
       
  1602      * This returns a {@code LocalDateTime} formed from this date at the specified time.
  1485      * All possible combinations of date and time are valid.
  1603      * All possible combinations of date and time are valid.
  1486      * <p>
       
  1487      * This instance is immutable and unaffected by this method call.
       
  1488      *
  1604      *
  1489      * @param time  the time to combine with, not null
  1605      * @param time  the time to combine with, not null
  1490      * @return the local date-time formed from this date and the specified time, not null
  1606      * @return the local date-time formed from this date and the specified time, not null
  1491      */
  1607      */
  1492     @Override
  1608     @Override
  1493     public LocalDateTime atTime(LocalTime time) {
  1609     public LocalDateTime atTime(LocalTime time) {
  1494         return LocalDateTime.of(this, time);
  1610         return LocalDateTime.of(this, time);
  1495     }
  1611     }
  1496 
  1612 
  1497     /**
  1613     /**
  1498      * Returns a local date-time formed from this date at the specified time.
  1614      * Combines this date with a time to create a {@code LocalDateTime}.
  1499      * <p>
  1615      * <p>
  1500      * This combines this date with the specified time to form a {@code LocalDateTime}.
  1616      * This returns a {@code LocalDateTime} formed from this date at the
       
  1617      * specified hour and minute.
       
  1618      * The seconds and nanosecond fields will be set to zero.
  1501      * The individual time fields must be within their valid range.
  1619      * The individual time fields must be within their valid range.
  1502      * All possible combinations of date and time are valid.
  1620      * All possible combinations of date and time are valid.
  1503      * <p>
       
  1504      * This instance is immutable and unaffected by this method call.
       
  1505      *
  1621      *
  1506      * @param hour  the hour-of-day to use, from 0 to 23
  1622      * @param hour  the hour-of-day to use, from 0 to 23
  1507      * @param minute  the minute-of-hour to use, from 0 to 59
  1623      * @param minute  the minute-of-hour to use, from 0 to 59
  1508      * @return the local date-time formed from this date and the specified time, not null
  1624      * @return the local date-time formed from this date and the specified time, not null
  1509      * @throws DateTimeException if the value of any field is out of range
  1625      * @throws DateTimeException if the value of any field is out of range
  1511     public LocalDateTime atTime(int hour, int minute) {
  1627     public LocalDateTime atTime(int hour, int minute) {
  1512         return atTime(LocalTime.of(hour, minute));
  1628         return atTime(LocalTime.of(hour, minute));
  1513     }
  1629     }
  1514 
  1630 
  1515     /**
  1631     /**
  1516      * Returns a local date-time formed from this date at the specified time.
  1632      * Combines this date with a time to create a {@code LocalDateTime}.
  1517      * <p>
  1633      * <p>
  1518      * This combines this date with the specified time to form a {@code LocalDateTime}.
  1634      * This returns a {@code LocalDateTime} formed from this date at the
       
  1635      * specified hour, minute and second.
       
  1636      * The nanosecond field will be set to zero.
  1519      * The individual time fields must be within their valid range.
  1637      * The individual time fields must be within their valid range.
  1520      * All possible combinations of date and time are valid.
  1638      * All possible combinations of date and time are valid.
  1521      * <p>
       
  1522      * This instance is immutable and unaffected by this method call.
       
  1523      *
  1639      *
  1524      * @param hour  the hour-of-day to use, from 0 to 23
  1640      * @param hour  the hour-of-day to use, from 0 to 23
  1525      * @param minute  the minute-of-hour to use, from 0 to 59
  1641      * @param minute  the minute-of-hour to use, from 0 to 59
  1526      * @param second  the second-of-minute to represent, from 0 to 59
  1642      * @param second  the second-of-minute to represent, from 0 to 59
  1527      * @return the local date-time formed from this date and the specified time, not null
  1643      * @return the local date-time formed from this date and the specified time, not null
  1530     public LocalDateTime atTime(int hour, int minute, int second) {
  1646     public LocalDateTime atTime(int hour, int minute, int second) {
  1531         return atTime(LocalTime.of(hour, minute, second));
  1647         return atTime(LocalTime.of(hour, minute, second));
  1532     }
  1648     }
  1533 
  1649 
  1534     /**
  1650     /**
  1535      * Returns a local date-time formed from this date at the specified time.
  1651      * Combines this date with a time to create a {@code LocalDateTime}.
  1536      * <p>
  1652      * <p>
  1537      * This combines this date with the specified time to form a {@code LocalDateTime}.
  1653      * This returns a {@code LocalDateTime} formed from this date at the
       
  1654      * specified hour, minute, second and nanosecond.
  1538      * The individual time fields must be within their valid range.
  1655      * The individual time fields must be within their valid range.
  1539      * All possible combinations of date and time are valid.
  1656      * All possible combinations of date and time are valid.
  1540      * <p>
       
  1541      * This instance is immutable and unaffected by this method call.
       
  1542      *
  1657      *
  1543      * @param hour  the hour-of-day to use, from 0 to 23
  1658      * @param hour  the hour-of-day to use, from 0 to 23
  1544      * @param minute  the minute-of-hour to use, from 0 to 59
  1659      * @param minute  the minute-of-hour to use, from 0 to 59
  1545      * @param second  the second-of-minute to represent, from 0 to 59
  1660      * @param second  the second-of-minute to represent, from 0 to 59
  1546      * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
  1661      * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
  1550     public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond) {
  1665     public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond) {
  1551         return atTime(LocalTime.of(hour, minute, second, nanoOfSecond));
  1666         return atTime(LocalTime.of(hour, minute, second, nanoOfSecond));
  1552     }
  1667     }
  1553 
  1668 
  1554     /**
  1669     /**
  1555      * Returns an offset date formed from this date and the specified offset.
  1670      * Combines this date with an offset time to create an {@code OffsetDateTime}.
  1556      * <p>
  1671      * <p>
  1557      * This combines this date with the specified offset to form an {@code OffsetDate}.
  1672      * This returns an {@code OffsetDateTime} formed from this date at the specified time.
  1558      * All possible combinations of date and offset are valid.
  1673      * All possible combinations of date and time are valid.
  1559      * <p>
  1674      *
  1560      * This instance is immutable and unaffected by this method call.
  1675      * @param time  the time to combine with, not null
  1561      *
  1676      * @return the offset date-time formed from this date and the specified time, not null
  1562      * @param offset  the offset to combine with, not null
  1677      */
  1563      * @return the offset date formed from this date and the specified offset, not null
  1678     public OffsetDateTime atTime(OffsetTime time) {
  1564      */
  1679         return OffsetDateTime.of(LocalDateTime.of(this, time.toLocalTime()), time.getOffset());
  1565     public OffsetDate atOffset(ZoneOffset offset) {
  1680     }
  1566         return OffsetDate.of(this, offset);
  1681 
       
  1682     /**
       
  1683      * Combines this date with the time of midnight to create a {@code LocalDateTime}
       
  1684      * at the start of this date.
       
  1685      * <p>
       
  1686      * This returns a {@code LocalDateTime} formed from this date at the time of
       
  1687      * midnight, 00:00, at the start of this date.
       
  1688      *
       
  1689      * @return the local date-time of midnight at the start of this date, not null
       
  1690      */
       
  1691     public LocalDateTime atStartOfDay() {
       
  1692         return LocalDateTime.of(this, LocalTime.MIDNIGHT);
  1567     }
  1693     }
  1568 
  1694 
  1569     /**
  1695     /**
  1570      * Returns a zoned date-time from this date at the earliest valid time according
  1696      * Returns a zoned date-time from this date at the earliest valid time according
  1571      * to the rules in the time-zone.
  1697      * to the rules in the time-zone.
  1580      * <p>
  1706      * <p>
  1581      * If the zone ID is a {@link ZoneOffset}, then the result always has a time of midnight.
  1707      * If the zone ID is a {@link ZoneOffset}, then the result always has a time of midnight.
  1582      * <p>
  1708      * <p>
  1583      * To convert to a specific time in a given time-zone call {@link #atTime(LocalTime)}
  1709      * To convert to a specific time in a given time-zone call {@link #atTime(LocalTime)}
  1584      * followed by {@link LocalDateTime#atZone(ZoneId)}.
  1710      * followed by {@link LocalDateTime#atZone(ZoneId)}.
  1585      * <p>
       
  1586      * This instance is immutable and unaffected by this method call.
       
  1587      *
  1711      *
  1588      * @param zone  the zone ID to use, not null
  1712      * @param zone  the zone ID to use, not null
  1589      * @return the zoned date-time formed from this date and the earliest valid time for the zone, not null
  1713      * @return the zoned date-time formed from this date and the earliest valid time for the zone, not null
  1590      */
  1714      */
  1591     public ZonedDateTime atStartOfDay(ZoneId zone) {
  1715     public ZonedDateTime atStartOfDay(ZoneId zone) {
  1634      * It is "consistent with equals", as defined by {@link Comparable}.
  1758      * It is "consistent with equals", as defined by {@link Comparable}.
  1635      * <p>
  1759      * <p>
  1636      * If all the dates being compared are instances of {@code LocalDate},
  1760      * If all the dates being compared are instances of {@code LocalDate},
  1637      * then the comparison will be entirely based on the date.
  1761      * then the comparison will be entirely based on the date.
  1638      * If some dates being compared are in different chronologies, then the
  1762      * If some dates being compared are in different chronologies, then the
  1639      * chronology is also considered, see {@link java.time.temporal.ChronoLocalDate#compareTo}.
  1763      * chronology is also considered, see {@link java.time.chrono.ChronoLocalDate#compareTo}.
  1640      *
  1764      *
  1641      * @param other  the other date to compare to, not null
  1765      * @param other  the other date to compare to, not null
  1642      * @return the comparator value, negative if less, positive if greater
  1766      * @return the comparator value, negative if less, positive if greater
  1643      */
  1767      */
  1644     @Override  // override for Javadoc and performance
  1768     @Override  // override for Javadoc and performance
  1820 
  1944 
  1821     /**
  1945     /**
  1822      * Outputs this date as a {@code String} using the formatter.
  1946      * Outputs this date as a {@code String} using the formatter.
  1823      * <p>
  1947      * <p>
  1824      * This date will be passed to the formatter
  1948      * This date will be passed to the formatter
  1825      * {@link DateTimeFormatter#print(TemporalAccessor) print method}.
  1949      * {@link DateTimeFormatter#format(TemporalAccessor) format method}.
  1826      *
  1950      *
  1827      * @param formatter  the formatter to use, not null
  1951      * @param formatter  the formatter to use, not null
  1828      * @return the formatted date string, not null
  1952      * @return the formatted date string, not null
  1829      * @throws DateTimeException if an error occurs during printing
  1953      * @throws DateTimeException if an error occurs during printing
  1830      */
  1954      */