jdk/src/share/classes/java/time/LocalDateTime.java
changeset 24256 da9a41004459
parent 22566 4ebe53dd7814
equal deleted inserted replaced
24255:91f5e4399160 24256:da9a41004459
   426 
   426 
   427     //-----------------------------------------------------------------------
   427     //-----------------------------------------------------------------------
   428     /**
   428     /**
   429      * Obtains an instance of {@code LocalDateTime} from a temporal object.
   429      * Obtains an instance of {@code LocalDateTime} from a temporal object.
   430      * <p>
   430      * <p>
   431      * This obtains an offset time based on the specified temporal.
   431      * This obtains a local date-time based on the specified temporal.
   432      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
   432      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
   433      * which this factory converts to an instance of {@code LocalDateTime}.
   433      * which this factory converts to an instance of {@code LocalDateTime}.
   434      * <p>
   434      * <p>
   435      * The conversion extracts and combines the {@code LocalDate} and the
   435      * The conversion extracts and combines the {@code LocalDate} and the
   436      * {@code LocalTime} from the temporal object.
   436      * {@code LocalTime} from the temporal object.
   654     }
   654     }
   655 
   655 
   656     /**
   656     /**
   657      * Gets the value of the specified field from this date-time as an {@code int}.
   657      * Gets the value of the specified field from this date-time as an {@code int}.
   658      * <p>
   658      * <p>
   659      * This queries this date-time for the value for the specified field.
   659      * This queries this date-time for the value of the specified field.
   660      * The returned value will always be within the valid range of values for the field.
   660      * The returned value will always be within the valid range of values for the field.
   661      * If it is not possible to return the value, because the field is not supported
   661      * If it is not possible to return the value, because the field is not supported
   662      * or for some other reason, an exception is thrown.
   662      * or for some other reason, an exception is thrown.
   663      * <p>
   663      * <p>
   664      * If the field is a {@link ChronoField} then the query is implemented here.
   664      * If the field is a {@link ChronoField} then the query is implemented here.
   665      * The {@link #isSupported(TemporalField) supported fields} will return valid
   665      * The {@link #isSupported(TemporalField) supported fields} will return valid
   666      * values based on this date-time, except {@code NANO_OF_DAY}, {@code MICRO_OF_DAY},
   666      * values based on this date-time, except {@code NANO_OF_DAY}, {@code MICRO_OF_DAY},
   667      * {@code EPOCH_DAY} and {@code PROLEPTIC_MONTH} which are too large to fit in
   667      * {@code EPOCH_DAY} and {@code PROLEPTIC_MONTH} which are too large to fit in
   668      * an {@code int} and throw a {@code DateTimeException}.
   668      * an {@code int} and throw a {@code UnsupportedTemporalTypeException}.
   669      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
   669      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
   670      * <p>
   670      * <p>
   671      * If the field is not a {@code ChronoField}, then the result of this method
   671      * If the field is not a {@code ChronoField}, then the result of this method
   672      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
   672      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
   673      * passing {@code this} as the argument. Whether the value can be obtained,
   673      * passing {@code this} as the argument. Whether the value can be obtained,
   691     }
   691     }
   692 
   692 
   693     /**
   693     /**
   694      * Gets the value of the specified field from this date-time as a {@code long}.
   694      * Gets the value of the specified field from this date-time as a {@code long}.
   695      * <p>
   695      * <p>
   696      * This queries this date-time for the value for the specified field.
   696      * This queries this date-time for the value of the specified field.
   697      * If it is not possible to return the value, because the field is not supported
   697      * If it is not possible to return the value, because the field is not supported
   698      * or for some other reason, an exception is thrown.
   698      * or for some other reason, an exception is thrown.
   699      * <p>
   699      * <p>
   700      * If the field is a {@link ChronoField} then the query is implemented here.
   700      * If the field is a {@link ChronoField} then the query is implemented here.
   701      * The {@link #isSupported(TemporalField) supported fields} will return valid
   701      * The {@link #isSupported(TemporalField) supported fields} will return valid
   876      * The adjustment takes place using the specified adjuster strategy object.
   876      * The adjustment takes place using the specified adjuster strategy object.
   877      * Read the documentation of the adjuster to understand what adjustment will be made.
   877      * Read the documentation of the adjuster to understand what adjustment will be made.
   878      * <p>
   878      * <p>
   879      * A simple adjuster might simply set the one of the fields, such as the year field.
   879      * A simple adjuster might simply set the one of the fields, such as the year field.
   880      * A more complex adjuster might set the date to the last day of the month.
   880      * A more complex adjuster might set the date to the last day of the month.
   881      * A selection of common adjustments is provided in {@link TemporalAdjuster}.
   881      * <p>
       
   882      * A selection of common adjustments is provided in
       
   883      * {@link java.time.temporal.TemporalAdjusters TemporalAdjusters}.
   882      * These include finding the "last day of the month" and "next Wednesday".
   884      * These include finding the "last day of the month" and "next Wednesday".
   883      * Key date-time classes also implement the {@code TemporalAdjuster} interface,
   885      * Key date-time classes also implement the {@code TemporalAdjuster} interface,
   884      * such as {@link Month} and {@link java.time.MonthDay MonthDay}.
   886      * such as {@link Month} and {@link java.time.MonthDay MonthDay}.
   885      * The adjuster is responsible for handling special cases, such as the varying
   887      * The adjuster is responsible for handling special cases, such as the varying
   886      * lengths of month and leap years.
   888      * lengths of month and leap years.
   887      * <p>
   889      * <p>
   888      * For example this code returns a date on the last day of July:
   890      * For example this code returns a date on the last day of July:
   889      * <pre>
   891      * <pre>
   890      *  import static java.time.Month.*;
   892      *  import static java.time.Month.*;
   891      *  import static java.time.temporal.Adjusters.*;
   893      *  import static java.time.temporal.TemporalAdjusters.*;
   892      *
   894      *
   893      *  result = localDateTime.with(JULY).with(lastDayOfMonth());
   895      *  result = localDateTime.with(JULY).with(lastDayOfMonth());
   894      * </pre>
   896      * </pre>
   895      * <p>
   897      * <p>
   896      * The classes {@link LocalDate} and {@link LocalTime} implement {@code TemporalAdjuster},
   898      * The classes {@link LocalDate} and {@link LocalTime} implement {@code TemporalAdjuster},
   972     }
   974     }
   973 
   975 
   974     //-----------------------------------------------------------------------
   976     //-----------------------------------------------------------------------
   975     /**
   977     /**
   976      * Returns a copy of this {@code LocalDateTime} with the year altered.
   978      * Returns a copy of this {@code LocalDateTime} with the year altered.
       
   979      * <p>
   977      * The time does not affect the calculation and will be the same in the result.
   980      * The time does not affect the calculation and will be the same in the result.
   978      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
   981      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
   979      * <p>
   982      * <p>
   980      * This instance is immutable and unaffected by this method call.
   983      * This instance is immutable and unaffected by this method call.
   981      *
   984      *
   987         return with(date.withYear(year), time);
   990         return with(date.withYear(year), time);
   988     }
   991     }
   989 
   992 
   990     /**
   993     /**
   991      * Returns a copy of this {@code LocalDateTime} with the month-of-year altered.
   994      * Returns a copy of this {@code LocalDateTime} with the month-of-year altered.
       
   995      * <p>
   992      * The time does not affect the calculation and will be the same in the result.
   996      * The time does not affect the calculation and will be the same in the result.
   993      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
   997      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
   994      * <p>
   998      * <p>
   995      * This instance is immutable and unaffected by this method call.
   999      * This instance is immutable and unaffected by this method call.
   996      *
  1000      *
  1002         return with(date.withMonth(month), time);
  1006         return with(date.withMonth(month), time);
  1003     }
  1007     }
  1004 
  1008 
  1005     /**
  1009     /**
  1006      * Returns a copy of this {@code LocalDateTime} with the day-of-month altered.
  1010      * Returns a copy of this {@code LocalDateTime} with the day-of-month altered.
  1007      * If the resulting {@code LocalDateTime} is invalid, an exception is thrown.
  1011      * <p>
       
  1012      * If the resulting date-time is invalid, an exception is thrown.
  1008      * The time does not affect the calculation and will be the same in the result.
  1013      * The time does not affect the calculation and will be the same in the result.
  1009      * <p>
  1014      * <p>
  1010      * This instance is immutable and unaffected by this method call.
  1015      * This instance is immutable and unaffected by this method call.
  1011      *
  1016      *
  1012      * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
  1017      * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
  1018         return with(date.withDayOfMonth(dayOfMonth), time);
  1023         return with(date.withDayOfMonth(dayOfMonth), time);
  1019     }
  1024     }
  1020 
  1025 
  1021     /**
  1026     /**
  1022      * Returns a copy of this {@code LocalDateTime} with the day-of-year altered.
  1027      * Returns a copy of this {@code LocalDateTime} with the day-of-year altered.
  1023      * If the resulting {@code LocalDateTime} is invalid, an exception is thrown.
  1028      * <p>
       
  1029      * If the resulting date-time is invalid, an exception is thrown.
  1024      * <p>
  1030      * <p>
  1025      * This instance is immutable and unaffected by this method call.
  1031      * This instance is immutable and unaffected by this method call.
  1026      *
  1032      *
  1027      * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
  1033      * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
  1028      * @return a {@code LocalDateTime} based on this date with the requested day, not null
  1034      * @return a {@code LocalDateTime} based on this date with the requested day, not null
  1033         return with(date.withDayOfYear(dayOfYear), time);
  1039         return with(date.withDayOfYear(dayOfYear), time);
  1034     }
  1040     }
  1035 
  1041 
  1036     //-----------------------------------------------------------------------
  1042     //-----------------------------------------------------------------------
  1037     /**
  1043     /**
  1038      * Returns a copy of this {@code LocalDateTime} with the hour-of-day value altered.
  1044      * Returns a copy of this {@code LocalDateTime} with the hour-of-day altered.
  1039      * <p>
  1045      * <p>
  1040      * This instance is immutable and unaffected by this method call.
  1046      * This instance is immutable and unaffected by this method call.
  1041      *
  1047      *
  1042      * @param hour  the hour-of-day to set in the result, from 0 to 23
  1048      * @param hour  the hour-of-day to set in the result, from 0 to 23
  1043      * @return a {@code LocalDateTime} based on this date-time with the requested hour, not null
  1049      * @return a {@code LocalDateTime} based on this date-time with the requested hour, not null
  1047         LocalTime newTime = time.withHour(hour);
  1053         LocalTime newTime = time.withHour(hour);
  1048         return with(date, newTime);
  1054         return with(date, newTime);
  1049     }
  1055     }
  1050 
  1056 
  1051     /**
  1057     /**
  1052      * Returns a copy of this {@code LocalDateTime} with the minute-of-hour value altered.
  1058      * Returns a copy of this {@code LocalDateTime} with the minute-of-hour altered.
  1053      * <p>
  1059      * <p>
  1054      * This instance is immutable and unaffected by this method call.
  1060      * This instance is immutable and unaffected by this method call.
  1055      *
  1061      *
  1056      * @param minute  the minute-of-hour to set in the result, from 0 to 59
  1062      * @param minute  the minute-of-hour to set in the result, from 0 to 59
  1057      * @return a {@code LocalDateTime} based on this date-time with the requested minute, not null
  1063      * @return a {@code LocalDateTime} based on this date-time with the requested minute, not null
  1061         LocalTime newTime = time.withMinute(minute);
  1067         LocalTime newTime = time.withMinute(minute);
  1062         return with(date, newTime);
  1068         return with(date, newTime);
  1063     }
  1069     }
  1064 
  1070 
  1065     /**
  1071     /**
  1066      * Returns a copy of this {@code LocalDateTime} with the second-of-minute value altered.
  1072      * Returns a copy of this {@code LocalDateTime} with the second-of-minute altered.
  1067      * <p>
  1073      * <p>
  1068      * This instance is immutable and unaffected by this method call.
  1074      * This instance is immutable and unaffected by this method call.
  1069      *
  1075      *
  1070      * @param second  the second-of-minute to set in the result, from 0 to 59
  1076      * @param second  the second-of-minute to set in the result, from 0 to 59
  1071      * @return a {@code LocalDateTime} based on this date-time with the requested second, not null
  1077      * @return a {@code LocalDateTime} based on this date-time with the requested second, not null
  1075         LocalTime newTime = time.withSecond(second);
  1081         LocalTime newTime = time.withSecond(second);
  1076         return with(date, newTime);
  1082         return with(date, newTime);
  1077     }
  1083     }
  1078 
  1084 
  1079     /**
  1085     /**
  1080      * Returns a copy of this {@code LocalDateTime} with the nano-of-second value altered.
  1086      * Returns a copy of this {@code LocalDateTime} with the nano-of-second altered.
  1081      * <p>
  1087      * <p>
  1082      * This instance is immutable and unaffected by this method call.
  1088      * This instance is immutable and unaffected by this method call.
  1083      *
  1089      *
  1084      * @param nanoOfSecond  the nano-of-second to set in the result, from 0 to 999,999,999
  1090      * @param nanoOfSecond  the nano-of-second to set in the result, from 0 to 999,999,999
  1085      * @return a {@code LocalDateTime} based on this date-time with the requested nanosecond, not null
  1091      * @return a {@code LocalDateTime} based on this date-time with the requested nanosecond, not null
  1107      * This instance is immutable and unaffected by this method call.
  1113      * This instance is immutable and unaffected by this method call.
  1108      *
  1114      *
  1109      * @param unit  the unit to truncate to, not null
  1115      * @param unit  the unit to truncate to, not null
  1110      * @return a {@code LocalDateTime} based on this date-time with the time truncated, not null
  1116      * @return a {@code LocalDateTime} based on this date-time with the time truncated, not null
  1111      * @throws DateTimeException if unable to truncate
  1117      * @throws DateTimeException if unable to truncate
  1112      * @throws UnsupportedTemporalTypeException if the field is not supported
  1118      * @throws UnsupportedTemporalTypeException if the unit is not supported
  1113      */
  1119      */
  1114     public LocalDateTime truncatedTo(TemporalUnit unit) {
  1120     public LocalDateTime truncatedTo(TemporalUnit unit) {
  1115         return with(date, time.truncatedTo(unit));
  1121         return with(date, time.truncatedTo(unit));
  1116     }
  1122     }
  1117 
  1123 
  1190         return unit.addTo(this, amountToAdd);
  1196         return unit.addTo(this, amountToAdd);
  1191     }
  1197     }
  1192 
  1198 
  1193     //-----------------------------------------------------------------------
  1199     //-----------------------------------------------------------------------
  1194     /**
  1200     /**
  1195      * Returns a copy of this {@code LocalDateTime} with the specified period in years added.
  1201      * Returns a copy of this {@code LocalDateTime} with the specified number of years added.
  1196      * <p>
  1202      * <p>
  1197      * This method adds the specified amount to the years field in three steps:
  1203      * This method adds the specified amount to the years field in three steps:
  1198      * <ol>
  1204      * <ol>
  1199      * <li>Add the input years to the year field</li>
  1205      * <li>Add the input years to the year field</li>
  1200      * <li>Check if the resulting date would be invalid</li>
  1206      * <li>Check if the resulting date would be invalid</li>
  1215         LocalDate newDate = date.plusYears(years);
  1221         LocalDate newDate = date.plusYears(years);
  1216         return with(newDate, time);
  1222         return with(newDate, time);
  1217     }
  1223     }
  1218 
  1224 
  1219     /**
  1225     /**
  1220      * Returns a copy of this {@code LocalDateTime} with the specified period in months added.
  1226      * Returns a copy of this {@code LocalDateTime} with the specified number of months added.
  1221      * <p>
  1227      * <p>
  1222      * This method adds the specified amount to the months field in three steps:
  1228      * This method adds the specified amount to the months field in three steps:
  1223      * <ol>
  1229      * <ol>
  1224      * <li>Add the input months to the month-of-year field</li>
  1230      * <li>Add the input months to the month-of-year field</li>
  1225      * <li>Check if the resulting date would be invalid</li>
  1231      * <li>Check if the resulting date would be invalid</li>
  1240         LocalDate newDate = date.plusMonths(months);
  1246         LocalDate newDate = date.plusMonths(months);
  1241         return with(newDate, time);
  1247         return with(newDate, time);
  1242     }
  1248     }
  1243 
  1249 
  1244     /**
  1250     /**
  1245      * Returns a copy of this {@code LocalDateTime} with the specified period in weeks added.
  1251      * Returns a copy of this {@code LocalDateTime} with the specified number of weeks added.
  1246      * <p>
  1252      * <p>
  1247      * This method adds the specified amount in weeks to the days field incrementing
  1253      * This method adds the specified amount in weeks to the days field incrementing
  1248      * the month and year fields as necessary to ensure the result remains valid.
  1254      * the month and year fields as necessary to ensure the result remains valid.
  1249      * The result is only invalid if the maximum/minimum year is exceeded.
  1255      * The result is only invalid if the maximum/minimum year is exceeded.
  1250      * <p>
  1256      * <p>
  1260         LocalDate newDate = date.plusWeeks(weeks);
  1266         LocalDate newDate = date.plusWeeks(weeks);
  1261         return with(newDate, time);
  1267         return with(newDate, time);
  1262     }
  1268     }
  1263 
  1269 
  1264     /**
  1270     /**
  1265      * Returns a copy of this {@code LocalDateTime} with the specified period in days added.
  1271      * Returns a copy of this {@code LocalDateTime} with the specified number of days added.
  1266      * <p>
  1272      * <p>
  1267      * This method adds the specified amount to the days field incrementing the
  1273      * This method adds the specified amount to the days field incrementing the
  1268      * month and year fields as necessary to ensure the result remains valid.
  1274      * month and year fields as necessary to ensure the result remains valid.
  1269      * The result is only invalid if the maximum/minimum year is exceeded.
  1275      * The result is only invalid if the maximum/minimum year is exceeded.
  1270      * <p>
  1276      * <p>
  1281         return with(newDate, time);
  1287         return with(newDate, time);
  1282     }
  1288     }
  1283 
  1289 
  1284     //-----------------------------------------------------------------------
  1290     //-----------------------------------------------------------------------
  1285     /**
  1291     /**
  1286      * Returns a copy of this {@code LocalDateTime} with the specified period in hours added.
  1292      * Returns a copy of this {@code LocalDateTime} with the specified number of hours added.
  1287      * <p>
  1293      * <p>
  1288      * This instance is immutable and unaffected by this method call.
  1294      * This instance is immutable and unaffected by this method call.
  1289      *
  1295      *
  1290      * @param hours  the hours to add, may be negative
  1296      * @param hours  the hours to add, may be negative
  1291      * @return a {@code LocalDateTime} based on this date-time with the hours added, not null
  1297      * @return a {@code LocalDateTime} based on this date-time with the hours added, not null
  1294     public LocalDateTime plusHours(long hours) {
  1300     public LocalDateTime plusHours(long hours) {
  1295         return plusWithOverflow(date, hours, 0, 0, 0, 1);
  1301         return plusWithOverflow(date, hours, 0, 0, 0, 1);
  1296     }
  1302     }
  1297 
  1303 
  1298     /**
  1304     /**
  1299      * Returns a copy of this {@code LocalDateTime} with the specified period in minutes added.
  1305      * Returns a copy of this {@code LocalDateTime} with the specified number of minutes added.
  1300      * <p>
  1306      * <p>
  1301      * This instance is immutable and unaffected by this method call.
  1307      * This instance is immutable and unaffected by this method call.
  1302      *
  1308      *
  1303      * @param minutes  the minutes to add, may be negative
  1309      * @param minutes  the minutes to add, may be negative
  1304      * @return a {@code LocalDateTime} based on this date-time with the minutes added, not null
  1310      * @return a {@code LocalDateTime} based on this date-time with the minutes added, not null
  1307     public LocalDateTime plusMinutes(long minutes) {
  1313     public LocalDateTime plusMinutes(long minutes) {
  1308         return plusWithOverflow(date, 0, minutes, 0, 0, 1);
  1314         return plusWithOverflow(date, 0, minutes, 0, 0, 1);
  1309     }
  1315     }
  1310 
  1316 
  1311     /**
  1317     /**
  1312      * Returns a copy of this {@code LocalDateTime} with the specified period in seconds added.
  1318      * Returns a copy of this {@code LocalDateTime} with the specified number of seconds added.
  1313      * <p>
  1319      * <p>
  1314      * This instance is immutable and unaffected by this method call.
  1320      * This instance is immutable and unaffected by this method call.
  1315      *
  1321      *
  1316      * @param seconds  the seconds to add, may be negative
  1322      * @param seconds  the seconds to add, may be negative
  1317      * @return a {@code LocalDateTime} based on this date-time with the seconds added, not null
  1323      * @return a {@code LocalDateTime} based on this date-time with the seconds added, not null
  1320     public LocalDateTime plusSeconds(long seconds) {
  1326     public LocalDateTime plusSeconds(long seconds) {
  1321         return plusWithOverflow(date, 0, 0, seconds, 0, 1);
  1327         return plusWithOverflow(date, 0, 0, seconds, 0, 1);
  1322     }
  1328     }
  1323 
  1329 
  1324     /**
  1330     /**
  1325      * Returns a copy of this {@code LocalDateTime} with the specified period in nanoseconds added.
  1331      * Returns a copy of this {@code LocalDateTime} with the specified number of nanoseconds added.
  1326      * <p>
  1332      * <p>
  1327      * This instance is immutable and unaffected by this method call.
  1333      * This instance is immutable and unaffected by this method call.
  1328      *
  1334      *
  1329      * @param nanos  the nanos to add, may be negative
  1335      * @param nanos  the nanos to add, may be negative
  1330      * @return a {@code LocalDateTime} based on this date-time with the nanoseconds added, not null
  1336      * @return a {@code LocalDateTime} based on this date-time with the nanoseconds added, not null
  1389         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
  1395         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
  1390     }
  1396     }
  1391 
  1397 
  1392     //-----------------------------------------------------------------------
  1398     //-----------------------------------------------------------------------
  1393     /**
  1399     /**
  1394      * Returns a copy of this {@code LocalDateTime} with the specified period in years subtracted.
  1400      * Returns a copy of this {@code LocalDateTime} with the specified number of years subtracted.
  1395      * <p>
  1401      * <p>
  1396      * This method subtracts the specified amount from the years field in three steps:
  1402      * This method subtracts the specified amount from the years field in three steps:
  1397      * <ol>
  1403      * <ol>
  1398      * <li>Subtract the input years from the year field</li>
  1404      * <li>Subtract the input years from the year field</li>
  1399      * <li>Check if the resulting date would be invalid</li>
  1405      * <li>Check if the resulting date would be invalid</li>
  1413     public LocalDateTime minusYears(long years) {
  1419     public LocalDateTime minusYears(long years) {
  1414         return (years == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-years));
  1420         return (years == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-years));
  1415     }
  1421     }
  1416 
  1422 
  1417     /**
  1423     /**
  1418      * Returns a copy of this {@code LocalDateTime} with the specified period in months subtracted.
  1424      * Returns a copy of this {@code LocalDateTime} with the specified number of months subtracted.
  1419      * <p>
  1425      * <p>
  1420      * This method subtracts the specified amount from the months field in three steps:
  1426      * This method subtracts the specified amount from the months field in three steps:
  1421      * <ol>
  1427      * <ol>
  1422      * <li>Subtract the input months from the month-of-year field</li>
  1428      * <li>Subtract the input months from the month-of-year field</li>
  1423      * <li>Check if the resulting date would be invalid</li>
  1429      * <li>Check if the resulting date would be invalid</li>
  1437     public LocalDateTime minusMonths(long months) {
  1443     public LocalDateTime minusMonths(long months) {
  1438         return (months == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-months));
  1444         return (months == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-months));
  1439     }
  1445     }
  1440 
  1446 
  1441     /**
  1447     /**
  1442      * Returns a copy of this {@code LocalDateTime} with the specified period in weeks subtracted.
  1448      * Returns a copy of this {@code LocalDateTime} with the specified number of weeks subtracted.
  1443      * <p>
  1449      * <p>
  1444      * This method subtracts the specified amount in weeks from the days field decrementing
  1450      * This method subtracts the specified amount in weeks from the days field decrementing
  1445      * the month and year fields as necessary to ensure the result remains valid.
  1451      * the month and year fields as necessary to ensure the result remains valid.
  1446      * The result is only invalid if the maximum/minimum year is exceeded.
  1452      * The result is only invalid if the maximum/minimum year is exceeded.
  1447      * <p>
  1453      * <p>
  1456     public LocalDateTime minusWeeks(long weeks) {
  1462     public LocalDateTime minusWeeks(long weeks) {
  1457         return (weeks == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeks));
  1463         return (weeks == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeks));
  1458     }
  1464     }
  1459 
  1465 
  1460     /**
  1466     /**
  1461      * Returns a copy of this {@code LocalDateTime} with the specified period in days subtracted.
  1467      * Returns a copy of this {@code LocalDateTime} with the specified number of days subtracted.
  1462      * <p>
  1468      * <p>
  1463      * This method subtracts the specified amount from the days field incrementing the
  1469      * This method subtracts the specified amount from the days field decrementing the
  1464      * month and year fields as necessary to ensure the result remains valid.
  1470      * month and year fields as necessary to ensure the result remains valid.
  1465      * The result is only invalid if the maximum/minimum year is exceeded.
  1471      * The result is only invalid if the maximum/minimum year is exceeded.
  1466      * <p>
  1472      * <p>
  1467      * For example, 2009-01-01 minus one day would result in 2008-12-31.
  1473      * For example, 2009-01-01 minus one day would result in 2008-12-31.
  1468      * <p>
  1474      * <p>
  1476         return (days == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-days));
  1482         return (days == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-days));
  1477     }
  1483     }
  1478 
  1484 
  1479     //-----------------------------------------------------------------------
  1485     //-----------------------------------------------------------------------
  1480     /**
  1486     /**
  1481      * Returns a copy of this {@code LocalDateTime} with the specified period in hours subtracted.
  1487      * Returns a copy of this {@code LocalDateTime} with the specified number of hours subtracted.
  1482      * <p>
  1488      * <p>
  1483      * This instance is immutable and unaffected by this method call.
  1489      * This instance is immutable and unaffected by this method call.
  1484      *
  1490      *
  1485      * @param hours  the hours to subtract, may be negative
  1491      * @param hours  the hours to subtract, may be negative
  1486      * @return a {@code LocalDateTime} based on this date-time with the hours subtracted, not null
  1492      * @return a {@code LocalDateTime} based on this date-time with the hours subtracted, not null
  1489     public LocalDateTime minusHours(long hours) {
  1495     public LocalDateTime minusHours(long hours) {
  1490         return plusWithOverflow(date, hours, 0, 0, 0, -1);
  1496         return plusWithOverflow(date, hours, 0, 0, 0, -1);
  1491    }
  1497    }
  1492 
  1498 
  1493     /**
  1499     /**
  1494      * Returns a copy of this {@code LocalDateTime} with the specified period in minutes subtracted.
  1500      * Returns a copy of this {@code LocalDateTime} with the specified number of minutes subtracted.
  1495      * <p>
  1501      * <p>
  1496      * This instance is immutable and unaffected by this method call.
  1502      * This instance is immutable and unaffected by this method call.
  1497      *
  1503      *
  1498      * @param minutes  the minutes to subtract, may be negative
  1504      * @param minutes  the minutes to subtract, may be negative
  1499      * @return a {@code LocalDateTime} based on this date-time with the minutes subtracted, not null
  1505      * @return a {@code LocalDateTime} based on this date-time with the minutes subtracted, not null
  1502     public LocalDateTime minusMinutes(long minutes) {
  1508     public LocalDateTime minusMinutes(long minutes) {
  1503         return plusWithOverflow(date, 0, minutes, 0, 0, -1);
  1509         return plusWithOverflow(date, 0, minutes, 0, 0, -1);
  1504     }
  1510     }
  1505 
  1511 
  1506     /**
  1512     /**
  1507      * Returns a copy of this {@code LocalDateTime} with the specified period in seconds subtracted.
  1513      * Returns a copy of this {@code LocalDateTime} with the specified number of seconds subtracted.
  1508      * <p>
  1514      * <p>
  1509      * This instance is immutable and unaffected by this method call.
  1515      * This instance is immutable and unaffected by this method call.
  1510      *
  1516      *
  1511      * @param seconds  the seconds to subtract, may be negative
  1517      * @param seconds  the seconds to subtract, may be negative
  1512      * @return a {@code LocalDateTime} based on this date-time with the seconds subtracted, not null
  1518      * @return a {@code LocalDateTime} based on this date-time with the seconds subtracted, not null
  1515     public LocalDateTime minusSeconds(long seconds) {
  1521     public LocalDateTime minusSeconds(long seconds) {
  1516         return plusWithOverflow(date, 0, 0, seconds, 0, -1);
  1522         return plusWithOverflow(date, 0, 0, seconds, 0, -1);
  1517     }
  1523     }
  1518 
  1524 
  1519     /**
  1525     /**
  1520      * Returns a copy of this {@code LocalDateTime} with the specified period in nanoseconds subtracted.
  1526      * Returns a copy of this {@code LocalDateTime} with the specified number of nanoseconds subtracted.
  1521      * <p>
  1527      * <p>
  1522      * This instance is immutable and unaffected by this method call.
  1528      * This instance is immutable and unaffected by this method call.
  1523      *
  1529      *
  1524      * @param nanos  the nanos to subtract, may be negative
  1530      * @param nanos  the nanos to subtract, may be negative
  1525      * @return a {@code LocalDateTime} based on this date-time with the nanoseconds subtracted, not null
  1531      * @return a {@code LocalDateTime} based on this date-time with the nanoseconds subtracted, not null
  1573      * The {@code TemporalQuery} object defines the logic to be used to
  1579      * The {@code TemporalQuery} object defines the logic to be used to
  1574      * obtain the result. Read the documentation of the query to understand
  1580      * obtain the result. Read the documentation of the query to understand
  1575      * what the result of this method will be.
  1581      * what the result of this method will be.
  1576      * <p>
  1582      * <p>
  1577      * The result of this method is obtained by invoking the
  1583      * The result of this method is obtained by invoking the
  1578      * {@link java.time.temporal.TemporalQuery#queryFrom(TemporalAccessor)} method on the
  1584      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
  1579      * specified query passing {@code this} as the argument.
  1585      * specified query passing {@code this} as the argument.
  1580      *
  1586      *
  1581      * @param <R> the type of the result
  1587      * @param <R> the type of the result
  1582      * @param query  the query to invoke, not null
  1588      * @param query  the query to invoke, not null
  1583      * @return the query result, null may be returned (defined by the query)
  1589      * @return the query result, null may be returned (defined by the query)