78 import java.io.DataOutput; |
78 import java.io.DataOutput; |
79 import java.io.IOException; |
79 import java.io.IOException; |
80 import java.io.InvalidObjectException; |
80 import java.io.InvalidObjectException; |
81 import java.io.ObjectStreamException; |
81 import java.io.ObjectStreamException; |
82 import java.io.Serializable; |
82 import java.io.Serializable; |
83 import java.time.format.DateTimeBuilder; |
83 import java.time.chrono.ChronoLocalDate; |
|
84 import java.time.chrono.Era; |
|
85 import java.time.chrono.IsoChronology; |
84 import java.time.format.DateTimeFormatter; |
86 import java.time.format.DateTimeFormatter; |
85 import java.time.format.DateTimeFormatters; |
|
86 import java.time.format.DateTimeParseException; |
87 import java.time.format.DateTimeParseException; |
87 import java.time.temporal.ChronoField; |
88 import java.time.temporal.ChronoField; |
88 import java.time.temporal.ChronoLocalDate; |
|
89 import java.time.temporal.ChronoUnit; |
89 import java.time.temporal.ChronoUnit; |
90 import java.time.temporal.Era; |
90 import java.time.temporal.Queries; |
91 import java.time.temporal.ISOChrono; |
|
92 import java.time.temporal.OffsetDate; |
|
93 import java.time.temporal.Temporal; |
91 import java.time.temporal.Temporal; |
94 import java.time.temporal.TemporalAccessor; |
92 import java.time.temporal.TemporalAccessor; |
95 import java.time.temporal.TemporalAdder; |
|
96 import java.time.temporal.TemporalAdjuster; |
93 import java.time.temporal.TemporalAdjuster; |
|
94 import java.time.temporal.TemporalAmount; |
97 import java.time.temporal.TemporalField; |
95 import java.time.temporal.TemporalField; |
98 import java.time.temporal.TemporalQuery; |
96 import java.time.temporal.TemporalQuery; |
99 import java.time.temporal.TemporalSubtractor; |
|
100 import java.time.temporal.TemporalUnit; |
97 import java.time.temporal.TemporalUnit; |
101 import java.time.temporal.ValueRange; |
98 import java.time.temporal.ValueRange; |
102 import java.time.temporal.Year; |
|
103 import java.time.zone.ZoneOffsetTransition; |
99 import java.time.zone.ZoneOffsetTransition; |
104 import java.time.zone.ZoneRules; |
100 import java.time.zone.ZoneRules; |
105 import java.util.Objects; |
101 import java.util.Objects; |
106 |
102 |
107 /** |
103 /** |
226 |
222 |
227 //----------------------------------------------------------------------- |
223 //----------------------------------------------------------------------- |
228 /** |
224 /** |
229 * Obtains an instance of {@code LocalDate} from a year, month and day. |
225 * Obtains an instance of {@code LocalDate} from a year, month and day. |
230 * <p> |
226 * <p> |
|
227 * This returns a {@code LocalDate} with the specified year, month and day-of-month. |
231 * The day must be valid for the year and month, otherwise an exception will be thrown. |
228 * The day must be valid for the year and month, otherwise an exception will be thrown. |
232 * |
229 * |
233 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
230 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
234 * @param month the month-of-year to represent, not null |
231 * @param month the month-of-year to represent, not null |
235 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
232 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
236 * @return the local date, not null |
233 * @return the local date, not null |
237 * @throws DateTimeException if the value of any field is out of range |
234 * @throws DateTimeException if the value of any field is out of range, |
238 * @throws DateTimeException if the day-of-month is invalid for the month-year |
235 * or if the day-of-month is invalid for the month-year |
239 */ |
236 */ |
240 public static LocalDate of(int year, Month month, int dayOfMonth) { |
237 public static LocalDate of(int year, Month month, int dayOfMonth) { |
241 YEAR.checkValidValue(year); |
238 YEAR.checkValidValue(year); |
242 Objects.requireNonNull(month, "month"); |
239 Objects.requireNonNull(month, "month"); |
243 DAY_OF_MONTH.checkValidValue(dayOfMonth); |
240 DAY_OF_MONTH.checkValidValue(dayOfMonth); |
245 } |
242 } |
246 |
243 |
247 /** |
244 /** |
248 * Obtains an instance of {@code LocalDate} from a year, month and day. |
245 * Obtains an instance of {@code LocalDate} from a year, month and day. |
249 * <p> |
246 * <p> |
|
247 * This returns a {@code LocalDate} with the specified year, month and day-of-month. |
250 * The day must be valid for the year and month, otherwise an exception will be thrown. |
248 * The day must be valid for the year and month, otherwise an exception will be thrown. |
251 * |
249 * |
252 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
250 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
253 * @param month the month-of-year to represent, from 1 (January) to 12 (December) |
251 * @param month the month-of-year to represent, from 1 (January) to 12 (December) |
254 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
252 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
255 * @return the local date, not null |
253 * @return the local date, not null |
256 * @throws DateTimeException if the value of any field is out of range |
254 * @throws DateTimeException if the value of any field is out of range, |
257 * @throws DateTimeException if the day-of-month is invalid for the month-year |
255 * or if the day-of-month is invalid for the month-year |
258 */ |
256 */ |
259 public static LocalDate of(int year, int month, int dayOfMonth) { |
257 public static LocalDate of(int year, int month, int dayOfMonth) { |
260 YEAR.checkValidValue(year); |
258 YEAR.checkValidValue(year); |
261 MONTH_OF_YEAR.checkValidValue(month); |
259 MONTH_OF_YEAR.checkValidValue(month); |
262 DAY_OF_MONTH.checkValidValue(dayOfMonth); |
260 DAY_OF_MONTH.checkValidValue(dayOfMonth); |
265 |
263 |
266 //----------------------------------------------------------------------- |
264 //----------------------------------------------------------------------- |
267 /** |
265 /** |
268 * Obtains an instance of {@code LocalDate} from a year and day-of-year. |
266 * Obtains an instance of {@code LocalDate} from a year and day-of-year. |
269 * <p> |
267 * <p> |
|
268 * This returns a {@code LocalDate} with the specified year and day-of-year. |
270 * The day-of-year must be valid for the year, otherwise an exception will be thrown. |
269 * The day-of-year must be valid for the year, otherwise an exception will be thrown. |
271 * |
270 * |
272 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
271 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
273 * @param dayOfYear the day-of-year to represent, from 1 to 366 |
272 * @param dayOfYear the day-of-year to represent, from 1 to 366 |
274 * @return the local date, not null |
273 * @return the local date, not null |
275 * @throws DateTimeException if the value of any field is out of range |
274 * @throws DateTimeException if the value of any field is out of range, |
276 * @throws DateTimeException if the day-of-year is invalid for the month-year |
275 * or if the day-of-year is invalid for the month-year |
277 */ |
276 */ |
278 public static LocalDate ofYearDay(int year, int dayOfYear) { |
277 public static LocalDate ofYearDay(int year, int dayOfYear) { |
279 YEAR.checkValidValue(year); |
278 YEAR.checkValidValue(year); |
280 DAY_OF_YEAR.checkValidValue(dayOfYear); |
279 DAY_OF_YEAR.checkValidValue(dayOfYear); |
281 boolean leap = ISOChrono.INSTANCE.isLeapYear(year); |
280 boolean leap = IsoChronology.INSTANCE.isLeapYear(year); |
282 if (dayOfYear == 366 && leap == false) { |
281 if (dayOfYear == 366 && leap == false) { |
283 throw new DateTimeException("Invalid date 'DayOfYear 366' as '" + year + "' is not a leap year"); |
282 throw new DateTimeException("Invalid date 'DayOfYear 366' as '" + year + "' is not a leap year"); |
284 } |
283 } |
285 Month moy = Month.of((dayOfYear - 1) / 31 + 1); |
284 Month moy = Month.of((dayOfYear - 1) / 31 + 1); |
286 int monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1; |
285 int monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1; |
336 |
336 |
337 //----------------------------------------------------------------------- |
337 //----------------------------------------------------------------------- |
338 /** |
338 /** |
339 * Obtains an instance of {@code LocalDate} from a temporal object. |
339 * Obtains an instance of {@code LocalDate} from a temporal object. |
340 * <p> |
340 * <p> |
341 * A {@code TemporalAccessor} represents some form of date and time information. |
341 * This obtains a local date based on the specified temporal. |
342 * This factory converts the arbitrary temporal object to an instance of {@code LocalDate}. |
342 * A {@code TemporalAccessor} represents an arbitrary set of date and time information, |
343 * <p> |
343 * which this factory converts to an instance of {@code LocalDate}. |
344 * The conversion extracts the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field. |
344 * <p> |
|
345 * The conversion uses the {@link Queries#localDate()} query, which relies |
|
346 * on extracting the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field. |
345 * <p> |
347 * <p> |
346 * This method matches the signature of the functional interface {@link TemporalQuery} |
348 * This method matches the signature of the functional interface {@link TemporalQuery} |
347 * allowing it to be used as a query via method reference, {@code LocalDate::from}. |
349 * allowing it to be used as a query via method reference, {@code LocalDate::from}. |
348 * |
350 * |
349 * @param temporal the temporal object to convert, not null |
351 * @param temporal the temporal object to convert, not null |
350 * @return the local date, not null |
352 * @return the local date, not null |
351 * @throws DateTimeException if unable to convert to a {@code LocalDate} |
353 * @throws DateTimeException if unable to convert to a {@code LocalDate} |
352 */ |
354 */ |
353 public static LocalDate from(TemporalAccessor temporal) { |
355 public static LocalDate from(TemporalAccessor temporal) { |
354 if (temporal instanceof LocalDate) { |
356 LocalDate date = temporal.query(Queries.localDate()); |
355 return (LocalDate) temporal; |
357 if (date == null) { |
356 } else if (temporal instanceof LocalDateTime) { |
358 throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " + temporal.getClass()); |
357 return ((LocalDateTime) temporal).getDate(); |
359 } |
358 } else if (temporal instanceof ZonedDateTime) { |
360 return date; |
359 return ((ZonedDateTime) temporal).getDate(); |
|
360 } |
|
361 // handle builder as a special case |
|
362 if (temporal instanceof DateTimeBuilder) { |
|
363 DateTimeBuilder builder = (DateTimeBuilder) temporal; |
|
364 LocalDate date = builder.extract(LocalDate.class); |
|
365 if (date != null) { |
|
366 return date; |
|
367 } |
|
368 } |
|
369 try { |
|
370 return ofEpochDay(temporal.getLong(EPOCH_DAY)); |
|
371 } catch (DateTimeException ex) { |
|
372 throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " + temporal.getClass(), ex); |
|
373 } |
|
374 } |
361 } |
375 |
362 |
376 //----------------------------------------------------------------------- |
363 //----------------------------------------------------------------------- |
377 /** |
364 /** |
378 * Obtains an instance of {@code LocalDate} from a text string such as {@code 2007-12-03}. |
365 * Obtains an instance of {@code LocalDate} from a text string such as {@code 2007-12-03}. |
379 * <p> |
366 * <p> |
380 * The string must represent a valid date and is parsed using |
367 * The string must represent a valid date and is parsed using |
381 * {@link java.time.format.DateTimeFormatters#isoLocalDate()}. |
368 * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE}. |
382 * |
369 * |
383 * @param text the text to parse such as "2007-12-03", not null |
370 * @param text the text to parse such as "2007-12-03", not null |
384 * @return the parsed local date, not null |
371 * @return the parsed local date, not null |
385 * @throws DateTimeParseException if the text cannot be parsed |
372 * @throws DateTimeParseException if the text cannot be parsed |
386 */ |
373 */ |
387 public static LocalDate parse(CharSequence text) { |
374 public static LocalDate parse(CharSequence text) { |
388 return parse(text, DateTimeFormatters.isoLocalDate()); |
375 return parse(text, DateTimeFormatter.ISO_LOCAL_DATE); |
389 } |
376 } |
390 |
377 |
391 /** |
378 /** |
392 * Obtains an instance of {@code LocalDate} from a text string using a specific formatter. |
379 * Obtains an instance of {@code LocalDate} from a text string using a specific formatter. |
393 * <p> |
380 * <p> |
636 |
621 |
637 //----------------------------------------------------------------------- |
622 //----------------------------------------------------------------------- |
638 /** |
623 /** |
639 * Gets the chronology of this date, which is the ISO calendar system. |
624 * Gets the chronology of this date, which is the ISO calendar system. |
640 * <p> |
625 * <p> |
641 * The {@code Chrono} represents the calendar system in use. |
626 * The {@code Chronology} represents the calendar system in use. |
642 * The ISO-8601 calendar system is the modern civil calendar system used today |
627 * The ISO-8601 calendar system is the modern civil calendar system used today |
643 * in most of the world. It is equivalent to the proleptic Gregorian calendar |
628 * in most of the world. It is equivalent to the proleptic Gregorian calendar |
644 * system, in which todays's rules for leap years are applied for all time. |
629 * system, in which todays's rules for leap years are applied for all time. |
645 * |
630 * |
646 * @return the ISO chronology, not null |
631 * @return the ISO chronology, not null |
647 */ |
632 */ |
648 @Override |
633 @Override |
649 public ISOChrono getChrono() { |
634 public IsoChronology getChronology() { |
650 return ISOChrono.INSTANCE; |
635 return IsoChronology.INSTANCE; |
651 } |
636 } |
652 |
637 |
653 /** |
638 /** |
654 * Gets the era applicable at this date. |
639 * Gets the era applicable at this date. |
655 * <p> |
640 * <p> |
656 * The official ISO-8601 standard does not define eras, however {@code ISOChrono} does. |
641 * The official ISO-8601 standard does not define eras, however {@code IsoChronology} does. |
657 * It defines two eras, 'CE' from year one onwards and 'BCE' from year zero backwards. |
642 * It defines two eras, 'CE' from year one onwards and 'BCE' from year zero backwards. |
658 * Since dates before the Julian-Gregorian cutover are not in line with history, |
643 * Since dates before the Julian-Gregorian cutover are not in line with history, |
659 * the cutover between 'BCE' and 'CE' is also not aligned with the commonly used |
644 * the cutover between 'BCE' and 'CE' is also not aligned with the commonly used |
660 * eras, often referred to using 'BC' and 'AD'. |
645 * eras, often referred to using 'BC' and 'AD'. |
661 * <p> |
646 * <p> |
662 * Users of this class should typically ignore this method as it exists primarily |
647 * Users of this class should typically ignore this method as it exists primarily |
663 * to fulfill the {@link ChronoLocalDate} contract where it is necessary to support |
648 * to fulfill the {@link ChronoLocalDate} contract where it is necessary to support |
664 * the Japanese calendar system. |
649 * the Japanese calendar system. |
665 * <p> |
650 * <p> |
666 * The returned era will be a singleton capable of being compared with the constants |
651 * The returned era will be a singleton capable of being compared with the constants |
667 * in {@link ISOChrono} using the {@code ==} operator. |
652 * in {@link IsoChronology} using the {@code ==} operator. |
668 * |
653 * |
669 * @return the {@code ISOChrono} era constant applicable at this date, not null |
654 * @return the {@code IsoChronology} era constant applicable at this date, not null |
670 */ |
655 */ |
671 @Override // override for Javadoc |
656 @Override // override for Javadoc |
672 public Era<ISOChrono> getEra() { |
657 public Era getEra() { |
673 return ChronoLocalDate.super.getEra(); |
658 return ChronoLocalDate.super.getEra(); |
674 } |
659 } |
675 |
660 |
676 /** |
661 /** |
677 * Gets the year field. |
662 * Gets the year field. |
678 * <p> |
663 * <p> |
679 * This method returns the primitive {@code int} value for the year. |
664 * This method returns the primitive {@code int} value for the year. |
680 * <p> |
665 * <p> |
681 * The year returned by this method is proleptic as per {@code get(YEAR)}. |
666 * The year returned by this method is proleptic as per {@code get(YEAR)}. |
682 * To obtain the year-of-era, use {@code get(YEAR_OF_ERA}. |
667 * To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}. |
683 * |
668 * |
684 * @return the year, from MIN_YEAR to MAX_YEAR |
669 * @return the year, from MIN_YEAR to MAX_YEAR |
685 */ |
670 */ |
686 public int getYear() { |
671 public int getYear() { |
687 return year; |
672 return year; |
817 |
802 |
818 //----------------------------------------------------------------------- |
803 //----------------------------------------------------------------------- |
819 /** |
804 /** |
820 * Returns an adjusted copy of this date. |
805 * Returns an adjusted copy of this date. |
821 * <p> |
806 * <p> |
822 * This returns a new {@code LocalDate}, based on this one, with the date adjusted. |
807 * This returns a {@code LocalDate}, based on this one, with the date adjusted. |
823 * The adjustment takes place using the specified adjuster strategy object. |
808 * The adjustment takes place using the specified adjuster strategy object. |
824 * Read the documentation of the adjuster to understand what adjustment will be made. |
809 * Read the documentation of the adjuster to understand what adjustment will be made. |
825 * <p> |
810 * <p> |
826 * A simple adjuster might simply set the one of the fields, such as the year field. |
811 * A simple adjuster might simply set the one of the fields, such as the year field. |
827 * A more complex adjuster might set the date to the last day of the month. |
812 * A more complex adjuster might set the date to the last day of the month. |
828 * A selection of common adjustments is provided in {@link java.time.temporal.Adjusters}. |
813 * A selection of common adjustments is provided in {@link java.time.temporal.Adjusters}. |
829 * These include finding the "last day of the month" and "next Wednesday". |
814 * These include finding the "last day of the month" and "next Wednesday". |
830 * Key date-time classes also implement the {@code TemporalAdjuster} interface, |
815 * Key date-time classes also implement the {@code TemporalAdjuster} interface, |
831 * such as {@link Month} and {@link java.time.temporal.MonthDay MonthDay}. |
816 * such as {@link Month} and {@link java.time.MonthDay MonthDay}. |
832 * The adjuster is responsible for handling special cases, such as the varying |
817 * The adjuster is responsible for handling special cases, such as the varying |
833 * lengths of month and leap years. |
818 * lengths of month and leap years. |
834 * <p> |
819 * <p> |
835 * For example this code returns a date on the last day of July: |
820 * For example this code returns a date on the last day of July: |
836 * <pre> |
821 * <pre> |
1049 * <p> |
1034 * <p> |
1050 * This instance is immutable and unaffected by this method call. |
1035 * This instance is immutable and unaffected by this method call. |
1051 * |
1036 * |
1052 * @param dayOfYear the day-of-year to set in the result, from 1 to 365-366 |
1037 * @param dayOfYear the day-of-year to set in the result, from 1 to 365-366 |
1053 * @return a {@code LocalDate} based on this date with the requested day, not null |
1038 * @return a {@code LocalDate} based on this date with the requested day, not null |
1054 * @throws DateTimeException if the day-of-year value is invalid |
1039 * @throws DateTimeException if the day-of-year value is invalid, |
1055 * @throws DateTimeException if the day-of-year is invalid for the year |
1040 * or if the day-of-year is invalid for the year |
1056 */ |
1041 */ |
1057 public LocalDate withDayOfYear(int dayOfYear) { |
1042 public LocalDate withDayOfYear(int dayOfYear) { |
1058 if (this.getDayOfYear() == dayOfYear) { |
1043 if (this.getDayOfYear() == dayOfYear) { |
1059 return this; |
1044 return this; |
1060 } |
1045 } |
1061 return ofYearDay(year, dayOfYear); |
1046 return ofYearDay(year, dayOfYear); |
1062 } |
1047 } |
1063 |
1048 |
1064 //----------------------------------------------------------------------- |
1049 //----------------------------------------------------------------------- |
1065 /** |
1050 /** |
1066 * Returns a copy of this date with the specified period added. |
1051 * Returns a copy of this date with the specified amount added. |
1067 * <p> |
1052 * <p> |
1068 * This method returns a new date based on this date with the specified period added. |
1053 * This returns a {@code LocalDate}, based on this one, with the specified amount added. |
1069 * The adder is typically {@link Period} but may be any other type implementing |
1054 * The amount is typically {@link Period} but may be any other type implementing |
1070 * the {@link TemporalAdder} interface. |
1055 * the {@link TemporalAmount} interface. |
1071 * The calculation is delegated to the specified adjuster, which typically calls |
1056 * <p> |
1072 * back to {@link #plus(long, TemporalUnit)}. |
1057 * The calculation is delegated to the amount object by calling |
|
1058 * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free |
|
1059 * to implement the addition in any way it wishes, however it typically |
|
1060 * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation |
|
1061 * of the amount implementation to determine if it can be successfully added. |
1073 * <p> |
1062 * <p> |
1074 * This instance is immutable and unaffected by this method call. |
1063 * This instance is immutable and unaffected by this method call. |
1075 * |
1064 * |
1076 * @param adder the adder to use, not null |
1065 * @param amountToAdd the amount to add, not null |
1077 * @return a {@code LocalDate} based on this date with the addition made, not null |
1066 * @return a {@code LocalDate} based on this date with the addition made, not null |
1078 * @throws DateTimeException if the addition cannot be made |
1067 * @throws DateTimeException if the addition cannot be made |
1079 * @throws ArithmeticException if numeric overflow occurs |
1068 * @throws ArithmeticException if numeric overflow occurs |
1080 */ |
1069 */ |
1081 @Override |
1070 @Override |
1082 public LocalDate plus(TemporalAdder adder) { |
1071 public LocalDate plus(TemporalAmount amountToAdd) { |
1083 return (LocalDate) adder.addTo(this); |
1072 return (LocalDate) amountToAdd.addTo(this); |
1084 } |
1073 } |
1085 |
1074 |
1086 /** |
1075 /** |
1087 * Returns a copy of this date with the specified period added. |
1076 * Returns a copy of this date with the specified amount added. |
1088 * <p> |
1077 * <p> |
1089 * This method returns a new date based on this date with the specified period added. |
1078 * This returns a {@code LocalDate}, based on this one, with the amount |
1090 * This can be used to add any period that is defined by a unit, for example to add years, months or days. |
1079 * in terms of the unit added. If it is not possible to add the amount, because the |
1091 * The unit is responsible for the details of the calculation, including the resolution |
1080 * unit is not supported or for some other reason, an exception is thrown. |
1092 * of any edge cases in the calculation. |
1081 * <p> |
|
1082 * In some cases, adding the amount can cause the resulting date to become invalid. |
|
1083 * For example, adding one month to 31st January would result in 31st February. |
|
1084 * In cases like this, the unit is responsible for resolving the date. |
|
1085 * Typically it will choose the previous valid date, which would be the last valid |
|
1086 * day of February in this example. |
|
1087 * <p> |
|
1088 * If the field is a {@link ChronoUnit} then the addition is implemented here. |
|
1089 * The supported fields behave as follows: |
|
1090 * <ul> |
|
1091 * <li>{@code DAYS} - |
|
1092 * Returns a {@code LocalDate} with the specified number of days added. |
|
1093 * This is equivalent to {@link #plusDays(long)}. |
|
1094 * <li>{@code WEEKS} - |
|
1095 * Returns a {@code LocalDate} with the specified number of weeks added. |
|
1096 * This is equivalent to {@link #plusWeeks(long)} and uses a 7 day week. |
|
1097 * <li>{@code MONTHS} - |
|
1098 * Returns a {@code LocalDate} with the specified number of months added. |
|
1099 * This is equivalent to {@link #plusMonths(long)}. |
|
1100 * The day-of-month will be unchanged unless it would be invalid for the new |
|
1101 * month and year. In that case, the day-of-month is adjusted to the maximum |
|
1102 * valid value for the new month and year. |
|
1103 * <li>{@code YEARS} - |
|
1104 * Returns a {@code LocalDate} with the specified number of years added. |
|
1105 * This is equivalent to {@link #plusYears(long)}. |
|
1106 * The day-of-month will be unchanged unless it would be invalid for the new |
|
1107 * month and year. In that case, the day-of-month is adjusted to the maximum |
|
1108 * valid value for the new month and year. |
|
1109 * <li>{@code DECADES} - |
|
1110 * Returns a {@code LocalDate} with the specified number of decades added. |
|
1111 * This is equivalent to calling {@link #plusYears(long)} with the amount |
|
1112 * multiplied by 10. |
|
1113 * The day-of-month will be unchanged unless it would be invalid for the new |
|
1114 * month and year. In that case, the day-of-month is adjusted to the maximum |
|
1115 * valid value for the new month and year. |
|
1116 * <li>{@code CENTURIES} - |
|
1117 * Returns a {@code LocalDate} with the specified number of centuries added. |
|
1118 * This is equivalent to calling {@link #plusYears(long)} with the amount |
|
1119 * multiplied by 100. |
|
1120 * The day-of-month will be unchanged unless it would be invalid for the new |
|
1121 * month and year. In that case, the day-of-month is adjusted to the maximum |
|
1122 * valid value for the new month and year. |
|
1123 * <li>{@code MILLENNIA} - |
|
1124 * Returns a {@code LocalDate} with the specified number of millennia added. |
|
1125 * This is equivalent to calling {@link #plusYears(long)} with the amount |
|
1126 * multiplied by 1,000. |
|
1127 * The day-of-month will be unchanged unless it would be invalid for the new |
|
1128 * month and year. In that case, the day-of-month is adjusted to the maximum |
|
1129 * valid value for the new month and year. |
|
1130 * <li>{@code ERAS} - |
|
1131 * Returns a {@code LocalDate} with the specified number of eras added. |
|
1132 * Only two eras are supported so the amount must be one, zero or minus one. |
|
1133 * If the amount is non-zero then the year is changed such that the year-of-era |
|
1134 * is unchanged. |
|
1135 * The day-of-month will be unchanged unless it would be invalid for the new |
|
1136 * month and year. In that case, the day-of-month is adjusted to the maximum |
|
1137 * valid value for the new month and year. |
|
1138 * </ul> |
|
1139 * <p> |
|
1140 * All other {@code ChronoUnit} instances will throw a {@code DateTimeException}. |
|
1141 * <p> |
|
1142 * If the field is not a {@code ChronoUnit}, then the result of this method |
|
1143 * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)} |
|
1144 * passing {@code this} as the argument. In this case, the unit determines |
|
1145 * whether and how to perform the addition. |
1093 * <p> |
1146 * <p> |
1094 * This instance is immutable and unaffected by this method call. |
1147 * This instance is immutable and unaffected by this method call. |
1095 * |
1148 * |
1096 * @param amountToAdd the amount of the unit to add to the result, may be negative |
1149 * @param amountToAdd the amount of the unit to add to the result, may be negative |
1097 * @param unit the unit of the period to add, not null |
1150 * @param unit the unit of the amount to add, not null |
1098 * @return a {@code LocalDate} based on this date with the specified period added, not null |
1151 * @return a {@code LocalDate} based on this date with the specified amount added, not null |
1099 * @throws DateTimeException if the unit cannot be added to this type |
1152 * @throws DateTimeException if the addition cannot be made |
|
1153 * @throws ArithmeticException if numeric overflow occurs |
1100 */ |
1154 */ |
1101 @Override |
1155 @Override |
1102 public LocalDate plus(long amountToAdd, TemporalUnit unit) { |
1156 public LocalDate plus(long amountToAdd, TemporalUnit unit) { |
1103 if (unit instanceof ChronoUnit) { |
1157 if (unit instanceof ChronoUnit) { |
1104 ChronoUnit f = (ChronoUnit) unit; |
1158 ChronoUnit f = (ChronoUnit) unit; |
1219 return LocalDate.ofEpochDay(mjDay); |
1273 return LocalDate.ofEpochDay(mjDay); |
1220 } |
1274 } |
1221 |
1275 |
1222 //----------------------------------------------------------------------- |
1276 //----------------------------------------------------------------------- |
1223 /** |
1277 /** |
1224 * Returns a copy of this date with the specified period subtracted. |
1278 * Returns a copy of this date with the specified amount subtracted. |
1225 * <p> |
1279 * <p> |
1226 * This method returns a new date based on this date with the specified period subtracted. |
1280 * This returns a {@code LocalDate}, based on this one, with the specified amount subtracted. |
1227 * The subtractor is typically {@link Period} but may be any other type implementing |
1281 * The amount is typically {@link Period} but may be any other type implementing |
1228 * the {@link TemporalSubtractor} interface. |
1282 * the {@link TemporalAmount} interface. |
1229 * The calculation is delegated to the specified adjuster, which typically calls |
1283 * <p> |
1230 * back to {@link #minus(long, TemporalUnit)}. |
1284 * The calculation is delegated to the amount object by calling |
|
1285 * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free |
|
1286 * to implement the subtraction in any way it wishes, however it typically |
|
1287 * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation |
|
1288 * of the amount implementation to determine if it can be successfully subtracted. |
1231 * <p> |
1289 * <p> |
1232 * This instance is immutable and unaffected by this method call. |
1290 * This instance is immutable and unaffected by this method call. |
1233 * |
1291 * |
1234 * @param subtractor the subtractor to use, not null |
1292 * @param amountToSubtract the amount to subtract, not null |
1235 * @return a {@code LocalDate} based on this date with the subtraction made, not null |
1293 * @return a {@code LocalDate} based on this date with the subtraction made, not null |
1236 * @throws DateTimeException if the subtraction cannot be made |
1294 * @throws DateTimeException if the subtraction cannot be made |
1237 * @throws ArithmeticException if numeric overflow occurs |
1295 * @throws ArithmeticException if numeric overflow occurs |
1238 */ |
1296 */ |
1239 @Override |
1297 @Override |
1240 public LocalDate minus(TemporalSubtractor subtractor) { |
1298 public LocalDate minus(TemporalAmount amountToSubtract) { |
1241 return (LocalDate) subtractor.subtractFrom(this); |
1299 return (LocalDate) amountToSubtract.subtractFrom(this); |
1242 } |
1300 } |
1243 |
1301 |
1244 /** |
1302 /** |
1245 * Returns a copy of this date with the specified period subtracted. |
1303 * Returns a copy of this date with the specified amount subtracted. |
1246 * <p> |
1304 * <p> |
1247 * This method returns a new date based on this date with the specified period subtracted. |
1305 * This returns a {@code LocalDate}, based on this one, with the amount |
1248 * This can be used to subtract any period that is defined by a unit, for example to subtract years, months or days. |
1306 * in terms of the unit subtracted. If it is not possible to subtract the amount, |
1249 * The unit is responsible for the details of the calculation, including the resolution |
1307 * because the unit is not supported or for some other reason, an exception is thrown. |
1250 * of any edge cases in the calculation. |
1308 * <p> |
|
1309 * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated. |
|
1310 * See that method for a full description of how addition, and thus subtraction, works. |
1251 * <p> |
1311 * <p> |
1252 * This instance is immutable and unaffected by this method call. |
1312 * This instance is immutable and unaffected by this method call. |
1253 * |
1313 * |
1254 * @param amountToSubtract the amount of the unit to subtract from the result, may be negative |
1314 * @param amountToSubtract the amount of the unit to subtract from the result, may be negative |
1255 * @param unit the unit of the period to subtract, not null |
1315 * @param unit the unit of the amount to subtract, not null |
1256 * @return a {@code LocalDate} based on this date with the specified period subtracted, not null |
1316 * @return a {@code LocalDate} based on this date with the specified amount subtracted, not null |
1257 * @throws DateTimeException if the unit cannot be added to this type |
1317 * @throws DateTimeException if the subtraction cannot be made |
|
1318 * @throws ArithmeticException if numeric overflow occurs |
1258 */ |
1319 */ |
1259 @Override |
1320 @Override |
1260 public LocalDate minus(long amountToSubtract, TemporalUnit unit) { |
1321 public LocalDate minus(long amountToSubtract, TemporalUnit unit) { |
1261 return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); |
1322 return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); |
1262 } |
1323 } |
1415 * The calculation returns a whole number, representing the number of |
1480 * The calculation returns a whole number, representing the number of |
1416 * complete units between the two dates. |
1481 * complete units between the two dates. |
1417 * For example, the period in months between 2012-06-15 and 2012-08-14 |
1482 * For example, the period in months between 2012-06-15 and 2012-08-14 |
1418 * will only be one month as it is one day short of two months. |
1483 * will only be one month as it is one day short of two months. |
1419 * <p> |
1484 * <p> |
1420 * This method operates in association with {@link TemporalUnit#between}. |
1485 * There are two equivalent ways of using this method. |
1421 * The result of this method is a {@code long} representing the amount of |
1486 * The first is to invoke this method. |
1422 * the specified unit. By contrast, the result of {@code between} is an |
1487 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: |
1423 * object that can be used directly in addition/subtraction: |
|
1424 * <pre> |
1488 * <pre> |
1425 * long period = start.periodUntil(end, MONTHS); // this method |
1489 * // these two lines are equivalent |
1426 * dateTime.plus(MONTHS.between(start, end)); // use in plus/minus |
1490 * amount = start.periodUntil(end, MONTHS); |
|
1491 * amount = MONTHS.between(start, end); |
1427 * </pre> |
1492 * </pre> |
|
1493 * The choice should be made based on which makes the code more readable. |
1428 * <p> |
1494 * <p> |
1429 * The calculation is implemented in this method for {@link ChronoUnit}. |
1495 * The calculation is implemented in this method for {@link ChronoUnit}. |
1430 * The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS}, |
1496 * The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS}, |
1431 * {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} |
1497 * {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} |
1432 * are supported. Other {@code ChronoUnit} values will throw an exception. |
1498 * are supported. Other {@code ChronoUnit} values will throw an exception. |
1475 long packed1 = getEpochMonth() * 32L + getDayOfMonth(); // no overflow |
1541 long packed1 = getEpochMonth() * 32L + getDayOfMonth(); // no overflow |
1476 long packed2 = end.getEpochMonth() * 32L + end.getDayOfMonth(); // no overflow |
1542 long packed2 = end.getEpochMonth() * 32L + end.getDayOfMonth(); // no overflow |
1477 return (packed2 - packed1) / 32; |
1543 return (packed2 - packed1) / 32; |
1478 } |
1544 } |
1479 |
1545 |
1480 //----------------------------------------------------------------------- |
1546 /** |
1481 /** |
1547 * Calculates the period between this date and another date as a {@code Period}. |
1482 * Returns a local date-time formed from this date at the specified time. |
1548 * <p> |
1483 * <p> |
1549 * This calculates the period between two dates in terms of years, months and days. |
1484 * This combines this date with the specified time to form a {@code LocalDateTime}. |
1550 * The start and end points are {@code this} and the specified date. |
|
1551 * The result will be negative if the end is before the start. |
|
1552 * <p> |
|
1553 * The calculation is performed using the ISO calendar system. |
|
1554 * If necessary, the input date will be converted to ISO. |
|
1555 * <p> |
|
1556 * The start date is included, but the end date is not. |
|
1557 * The period is calculated by removing complete months, then calculating |
|
1558 * the remaining number of days, adjusting to ensure that both have the same sign. |
|
1559 * The number of months is then normalized into years and months based on a 12 month year. |
|
1560 * A month is considered to be complete if the end day-of-month is greater |
|
1561 * than or equal to the start day-of-month. |
|
1562 * For example, from {@code 2010-01-15} to {@code 2011-03-18} is "1 year, 2 months and 3 days". |
|
1563 * <p> |
|
1564 * The result of this method can be a negative period if the end is before the start. |
|
1565 * The negative sign will be the same in each of year, month and day. |
|
1566 * <p> |
|
1567 * There are two equivalent ways of using this method. |
|
1568 * The first is to invoke this method. |
|
1569 * The second is to use {@link Period#between(LocalDate, LocalDate)}: |
|
1570 * <pre> |
|
1571 * // these two lines are equivalent |
|
1572 * period = start.periodUntil(end); |
|
1573 * period = Period.between(start, end); |
|
1574 * </pre> |
|
1575 * The choice should be made based on which makes the code more readable. |
|
1576 * |
|
1577 * @param endDate the end date, exclusive, which may be in any chronology, not null |
|
1578 * @return the period between this date and the end date, not null |
|
1579 */ |
|
1580 @Override |
|
1581 public Period periodUntil(ChronoLocalDate<?> endDate) { |
|
1582 LocalDate end = LocalDate.from(endDate); |
|
1583 long totalMonths = end.getEpochMonth() - this.getEpochMonth(); // safe |
|
1584 int days = end.day - this.day; |
|
1585 if (totalMonths > 0 && days < 0) { |
|
1586 totalMonths--; |
|
1587 LocalDate calcDate = this.plusMonths(totalMonths); |
|
1588 days = (int) (end.toEpochDay() - calcDate.toEpochDay()); // safe |
|
1589 } else if (totalMonths < 0 && days > 0) { |
|
1590 totalMonths++; |
|
1591 days -= end.lengthOfMonth(); |
|
1592 } |
|
1593 long years = totalMonths / 12; // safe |
|
1594 int months = (int) (totalMonths % 12); // safe |
|
1595 return Period.of(Math.toIntExact(years), months, days); |
|
1596 } |
|
1597 |
|
1598 //----------------------------------------------------------------------- |
|
1599 /** |
|
1600 * Combines this date with a time to create a {@code LocalDateTime}. |
|
1601 * <p> |
|
1602 * This returns a {@code LocalDateTime} formed from this date at the specified time. |
1485 * All possible combinations of date and time are valid. |
1603 * All possible combinations of date and time are valid. |
1486 * <p> |
|
1487 * This instance is immutable and unaffected by this method call. |
|
1488 * |
1604 * |
1489 * @param time the time to combine with, not null |
1605 * @param time the time to combine with, not null |
1490 * @return the local date-time formed from this date and the specified time, not null |
1606 * @return the local date-time formed from this date and the specified time, not null |
1491 */ |
1607 */ |
1492 @Override |
1608 @Override |
1493 public LocalDateTime atTime(LocalTime time) { |
1609 public LocalDateTime atTime(LocalTime time) { |
1494 return LocalDateTime.of(this, time); |
1610 return LocalDateTime.of(this, time); |
1495 } |
1611 } |
1496 |
1612 |
1497 /** |
1613 /** |
1498 * Returns a local date-time formed from this date at the specified time. |
1614 * Combines this date with a time to create a {@code LocalDateTime}. |
1499 * <p> |
1615 * <p> |
1500 * This combines this date with the specified time to form a {@code LocalDateTime}. |
1616 * This returns a {@code LocalDateTime} formed from this date at the |
|
1617 * specified hour and minute. |
|
1618 * The seconds and nanosecond fields will be set to zero. |
1501 * The individual time fields must be within their valid range. |
1619 * The individual time fields must be within their valid range. |
1502 * All possible combinations of date and time are valid. |
1620 * All possible combinations of date and time are valid. |
1503 * <p> |
|
1504 * This instance is immutable and unaffected by this method call. |
|
1505 * |
1621 * |
1506 * @param hour the hour-of-day to use, from 0 to 23 |
1622 * @param hour the hour-of-day to use, from 0 to 23 |
1507 * @param minute the minute-of-hour to use, from 0 to 59 |
1623 * @param minute the minute-of-hour to use, from 0 to 59 |
1508 * @return the local date-time formed from this date and the specified time, not null |
1624 * @return the local date-time formed from this date and the specified time, not null |
1509 * @throws DateTimeException if the value of any field is out of range |
1625 * @throws DateTimeException if the value of any field is out of range |
1530 public LocalDateTime atTime(int hour, int minute, int second) { |
1646 public LocalDateTime atTime(int hour, int minute, int second) { |
1531 return atTime(LocalTime.of(hour, minute, second)); |
1647 return atTime(LocalTime.of(hour, minute, second)); |
1532 } |
1648 } |
1533 |
1649 |
1534 /** |
1650 /** |
1535 * Returns a local date-time formed from this date at the specified time. |
1651 * Combines this date with a time to create a {@code LocalDateTime}. |
1536 * <p> |
1652 * <p> |
1537 * This combines this date with the specified time to form a {@code LocalDateTime}. |
1653 * This returns a {@code LocalDateTime} formed from this date at the |
|
1654 * specified hour, minute, second and nanosecond. |
1538 * The individual time fields must be within their valid range. |
1655 * The individual time fields must be within their valid range. |
1539 * All possible combinations of date and time are valid. |
1656 * All possible combinations of date and time are valid. |
1540 * <p> |
|
1541 * This instance is immutable and unaffected by this method call. |
|
1542 * |
1657 * |
1543 * @param hour the hour-of-day to use, from 0 to 23 |
1658 * @param hour the hour-of-day to use, from 0 to 23 |
1544 * @param minute the minute-of-hour to use, from 0 to 59 |
1659 * @param minute the minute-of-hour to use, from 0 to 59 |
1545 * @param second the second-of-minute to represent, from 0 to 59 |
1660 * @param second the second-of-minute to represent, from 0 to 59 |
1546 * @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 |
1661 * @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 |
1550 public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond) { |
1665 public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond) { |
1551 return atTime(LocalTime.of(hour, minute, second, nanoOfSecond)); |
1666 return atTime(LocalTime.of(hour, minute, second, nanoOfSecond)); |
1552 } |
1667 } |
1553 |
1668 |
1554 /** |
1669 /** |
1555 * Returns an offset date formed from this date and the specified offset. |
1670 * Combines this date with an offset time to create an {@code OffsetDateTime}. |
1556 * <p> |
1671 * <p> |
1557 * This combines this date with the specified offset to form an {@code OffsetDate}. |
1672 * This returns an {@code OffsetDateTime} formed from this date at the specified time. |
1558 * All possible combinations of date and offset are valid. |
1673 * All possible combinations of date and time are valid. |
1559 * <p> |
1674 * |
1560 * This instance is immutable and unaffected by this method call. |
1675 * @param time the time to combine with, not null |
1561 * |
1676 * @return the offset date-time formed from this date and the specified time, not null |
1562 * @param offset the offset to combine with, not null |
1677 */ |
1563 * @return the offset date formed from this date and the specified offset, not null |
1678 public OffsetDateTime atTime(OffsetTime time) { |
1564 */ |
1679 return OffsetDateTime.of(LocalDateTime.of(this, time.toLocalTime()), time.getOffset()); |
1565 public OffsetDate atOffset(ZoneOffset offset) { |
1680 } |
1566 return OffsetDate.of(this, offset); |
1681 |
|
1682 /** |
|
1683 * Combines this date with the time of midnight to create a {@code LocalDateTime} |
|
1684 * at the start of this date. |
|
1685 * <p> |
|
1686 * This returns a {@code LocalDateTime} formed from this date at the time of |
|
1687 * midnight, 00:00, at the start of this date. |
|
1688 * |
|
1689 * @return the local date-time of midnight at the start of this date, not null |
|
1690 */ |
|
1691 public LocalDateTime atStartOfDay() { |
|
1692 return LocalDateTime.of(this, LocalTime.MIDNIGHT); |
1567 } |
1693 } |
1568 |
1694 |
1569 /** |
1695 /** |
1570 * Returns a zoned date-time from this date at the earliest valid time according |
1696 * Returns a zoned date-time from this date at the earliest valid time according |
1571 * to the rules in the time-zone. |
1697 * to the rules in the time-zone. |