416 * the {@code LocalDateTime} and {@code ZoneOffset}. |
416 * the {@code LocalDateTime} and {@code ZoneOffset}. |
417 * This combination uniquely specifies an instant without ambiguity. |
417 * This combination uniquely specifies an instant without ambiguity. |
418 * <p> |
418 * <p> |
419 * Converting an instant to a zoned date-time is simple as there is only one valid |
419 * Converting an instant to a zoned date-time is simple as there is only one valid |
420 * offset for each instant. If the valid offset is different to the offset specified, |
420 * offset for each instant. If the valid offset is different to the offset specified, |
421 * the the date-time and offset of the zoned date-time will differ from those specified. |
421 * then the date-time and offset of the zoned date-time will differ from those specified. |
422 * <p> |
422 * <p> |
423 * If the {@code ZoneId} to be used is a {@code ZoneOffset}, this method is equivalent |
423 * If the {@code ZoneId} to be used is a {@code ZoneOffset}, this method is equivalent |
424 * to {@link #of(LocalDateTime, ZoneId)}. |
424 * to {@link #of(LocalDateTime, ZoneId)}. |
425 * |
425 * |
426 * @param localDateTime the local date-time, not null |
426 * @param localDateTime the local date-time, not null |
467 * |
467 * |
468 * @param localDateTime the local date-time, not null |
468 * @param localDateTime the local date-time, not null |
469 * @param offset the zone offset, not null |
469 * @param offset the zone offset, not null |
470 * @param zone the time-zone, not null |
470 * @param zone the time-zone, not null |
471 * @return the zoned date-time, not null |
471 * @return the zoned date-time, not null |
|
472 * @throws DateTimeException if the combination of arguments is invalid |
472 */ |
473 */ |
473 public static ZonedDateTime ofStrict(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) { |
474 public static ZonedDateTime ofStrict(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) { |
474 Objects.requireNonNull(localDateTime, "localDateTime"); |
475 Objects.requireNonNull(localDateTime, "localDateTime"); |
475 Objects.requireNonNull(offset, "offset"); |
476 Objects.requireNonNull(offset, "offset"); |
476 Objects.requireNonNull(zone, "zone"); |
477 Objects.requireNonNull(zone, "zone"); |
536 * with {@code Instant} or {@code LocalDateTime}. |
537 * with {@code Instant} or {@code LocalDateTime}. |
537 * Implementations are permitted to perform optimizations such as accessing |
538 * Implementations are permitted to perform optimizations such as accessing |
538 * those fields that are equivalent to the relevant objects. |
539 * those fields that are equivalent to the relevant objects. |
539 * <p> |
540 * <p> |
540 * This method matches the signature of the functional interface {@link TemporalQuery} |
541 * This method matches the signature of the functional interface {@link TemporalQuery} |
541 * allowing it to be used in queries via method reference, {@code ZonedDateTime::from}. |
542 * allowing it to be used as a query via method reference, {@code ZonedDateTime::from}. |
542 * |
543 * |
543 * @param temporal the temporal object to convert, not null |
544 * @param temporal the temporal object to convert, not null |
544 * @return the zoned date-time, not null |
545 * @return the zoned date-time, not null |
545 * @throws DateTimeException if unable to convert to an {@code ZonedDateTime} |
546 * @throws DateTimeException if unable to convert to an {@code ZonedDateTime} |
546 */ |
547 */ |
780 } |
781 } |
781 |
782 |
782 /** |
783 /** |
783 * Gets the value of the specified field from this date-time as an {@code int}. |
784 * Gets the value of the specified field from this date-time as an {@code int}. |
784 * <p> |
785 * <p> |
785 * This queries this date-time for the value for the specified field. |
786 * This queries this date-time for the value of the specified field. |
786 * The returned value will always be within the valid range of values for the field. |
787 * The returned value will always be within the valid range of values for the field. |
787 * If it is not possible to return the value, because the field is not supported |
788 * If it is not possible to return the value, because the field is not supported |
788 * or for some other reason, an exception is thrown. |
789 * or for some other reason, an exception is thrown. |
789 * <p> |
790 * <p> |
790 * If the field is a {@link ChronoField} then the query is implemented here. |
791 * If the field is a {@link ChronoField} then the query is implemented here. |
791 * The {@link #isSupported(TemporalField) supported fields} will return valid |
792 * The {@link #isSupported(TemporalField) supported fields} will return valid |
792 * values based on this date-time, except {@code NANO_OF_DAY}, {@code MICRO_OF_DAY}, |
793 * values based on this date-time, except {@code NANO_OF_DAY}, {@code MICRO_OF_DAY}, |
793 * {@code EPOCH_DAY}, {@code PROLEPTIC_MONTH} and {@code INSTANT_SECONDS} which are too |
794 * {@code EPOCH_DAY}, {@code PROLEPTIC_MONTH} and {@code INSTANT_SECONDS} which are too |
794 * large to fit in an {@code int} and throw a {@code DateTimeException}. |
795 * large to fit in an {@code int} and throw a {@code UnsupportedTemporalTypeException}. |
795 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
796 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
796 * <p> |
797 * <p> |
797 * If the field is not a {@code ChronoField}, then the result of this method |
798 * If the field is not a {@code ChronoField}, then the result of this method |
798 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
799 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
799 * passing {@code this} as the argument. Whether the value can be obtained, |
800 * passing {@code this} as the argument. Whether the value can be obtained, |
822 } |
823 } |
823 |
824 |
824 /** |
825 /** |
825 * Gets the value of the specified field from this date-time as a {@code long}. |
826 * Gets the value of the specified field from this date-time as a {@code long}. |
826 * <p> |
827 * <p> |
827 * This queries this date-time for the value for the specified field. |
828 * This queries this date-time for the value of the specified field. |
828 * If it is not possible to return the value, because the field is not supported |
829 * If it is not possible to return the value, because the field is not supported |
829 * or for some other reason, an exception is thrown. |
830 * or for some other reason, an exception is thrown. |
830 * <p> |
831 * <p> |
831 * If the field is a {@link ChronoField} then the query is implemented here. |
832 * If the field is a {@link ChronoField} then the query is implemented here. |
832 * The {@link #isSupported(TemporalField) supported fields} will return valid |
833 * The {@link #isSupported(TemporalField) supported fields} will return valid |
1180 * The adjustment takes place using the specified adjuster strategy object. |
1181 * The adjustment takes place using the specified adjuster strategy object. |
1181 * Read the documentation of the adjuster to understand what adjustment will be made. |
1182 * Read the documentation of the adjuster to understand what adjustment will be made. |
1182 * <p> |
1183 * <p> |
1183 * A simple adjuster might simply set the one of the fields, such as the year field. |
1184 * A simple adjuster might simply set the one of the fields, such as the year field. |
1184 * A more complex adjuster might set the date to the last day of the month. |
1185 * A more complex adjuster might set the date to the last day of the month. |
1185 * A selection of common adjustments is provided in {@link TemporalAdjuster}. |
1186 * A selection of common adjustments is provided in |
|
1187 * {@link java.time.temporal.TemporalAdjusters TemporalAdjusters}. |
1186 * These include finding the "last day of the month" and "next Wednesday". |
1188 * These include finding the "last day of the month" and "next Wednesday". |
1187 * Key date-time classes also implement the {@code TemporalAdjuster} interface, |
1189 * Key date-time classes also implement the {@code TemporalAdjuster} interface, |
1188 * such as {@link Month} and {@link java.time.MonthDay MonthDay}. |
1190 * such as {@link Month} and {@link java.time.MonthDay MonthDay}. |
1189 * The adjuster is responsible for handling special cases, such as the varying |
1191 * The adjuster is responsible for handling special cases, such as the varying |
1190 * lengths of month and leap years. |
1192 * lengths of month and leap years. |
1191 * <p> |
1193 * <p> |
1192 * For example this code returns a date on the last day of July: |
1194 * For example this code returns a date on the last day of July: |
1193 * <pre> |
1195 * <pre> |
1194 * import static java.time.Month.*; |
1196 * import static java.time.Month.*; |
1195 * import static java.time.temporal.Adjusters.*; |
1197 * import static java.time.temporal.TemporalAdjusters.*; |
1196 * |
1198 * |
1197 * result = zonedDateTime.with(JULY).with(lastDayOfMonth()); |
1199 * result = zonedDateTime.with(JULY).with(lastDayOfMonth()); |
1198 * </pre> |
1200 * </pre> |
1199 * <p> |
1201 * <p> |
1200 * The classes {@link LocalDate} and {@link LocalTime} implement {@code TemporalAdjuster}, |
1202 * The classes {@link LocalDate} and {@link LocalTime} implement {@code TemporalAdjuster}, |
1311 return field.adjustInto(this, newValue); |
1313 return field.adjustInto(this, newValue); |
1312 } |
1314 } |
1313 |
1315 |
1314 //----------------------------------------------------------------------- |
1316 //----------------------------------------------------------------------- |
1315 /** |
1317 /** |
1316 * Returns a copy of this {@code ZonedDateTime} with the year value altered. |
1318 * Returns a copy of this {@code ZonedDateTime} with the year altered. |
1317 * <p> |
1319 * <p> |
1318 * This operates on the local time-line, |
1320 * This operates on the local time-line, |
1319 * {@link LocalDateTime#withYear(int) changing the year} of the local date-time. |
1321 * {@link LocalDateTime#withYear(int) changing the year} of the local date-time. |
1320 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1322 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1321 * to obtain the offset. |
1323 * to obtain the offset. |
1333 public ZonedDateTime withYear(int year) { |
1335 public ZonedDateTime withYear(int year) { |
1334 return resolveLocal(dateTime.withYear(year)); |
1336 return resolveLocal(dateTime.withYear(year)); |
1335 } |
1337 } |
1336 |
1338 |
1337 /** |
1339 /** |
1338 * Returns a copy of this {@code ZonedDateTime} with the month-of-year value altered. |
1340 * Returns a copy of this {@code ZonedDateTime} with the month-of-year altered. |
1339 * <p> |
1341 * <p> |
1340 * This operates on the local time-line, |
1342 * This operates on the local time-line, |
1341 * {@link LocalDateTime#withMonth(int) changing the month} of the local date-time. |
1343 * {@link LocalDateTime#withMonth(int) changing the month} of the local date-time. |
1342 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1344 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1343 * to obtain the offset. |
1345 * to obtain the offset. |
1355 public ZonedDateTime withMonth(int month) { |
1357 public ZonedDateTime withMonth(int month) { |
1356 return resolveLocal(dateTime.withMonth(month)); |
1358 return resolveLocal(dateTime.withMonth(month)); |
1357 } |
1359 } |
1358 |
1360 |
1359 /** |
1361 /** |
1360 * Returns a copy of this {@code ZonedDateTime} with the day-of-month value altered. |
1362 * Returns a copy of this {@code ZonedDateTime} with the day-of-month altered. |
1361 * <p> |
1363 * <p> |
1362 * This operates on the local time-line, |
1364 * This operates on the local time-line, |
1363 * {@link LocalDateTime#withDayOfMonth(int) changing the day-of-month} of the local date-time. |
1365 * {@link LocalDateTime#withDayOfMonth(int) changing the day-of-month} of the local date-time. |
1364 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1366 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1365 * to obtain the offset. |
1367 * to obtain the offset. |
1402 return resolveLocal(dateTime.withDayOfYear(dayOfYear)); |
1404 return resolveLocal(dateTime.withDayOfYear(dayOfYear)); |
1403 } |
1405 } |
1404 |
1406 |
1405 //----------------------------------------------------------------------- |
1407 //----------------------------------------------------------------------- |
1406 /** |
1408 /** |
1407 * Returns a copy of this {@code ZonedDateTime} with the hour-of-day value altered. |
1409 * Returns a copy of this {@code ZonedDateTime} with the hour-of-day altered. |
1408 * <p> |
1410 * <p> |
1409 * This operates on the local time-line, |
1411 * This operates on the local time-line, |
1410 * {@linkplain LocalDateTime#withHour(int) changing the time} of the local date-time. |
1412 * {@linkplain LocalDateTime#withHour(int) changing the time} of the local date-time. |
1411 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1413 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1412 * to obtain the offset. |
1414 * to obtain the offset. |
1424 public ZonedDateTime withHour(int hour) { |
1426 public ZonedDateTime withHour(int hour) { |
1425 return resolveLocal(dateTime.withHour(hour)); |
1427 return resolveLocal(dateTime.withHour(hour)); |
1426 } |
1428 } |
1427 |
1429 |
1428 /** |
1430 /** |
1429 * Returns a copy of this {@code ZonedDateTime} with the minute-of-hour value altered. |
1431 * Returns a copy of this {@code ZonedDateTime} with the minute-of-hour altered. |
1430 * <p> |
1432 * <p> |
1431 * This operates on the local time-line, |
1433 * This operates on the local time-line, |
1432 * {@linkplain LocalDateTime#withMinute(int) changing the time} of the local date-time. |
1434 * {@linkplain LocalDateTime#withMinute(int) changing the time} of the local date-time. |
1433 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1435 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1434 * to obtain the offset. |
1436 * to obtain the offset. |
1446 public ZonedDateTime withMinute(int minute) { |
1448 public ZonedDateTime withMinute(int minute) { |
1447 return resolveLocal(dateTime.withMinute(minute)); |
1449 return resolveLocal(dateTime.withMinute(minute)); |
1448 } |
1450 } |
1449 |
1451 |
1450 /** |
1452 /** |
1451 * Returns a copy of this {@code ZonedDateTime} with the second-of-minute value altered. |
1453 * Returns a copy of this {@code ZonedDateTime} with the second-of-minute altered. |
1452 * <p> |
1454 * <p> |
1453 * This operates on the local time-line, |
1455 * This operates on the local time-line, |
1454 * {@linkplain LocalDateTime#withSecond(int) changing the time} of the local date-time. |
1456 * {@linkplain LocalDateTime#withSecond(int) changing the time} of the local date-time. |
1455 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1457 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1456 * to obtain the offset. |
1458 * to obtain the offset. |
1468 public ZonedDateTime withSecond(int second) { |
1470 public ZonedDateTime withSecond(int second) { |
1469 return resolveLocal(dateTime.withSecond(second)); |
1471 return resolveLocal(dateTime.withSecond(second)); |
1470 } |
1472 } |
1471 |
1473 |
1472 /** |
1474 /** |
1473 * Returns a copy of this {@code ZonedDateTime} with the nano-of-second value altered. |
1475 * Returns a copy of this {@code ZonedDateTime} with the nano-of-second altered. |
1474 * <p> |
1476 * <p> |
1475 * This operates on the local time-line, |
1477 * This operates on the local time-line, |
1476 * {@linkplain LocalDateTime#withNano(int) changing the time} of the local date-time. |
1478 * {@linkplain LocalDateTime#withNano(int) changing the time} of the local date-time. |
1477 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1479 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1478 * to obtain the offset. |
1480 * to obtain the offset. |
1504 * that divides into the length of a standard day without remainder. |
1506 * that divides into the length of a standard day without remainder. |
1505 * This includes all supplied time units on {@link ChronoUnit} and |
1507 * This includes all supplied time units on {@link ChronoUnit} and |
1506 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception. |
1508 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception. |
1507 * <p> |
1509 * <p> |
1508 * This operates on the local time-line, |
1510 * This operates on the local time-line, |
1509 * {@link LocalDateTime#truncatedTo(java.time.temporal.TemporalUnit) truncating} |
1511 * {@link LocalDateTime#truncatedTo(TemporalUnit) truncating} |
1510 * the underlying local date-time. This is then converted back to a |
1512 * the underlying local date-time. This is then converted back to a |
1511 * {@code ZonedDateTime}, using the zone ID to obtain the offset. |
1513 * {@code ZonedDateTime}, using the zone ID to obtain the offset. |
1512 * <p> |
1514 * <p> |
1513 * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap, |
1515 * When converting back to {@code ZonedDateTime}, if the local date-time is in an overlap, |
1514 * then the offset will be retained if possible, otherwise the earlier offset will be used. |
1516 * then the offset will be retained if possible, otherwise the earlier offset will be used. |
1605 return unit.addTo(this, amountToAdd); |
1607 return unit.addTo(this, amountToAdd); |
1606 } |
1608 } |
1607 |
1609 |
1608 //----------------------------------------------------------------------- |
1610 //----------------------------------------------------------------------- |
1609 /** |
1611 /** |
1610 * Returns a copy of this {@code ZonedDateTime} with the specified period in years added. |
1612 * Returns a copy of this {@code ZonedDateTime} with the specified number of years added. |
1611 * <p> |
1613 * <p> |
1612 * This operates on the local time-line, |
1614 * This operates on the local time-line, |
1613 * {@link LocalDateTime#plusYears(long) adding years} to the local date-time. |
1615 * {@link LocalDateTime#plusYears(long) adding years} to the local date-time. |
1614 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1616 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1615 * to obtain the offset. |
1617 * to obtain the offset. |
1627 public ZonedDateTime plusYears(long years) { |
1629 public ZonedDateTime plusYears(long years) { |
1628 return resolveLocal(dateTime.plusYears(years)); |
1630 return resolveLocal(dateTime.plusYears(years)); |
1629 } |
1631 } |
1630 |
1632 |
1631 /** |
1633 /** |
1632 * Returns a copy of this {@code ZonedDateTime} with the specified period in months added. |
1634 * Returns a copy of this {@code ZonedDateTime} with the specified number of months added. |
1633 * <p> |
1635 * <p> |
1634 * This operates on the local time-line, |
1636 * This operates on the local time-line, |
1635 * {@link LocalDateTime#plusMonths(long) adding months} to the local date-time. |
1637 * {@link LocalDateTime#plusMonths(long) adding months} to the local date-time. |
1636 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1638 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1637 * to obtain the offset. |
1639 * to obtain the offset. |
1649 public ZonedDateTime plusMonths(long months) { |
1651 public ZonedDateTime plusMonths(long months) { |
1650 return resolveLocal(dateTime.plusMonths(months)); |
1652 return resolveLocal(dateTime.plusMonths(months)); |
1651 } |
1653 } |
1652 |
1654 |
1653 /** |
1655 /** |
1654 * Returns a copy of this {@code ZonedDateTime} with the specified period in weeks added. |
1656 * Returns a copy of this {@code ZonedDateTime} with the specified number of weeks added. |
1655 * <p> |
1657 * <p> |
1656 * This operates on the local time-line, |
1658 * This operates on the local time-line, |
1657 * {@link LocalDateTime#plusWeeks(long) adding weeks} to the local date-time. |
1659 * {@link LocalDateTime#plusWeeks(long) adding weeks} to the local date-time. |
1658 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1660 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1659 * to obtain the offset. |
1661 * to obtain the offset. |
1671 public ZonedDateTime plusWeeks(long weeks) { |
1673 public ZonedDateTime plusWeeks(long weeks) { |
1672 return resolveLocal(dateTime.plusWeeks(weeks)); |
1674 return resolveLocal(dateTime.plusWeeks(weeks)); |
1673 } |
1675 } |
1674 |
1676 |
1675 /** |
1677 /** |
1676 * Returns a copy of this {@code ZonedDateTime} with the specified period in days added. |
1678 * Returns a copy of this {@code ZonedDateTime} with the specified number of days added. |
1677 * <p> |
1679 * <p> |
1678 * This operates on the local time-line, |
1680 * This operates on the local time-line, |
1679 * {@link LocalDateTime#plusDays(long) adding days} to the local date-time. |
1681 * {@link LocalDateTime#plusDays(long) adding days} to the local date-time. |
1680 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1682 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1681 * to obtain the offset. |
1683 * to obtain the offset. |
1694 return resolveLocal(dateTime.plusDays(days)); |
1696 return resolveLocal(dateTime.plusDays(days)); |
1695 } |
1697 } |
1696 |
1698 |
1697 //----------------------------------------------------------------------- |
1699 //----------------------------------------------------------------------- |
1698 /** |
1700 /** |
1699 * Returns a copy of this {@code ZonedDateTime} with the specified period in hours added. |
1701 * Returns a copy of this {@code ZonedDateTime} with the specified number of hours added. |
1700 * <p> |
1702 * <p> |
1701 * This operates on the instant time-line, such that adding one hour will |
1703 * This operates on the instant time-line, such that adding one hour will |
1702 * always be a duration of one hour later. |
1704 * always be a duration of one hour later. |
1703 * This may cause the local date-time to change by an amount other than one hour. |
1705 * This may cause the local date-time to change by an amount other than one hour. |
1704 * Note that this is a different approach to that used by days, months and years, |
1706 * Note that this is a different approach to that used by days, months and years, |
1722 public ZonedDateTime plusHours(long hours) { |
1724 public ZonedDateTime plusHours(long hours) { |
1723 return resolveInstant(dateTime.plusHours(hours)); |
1725 return resolveInstant(dateTime.plusHours(hours)); |
1724 } |
1726 } |
1725 |
1727 |
1726 /** |
1728 /** |
1727 * Returns a copy of this {@code ZonedDateTime} with the specified period in minutes added. |
1729 * Returns a copy of this {@code ZonedDateTime} with the specified number of minutes added. |
1728 * <p> |
1730 * <p> |
1729 * This operates on the instant time-line, such that adding one minute will |
1731 * This operates on the instant time-line, such that adding one minute will |
1730 * always be a duration of one minute later. |
1732 * always be a duration of one minute later. |
1731 * This may cause the local date-time to change by an amount other than one minute. |
1733 * This may cause the local date-time to change by an amount other than one minute. |
1732 * Note that this is a different approach to that used by days, months and years. |
1734 * Note that this is a different approach to that used by days, months and years. |
1740 public ZonedDateTime plusMinutes(long minutes) { |
1742 public ZonedDateTime plusMinutes(long minutes) { |
1741 return resolveInstant(dateTime.plusMinutes(minutes)); |
1743 return resolveInstant(dateTime.plusMinutes(minutes)); |
1742 } |
1744 } |
1743 |
1745 |
1744 /** |
1746 /** |
1745 * Returns a copy of this {@code ZonedDateTime} with the specified period in seconds added. |
1747 * Returns a copy of this {@code ZonedDateTime} with the specified number of seconds added. |
1746 * <p> |
1748 * <p> |
1747 * This operates on the instant time-line, such that adding one second will |
1749 * This operates on the instant time-line, such that adding one second will |
1748 * always be a duration of one second later. |
1750 * always be a duration of one second later. |
1749 * This may cause the local date-time to change by an amount other than one second. |
1751 * This may cause the local date-time to change by an amount other than one second. |
1750 * Note that this is a different approach to that used by days, months and years. |
1752 * Note that this is a different approach to that used by days, months and years. |
1758 public ZonedDateTime plusSeconds(long seconds) { |
1760 public ZonedDateTime plusSeconds(long seconds) { |
1759 return resolveInstant(dateTime.plusSeconds(seconds)); |
1761 return resolveInstant(dateTime.plusSeconds(seconds)); |
1760 } |
1762 } |
1761 |
1763 |
1762 /** |
1764 /** |
1763 * Returns a copy of this {@code ZonedDateTime} with the specified period in nanoseconds added. |
1765 * Returns a copy of this {@code ZonedDateTime} with the specified number of nanoseconds added. |
1764 * <p> |
1766 * <p> |
1765 * This operates on the instant time-line, such that adding one nano will |
1767 * This operates on the instant time-line, such that adding one nano will |
1766 * always be a duration of one nano later. |
1768 * always be a duration of one nano later. |
1767 * This may cause the local date-time to change by an amount other than one nano. |
1769 * This may cause the local date-time to change by an amount other than one nano. |
1768 * Note that this is a different approach to that used by days, months and years. |
1770 * Note that this is a different approach to that used by days, months and years. |
1846 return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); |
1848 return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); |
1847 } |
1849 } |
1848 |
1850 |
1849 //----------------------------------------------------------------------- |
1851 //----------------------------------------------------------------------- |
1850 /** |
1852 /** |
1851 * Returns a copy of this {@code ZonedDateTime} with the specified period in years subtracted. |
1853 * Returns a copy of this {@code ZonedDateTime} with the specified number of years subtracted. |
1852 * <p> |
1854 * <p> |
1853 * This operates on the local time-line, |
1855 * This operates on the local time-line, |
1854 * {@link LocalDateTime#minusYears(long) subtracting years} to the local date-time. |
1856 * {@link LocalDateTime#minusYears(long) subtracting years} to the local date-time. |
1855 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1857 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1856 * to obtain the offset. |
1858 * to obtain the offset. |
1868 public ZonedDateTime minusYears(long years) { |
1870 public ZonedDateTime minusYears(long years) { |
1869 return (years == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-years)); |
1871 return (years == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-years)); |
1870 } |
1872 } |
1871 |
1873 |
1872 /** |
1874 /** |
1873 * Returns a copy of this {@code ZonedDateTime} with the specified period in months subtracted. |
1875 * Returns a copy of this {@code ZonedDateTime} with the specified number of months subtracted. |
1874 * <p> |
1876 * <p> |
1875 * This operates on the local time-line, |
1877 * This operates on the local time-line, |
1876 * {@link LocalDateTime#minusMonths(long) subtracting months} to the local date-time. |
1878 * {@link LocalDateTime#minusMonths(long) subtracting months} to the local date-time. |
1877 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1879 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1878 * to obtain the offset. |
1880 * to obtain the offset. |
1890 public ZonedDateTime minusMonths(long months) { |
1892 public ZonedDateTime minusMonths(long months) { |
1891 return (months == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-months)); |
1893 return (months == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-months)); |
1892 } |
1894 } |
1893 |
1895 |
1894 /** |
1896 /** |
1895 * Returns a copy of this {@code ZonedDateTime} with the specified period in weeks subtracted. |
1897 * Returns a copy of this {@code ZonedDateTime} with the specified number of weeks subtracted. |
1896 * <p> |
1898 * <p> |
1897 * This operates on the local time-line, |
1899 * This operates on the local time-line, |
1898 * {@link LocalDateTime#minusWeeks(long) subtracting weeks} to the local date-time. |
1900 * {@link LocalDateTime#minusWeeks(long) subtracting weeks} to the local date-time. |
1899 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1901 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1900 * to obtain the offset. |
1902 * to obtain the offset. |
1912 public ZonedDateTime minusWeeks(long weeks) { |
1914 public ZonedDateTime minusWeeks(long weeks) { |
1913 return (weeks == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeks)); |
1915 return (weeks == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeks)); |
1914 } |
1916 } |
1915 |
1917 |
1916 /** |
1918 /** |
1917 * Returns a copy of this {@code ZonedDateTime} with the specified period in days subtracted. |
1919 * Returns a copy of this {@code ZonedDateTime} with the specified number of days subtracted. |
1918 * <p> |
1920 * <p> |
1919 * This operates on the local time-line, |
1921 * This operates on the local time-line, |
1920 * {@link LocalDateTime#minusDays(long) subtracting days} to the local date-time. |
1922 * {@link LocalDateTime#minusDays(long) subtracting days} to the local date-time. |
1921 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1923 * This is then converted back to a {@code ZonedDateTime}, using the zone ID |
1922 * to obtain the offset. |
1924 * to obtain the offset. |
1935 return (days == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-days)); |
1937 return (days == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-days)); |
1936 } |
1938 } |
1937 |
1939 |
1938 //----------------------------------------------------------------------- |
1940 //----------------------------------------------------------------------- |
1939 /** |
1941 /** |
1940 * Returns a copy of this {@code ZonedDateTime} with the specified period in hours subtracted. |
1942 * Returns a copy of this {@code ZonedDateTime} with the specified number of hours subtracted. |
1941 * <p> |
1943 * <p> |
1942 * This operates on the instant time-line, such that subtracting one hour will |
1944 * This operates on the instant time-line, such that subtracting one hour will |
1943 * always be a duration of one hour earlier. |
1945 * always be a duration of one hour earlier. |
1944 * This may cause the local date-time to change by an amount other than one hour. |
1946 * This may cause the local date-time to change by an amount other than one hour. |
1945 * Note that this is a different approach to that used by days, months and years, |
1947 * Note that this is a different approach to that used by days, months and years, |
1963 public ZonedDateTime minusHours(long hours) { |
1965 public ZonedDateTime minusHours(long hours) { |
1964 return (hours == Long.MIN_VALUE ? plusHours(Long.MAX_VALUE).plusHours(1) : plusHours(-hours)); |
1966 return (hours == Long.MIN_VALUE ? plusHours(Long.MAX_VALUE).plusHours(1) : plusHours(-hours)); |
1965 } |
1967 } |
1966 |
1968 |
1967 /** |
1969 /** |
1968 * Returns a copy of this {@code ZonedDateTime} with the specified period in minutes subtracted. |
1970 * Returns a copy of this {@code ZonedDateTime} with the specified number of minutes subtracted. |
1969 * <p> |
1971 * <p> |
1970 * This operates on the instant time-line, such that subtracting one minute will |
1972 * This operates on the instant time-line, such that subtracting one minute will |
1971 * always be a duration of one minute earlier. |
1973 * always be a duration of one minute earlier. |
1972 * This may cause the local date-time to change by an amount other than one minute. |
1974 * This may cause the local date-time to change by an amount other than one minute. |
1973 * Note that this is a different approach to that used by days, months and years. |
1975 * Note that this is a different approach to that used by days, months and years. |
1981 public ZonedDateTime minusMinutes(long minutes) { |
1983 public ZonedDateTime minusMinutes(long minutes) { |
1982 return (minutes == Long.MIN_VALUE ? plusMinutes(Long.MAX_VALUE).plusMinutes(1) : plusMinutes(-minutes)); |
1984 return (minutes == Long.MIN_VALUE ? plusMinutes(Long.MAX_VALUE).plusMinutes(1) : plusMinutes(-minutes)); |
1983 } |
1985 } |
1984 |
1986 |
1985 /** |
1987 /** |
1986 * Returns a copy of this {@code ZonedDateTime} with the specified period in seconds subtracted. |
1988 * Returns a copy of this {@code ZonedDateTime} with the specified number of seconds subtracted. |
1987 * <p> |
1989 * <p> |
1988 * This operates on the instant time-line, such that subtracting one second will |
1990 * This operates on the instant time-line, such that subtracting one second will |
1989 * always be a duration of one second earlier. |
1991 * always be a duration of one second earlier. |
1990 * This may cause the local date-time to change by an amount other than one second. |
1992 * This may cause the local date-time to change by an amount other than one second. |
1991 * Note that this is a different approach to that used by days, months and years. |
1993 * Note that this is a different approach to that used by days, months and years. |
1999 public ZonedDateTime minusSeconds(long seconds) { |
2001 public ZonedDateTime minusSeconds(long seconds) { |
2000 return (seconds == Long.MIN_VALUE ? plusSeconds(Long.MAX_VALUE).plusSeconds(1) : plusSeconds(-seconds)); |
2002 return (seconds == Long.MIN_VALUE ? plusSeconds(Long.MAX_VALUE).plusSeconds(1) : plusSeconds(-seconds)); |
2001 } |
2003 } |
2002 |
2004 |
2003 /** |
2005 /** |
2004 * Returns a copy of this {@code ZonedDateTime} with the specified period in nanoseconds subtracted. |
2006 * Returns a copy of this {@code ZonedDateTime} with the specified number of nanoseconds subtracted. |
2005 * <p> |
2007 * <p> |
2006 * This operates on the instant time-line, such that subtracting one nano will |
2008 * This operates on the instant time-line, such that subtracting one nano will |
2007 * always be a duration of one nano earlier. |
2009 * always be a duration of one nano earlier. |
2008 * This may cause the local date-time to change by an amount other than one nano. |
2010 * This may cause the local date-time to change by an amount other than one nano. |
2009 * Note that this is a different approach to that used by days, months and years. |
2011 * Note that this is a different approach to that used by days, months and years. |
2026 * The {@code TemporalQuery} object defines the logic to be used to |
2028 * The {@code TemporalQuery} object defines the logic to be used to |
2027 * obtain the result. Read the documentation of the query to understand |
2029 * obtain the result. Read the documentation of the query to understand |
2028 * what the result of this method will be. |
2030 * what the result of this method will be. |
2029 * <p> |
2031 * <p> |
2030 * The result of this method is obtained by invoking the |
2032 * The result of this method is obtained by invoking the |
2031 * {@link java.time.temporal.TemporalQuery#queryFrom(TemporalAccessor)} method on the |
2033 * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the |
2032 * specified query passing {@code this} as the argument. |
2034 * specified query passing {@code this} as the argument. |
2033 * |
2035 * |
2034 * @param <R> the type of the result |
2036 * @param <R> the type of the result |
2035 * @param query the query to invoke, not null |
2037 * @param query the query to invoke, not null |
2036 * @return the query result, null may be returned (defined by the query) |
2038 * @return the query result, null may be returned (defined by the query) |
2047 * <p> |
2049 * <p> |
2048 * This calculates the amount of time between two {@code ZonedDateTime} |
2050 * This calculates the amount of time between two {@code ZonedDateTime} |
2049 * objects in terms of a single {@code TemporalUnit}. |
2051 * objects in terms of a single {@code TemporalUnit}. |
2050 * The start and end points are {@code this} and the specified date-time. |
2052 * The start and end points are {@code this} and the specified date-time. |
2051 * The result will be negative if the end is before the start. |
2053 * The result will be negative if the end is before the start. |
2052 * For example, the period in days between two date-times can be calculated |
2054 * For example, the amount in days between two date-times can be calculated |
2053 * using {@code startDateTime.until(endDateTime, DAYS)}. |
2055 * using {@code startDateTime.until(endDateTime, DAYS)}. |
2054 * <p> |
2056 * <p> |
2055 * The {@code Temporal} passed to this method is converted to a |
2057 * The {@code Temporal} passed to this method is converted to a |
2056 * {@code ZonedDateTime} using {@link #from(TemporalAccessor)}. |
2058 * {@code ZonedDateTime} using {@link #from(TemporalAccessor)}. |
2057 * If the time-zone differs between the two zoned date-times, the specified |
2059 * If the time-zone differs between the two zoned date-times, the specified |
2058 * end date-time is normalized to have the same zone as this date-time. |
2060 * end date-time is normalized to have the same zone as this date-time. |
2059 * <p> |
2061 * <p> |
2060 * The calculation returns a whole number, representing the number of |
2062 * The calculation returns a whole number, representing the number of |
2061 * complete units between the two date-times. |
2063 * complete units between the two date-times. |
2062 * For example, the period in months between 2012-06-15T00:00Z and 2012-08-14T23:59Z |
2064 * For example, the amount in months between 2012-06-15T00:00Z and 2012-08-14T23:59Z |
2063 * will only be one month as it is one minute short of two months. |
2065 * will only be one month as it is one minute short of two months. |
2064 * <p> |
2066 * <p> |
2065 * There are two equivalent ways of using this method. |
2067 * There are two equivalent ways of using this method. |
2066 * The first is to invoke this method. |
2068 * The first is to invoke this method. |
2067 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: |
2069 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: |