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

Print this page

        

*** 67,79 **** import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; import static java.time.temporal.ChronoField.DAY_OF_MONTH; import static java.time.temporal.ChronoField.DAY_OF_YEAR; import static java.time.temporal.ChronoField.EPOCH_DAY; - import static java.time.temporal.ChronoField.EPOCH_MONTH; import static java.time.temporal.ChronoField.ERA; import static java.time.temporal.ChronoField.MONTH_OF_YEAR; import static java.time.temporal.ChronoField.YEAR; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; --- 67,79 ---- import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; import static java.time.temporal.ChronoField.DAY_OF_MONTH; import static java.time.temporal.ChronoField.DAY_OF_YEAR; import static java.time.temporal.ChronoField.EPOCH_DAY; import static java.time.temporal.ChronoField.ERA; import static java.time.temporal.ChronoField.MONTH_OF_YEAR; + import static java.time.temporal.ChronoField.PROLEPTIC_MONTH; import static java.time.temporal.ChronoField.YEAR; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException;
*** 85,102 **** import java.time.chrono.IsoChronology; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; - import java.time.temporal.Queries; import java.time.temporal.Temporal; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAdjuster; import java.time.temporal.TemporalAmount; import java.time.temporal.TemporalField; import java.time.temporal.TemporalQuery; import java.time.temporal.TemporalUnit; import java.time.temporal.ValueRange; import java.time.zone.ZoneOffsetTransition; import java.time.zone.ZoneRules; import java.util.Objects; --- 85,102 ---- import java.time.chrono.IsoChronology; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAdjuster; import java.time.temporal.TemporalAmount; import java.time.temporal.TemporalField; import java.time.temporal.TemporalQuery; import java.time.temporal.TemporalUnit; + import java.time.temporal.UnsupportedTemporalTypeException; import java.time.temporal.ValueRange; import java.time.zone.ZoneOffsetTransition; import java.time.zone.ZoneRules; import java.util.Objects;
*** 236,246 **** */ public static LocalDate of(int year, Month month, int dayOfMonth) { YEAR.checkValidValue(year); Objects.requireNonNull(month, "month"); DAY_OF_MONTH.checkValidValue(dayOfMonth); ! return create(year, month, dayOfMonth); } /** * Obtains an instance of {@code LocalDate} from a year, month and day. * <p> --- 236,246 ---- */ public static LocalDate of(int year, Month month, int dayOfMonth) { YEAR.checkValidValue(year); Objects.requireNonNull(month, "month"); DAY_OF_MONTH.checkValidValue(dayOfMonth); ! return create(year, month.getValue(), dayOfMonth); } /** * Obtains an instance of {@code LocalDate} from a year, month and day. * <p>
*** 256,266 **** */ public static LocalDate of(int year, int month, int dayOfMonth) { YEAR.checkValidValue(year); MONTH_OF_YEAR.checkValidValue(month); DAY_OF_MONTH.checkValidValue(dayOfMonth); ! return create(year, Month.of(month), dayOfMonth); } //----------------------------------------------------------------------- /** * Obtains an instance of {@code LocalDate} from a year and day-of-year. --- 256,266 ---- */ public static LocalDate of(int year, int month, int dayOfMonth) { YEAR.checkValidValue(year); MONTH_OF_YEAR.checkValidValue(month); DAY_OF_MONTH.checkValidValue(dayOfMonth); ! return create(year, month, dayOfMonth); } //----------------------------------------------------------------------- /** * Obtains an instance of {@code LocalDate} from a year and day-of-year.
*** 285,295 **** int monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1; if (dayOfYear > monthEnd) { moy = moy.plus(1); } int dom = dayOfYear - moy.firstDayOfYear(leap) + 1; ! return create(year, moy, dom); } //----------------------------------------------------------------------- /** * Obtains an instance of {@code LocalDate} from the epoch day count. --- 285,295 ---- int monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1; if (dayOfYear > monthEnd) { moy = moy.plus(1); } int dom = dayOfYear - moy.firstDayOfYear(leap) + 1; ! return new LocalDate(year, moy.getValue(), dom); } //----------------------------------------------------------------------- /** * Obtains an instance of {@code LocalDate} from the epoch day count.
*** 340,361 **** * <p> * This obtains a local date based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code LocalDate}. * <p> ! * The conversion uses the {@link Queries#localDate()} query, which relies * on extracting the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code LocalDate::from}. * * @param temporal the temporal object to convert, not null * @return the local date, not null * @throws DateTimeException if unable to convert to a {@code LocalDate} */ public static LocalDate from(TemporalAccessor temporal) { ! LocalDate date = temporal.query(Queries.localDate()); if (date == null) { throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " + temporal.getClass()); } return date; } --- 340,361 ---- * <p> * This obtains a local date based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code LocalDate}. * <p> ! * The conversion uses the {@link TemporalQuery#localDate()} query, which relies * on extracting the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code LocalDate::from}. * * @param temporal the temporal object to convert, not null * @return the local date, not null * @throws DateTimeException if unable to convert to a {@code LocalDate} */ public static LocalDate from(TemporalAccessor temporal) { ! LocalDate date = temporal.query(TemporalQuery.localDate()); if (date == null) { throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " + temporal.getClass()); } return date; }
*** 393,416 **** //----------------------------------------------------------------------- /** * Creates a local date from the year, month and day fields. * * @param year the year to represent, validated from MIN_YEAR to MAX_YEAR ! * @param month the month-of-year to represent, validated not null * @param dayOfMonth the day-of-month to represent, validated from 1 to 31 * @return the local date, not null * @throws DateTimeException if the day-of-month is invalid for the month-year */ ! private static LocalDate create(int year, Month month, int dayOfMonth) { ! if (dayOfMonth > 28 && dayOfMonth > month.length(IsoChronology.INSTANCE.isLeapYear(year))) { if (dayOfMonth == 29) { throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year"); } else { ! throw new DateTimeException("Invalid date '" + month.name() + " " + dayOfMonth + "'"); } } ! return new LocalDate(year, month.getValue(), dayOfMonth); } /** * Resolves the date, resolving days past the end of month. * --- 393,430 ---- //----------------------------------------------------------------------- /** * Creates a local date from the year, month and day fields. * * @param year the year to represent, validated from MIN_YEAR to MAX_YEAR ! * @param month the month-of-year to represent, from 1 to 12, validated * @param dayOfMonth the day-of-month to represent, validated from 1 to 31 * @return the local date, not null * @throws DateTimeException if the day-of-month is invalid for the month-year */ ! private static LocalDate create(int year, int month, int dayOfMonth) { ! if (dayOfMonth > 28) { ! int dom = 31; ! switch (month) { ! case 2: ! dom = (IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28); ! break; ! case 4: ! case 6: ! case 9: ! case 11: ! dom = 30; ! break; ! } ! if (dayOfMonth > dom) { if (dayOfMonth == 29) { throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year"); } else { ! throw new DateTimeException("Invalid date '" + Month.of(month).name() + " " + dayOfMonth + "'"); ! } } } ! return new LocalDate(year, month, dayOfMonth); } /** * Resolves the date, resolving days past the end of month. *
*** 429,439 **** case 9: case 11: day = Math.min(day, 30); break; } ! return LocalDate.of(year, month, day); } /** * Constructor, previously validated. * --- 443,453 ---- case 9: case 11: day = Math.min(day, 30); break; } ! return new LocalDate(year, month, day); } /** * Constructor, previously validated. *
*** 465,475 **** * <li>{@code DAY_OF_YEAR} * <li>{@code EPOCH_DAY} * <li>{@code ALIGNED_WEEK_OF_MONTH} * <li>{@code ALIGNED_WEEK_OF_YEAR} * <li>{@code MONTH_OF_YEAR} ! * <li>{@code EPOCH_MONTH} * <li>{@code YEAR_OF_ERA} * <li>{@code YEAR} * <li>{@code ERA} * </ul> * All other {@code ChronoField} instances will return false. --- 479,489 ---- * <li>{@code DAY_OF_YEAR} * <li>{@code EPOCH_DAY} * <li>{@code ALIGNED_WEEK_OF_MONTH} * <li>{@code ALIGNED_WEEK_OF_YEAR} * <li>{@code MONTH_OF_YEAR} ! * <li>{@code PROLEPTIC_MONTH} * <li>{@code YEAR_OF_ERA} * <li>{@code YEAR} * <li>{@code ERA} * </ul> * All other {@code ChronoField} instances will return false.
*** 496,531 **** * or for some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the query is implemented here. * The {@link #isSupported(TemporalField) supported fields} will return * appropriate range instances. ! * All other {@code ChronoField} instances will throw a {@code DateTimeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} * passing {@code this} as the argument. * Whether the range can be obtained is determined by the field. * * @param field the field to query the range for, not null * @return the range of valid values for the field, not null * @throws DateTimeException if the range for the field cannot be obtained */ @Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; ! if (f.isDateField()) { switch (f) { case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, getMonth() == Month.FEBRUARY && isLeapYear() == false ? 4 : 5); case YEAR_OF_ERA: return (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE)); } return field.range(); } ! throw new DateTimeException("Unsupported field: " + field.getName()); } return field.rangeRefinedBy(this); } /** --- 510,546 ---- * or for some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the query is implemented here. * The {@link #isSupported(TemporalField) supported fields} will return * appropriate range instances. ! * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} * passing {@code this} as the argument. * Whether the range can be obtained is determined by the field. * * @param field the field to query the range for, not null * @return the range of valid values for the field, not null * @throws DateTimeException if the range for the field cannot be obtained + * @throws UnsupportedTemporalTypeException if the field is not supported */ @Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; ! if (f.isDateBased()) { switch (f) { case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, getMonth() == Month.FEBRUARY && isLeapYear() == false ? 4 : 5); case YEAR_OF_ERA: return (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE)); } return field.range(); } ! throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName()); } return field.rangeRefinedBy(this); } /**
*** 536,557 **** * If it is not possible to return the value, because the field is not supported * or for some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the query is implemented here. * The {@link #isSupported(TemporalField) supported fields} will return valid ! * values based on this date, except {@code EPOCH_DAY} and {@code EPOCH_MONTH} * which are too large to fit in an {@code int} and throw a {@code DateTimeException}. ! * All other {@code ChronoField} instances will throw a {@code DateTimeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} * passing {@code this} as the argument. Whether the value can be obtained, * and what the value represents, is determined by the field. * * @param field the field to get, not null * @return the value for the field ! * @throws DateTimeException if a value for the field cannot be obtained * @throws ArithmeticException if numeric overflow occurs */ @Override // override for Javadoc and performance public int get(TemporalField field) { if (field instanceof ChronoField) { --- 551,575 ---- * If it is not possible to return the value, because the field is not supported * or for some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the query is implemented here. * The {@link #isSupported(TemporalField) supported fields} will return valid ! * values based on this date, except {@code EPOCH_DAY} and {@code PROLEPTIC_MONTH} * which are too large to fit in an {@code int} and throw a {@code DateTimeException}. ! * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} * passing {@code this} as the argument. Whether the value can be obtained, * and what the value represents, is determined by the field. * * @param field the field to get, not null * @return the value for the field ! * @throws DateTimeException if a value for the field cannot be obtained or ! * the value is outside the range of valid values for the field ! * @throws UnsupportedTemporalTypeException if the field is not supported or ! * the range of values exceeds an {@code int} * @throws ArithmeticException if numeric overflow occurs */ @Override // override for Javadoc and performance public int get(TemporalField field) { if (field instanceof ChronoField) {
*** 568,597 **** * or for some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the query is implemented here. * The {@link #isSupported(TemporalField) supported fields} will return valid * values based on this date. ! * All other {@code ChronoField} instances will throw a {@code DateTimeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} * passing {@code this} as the argument. Whether the value can be obtained, * and what the value represents, is determined by the field. * * @param field the field to get, not null * @return the value for the field * @throws DateTimeException if a value for the field cannot be obtained * @throws ArithmeticException if numeric overflow occurs */ @Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { if (field == EPOCH_DAY) { return toEpochDay(); } ! if (field == EPOCH_MONTH) { ! return getEpochMonth(); } return get0(field); } return field.getFrom(this); } --- 586,616 ---- * or for some other reason, an exception is thrown. * <p> * If the field is a {@link ChronoField} then the query is implemented here. * The {@link #isSupported(TemporalField) supported fields} will return valid * values based on this date. ! * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} * passing {@code this} as the argument. Whether the value can be obtained, * and what the value represents, is determined by the field. * * @param field the field to get, not null * @return the value for the field * @throws DateTimeException if a value for the field cannot be obtained + * @throws UnsupportedTemporalTypeException if the field is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { if (field == EPOCH_DAY) { return toEpochDay(); } ! if (field == PROLEPTIC_MONTH) { ! return getProlepticMonth(); } return get0(field); } return field.getFrom(this); }
*** 601,634 **** case DAY_OF_WEEK: return getDayOfWeek().getValue(); case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1; case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1; case DAY_OF_MONTH: return day; case DAY_OF_YEAR: return getDayOfYear(); ! case EPOCH_DAY: throw new DateTimeException("Field too large for an int: " + field); case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1; case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1; case MONTH_OF_YEAR: return month; ! case EPOCH_MONTH: throw new DateTimeException("Field too large for an int: " + field); case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year); case YEAR: return year; case ERA: return (year >= 1 ? 1 : 0); } ! throw new DateTimeException("Unsupported field: " + field.getName()); } ! private long getEpochMonth() { ! return ((year - 1970) * 12L) + (month - 1); } //----------------------------------------------------------------------- /** * Gets the chronology of this date, which is the ISO calendar system. * <p> * The {@code Chronology} represents the calendar system in use. * The ISO-8601 calendar system is the modern civil calendar system used today * in most of the world. It is equivalent to the proleptic Gregorian calendar ! * system, in which todays's rules for leap years are applied for all time. * * @return the ISO chronology, not null */ @Override public IsoChronology getChronology() { --- 620,653 ---- case DAY_OF_WEEK: return getDayOfWeek().getValue(); case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1; case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1; case DAY_OF_MONTH: return day; case DAY_OF_YEAR: return getDayOfYear(); ! case EPOCH_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'EpochDay' for get() method, use getLong() instead"); case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1; case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1; case MONTH_OF_YEAR: return month; ! case PROLEPTIC_MONTH: throw new UnsupportedTemporalTypeException("Invalid field 'ProlepticMonth' for get() method, use getLong() instead"); case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year); case YEAR: return year; case ERA: return (year >= 1 ? 1 : 0); } ! throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName()); } ! private long getProlepticMonth() { ! return (year * 12L + month - 1); } //----------------------------------------------------------------------- /** * Gets the chronology of this date, which is the ISO calendar system. * <p> * The {@code Chronology} represents the calendar system in use. * The ISO-8601 calendar system is the modern civil calendar system used today * in most of the world. It is equivalent to the proleptic Gregorian calendar ! * system, in which today's rules for leap years are applied for all time. * * @return the ISO chronology, not null */ @Override public IsoChronology getChronology() {
*** 808,818 **** * The adjustment takes place using the specified adjuster strategy object. * Read the documentation of the adjuster to understand what adjustment will be made. * <p> * A simple adjuster might simply set the one of the fields, such as the year field. * A more complex adjuster might set the date to the last day of the month. ! * A selection of common adjustments is provided in {@link java.time.temporal.Adjusters}. * These include finding the "last day of the month" and "next Wednesday". * Key date-time classes also implement the {@code TemporalAdjuster} interface, * such as {@link Month} and {@link java.time.MonthDay MonthDay}. * The adjuster is responsible for handling special cases, such as the varying * lengths of month and leap years. --- 827,837 ---- * The adjustment takes place using the specified adjuster strategy object. * Read the documentation of the adjuster to understand what adjustment will be made. * <p> * A simple adjuster might simply set the one of the fields, such as the year field. * A more complex adjuster might set the date to the last day of the month. ! * A selection of common adjustments is provided in {@link TemporalAdjuster}. * These include finding the "last day of the month" and "next Wednesday". * Key date-time classes also implement the {@code TemporalAdjuster} interface, * such as {@link Month} and {@link java.time.MonthDay MonthDay}. * The adjuster is responsible for handling special cases, such as the varying * lengths of month and leap years.
*** 906,917 **** * <li>{@code MONTH_OF_YEAR} - * Returns a {@code LocalDate} with the specified month-of-year. * The year will be unchanged. The day-of-month will also be unchanged, * unless it would be invalid for the new month and year. In that case, the * day-of-month is adjusted to the maximum valid value for the new month and year. ! * <li>{@code EPOCH_MONTH} - ! * Returns a {@code LocalDate} with the specified epoch-month. * The day-of-month will be unchanged, unless it would be invalid for the new month * and year. In that case, the day-of-month is adjusted to the maximum valid value * for the new month and year. * <li>{@code YEAR_OF_ERA} - * Returns a {@code LocalDate} with the specified year-of-era. --- 925,936 ---- * <li>{@code MONTH_OF_YEAR} - * Returns a {@code LocalDate} with the specified month-of-year. * The year will be unchanged. The day-of-month will also be unchanged, * unless it would be invalid for the new month and year. In that case, the * day-of-month is adjusted to the maximum valid value for the new month and year. ! * <li>{@code PROLEPTIC_MONTH} - ! * Returns a {@code LocalDate} with the specified proleptic-month. * The day-of-month will be unchanged, unless it would be invalid for the new month * and year. In that case, the day-of-month is adjusted to the maximum valid value * for the new month and year. * <li>{@code YEAR_OF_ERA} - * Returns a {@code LocalDate} with the specified year-of-era.
*** 931,941 **** * </ul> * <p> * In all cases, if the new value is outside the valid range of values for the field * then a {@code DateTimeException} will be thrown. * <p> ! * All other {@code ChronoField} instances will throw a {@code DateTimeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} * passing {@code this} as the argument. In this case, the field determines * whether and how to adjust the instant. --- 950,960 ---- * </ul> * <p> * In all cases, if the new value is outside the valid range of values for the field * then a {@code DateTimeException} will be thrown. * <p> ! * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoField}, then the result of this method * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} * passing {@code this} as the argument. In this case, the field determines * whether and how to adjust the instant.
*** 944,953 **** --- 963,973 ---- * * @param field the field to set in the result, not null * @param newValue the new value of the field in the result * @return a {@code LocalDate} based on {@code this} with the specified field set, not null * @throws DateTimeException if the field cannot be set + * @throws UnsupportedTemporalTypeException if the field is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override public LocalDate with(TemporalField field, long newValue) { if (field instanceof ChronoField) {
*** 961,976 **** case DAY_OF_YEAR: return withDayOfYear((int) newValue); case EPOCH_DAY: return LocalDate.ofEpochDay(newValue); case ALIGNED_WEEK_OF_MONTH: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_MONTH)); case ALIGNED_WEEK_OF_YEAR: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_YEAR)); case MONTH_OF_YEAR: return withMonth((int) newValue); ! case EPOCH_MONTH: return plusMonths(newValue - getLong(EPOCH_MONTH)); case YEAR_OF_ERA: return withYear((int) (year >= 1 ? newValue : 1 - newValue)); case YEAR: return withYear((int) newValue); case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year)); } ! throw new DateTimeException("Unsupported field: " + field.getName()); } return field.adjustInto(this, newValue); } //----------------------------------------------------------------------- --- 981,996 ---- case DAY_OF_YEAR: return withDayOfYear((int) newValue); case EPOCH_DAY: return LocalDate.ofEpochDay(newValue); case ALIGNED_WEEK_OF_MONTH: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_MONTH)); case ALIGNED_WEEK_OF_YEAR: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_YEAR)); case MONTH_OF_YEAR: return withMonth((int) newValue); ! case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth()); case YEAR_OF_ERA: return withYear((int) (year >= 1 ? newValue : 1 - newValue)); case YEAR: return withYear((int) newValue); case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - year)); } ! throw new UnsupportedTemporalTypeException("Unsupported field: " + field.getName()); } return field.adjustInto(this, newValue); } //-----------------------------------------------------------------------
*** 1135,1145 **** * The day-of-month will be unchanged unless it would be invalid for the new * month and year. In that case, the day-of-month is adjusted to the maximum * valid value for the new month and year. * </ul> * <p> ! * All other {@code ChronoUnit} instances will throw a {@code DateTimeException}. * <p> * If the field is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)} * passing {@code this} as the argument. In this case, the unit determines * whether and how to perform the addition. --- 1155,1165 ---- * The day-of-month will be unchanged unless it would be invalid for the new * month and year. In that case, the day-of-month is adjusted to the maximum * valid value for the new month and year. * </ul> * <p> ! * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}. * <p> * If the field is not a {@code ChronoUnit}, then the result of this method * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)} * passing {@code this} as the argument. In this case, the unit determines * whether and how to perform the addition.
*** 1148,1157 **** --- 1168,1178 ---- * * @param amountToAdd the amount of the unit to add to the result, may be negative * @param unit the unit of the amount to add, not null * @return a {@code LocalDate} based on this date with the specified amount added, not null * @throws DateTimeException if the addition cannot be made + * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override public LocalDate plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) {
*** 1164,1174 **** case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10)); case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100)); case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000)); case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd)); } ! throw new DateTimeException("Unsupported unit: " + unit.getName()); } return unit.addTo(this, amountToAdd); } //----------------------------------------------------------------------- --- 1185,1195 ---- case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10)); case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100)); case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000)); case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd)); } ! throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit.getName()); } return unit.addTo(this, amountToAdd); } //-----------------------------------------------------------------------
*** 1313,1322 **** --- 1334,1344 ---- * * @param amountToSubtract the amount of the unit to subtract from the result, may be negative * @param unit the unit of the amount to subtract, not null * @return a {@code LocalDate} based on this date with the specified amount subtracted, not null * @throws DateTimeException if the subtraction cannot be made + * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override public LocalDate minus(long amountToSubtract, TemporalUnit unit) { return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
*** 1429,1439 **** * @throws ArithmeticException if numeric overflow occurs (defined by the query) */ @SuppressWarnings("unchecked") @Override public <R> R query(TemporalQuery<R> query) { ! if (query == Queries.localDate()) { return (R) this; } return ChronoLocalDate.super.query(query); } --- 1451,1461 ---- * @throws ArithmeticException if numeric overflow occurs (defined by the query) */ @SuppressWarnings("unchecked") @Override public <R> R query(TemporalQuery<R> query) { ! if (query == TemporalQuery.localDate()) { return (R) this; } return ChronoLocalDate.super.query(query); }
*** 1506,1519 **** --- 1528,1543 ---- * * @param endDate the end date, which must be a {@code LocalDate}, not null * @param unit the unit to measure the period in, not null * @return the amount of the period between this date and the end date * @throws DateTimeException if the period cannot be calculated + * @throws UnsupportedTemporalTypeException if the unit is not supported * @throws ArithmeticException if numeric overflow occurs */ @Override public long periodUntil(Temporal endDate, TemporalUnit unit) { + Objects.requireNonNull(unit, "unit"); if (endDate instanceof LocalDate == false) { Objects.requireNonNull(endDate, "endDate"); throw new DateTimeException("Unable to calculate period between objects of two different types"); } LocalDate end = (LocalDate) endDate;
*** 1526,1556 **** case DECADES: return monthsUntil(end) / 120; case CENTURIES: return monthsUntil(end) / 1200; case MILLENNIA: return monthsUntil(end) / 12000; case ERAS: return end.getLong(ERA) - getLong(ERA); } ! throw new DateTimeException("Unsupported unit: " + unit.getName()); } return unit.between(this, endDate); } long daysUntil(LocalDate end) { return end.toEpochDay() - toEpochDay(); // no overflow } private long monthsUntil(LocalDate end) { ! long packed1 = getEpochMonth() * 32L + getDayOfMonth(); // no overflow ! long packed2 = end.getEpochMonth() * 32L + end.getDayOfMonth(); // no overflow return (packed2 - packed1) / 32; } /** * Calculates the period between this date and another date as a {@code Period}. * <p> * This calculates the period between two dates in terms of years, months and days. * The start and end points are {@code this} and the specified date. * The result will be negative if the end is before the start. * <p> * The calculation is performed using the ISO calendar system. * If necessary, the input date will be converted to ISO. * <p> * The start date is included, but the end date is not. --- 1550,1581 ---- case DECADES: return monthsUntil(end) / 120; case CENTURIES: return monthsUntil(end) / 1200; case MILLENNIA: return monthsUntil(end) / 12000; case ERAS: return end.getLong(ERA) - getLong(ERA); } ! throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit.getName()); } return unit.between(this, endDate); } long daysUntil(LocalDate end) { return end.toEpochDay() - toEpochDay(); // no overflow } private long monthsUntil(LocalDate end) { ! long packed1 = getProlepticMonth() * 32L + getDayOfMonth(); // no overflow ! long packed2 = end.getProlepticMonth() * 32L + end.getDayOfMonth(); // no overflow return (packed2 - packed1) / 32; } /** * Calculates the period between this date and another date as a {@code Period}. * <p> * This calculates the period between two dates in terms of years, months and days. * The start and end points are {@code this} and the specified date. * The result will be negative if the end is before the start. + * The negative sign will be the same in each of year, month and day. * <p> * The calculation is performed using the ISO calendar system. * If necessary, the input date will be converted to ISO. * <p> * The start date is included, but the end date is not.
*** 1559,1571 **** * The number of months is then normalized into years and months based on a 12 month year. * A month is considered to be complete if the end day-of-month is greater * than or equal to the start day-of-month. * For example, from {@code 2010-01-15} to {@code 2011-03-18} is "1 year, 2 months and 3 days". * <p> - * The result of this method can be a negative period if the end is before the start. - * The negative sign will be the same in each of year, month and day. - * <p> * There are two equivalent ways of using this method. * The first is to invoke this method. * The second is to use {@link Period#between(LocalDate, LocalDate)}: * <pre> * // these two lines are equivalent --- 1584,1593 ----
*** 1578,1588 **** * @return the period between this date and the end date, not null */ @Override public Period periodUntil(ChronoLocalDate<?> endDate) { LocalDate end = LocalDate.from(endDate); ! long totalMonths = end.getEpochMonth() - this.getEpochMonth(); // safe int days = end.day - this.day; if (totalMonths > 0 && days < 0) { totalMonths--; LocalDate calcDate = this.plusMonths(totalMonths); days = (int) (end.toEpochDay() - calcDate.toEpochDay()); // safe --- 1600,1610 ---- * @return the period between this date and the end date, not null */ @Override public Period periodUntil(ChronoLocalDate<?> endDate) { LocalDate end = LocalDate.from(endDate); ! long totalMonths = end.getProlepticMonth() - this.getProlepticMonth(); // safe int days = end.day - this.day; if (totalMonths > 0 && days < 0) { totalMonths--; LocalDate calcDate = this.plusMonths(totalMonths); days = (int) (end.toEpochDay() - calcDate.toEpochDay()); // safe
*** 1593,1602 **** --- 1615,1639 ---- long years = totalMonths / 12; // safe int months = (int) (totalMonths % 12); // safe return Period.of(Math.toIntExact(years), months, days); } + /** + * Formats this date using the specified formatter. + * <p> + * This date will be passed to the formatter to produce a string. + * + * @param formatter the formatter to use, not null + * @return the formatted date string, not null + * @throws DateTimeException if an error occurs during printing + */ + @Override // override for Javadoc and performance + public String format(DateTimeFormatter formatter) { + Objects.requireNonNull(formatter, "formatter"); + return formatter.format(this); + } + //----------------------------------------------------------------------- /** * Combines this date with a time to create a {@code LocalDateTime}. * <p> * This returns a {@code LocalDateTime} formed from this date at the specified time.
*** 1798,1808 **** * </pre> * <p> * This method only considers the position of the two dates on the local time-line. * It does not take into account the chronology, or calendar system. * This is different from the comparison in {@link #compareTo(ChronoLocalDate)}, ! * but is the same approach as {@link #DATE_COMPARATOR}. * * @param other the other date to compare to, not null * @return true if this date is after the specified date */ @Override // override for Javadoc and performance --- 1835,1845 ---- * </pre> * <p> * This method only considers the position of the two dates on the local time-line. * It does not take into account the chronology, or calendar system. * This is different from the comparison in {@link #compareTo(ChronoLocalDate)}, ! * but is the same approach as {@link ChronoLocalDate#timeLineOrder()}. * * @param other the other date to compare to, not null * @return true if this date is after the specified date */ @Override // override for Javadoc and performance
*** 1827,1837 **** * </pre> * <p> * This method only considers the position of the two dates on the local time-line. * It does not take into account the chronology, or calendar system. * This is different from the comparison in {@link #compareTo(ChronoLocalDate)}, ! * but is the same approach as {@link #DATE_COMPARATOR}. * * @param other the other date to compare to, not null * @return true if this date is before the specified date */ @Override // override for Javadoc and performance --- 1864,1874 ---- * </pre> * <p> * This method only considers the position of the two dates on the local time-line. * It does not take into account the chronology, or calendar system. * This is different from the comparison in {@link #compareTo(ChronoLocalDate)}, ! * but is the same approach as {@link ChronoLocalDate#timeLineOrder()}. * * @param other the other date to compare to, not null * @return true if this date is before the specified date */ @Override // override for Javadoc and performance
*** 1856,1866 **** * </pre> * <p> * This method only considers the position of the two dates on the local time-line. * It does not take into account the chronology, or calendar system. * This is different from the comparison in {@link #compareTo(ChronoLocalDate)} ! * but is the same approach as {@link #DATE_COMPARATOR}. * * @param other the other date to compare to, not null * @return true if this date is equal to the specified date */ @Override // override for Javadoc and performance --- 1893,1903 ---- * </pre> * <p> * This method only considers the position of the two dates on the local time-line. * It does not take into account the chronology, or calendar system. * This is different from the comparison in {@link #compareTo(ChronoLocalDate)} ! * but is the same approach as {@link ChronoLocalDate#timeLineOrder()}. * * @param other the other date to compare to, not null * @return true if this date is equal to the specified date */ @Override // override for Javadoc and performance
*** 1910,1920 **** //----------------------------------------------------------------------- /** * Outputs this date as a {@code String}, such as {@code 2007-12-03}. * <p> ! * The output will be in the ISO-8601 format {@code yyyy-MM-dd}. * * @return a string representation of this date, not null */ @Override public String toString() { --- 1947,1957 ---- //----------------------------------------------------------------------- /** * Outputs this date as a {@code String}, such as {@code 2007-12-03}. * <p> ! * The output will be in the ISO-8601 format {@code uuuu-MM-dd}. * * @return a string representation of this date, not null */ @Override public String toString() {
*** 1940,1964 **** .append(dayValue < 10 ? "-0" : "-") .append(dayValue) .toString(); } - /** - * Outputs this date as a {@code String} using the formatter. - * <p> - * This date will be passed to the formatter - * {@link DateTimeFormatter#format(TemporalAccessor) format method}. - * - * @param formatter the formatter to use, not null - * @return the formatted date string, not null - * @throws DateTimeException if an error occurs during printing - */ - @Override // override for Javadoc - public String toString(DateTimeFormatter formatter) { - return ChronoLocalDate.super.toString(formatter); - } - //----------------------------------------------------------------------- /** * Writes the object using a * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>. * <pre> --- 1977,1986 ----