jdk/src/share/classes/java/time/LocalDate.java
changeset 24256 da9a41004459
parent 22566 4ebe53dd7814
equal deleted inserted replaced
24255:91f5e4399160 24256:da9a41004459
   278      *
   278      *
   279      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
   279      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
   280      * @param dayOfYear  the day-of-year to represent, from 1 to 366
   280      * @param dayOfYear  the day-of-year to represent, from 1 to 366
   281      * @return the local date, not null
   281      * @return the local date, not null
   282      * @throws DateTimeException if the value of any field is out of range,
   282      * @throws DateTimeException if the value of any field is out of range,
   283      *  or if the day-of-year is invalid for the month-year
   283      *  or if the day-of-year is invalid for the year
   284      */
   284      */
   285     public static LocalDate ofYearDay(int year, int dayOfYear) {
   285     public static LocalDate ofYearDay(int year, int dayOfYear) {
   286         YEAR.checkValidValue(year);
   286         YEAR.checkValidValue(year);
   287         DAY_OF_YEAR.checkValidValue(dayOfYear);
   287         DAY_OF_YEAR.checkValidValue(dayOfYear);
   288         boolean leap = IsoChronology.INSTANCE.isLeapYear(year);
   288         boolean leap = IsoChronology.INSTANCE.isLeapYear(year);
   306      * The {@link ChronoField#EPOCH_DAY EPOCH_DAY} is a simple incrementing count
   306      * The {@link ChronoField#EPOCH_DAY EPOCH_DAY} is a simple incrementing count
   307      * of days where day 0 is 1970-01-01. Negative numbers represent earlier days.
   307      * of days where day 0 is 1970-01-01. Negative numbers represent earlier days.
   308      *
   308      *
   309      * @param epochDay  the Epoch Day to convert, based on the epoch 1970-01-01
   309      * @param epochDay  the Epoch Day to convert, based on the epoch 1970-01-01
   310      * @return the local date, not null
   310      * @return the local date, not null
   311      * @throws DateTimeException if the epoch days exceeds the supported date range
   311      * @throws DateTimeException if the epoch day exceeds the supported date range
   312      */
   312      */
   313     public static LocalDate ofEpochDay(long epochDay) {
   313     public static LocalDate ofEpochDay(long epochDay) {
   314         long zeroDay = epochDay + DAYS_0000_TO_1970;
   314         long zeroDay = epochDay + DAYS_0000_TO_1970;
   315         // find the march-based year
   315         // find the march-based year
   316         zeroDay -= 60;  // adjust to 0000-03-01 so leap day is at end of four year cycle
   316         zeroDay -= 60;  // adjust to 0000-03-01 so leap day is at end of four year cycle
   513     }
   513     }
   514 
   514 
   515     /**
   515     /**
   516      * Checks if the specified unit is supported.
   516      * Checks if the specified unit is supported.
   517      * <p>
   517      * <p>
   518      * This checks if the specified unit can be added to, or subtracted from, this date-time.
   518      * This checks if the specified unit can be added to, or subtracted from, this date.
   519      * If false, then calling the {@link #plus(long, TemporalUnit)} and
   519      * If false, then calling the {@link #plus(long, TemporalUnit)} and
   520      * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
   520      * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
   521      * <p>
   521      * <p>
   522      * If the unit is a {@link ChronoUnit} then the query is implemented here.
   522      * If the unit is a {@link ChronoUnit} then the query is implemented here.
   523      * The supported units are:
   523      * The supported units are:
   590     }
   590     }
   591 
   591 
   592     /**
   592     /**
   593      * Gets the value of the specified field from this date as an {@code int}.
   593      * Gets the value of the specified field from this date as an {@code int}.
   594      * <p>
   594      * <p>
   595      * This queries this date for the value for the specified field.
   595      * This queries this date for the value of the specified field.
   596      * The returned value will always be within the valid range of values for the field.
   596      * The returned value will always be within the valid range of values for the field.
   597      * If it is not possible to return the value, because the field is not supported
   597      * If it is not possible to return the value, because the field is not supported
   598      * or for some other reason, an exception is thrown.
   598      * or for some other reason, an exception is thrown.
   599      * <p>
   599      * <p>
   600      * If the field is a {@link ChronoField} then the query is implemented here.
   600      * If the field is a {@link ChronoField} then the query is implemented here.
   601      * The {@link #isSupported(TemporalField) supported fields} will return valid
   601      * The {@link #isSupported(TemporalField) supported fields} will return valid
   602      * values based on this date, except {@code EPOCH_DAY} and {@code PROLEPTIC_MONTH}
   602      * values based on this date, except {@code EPOCH_DAY} and {@code PROLEPTIC_MONTH}
   603      * which are too large to fit in an {@code int} and throw a {@code DateTimeException}.
   603      * which are too large to fit in an {@code int} and throw an {@code UnsupportedTemporalTypeException}.
   604      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
   604      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
   605      * <p>
   605      * <p>
   606      * If the field is not a {@code ChronoField}, then the result of this method
   606      * If the field is not a {@code ChronoField}, then the result of this method
   607      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
   607      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
   608      * passing {@code this} as the argument. Whether the value can be obtained,
   608      * passing {@code this} as the argument. Whether the value can be obtained,
   625     }
   625     }
   626 
   626 
   627     /**
   627     /**
   628      * Gets the value of the specified field from this date as a {@code long}.
   628      * Gets the value of the specified field from this date as a {@code long}.
   629      * <p>
   629      * <p>
   630      * This queries this date for the value for the specified field.
   630      * This queries this date for the value of the specified field.
   631      * If it is not possible to return the value, because the field is not supported
   631      * If it is not possible to return the value, because the field is not supported
   632      * or for some other reason, an exception is thrown.
   632      * or for some other reason, an exception is thrown.
   633      * <p>
   633      * <p>
   634      * If the field is a {@link ChronoField} then the query is implemented here.
   634      * If the field is a {@link ChronoField} then the query is implemented here.
   635      * The {@link #isSupported(TemporalField) supported fields} will return valid
   635      * The {@link #isSupported(TemporalField) supported fields} will return valid
   873      * The adjustment takes place using the specified adjuster strategy object.
   873      * The adjustment takes place using the specified adjuster strategy object.
   874      * Read the documentation of the adjuster to understand what adjustment will be made.
   874      * Read the documentation of the adjuster to understand what adjustment will be made.
   875      * <p>
   875      * <p>
   876      * A simple adjuster might simply set the one of the fields, such as the year field.
   876      * A simple adjuster might simply set the one of the fields, such as the year field.
   877      * A more complex adjuster might set the date to the last day of the month.
   877      * A more complex adjuster might set the date to the last day of the month.
   878      * A selection of common adjustments is provided in {@link TemporalAdjuster}.
   878      * <p>
       
   879      * A selection of common adjustments is provided in
       
   880      * {@link java.time.temporal.TemporalAdjusters TemporalAdjusters}.
   879      * These include finding the "last day of the month" and "next Wednesday".
   881      * These include finding the "last day of the month" and "next Wednesday".
   880      * Key date-time classes also implement the {@code TemporalAdjuster} interface,
   882      * Key date-time classes also implement the {@code TemporalAdjuster} interface,
   881      * such as {@link Month} and {@link java.time.MonthDay MonthDay}.
   883      * such as {@link Month} and {@link java.time.MonthDay MonthDay}.
   882      * The adjuster is responsible for handling special cases, such as the varying
   884      * The adjuster is responsible for handling special cases, such as the varying
   883      * lengths of month and leap years.
   885      * lengths of month and leap years.
   884      * <p>
   886      * <p>
   885      * For example this code returns a date on the last day of July:
   887      * For example this code returns a date on the last day of July:
   886      * <pre>
   888      * <pre>
   887      *  import static java.time.Month.*;
   889      *  import static java.time.Month.*;
   888      *  import static java.time.temporal.Adjusters.*;
   890      *  import static java.time.temporal.TemporalAdjusters.*;
   889      *
   891      *
   890      *  result = localDate.with(JULY).with(lastDayOfMonth());
   892      *  result = localDate.with(JULY).with(lastDayOfMonth());
   891      * </pre>
   893      * </pre>
   892      * <p>
   894      * <p>
   893      * The result of this method is obtained by invoking the
   895      * The result of this method is obtained by invoking the
  1039         return field.adjustInto(this, newValue);
  1041         return field.adjustInto(this, newValue);
  1040     }
  1042     }
  1041 
  1043 
  1042     //-----------------------------------------------------------------------
  1044     //-----------------------------------------------------------------------
  1043     /**
  1045     /**
  1044      * Returns a copy of this date with the year altered.
  1046      * Returns a copy of this {@code LocalDate} with the year altered.
       
  1047      * <p>
  1045      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
  1048      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
  1046      * <p>
  1049      * <p>
  1047      * This instance is immutable and unaffected by this method call.
  1050      * This instance is immutable and unaffected by this method call.
  1048      *
  1051      *
  1049      * @param year  the year to set in the result, from MIN_YEAR to MAX_YEAR
  1052      * @param year  the year to set in the result, from MIN_YEAR to MAX_YEAR
  1057         YEAR.checkValidValue(year);
  1060         YEAR.checkValidValue(year);
  1058         return resolvePreviousValid(year, month, day);
  1061         return resolvePreviousValid(year, month, day);
  1059     }
  1062     }
  1060 
  1063 
  1061     /**
  1064     /**
  1062      * Returns a copy of this date with the month-of-year altered.
  1065      * Returns a copy of this {@code LocalDate} with the month-of-year altered.
       
  1066      * <p>
  1063      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
  1067      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
  1064      * <p>
  1068      * <p>
  1065      * This instance is immutable and unaffected by this method call.
  1069      * This instance is immutable and unaffected by this method call.
  1066      *
  1070      *
  1067      * @param month  the month-of-year to set in the result, from 1 (January) to 12 (December)
  1071      * @param month  the month-of-year to set in the result, from 1 (January) to 12 (December)
  1075         MONTH_OF_YEAR.checkValidValue(month);
  1079         MONTH_OF_YEAR.checkValidValue(month);
  1076         return resolvePreviousValid(year, month, day);
  1080         return resolvePreviousValid(year, month, day);
  1077     }
  1081     }
  1078 
  1082 
  1079     /**
  1083     /**
  1080      * Returns a copy of this date with the day-of-month altered.
  1084      * Returns a copy of this {@code LocalDate} with the day-of-month altered.
       
  1085      * <p>
  1081      * If the resulting date is invalid, an exception is thrown.
  1086      * If the resulting date is invalid, an exception is thrown.
  1082      * <p>
  1087      * <p>
  1083      * This instance is immutable and unaffected by this method call.
  1088      * This instance is immutable and unaffected by this method call.
  1084      *
  1089      *
  1085      * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
  1090      * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
  1093         }
  1098         }
  1094         return of(year, month, dayOfMonth);
  1099         return of(year, month, dayOfMonth);
  1095     }
  1100     }
  1096 
  1101 
  1097     /**
  1102     /**
  1098      * Returns a copy of this date with the day-of-year altered.
  1103      * Returns a copy of this {@code LocalDate} with the day-of-year altered.
       
  1104      * <p>
  1099      * If the resulting date is invalid, an exception is thrown.
  1105      * If the resulting date is invalid, an exception is thrown.
  1100      * <p>
  1106      * <p>
  1101      * This instance is immutable and unaffected by this method call.
  1107      * This instance is immutable and unaffected by this method call.
  1102      *
  1108      *
  1103      * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
  1109      * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
  1243         return unit.addTo(this, amountToAdd);
  1249         return unit.addTo(this, amountToAdd);
  1244     }
  1250     }
  1245 
  1251 
  1246     //-----------------------------------------------------------------------
  1252     //-----------------------------------------------------------------------
  1247     /**
  1253     /**
  1248      * Returns a copy of this {@code LocalDate} with the specified period in years added.
  1254      * Returns a copy of this {@code LocalDate} with the specified number of years added.
  1249      * <p>
  1255      * <p>
  1250      * This method adds the specified amount to the years field in three steps:
  1256      * This method adds the specified amount to the years field in three steps:
  1251      * <ol>
  1257      * <ol>
  1252      * <li>Add the input years to the year field</li>
  1258      * <li>Add the input years to the year field</li>
  1253      * <li>Check if the resulting date would be invalid</li>
  1259      * <li>Check if the resulting date would be invalid</li>
  1271         int newYear = YEAR.checkValidIntValue(year + yearsToAdd);  // safe overflow
  1277         int newYear = YEAR.checkValidIntValue(year + yearsToAdd);  // safe overflow
  1272         return resolvePreviousValid(newYear, month, day);
  1278         return resolvePreviousValid(newYear, month, day);
  1273     }
  1279     }
  1274 
  1280 
  1275     /**
  1281     /**
  1276      * Returns a copy of this {@code LocalDate} with the specified period in months added.
  1282      * Returns a copy of this {@code LocalDate} with the specified number of months added.
  1277      * <p>
  1283      * <p>
  1278      * This method adds the specified amount to the months field in three steps:
  1284      * This method adds the specified amount to the months field in three steps:
  1279      * <ol>
  1285      * <ol>
  1280      * <li>Add the input months to the month-of-year field</li>
  1286      * <li>Add the input months to the month-of-year field</li>
  1281      * <li>Check if the resulting date would be invalid</li>
  1287      * <li>Check if the resulting date would be invalid</li>
  1302         int newMonth = (int)Math.floorMod(calcMonths, 12) + 1;
  1308         int newMonth = (int)Math.floorMod(calcMonths, 12) + 1;
  1303         return resolvePreviousValid(newYear, newMonth, day);
  1309         return resolvePreviousValid(newYear, newMonth, day);
  1304     }
  1310     }
  1305 
  1311 
  1306     /**
  1312     /**
  1307      * Returns a copy of this {@code LocalDate} with the specified period in weeks added.
  1313      * Returns a copy of this {@code LocalDate} with the specified number of weeks added.
  1308      * <p>
  1314      * <p>
  1309      * This method adds the specified amount in weeks to the days field incrementing
  1315      * This method adds the specified amount in weeks to the days field incrementing
  1310      * the month and year fields as necessary to ensure the result remains valid.
  1316      * the month and year fields as necessary to ensure the result remains valid.
  1311      * The result is only invalid if the maximum/minimum year is exceeded.
  1317      * The result is only invalid if the maximum/minimum year is exceeded.
  1312      * <p>
  1318      * <p>
  1400         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
  1406         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
  1401     }
  1407     }
  1402 
  1408 
  1403     //-----------------------------------------------------------------------
  1409     //-----------------------------------------------------------------------
  1404     /**
  1410     /**
  1405      * Returns a copy of this {@code LocalDate} with the specified period in years subtracted.
  1411      * Returns a copy of this {@code LocalDate} with the specified number of years subtracted.
  1406      * <p>
  1412      * <p>
  1407      * This method subtracts the specified amount from the years field in three steps:
  1413      * This method subtracts the specified amount from the years field in three steps:
  1408      * <ol>
  1414      * <ol>
  1409      * <li>Subtract the input years to the year field</li>
  1415      * <li>Subtract the input years from the year field</li>
  1410      * <li>Check if the resulting date would be invalid</li>
  1416      * <li>Check if the resulting date would be invalid</li>
  1411      * <li>Adjust the day-of-month to the last valid day if necessary</li>
  1417      * <li>Adjust the day-of-month to the last valid day if necessary</li>
  1412      * </ol>
  1418      * </ol>
  1413      * <p>
  1419      * <p>
  1414      * For example, 2008-02-29 (leap year) minus one year would result in the
  1420      * For example, 2008-02-29 (leap year) minus one year would result in the
  1424     public LocalDate minusYears(long yearsToSubtract) {
  1430     public LocalDate minusYears(long yearsToSubtract) {
  1425         return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract));
  1431         return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract));
  1426     }
  1432     }
  1427 
  1433 
  1428     /**
  1434     /**
  1429      * Returns a copy of this {@code LocalDate} with the specified period in months subtracted.
  1435      * Returns a copy of this {@code LocalDate} with the specified number of months subtracted.
  1430      * <p>
  1436      * <p>
  1431      * This method subtracts the specified amount from the months field in three steps:
  1437      * This method subtracts the specified amount from the months field in three steps:
  1432      * <ol>
  1438      * <ol>
  1433      * <li>Subtract the input months to the month-of-year field</li>
  1439      * <li>Subtract the input months from the month-of-year field</li>
  1434      * <li>Check if the resulting date would be invalid</li>
  1440      * <li>Check if the resulting date would be invalid</li>
  1435      * <li>Adjust the day-of-month to the last valid day if necessary</li>
  1441      * <li>Adjust the day-of-month to the last valid day if necessary</li>
  1436      * </ol>
  1442      * </ol>
  1437      * <p>
  1443      * <p>
  1438      * For example, 2007-03-31 minus one month would result in the invalid date
  1444      * For example, 2007-03-31 minus one month would result in the invalid date
  1448     public LocalDate minusMonths(long monthsToSubtract) {
  1454     public LocalDate minusMonths(long monthsToSubtract) {
  1449         return (monthsToSubtract == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-monthsToSubtract));
  1455         return (monthsToSubtract == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-monthsToSubtract));
  1450     }
  1456     }
  1451 
  1457 
  1452     /**
  1458     /**
  1453      * Returns a copy of this {@code LocalDate} with the specified period in weeks subtracted.
  1459      * Returns a copy of this {@code LocalDate} with the specified number of weeks subtracted.
  1454      * <p>
  1460      * <p>
  1455      * This method subtracts the specified amount in weeks from the days field decrementing
  1461      * This method subtracts the specified amount in weeks from the days field decrementing
  1456      * the month and year fields as necessary to ensure the result remains valid.
  1462      * the month and year fields as necessary to ensure the result remains valid.
  1457      * The result is only invalid if the maximum/minimum year is exceeded.
  1463      * The result is only invalid if the maximum/minimum year is exceeded.
  1458      * <p>
  1464      * <p>