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

Print this page

        

*** 78,107 **** import java.io.DataOutput; import java.io.IOException; import java.io.InvalidObjectException; import java.io.ObjectStreamException; import java.io.Serializable; ! import java.time.format.DateTimeBuilder; import java.time.format.DateTimeFormatter; - import java.time.format.DateTimeFormatters; import java.time.format.DateTimeParseException; import java.time.temporal.ChronoField; - import java.time.temporal.ChronoLocalDate; import java.time.temporal.ChronoUnit; ! import java.time.temporal.Era; ! import java.time.temporal.ISOChrono; ! import java.time.temporal.OffsetDate; import java.time.temporal.Temporal; import java.time.temporal.TemporalAccessor; - import java.time.temporal.TemporalAdder; import java.time.temporal.TemporalAdjuster; import java.time.temporal.TemporalField; import java.time.temporal.TemporalQuery; - import java.time.temporal.TemporalSubtractor; import java.time.temporal.TemporalUnit; import java.time.temporal.ValueRange; - import java.time.temporal.Year; import java.time.zone.ZoneOffsetTransition; import java.time.zone.ZoneRules; import java.util.Objects; /** --- 78,103 ---- import java.io.DataOutput; import java.io.IOException; import java.io.InvalidObjectException; import java.io.ObjectStreamException; import java.io.Serializable; ! import java.time.chrono.ChronoLocalDate; ! import java.time.chrono.Era; ! 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; /**
*** 129,139 **** * This class is immutable and thread-safe. * * @since 1.8 */ public final class LocalDate ! implements Temporal, TemporalAdjuster, ChronoLocalDate<ISOChrono>, Serializable { /** * The minimum supported {@code LocalDate}, '-999999999-01-01'. * This could be used by an application as a "far past" date. */ --- 125,135 ---- * This class is immutable and thread-safe. * * @since 1.8 */ public final class LocalDate ! implements Temporal, TemporalAdjuster, ChronoLocalDate<LocalDate>, Serializable { /** * The minimum supported {@code LocalDate}, '-999999999-01-01'. * This could be used by an application as a "far past" date. */
*** 214,224 **** * @param clock the clock to use, not null * @return the current date, not null */ public static LocalDate now(Clock clock) { Objects.requireNonNull(clock, "clock"); ! // inline OffsetDate factory to avoid creating object and InstantProvider checks final Instant now = clock.instant(); // called once ZoneOffset offset = clock.getZone().getRules().getOffset(now); long epochSec = now.getEpochSecond() + offset.getTotalSeconds(); // overflow caught later long epochDay = Math.floorDiv(epochSec, SECONDS_PER_DAY); return LocalDate.ofEpochDay(epochDay); --- 210,220 ---- * @param clock the clock to use, not null * @return the current date, not null */ public static LocalDate now(Clock clock) { Objects.requireNonNull(clock, "clock"); ! // inline to avoid creating object and Instant checks final Instant now = clock.instant(); // called once ZoneOffset offset = clock.getZone().getRules().getOffset(now); long epochSec = now.getEpochSecond() + offset.getTotalSeconds(); // overflow caught later long epochDay = Math.floorDiv(epochSec, SECONDS_PER_DAY); return LocalDate.ofEpochDay(epochDay);
*** 226,243 **** //----------------------------------------------------------------------- /** * Obtains an instance of {@code LocalDate} from a year, month and day. * <p> * The day must be valid for the year and month, otherwise an exception will be thrown. * * @param year the year to represent, from MIN_YEAR to MAX_YEAR * @param month the month-of-year to represent, not null * @param dayOfMonth the day-of-month to represent, from 1 to 31 * @return the local date, not null ! * @throws DateTimeException if the value of any field is out of range ! * @throws DateTimeException if the day-of-month is invalid for the month-year */ public static LocalDate of(int year, Month month, int dayOfMonth) { YEAR.checkValidValue(year); Objects.requireNonNull(month, "month"); DAY_OF_MONTH.checkValidValue(dayOfMonth); --- 222,240 ---- //----------------------------------------------------------------------- /** * Obtains an instance of {@code LocalDate} from a year, month and day. * <p> + * This returns a {@code LocalDate} with the specified year, month and day-of-month. * The day must be valid for the year and month, otherwise an exception will be thrown. * * @param year the year to represent, from MIN_YEAR to MAX_YEAR * @param month the month-of-year to represent, not null * @param dayOfMonth the day-of-month to represent, from 1 to 31 * @return the local date, not null ! * @throws DateTimeException if the value of any field is out of range, ! * or if the day-of-month is invalid for the month-year */ public static LocalDate of(int year, Month month, int dayOfMonth) { YEAR.checkValidValue(year); Objects.requireNonNull(month, "month"); DAY_OF_MONTH.checkValidValue(dayOfMonth);
*** 245,262 **** } /** * Obtains an instance of {@code LocalDate} from a year, month and day. * <p> * The day must be valid for the year and month, otherwise an exception will be thrown. * * @param year the year to represent, from MIN_YEAR to MAX_YEAR * @param month the month-of-year to represent, from 1 (January) to 12 (December) * @param dayOfMonth the day-of-month to represent, from 1 to 31 * @return the local date, not null ! * @throws DateTimeException if the value of any field is out of range ! * @throws DateTimeException if the day-of-month is invalid for the month-year */ public static LocalDate of(int year, int month, int dayOfMonth) { YEAR.checkValidValue(year); MONTH_OF_YEAR.checkValidValue(month); DAY_OF_MONTH.checkValidValue(dayOfMonth); --- 242,260 ---- } /** * Obtains an instance of {@code LocalDate} from a year, month and day. * <p> + * This returns a {@code LocalDate} with the specified year, month and day-of-month. * The day must be valid for the year and month, otherwise an exception will be thrown. * * @param year the year to represent, from MIN_YEAR to MAX_YEAR * @param month the month-of-year to represent, from 1 (January) to 12 (December) * @param dayOfMonth the day-of-month to represent, from 1 to 31 * @return the local date, not null ! * @throws DateTimeException if the value of any field is out of range, ! * or if the day-of-month is invalid for the month-year */ public static LocalDate of(int year, int month, int dayOfMonth) { YEAR.checkValidValue(year); MONTH_OF_YEAR.checkValidValue(month); DAY_OF_MONTH.checkValidValue(dayOfMonth);
*** 265,286 **** //----------------------------------------------------------------------- /** * Obtains an instance of {@code LocalDate} from a year and day-of-year. * <p> * The day-of-year must be valid for the year, otherwise an exception will be thrown. * * @param year the year to represent, from MIN_YEAR to MAX_YEAR * @param dayOfYear the day-of-year to represent, from 1 to 366 * @return the local date, not null ! * @throws DateTimeException if the value of any field is out of range ! * @throws DateTimeException if the day-of-year is invalid for the month-year */ public static LocalDate ofYearDay(int year, int dayOfYear) { YEAR.checkValidValue(year); DAY_OF_YEAR.checkValidValue(dayOfYear); ! boolean leap = ISOChrono.INSTANCE.isLeapYear(year); if (dayOfYear == 366 && leap == false) { throw new DateTimeException("Invalid date 'DayOfYear 366' as '" + year + "' is not a leap year"); } Month moy = Month.of((dayOfYear - 1) / 31 + 1); int monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1; --- 263,285 ---- //----------------------------------------------------------------------- /** * Obtains an instance of {@code LocalDate} from a year and day-of-year. * <p> + * This returns a {@code LocalDate} with the specified year and day-of-year. * The day-of-year must be valid for the year, otherwise an exception will be thrown. * * @param year the year to represent, from MIN_YEAR to MAX_YEAR * @param dayOfYear the day-of-year to represent, from 1 to 366 * @return the local date, not null ! * @throws DateTimeException if the value of any field is out of range, ! * or if the day-of-year is invalid for the month-year */ public static LocalDate ofYearDay(int year, int dayOfYear) { YEAR.checkValidValue(year); DAY_OF_YEAR.checkValidValue(dayOfYear); ! boolean leap = IsoChronology.INSTANCE.isLeapYear(year); if (dayOfYear == 366 && leap == false) { throw new DateTimeException("Invalid date 'DayOfYear 366' as '" + year + "' is not a leap year"); } Month moy = Month.of((dayOfYear - 1) / 31 + 1); int monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1;
*** 293,304 **** //----------------------------------------------------------------------- /** * Obtains an instance of {@code LocalDate} from the epoch day count. * <p> ! * The Epoch Day count is a simple incrementing count of days ! * where day 0 is 1970-01-01. Negative numbers represent earlier days. * * @param epochDay the Epoch Day to convert, based on the epoch 1970-01-01 * @return the local date, not null * @throws DateTimeException if the epoch days exceeds the supported date range */ --- 292,304 ---- //----------------------------------------------------------------------- /** * Obtains an instance of {@code LocalDate} from the epoch day count. * <p> ! * This returns a {@code LocalDate} with the specified epoch-day. ! * The {@link ChronoField#EPOCH_DAY EPOCH_DAY} is a simple incrementing count ! * of days where day 0 is 1970-01-01. Negative numbers represent earlier days. * * @param epochDay the Epoch Day to convert, based on the epoch 1970-01-01 * @return the local date, not null * @throws DateTimeException if the epoch days exceeds the supported date range */
*** 336,393 **** //----------------------------------------------------------------------- /** * Obtains an instance of {@code LocalDate} from a temporal object. * <p> ! * A {@code TemporalAccessor} represents some form of date and time information. ! * This factory converts the arbitrary temporal object to an instance of {@code LocalDate}. * <p> ! * The conversion extracts 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) { ! if (temporal instanceof LocalDate) { ! return (LocalDate) temporal; ! } else if (temporal instanceof LocalDateTime) { ! return ((LocalDateTime) temporal).getDate(); ! } else if (temporal instanceof ZonedDateTime) { ! return ((ZonedDateTime) temporal).getDate(); ! } ! // handle builder as a special case ! if (temporal instanceof DateTimeBuilder) { ! DateTimeBuilder builder = (DateTimeBuilder) temporal; ! LocalDate date = builder.extract(LocalDate.class); ! if (date != null) { ! return date; ! } ! } ! try { ! return ofEpochDay(temporal.getLong(EPOCH_DAY)); ! } catch (DateTimeException ex) { ! throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " + temporal.getClass(), ex); } } //----------------------------------------------------------------------- /** * Obtains an instance of {@code LocalDate} from a text string such as {@code 2007-12-03}. * <p> * The string must represent a valid date and is parsed using ! * {@link java.time.format.DateTimeFormatters#isoLocalDate()}. * * @param text the text to parse such as "2007-12-03", not null * @return the parsed local date, not null * @throws DateTimeParseException if the text cannot be parsed */ public static LocalDate parse(CharSequence text) { ! return parse(text, DateTimeFormatters.isoLocalDate()); } /** * Obtains an instance of {@code LocalDate} from a text string using a specific formatter. * <p> --- 336,380 ---- //----------------------------------------------------------------------- /** * Obtains an instance of {@code LocalDate} from a temporal object. * <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; } //----------------------------------------------------------------------- /** * Obtains an instance of {@code LocalDate} from a text string such as {@code 2007-12-03}. * <p> * The string must represent a valid date and is parsed using ! * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE}. * * @param text the text to parse such as "2007-12-03", not null * @return the parsed local date, not null * @throws DateTimeParseException if the text cannot be parsed */ public static LocalDate parse(CharSequence text) { ! return parse(text, DateTimeFormatter.ISO_LOCAL_DATE); } /** * Obtains an instance of {@code LocalDate} from a text string using a specific formatter. * <p>
*** 412,422 **** * @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(ISOChrono.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 + "'"); } --- 399,409 ---- * @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 + "'"); }
*** 433,443 **** * @return the resolved date, not null */ private static LocalDate resolvePreviousValid(int year, int month, int day) { switch (month) { case 2: ! day = Math.min(day, ISOChrono.INSTANCE.isLeapYear(year) ? 29 : 28); break; case 4: case 6: case 9: case 11: --- 420,430 ---- * @return the resolved date, not null */ private static LocalDate resolvePreviousValid(int year, int month, int day) { switch (month) { case 2: ! day = Math.min(day, IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28); break; case 4: case 6: case 9: case 11:
*** 467,478 **** * This checks if this date can be queried for the specified field. * If false, then calling the {@link #range(TemporalField) range} and * {@link #get(TemporalField) get} methods will throw an exception. * <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-time. * The supported fields are: * <ul> * <li>{@code DAY_OF_WEEK} * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR} --- 454,463 ----
*** 488,498 **** * <li>{@code ERA} * </ul> * All other {@code ChronoField} instances will return false. * <p> * If the field is not a {@code ChronoField}, then the result of this method ! * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)} * passing {@code this} as the argument. * Whether the field is supported is determined by the field. * * @param field the field to check, null returns false * @return true if the field is supported on this date, false if not --- 473,483 ---- * <li>{@code ERA} * </ul> * All other {@code ChronoField} instances will return false. * <p> * If the field is not a {@code ChronoField}, then the result of this method ! * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} * passing {@code this} as the argument. * Whether the field is supported is determined by the field. * * @param field the field to check, null returns false * @return true if the field is supported on this date, false if not
*** 514,524 **** * 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.doRange(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 --- 499,509 ---- * 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
*** 538,548 **** } return field.range(); } throw new DateTimeException("Unsupported field: " + field.getName()); } ! return field.doRange(this); } /** * Gets the value of the specified field from this date as an {@code int}. * <p> --- 523,533 ---- } return field.range(); } throw new DateTimeException("Unsupported field: " + field.getName()); } ! return field.rangeRefinedBy(this); } /** * Gets the value of the specified field from this date as an {@code int}. * <p>
*** 556,566 **** * 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.doGet(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 --- 541,551 ---- * 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
*** 586,596 **** * 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.doGet(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 --- 571,581 ---- * 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
*** 606,616 **** if (field == EPOCH_MONTH) { return getEpochMonth(); } return get0(field); } ! return field.doGet(this); } private int get0(TemporalField field) { switch ((ChronoField) field) { case DAY_OF_WEEK: return getDayOfWeek().getValue(); --- 591,601 ---- if (field == EPOCH_MONTH) { return getEpochMonth(); } return get0(field); } ! return field.getFrom(this); } private int get0(TemporalField field) { switch ((ChronoField) field) { case DAY_OF_WEEK: return getDayOfWeek().getValue();
*** 636,687 **** //----------------------------------------------------------------------- /** * Gets the chronology of this date, which is the ISO calendar system. * <p> ! * The {@code Chrono} 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 ISOChrono getChrono() { ! return ISOChrono.INSTANCE; } /** * Gets the era applicable at this date. * <p> ! * The official ISO-8601 standard does not define eras, however {@code ISOChrono} does. * It defines two eras, 'CE' from year one onwards and 'BCE' from year zero backwards. * Since dates before the Julian-Gregorian cutover are not in line with history, * the cutover between 'BCE' and 'CE' is also not aligned with the commonly used * eras, often referred to using 'BC' and 'AD'. * <p> * Users of this class should typically ignore this method as it exists primarily * to fulfill the {@link ChronoLocalDate} contract where it is necessary to support * the Japanese calendar system. * <p> * The returned era will be a singleton capable of being compared with the constants ! * in {@link ISOChrono} using the {@code ==} operator. * ! * @return the {@code ISOChrono} era constant applicable at this date, not null */ @Override // override for Javadoc ! public Era<ISOChrono> getEra() { return ChronoLocalDate.super.getEra(); } /** * Gets the year field. * <p> * This method returns the primitive {@code int} value for the year. * <p> * The year returned by this method is proleptic as per {@code get(YEAR)}. ! * To obtain the year-of-era, use {@code get(YEAR_OF_ERA}. * * @return the year, from MIN_YEAR to MAX_YEAR */ public int getYear() { return year; --- 621,672 ---- //----------------------------------------------------------------------- /** * 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() { ! return IsoChronology.INSTANCE; } /** * Gets the era applicable at this date. * <p> ! * The official ISO-8601 standard does not define eras, however {@code IsoChronology} does. * It defines two eras, 'CE' from year one onwards and 'BCE' from year zero backwards. * Since dates before the Julian-Gregorian cutover are not in line with history, * the cutover between 'BCE' and 'CE' is also not aligned with the commonly used * eras, often referred to using 'BC' and 'AD'. * <p> * Users of this class should typically ignore this method as it exists primarily * to fulfill the {@link ChronoLocalDate} contract where it is necessary to support * the Japanese calendar system. * <p> * The returned era will be a singleton capable of being compared with the constants ! * in {@link IsoChronology} using the {@code ==} operator. * ! * @return the {@code IsoChronology} era constant applicable at this date, not null */ @Override // override for Javadoc ! public Era getEra() { return ChronoLocalDate.super.getEra(); } /** * Gets the year field. * <p> * This method returns the primitive {@code int} value for the year. * <p> * The year returned by this method is proleptic as per {@code get(YEAR)}. ! * To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}. * * @return the year, from MIN_YEAR to MAX_YEAR */ public int getYear() { return year;
*** 775,785 **** * * @return true if the year is leap, false otherwise */ @Override // override for Javadoc and performance public boolean isLeapYear() { ! return ISOChrono.INSTANCE.isLeapYear(year); } /** * Returns the length of the month represented by this date. * <p> --- 760,770 ---- * * @return true if the year is leap, false otherwise */ @Override // override for Javadoc and performance public boolean isLeapYear() { ! return IsoChronology.INSTANCE.isLeapYear(year); } /** * Returns the length of the month represented by this date. * <p>
*** 817,836 **** //----------------------------------------------------------------------- /** * Returns an adjusted copy of this date. * <p> ! * This returns a new {@code LocalDate}, based on this one, with the date adjusted. * 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.temporal.MonthDay MonthDay}. * The adjuster is responsible for handling special cases, such as the varying * lengths of month and leap years. * <p> * For example this code returns a date on the last day of July: * <pre> --- 802,821 ---- //----------------------------------------------------------------------- /** * Returns an adjusted copy of this date. * <p> ! * This returns a {@code LocalDate}, based on this one, with the date adjusted. * 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. * <p> * For example this code returns a date on the last day of July: * <pre>
*** 861,871 **** } /** * Returns a copy of this date with the specified field set to a new value. * <p> ! * This returns a new {@code LocalDate}, based on this one, with the value * for the specified field changed. * This can be used to change any supported field, such as the year, month or day-of-month. * If it is not possible to set the value, because the field is not supported or for * some other reason, an exception is thrown. * <p> --- 846,856 ---- } /** * Returns a copy of this date with the specified field set to a new value. * <p> ! * This returns a {@code LocalDate}, based on this one, with the value * for the specified field changed. * This can be used to change any supported field, such as the year, month or day-of-month. * If it is not possible to set the value, because the field is not supported or for * some other reason, an exception is thrown. * <p>
*** 949,959 **** * 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.doWith(Temporal, long)} * passing {@code this} as the argument. In this case, the field determines * whether and how to adjust the instant. * <p> * This instance is immutable and unaffected by this method call. * --- 934,944 ---- * 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. * <p> * This instance is immutable and unaffected by this method call. *
*** 983,993 **** 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.doWith(this, newValue); } //----------------------------------------------------------------------- /** * Returns a copy of this date with the year altered. --- 968,978 ---- 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); } //----------------------------------------------------------------------- /** * Returns a copy of this date with the year altered.
*** 1031,1042 **** * <p> * This instance is immutable and unaffected by this method call. * * @param dayOfMonth the day-of-month to set in the result, from 1 to 28-31 * @return a {@code LocalDate} based on this date with the requested day, not null ! * @throws DateTimeException if the day-of-month value is invalid ! * @throws DateTimeException if the day-of-month is invalid for the month-year */ public LocalDate withDayOfMonth(int dayOfMonth) { if (this.day == dayOfMonth) { return this; } --- 1016,1027 ---- * <p> * This instance is immutable and unaffected by this method call. * * @param dayOfMonth the day-of-month to set in the result, from 1 to 28-31 * @return a {@code LocalDate} based on this date with the requested day, not null ! * @throws DateTimeException if the day-of-month value is invalid, ! * or if the day-of-month is invalid for the month-year */ public LocalDate withDayOfMonth(int dayOfMonth) { if (this.day == dayOfMonth) { return this; }
*** 1049,1104 **** * <p> * This instance is immutable and unaffected by this method call. * * @param dayOfYear the day-of-year to set in the result, from 1 to 365-366 * @return a {@code LocalDate} based on this date with the requested day, not null ! * @throws DateTimeException if the day-of-year value is invalid ! * @throws DateTimeException if the day-of-year is invalid for the year */ public LocalDate withDayOfYear(int dayOfYear) { if (this.getDayOfYear() == dayOfYear) { return this; } return ofYearDay(year, dayOfYear); } //----------------------------------------------------------------------- /** ! * Returns a copy of this date with the specified period added. * <p> ! * This method returns a new date based on this date with the specified period added. ! * The adder is typically {@link Period} but may be any other type implementing ! * the {@link TemporalAdder} interface. ! * The calculation is delegated to the specified adjuster, which typically calls ! * back to {@link #plus(long, TemporalUnit)}. * <p> * This instance is immutable and unaffected by this method call. * ! * @param adder the adder to use, not null * @return a {@code LocalDate} based on this date with the addition made, not null * @throws DateTimeException if the addition cannot be made * @throws ArithmeticException if numeric overflow occurs */ @Override ! public LocalDate plus(TemporalAdder adder) { ! return (LocalDate) adder.addTo(this); } /** ! * Returns a copy of this date with the specified period added. * <p> ! * This method returns a new date based on this date with the specified period added. ! * This can be used to add any period that is defined by a unit, for example to add years, months or days. ! * The unit is responsible for the details of the calculation, including the resolution ! * of any edge cases in the calculation. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToAdd the amount of the unit to add to the result, may be negative ! * @param unit the unit of the period to add, not null ! * @return a {@code LocalDate} based on this date with the specified period added, not null ! * @throws DateTimeException if the unit cannot be added to this type */ @Override public LocalDate plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; --- 1034,1158 ---- * <p> * This instance is immutable and unaffected by this method call. * * @param dayOfYear the day-of-year to set in the result, from 1 to 365-366 * @return a {@code LocalDate} based on this date with the requested day, not null ! * @throws DateTimeException if the day-of-year value is invalid, ! * or if the day-of-year is invalid for the year */ public LocalDate withDayOfYear(int dayOfYear) { if (this.getDayOfYear() == dayOfYear) { return this; } return ofYearDay(year, dayOfYear); } //----------------------------------------------------------------------- /** ! * Returns a copy of this date with the specified amount added. ! * <p> ! * This returns a {@code LocalDate}, based on this one, with the specified amount added. ! * The amount is typically {@link Period} but may be any other type implementing ! * the {@link TemporalAmount} interface. * <p> ! * The calculation is delegated to the amount object by calling ! * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free ! * to implement the addition in any way it wishes, however it typically ! * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation ! * of the amount implementation to determine if it can be successfully added. * <p> * This instance is immutable and unaffected by this method call. * ! * @param amountToAdd the amount to add, not null * @return a {@code LocalDate} based on this date with the addition made, not null * @throws DateTimeException if the addition cannot be made * @throws ArithmeticException if numeric overflow occurs */ @Override ! public LocalDate plus(TemporalAmount amountToAdd) { ! return (LocalDate) amountToAdd.addTo(this); } /** ! * Returns a copy of this date with the specified amount added. ! * <p> ! * This returns a {@code LocalDate}, based on this one, with the amount ! * in terms of the unit added. If it is not possible to add the amount, because the ! * unit is not supported or for some other reason, an exception is thrown. ! * <p> ! * In some cases, adding the amount can cause the resulting date to become invalid. ! * For example, adding one month to 31st January would result in 31st February. ! * In cases like this, the unit is responsible for resolving the date. ! * Typically it will choose the previous valid date, which would be the last valid ! * day of February in this example. ! * <p> ! * If the field is a {@link ChronoUnit} then the addition is implemented here. ! * The supported fields behave as follows: ! * <ul> ! * <li>{@code DAYS} - ! * Returns a {@code LocalDate} with the specified number of days added. ! * This is equivalent to {@link #plusDays(long)}. ! * <li>{@code WEEKS} - ! * Returns a {@code LocalDate} with the specified number of weeks added. ! * This is equivalent to {@link #plusWeeks(long)} and uses a 7 day week. ! * <li>{@code MONTHS} - ! * Returns a {@code LocalDate} with the specified number of months added. ! * This is equivalent to {@link #plusMonths(long)}. ! * 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 YEARS} - ! * Returns a {@code LocalDate} with the specified number of years added. ! * This is equivalent to {@link #plusYears(long)}. ! * 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 DECADES} - ! * Returns a {@code LocalDate} with the specified number of decades added. ! * This is equivalent to calling {@link #plusYears(long)} with the amount ! * multiplied by 10. ! * 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 CENTURIES} - ! * Returns a {@code LocalDate} with the specified number of centuries added. ! * This is equivalent to calling {@link #plusYears(long)} with the amount ! * multiplied by 100. ! * 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 MILLENNIA} - ! * Returns a {@code LocalDate} with the specified number of millennia added. ! * This is equivalent to calling {@link #plusYears(long)} with the amount ! * multiplied by 1,000. ! * 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 ERAS} - ! * Returns a {@code LocalDate} with the specified number of eras added. ! * Only two eras are supported so the amount must be one, zero or minus one. ! * If the amount is non-zero then the year is changed such that the year-of-era ! * is unchanged. ! * 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. * <p> * This instance is immutable and unaffected by this method call. * * @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 ArithmeticException if numeric overflow occurs */ @Override public LocalDate plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit;
*** 1112,1122 **** 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.doPlus(this, amountToAdd); } //----------------------------------------------------------------------- /** * Returns a copy of this {@code LocalDate} with the specified period in years added. --- 1166,1176 ---- 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); } //----------------------------------------------------------------------- /** * Returns a copy of this {@code LocalDate} with the specified period in years added.
*** 1219,1262 **** return LocalDate.ofEpochDay(mjDay); } //----------------------------------------------------------------------- /** ! * Returns a copy of this date with the specified period subtracted. * <p> ! * This method returns a new date based on this date with the specified period subtracted. ! * The subtractor is typically {@link Period} but may be any other type implementing ! * the {@link TemporalSubtractor} interface. ! * The calculation is delegated to the specified adjuster, which typically calls ! * back to {@link #minus(long, TemporalUnit)}. * <p> * This instance is immutable and unaffected by this method call. * ! * @param subtractor the subtractor to use, not null * @return a {@code LocalDate} based on this date with the subtraction made, not null * @throws DateTimeException if the subtraction cannot be made * @throws ArithmeticException if numeric overflow occurs */ @Override ! public LocalDate minus(TemporalSubtractor subtractor) { ! return (LocalDate) subtractor.subtractFrom(this); } /** ! * Returns a copy of this date with the specified period subtracted. * <p> ! * This method returns a new date based on this date with the specified period subtracted. ! * This can be used to subtract any period that is defined by a unit, for example to subtract years, months or days. ! * The unit is responsible for the details of the calculation, including the resolution ! * of any edge cases in the calculation. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToSubtract the amount of the unit to subtract from the result, may be negative ! * @param unit the unit of the period to subtract, not null ! * @return a {@code LocalDate} based on this date with the specified period subtracted, not null ! * @throws DateTimeException if the unit cannot be added to this type */ @Override public LocalDate minus(long amountToSubtract, TemporalUnit unit) { return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); } --- 1273,1323 ---- return LocalDate.ofEpochDay(mjDay); } //----------------------------------------------------------------------- /** ! * Returns a copy of this date with the specified amount subtracted. * <p> ! * This returns a {@code LocalDate}, based on this one, with the specified amount subtracted. ! * The amount is typically {@link Period} but may be any other type implementing ! * the {@link TemporalAmount} interface. ! * <p> ! * The calculation is delegated to the amount object by calling ! * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free ! * to implement the subtraction in any way it wishes, however it typically ! * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation ! * of the amount implementation to determine if it can be successfully subtracted. * <p> * This instance is immutable and unaffected by this method call. * ! * @param amountToSubtract the amount to subtract, not null * @return a {@code LocalDate} based on this date with the subtraction made, not null * @throws DateTimeException if the subtraction cannot be made * @throws ArithmeticException if numeric overflow occurs */ @Override ! public LocalDate minus(TemporalAmount amountToSubtract) { ! return (LocalDate) amountToSubtract.subtractFrom(this); } /** ! * Returns a copy of this date with the specified amount subtracted. ! * <p> ! * This returns a {@code LocalDate}, based on this one, with the amount ! * in terms of the unit subtracted. If it is not possible to subtract the amount, ! * because the unit is not supported or for some other reason, an exception is thrown. * <p> ! * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated. ! * See that method for a full description of how addition, and thus subtraction, works. * <p> * This instance is immutable and unaffected by this method call. * * @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 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)); }
*** 1365,1376 **** * @param query the query to invoke, not null * @return the query result, null may be returned (defined by the query) * @throws DateTimeException if unable to query (defined by the query) * @throws ArithmeticException if numeric overflow occurs (defined by the query) */ ! @Override // override for Javadoc public <R> R query(TemporalQuery<R> query) { return ChronoLocalDate.super.query(query); } /** * Adjusts the specified temporal object to have the same date as this object. --- 1426,1441 ---- * @param query the query to invoke, not null * @return the query result, null may be returned (defined by the query) * @throws DateTimeException if unable to query (defined by the query) * @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); } /** * Adjusts the specified temporal object to have the same date as this object.
*** 1415,1432 **** * The calculation returns a whole number, representing the number of * complete units between the two dates. * For example, the period in months between 2012-06-15 and 2012-08-14 * will only be one month as it is one day short of two months. * <p> ! * This method operates in association with {@link TemporalUnit#between}. ! * The result of this method is a {@code long} representing the amount of ! * the specified unit. By contrast, the result of {@code between} is an ! * object that can be used directly in addition/subtraction: * <pre> ! * long period = start.periodUntil(end, MONTHS); // this method ! * dateTime.plus(MONTHS.between(start, end)); // use in plus/minus * </pre> * <p> * The calculation is implemented in this method for {@link ChronoUnit}. * The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS}, * {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} * are supported. Other {@code ChronoUnit} values will throw an exception. --- 1480,1498 ---- * The calculation returns a whole number, representing the number of * complete units between the two dates. * For example, the period in months between 2012-06-15 and 2012-08-14 * will only be one month as it is one day short of two months. * <p> ! * There are two equivalent ways of using this method. ! * The first is to invoke this method. ! * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: * <pre> ! * // these two lines are equivalent ! * amount = start.periodUntil(end, MONTHS); ! * amount = MONTHS.between(start, end); * </pre> + * The choice should be made based on which makes the code more readable. * <p> * The calculation is implemented in this method for {@link ChronoUnit}. * The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS}, * {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} * are supported. Other {@code ChronoUnit} values will throw an exception.
*** 1462,1472 **** 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).getAmount(); } long daysUntil(LocalDate end) { return end.toEpochDay() - toEpochDay(); // no overflow } --- 1528,1538 ---- 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 }
*** 1475,1509 **** long packed1 = getEpochMonth() * 32L + getDayOfMonth(); // no overflow long packed2 = end.getEpochMonth() * 32L + end.getDayOfMonth(); // no overflow return (packed2 - packed1) / 32; } //----------------------------------------------------------------------- /** ! * Returns a local date-time formed from this date at the specified time. * <p> ! * This combines this date with the specified time to form a {@code LocalDateTime}. * All possible combinations of date and time are valid. - * <p> - * This instance is immutable and unaffected by this method call. * * @param time the time to combine with, not null * @return the local date-time formed from this date and the specified time, not null */ @Override public LocalDateTime atTime(LocalTime time) { return LocalDateTime.of(this, time); } /** ! * Returns a local date-time formed from this date at the specified time. * <p> ! * This combines this date with the specified time to form a {@code LocalDateTime}. * The individual time fields must be within their valid range. * All possible combinations of date and time are valid. - * <p> - * This instance is immutable and unaffected by this method call. * * @param hour the hour-of-day to use, from 0 to 23 * @param minute the minute-of-hour to use, from 0 to 59 * @return the local date-time formed from this date and the specified time, not null * @throws DateTimeException if the value of any field is out of range --- 1541,1625 ---- 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. + * The period is calculated by removing complete months, then calculating + * the remaining number of days, adjusting to ensure that both have the same sign. + * 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 + * period = start.periodUntil(end); + * period = Period.between(start, end); + * </pre> + * The choice should be made based on which makes the code more readable. + * + * @param endDate the end date, exclusive, which may be in any chronology, not null + * @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 + } else if (totalMonths < 0 && days > 0) { + totalMonths++; + days -= end.lengthOfMonth(); + } + long years = totalMonths / 12; // safe + int months = (int) (totalMonths % 12); // safe + return Period.of(Math.toIntExact(years), months, days); + } + //----------------------------------------------------------------------- /** ! * 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. * All possible combinations of date and time are valid. * * @param time the time to combine with, not null * @return the local date-time formed from this date and the specified time, not null */ @Override public LocalDateTime atTime(LocalTime time) { return LocalDateTime.of(this, time); } /** ! * Combines this date with a time to create a {@code LocalDateTime}. * <p> ! * This returns a {@code LocalDateTime} formed from this date at the ! * specified hour and minute. ! * The seconds and nanosecond fields will be set to zero. * The individual time fields must be within their valid range. * All possible combinations of date and time are valid. * * @param hour the hour-of-day to use, from 0 to 23 * @param minute the minute-of-hour to use, from 0 to 59 * @return the local date-time formed from this date and the specified time, not null * @throws DateTimeException if the value of any field is out of range
*** 1511,1527 **** public LocalDateTime atTime(int hour, int minute) { return atTime(LocalTime.of(hour, minute)); } /** ! * Returns a local date-time formed from this date at the specified time. * <p> ! * This combines this date with the specified time to form a {@code LocalDateTime}. * The individual time fields must be within their valid range. * All possible combinations of date and time are valid. - * <p> - * This instance is immutable and unaffected by this method call. * * @param hour the hour-of-day to use, from 0 to 23 * @param minute the minute-of-hour to use, from 0 to 59 * @param second the second-of-minute to represent, from 0 to 59 * @return the local date-time formed from this date and the specified time, not null --- 1627,1643 ---- public LocalDateTime atTime(int hour, int minute) { return atTime(LocalTime.of(hour, minute)); } /** ! * Combines this date with a time to create a {@code LocalDateTime}. * <p> ! * This returns a {@code LocalDateTime} formed from this date at the ! * specified hour, minute and second. ! * The nanosecond field will be set to zero. * The individual time fields must be within their valid range. * All possible combinations of date and time are valid. * * @param hour the hour-of-day to use, from 0 to 23 * @param minute the minute-of-hour to use, from 0 to 59 * @param second the second-of-minute to represent, from 0 to 59 * @return the local date-time formed from this date and the specified time, not null
*** 1530,1546 **** public LocalDateTime atTime(int hour, int minute, int second) { return atTime(LocalTime.of(hour, minute, second)); } /** ! * Returns a local date-time formed from this date at the specified time. * <p> ! * This combines this date with the specified time to form a {@code LocalDateTime}. * The individual time fields must be within their valid range. * All possible combinations of date and time are valid. - * <p> - * This instance is immutable and unaffected by this method call. * * @param hour the hour-of-day to use, from 0 to 23 * @param minute the minute-of-hour to use, from 0 to 59 * @param second the second-of-minute to represent, from 0 to 59 * @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 --- 1646,1661 ---- public LocalDateTime atTime(int hour, int minute, int second) { return atTime(LocalTime.of(hour, minute, second)); } /** ! * Combines this date with a time to create a {@code LocalDateTime}. * <p> ! * This returns a {@code LocalDateTime} formed from this date at the ! * specified hour, minute, second and nanosecond. * The individual time fields must be within their valid range. * All possible combinations of date and time are valid. * * @param hour the hour-of-day to use, from 0 to 23 * @param minute the minute-of-hour to use, from 0 to 59 * @param second the second-of-minute to represent, from 0 to 59 * @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999
*** 1550,1571 **** public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond) { return atTime(LocalTime.of(hour, minute, second, nanoOfSecond)); } /** ! * Returns an offset date formed from this date and the specified offset. * <p> ! * This combines this date with the specified offset to form an {@code OffsetDate}. ! * All possible combinations of date and offset are valid. * <p> ! * This instance is immutable and unaffected by this method call. * ! * @param offset the offset to combine with, not null ! * @return the offset date formed from this date and the specified offset, not null */ ! public OffsetDate atOffset(ZoneOffset offset) { ! return OffsetDate.of(this, offset); } /** * Returns a zoned date-time from this date at the earliest valid time according * to the rules in the time-zone. --- 1665,1697 ---- public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond) { return atTime(LocalTime.of(hour, minute, second, nanoOfSecond)); } /** ! * Combines this date with an offset time to create an {@code OffsetDateTime}. * <p> ! * This returns an {@code OffsetDateTime} formed from this date at the specified time. ! * All possible combinations of date and time are valid. ! * ! * @param time the time to combine with, not null ! * @return the offset date-time formed from this date and the specified time, not null ! */ ! public OffsetDateTime atTime(OffsetTime time) { ! return OffsetDateTime.of(LocalDateTime.of(this, time.toLocalTime()), time.getOffset()); ! } ! ! /** ! * Combines this date with the time of midnight to create a {@code LocalDateTime} ! * at the start of this date. * <p> ! * This returns a {@code LocalDateTime} formed from this date at the time of ! * midnight, 00:00, at the start of this date. * ! * @return the local date-time of midnight at the start of this date, not null */ ! public LocalDateTime atStartOfDay() { ! return LocalDateTime.of(this, LocalTime.MIDNIGHT); } /** * Returns a zoned date-time from this date at the earliest valid time according * to the rules in the time-zone.
*** 1580,1591 **** * <p> * If the zone ID is a {@link ZoneOffset}, then the result always has a time of midnight. * <p> * To convert to a specific time in a given time-zone call {@link #atTime(LocalTime)} * followed by {@link LocalDateTime#atZone(ZoneId)}. - * <p> - * This instance is immutable and unaffected by this method call. * * @param zone the zone ID to use, not null * @return the zoned date-time formed from this date and the earliest valid time for the zone, not null */ public ZonedDateTime atStartOfDay(ZoneId zone) { --- 1706,1715 ----
*** 1634,1644 **** * It is "consistent with equals", as defined by {@link Comparable}. * <p> * If all the dates being compared are instances of {@code LocalDate}, * then the comparison will be entirely based on the date. * If some dates being compared are in different chronologies, then the ! * chronology is also considered, see {@link java.time.temporal.ChronoLocalDate#compareTo}. * * @param other the other date to compare to, not null * @return the comparator value, negative if less, positive if greater */ @Override // override for Javadoc and performance --- 1758,1768 ---- * It is "consistent with equals", as defined by {@link Comparable}. * <p> * If all the dates being compared are instances of {@code LocalDate}, * then the comparison will be entirely based on the date. * If some dates being compared are in different chronologies, then the ! * chronology is also considered, see {@link java.time.chrono.ChronoLocalDate#compareTo}. * * @param other the other date to compare to, not null * @return the comparator value, negative if less, positive if greater */ @Override // override for Javadoc and performance
*** 1820,1830 **** /** * Outputs this date as a {@code String} using the formatter. * <p> * This date will be passed to the formatter ! * {@link DateTimeFormatter#print(TemporalAccessor) print method}. * * @param formatter the formatter to use, not null * @return the formatted date string, not null * @throws DateTimeException if an error occurs during printing */ --- 1944,1954 ---- /** * 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 */