src/share/classes/java/time/LocalDateTime.java

Print this page




  53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61  */
  62 package java.time;
  63 
  64 import static java.time.LocalTime.HOURS_PER_DAY;
  65 import static java.time.LocalTime.MICROS_PER_DAY;
  66 import static java.time.LocalTime.MILLIS_PER_DAY;
  67 import static java.time.LocalTime.MINUTES_PER_DAY;
  68 import static java.time.LocalTime.NANOS_PER_DAY;
  69 import static java.time.LocalTime.NANOS_PER_HOUR;
  70 import static java.time.LocalTime.NANOS_PER_MINUTE;
  71 import static java.time.LocalTime.NANOS_PER_SECOND;
  72 import static java.time.LocalTime.SECONDS_PER_DAY;

  73 
  74 import java.io.DataInput;
  75 import java.io.DataOutput;
  76 import java.io.IOException;
  77 import java.io.InvalidObjectException;
  78 import java.io.ObjectStreamException;
  79 import java.io.Serializable;


  80 import java.time.format.DateTimeFormatter;
  81 import java.time.format.DateTimeFormatters;
  82 import java.time.format.DateTimeParseException;
  83 import java.time.temporal.ChronoField;
  84 import java.time.temporal.ChronoLocalDateTime;
  85 import java.time.temporal.ChronoUnit;
  86 import java.time.temporal.ISOChrono;
  87 import java.time.temporal.OffsetDateTime;
  88 import java.time.temporal.Temporal;
  89 import java.time.temporal.TemporalAccessor;
  90 import java.time.temporal.TemporalAdder;
  91 import java.time.temporal.TemporalAdjuster;

  92 import java.time.temporal.TemporalField;
  93 import java.time.temporal.TemporalQuery;
  94 import java.time.temporal.TemporalSubtractor;
  95 import java.time.temporal.TemporalUnit;
  96 import java.time.temporal.ValueRange;
  97 import java.time.zone.ZoneRules;
  98 import java.util.Objects;
  99 
 100 /**
 101  * A date-time without a time-zone in the ISO-8601 calendar system,
 102  * such as {@code 2007-12-03T10:15:30}.
 103  * <p>
 104  * {@code LocalDateTime} is an immutable date-time object that represents a date-time,
 105  * often viewed as year-month-day-hour-minute-second. Other date and time fields,
 106  * such as day-of-year, day-of-week and week-of-year, can also be accessed.
 107  * Time is represented to nanosecond precision.
 108  * For example, the value "2nd October 2007 at 13:45.30.123456789" can be
 109  * stored in a {@code LocalDateTime}.
 110  * <p>
 111  * This class does not store or represent a time-zone.
 112  * Instead, it is a description of the date, as used for birthdays, combined with
 113  * the local time as seen on a wall clock.
 114  * It cannot represent an instant on the time-line without additional information
 115  * such as an offset or time-zone.
 116  * <p>
 117  * The ISO-8601 calendar system is the modern civil calendar system used today
 118  * in most of the world. It is equivalent to the proleptic Gregorian calendar
 119  * system, in which today's rules for leap years are applied for all time.
 120  * For most applications written today, the ISO-8601 rules are entirely suitable.
 121  * However, any application that makes use of historical dates, and requires them
 122  * to be accurate will find the ISO-8601 approach unsuitable.
 123  *
 124  * <h3>Specification for implementors</h3>
 125  * This class is immutable and thread-safe.
 126  *
 127  * @since 1.8
 128  */
 129 public final class LocalDateTime
 130         implements Temporal, TemporalAdjuster, ChronoLocalDateTime<ISOChrono>, Serializable {
 131 
 132     /**
 133      * The minimum supported {@code LocalDateTime}, '-999999999-01-01T00:00:00'.
 134      * This is the local date-time of midnight at the start of the minimum date.
 135      * This combines {@link LocalDate#MIN} and {@link LocalTime#MIN}.
 136      * This could be used by an application as a "far past" date-time.
 137      */
 138     public static final LocalDateTime MIN = LocalDateTime.of(LocalDate.MIN, LocalTime.MIN);
 139     /**
 140      * The maximum supported {@code LocalDateTime}, '+999999999-12-31T23:59:59.999999999'.
 141      * This is the local date-time just before midnight at the end of the maximum date.
 142      * This combines {@link LocalDate#MAX} and {@link LocalTime#MAX}.
 143      * This could be used by an application as a "far future" date-time.
 144      */
 145     public static final LocalDateTime MAX = LocalDateTime.of(LocalDate.MAX, LocalTime.MAX);
 146 
 147     /**
 148      * Serialization version.
 149      */
 150     private static final long serialVersionUID = 6207766400415563566L;


 195      * <p>
 196      * This will query the specified clock to obtain the current date-time.
 197      * Using this method allows the use of an alternate clock for testing.
 198      * The alternate clock may be introduced using {@link Clock dependency injection}.
 199      *
 200      * @param clock  the clock to use, not null
 201      * @return the current date-time, not null
 202      */
 203     public static LocalDateTime now(Clock clock) {
 204         Objects.requireNonNull(clock, "clock");
 205         final Instant now = clock.instant();  // called once
 206         ZoneOffset offset = clock.getZone().getRules().getOffset(now);
 207         return ofEpochSecond(now.getEpochSecond(), now.getNano(), offset);
 208     }
 209 
 210     //-----------------------------------------------------------------------
 211     /**
 212      * Obtains an instance of {@code LocalDateTime} from year, month,
 213      * day, hour and minute, setting the second and nanosecond to zero.
 214      * <p>


 215      * The day must be valid for the year and month, otherwise an exception will be thrown.
 216      * The second and nanosecond fields will be set to zero.
 217      *
 218      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 219      * @param month  the month-of-year to represent, not null
 220      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 221      * @param hour  the hour-of-day to represent, from 0 to 23
 222      * @param minute  the minute-of-hour to represent, from 0 to 59
 223      * @return the local date-time, not null
 224      * @throws DateTimeException if the value of any field is out of range
 225      * @throws DateTimeException if the day-of-month is invalid for the month-year
 226      */
 227     public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute) {
 228         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 229         LocalTime time = LocalTime.of(hour, minute);
 230         return new LocalDateTime(date, time);
 231     }
 232 
 233     /**
 234      * Obtains an instance of {@code LocalDateTime} from year, month,
 235      * day, hour, minute and second, setting the nanosecond to zero.
 236      * <p>


 237      * The day must be valid for the year and month, otherwise an exception will be thrown.
 238      * The nanosecond field will be set to zero.
 239      *
 240      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 241      * @param month  the month-of-year to represent, not null
 242      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 243      * @param hour  the hour-of-day to represent, from 0 to 23
 244      * @param minute  the minute-of-hour to represent, from 0 to 59
 245      * @param second  the second-of-minute to represent, from 0 to 59
 246      * @return the local date-time, not null
 247      * @throws DateTimeException if the value of any field is out of range
 248      * @throws DateTimeException if the day-of-month is invalid for the month-year
 249      */
 250     public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) {
 251         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 252         LocalTime time = LocalTime.of(hour, minute, second);
 253         return new LocalDateTime(date, time);
 254     }
 255 
 256     /**
 257      * Obtains an instance of {@code LocalDateTime} from year, month,
 258      * day, hour, minute, second and nanosecond.
 259      * <p>


 260      * The day must be valid for the year and month, otherwise an exception will be thrown.
 261      *
 262      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 263      * @param month  the month-of-year to represent, not null
 264      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 265      * @param hour  the hour-of-day to represent, from 0 to 23
 266      * @param minute  the minute-of-hour to represent, from 0 to 59
 267      * @param second  the second-of-minute to represent, from 0 to 59
 268      * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
 269      * @return the local date-time, not null
 270      * @throws DateTimeException if the value of any field is out of range
 271      * @throws DateTimeException if the day-of-month is invalid for the month-year
 272      */
 273     public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) {
 274         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 275         LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond);
 276         return new LocalDateTime(date, time);
 277     }
 278 
 279     //-----------------------------------------------------------------------
 280     /**
 281      * Obtains an instance of {@code LocalDateTime} from year, month,
 282      * day, hour and minute, setting the second and nanosecond to zero.
 283      * <p>


 284      * The day must be valid for the year and month, otherwise an exception will be thrown.
 285      * The second and nanosecond fields will be set to zero.
 286      *
 287      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 288      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 289      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 290      * @param hour  the hour-of-day to represent, from 0 to 23
 291      * @param minute  the minute-of-hour to represent, from 0 to 59
 292      * @return the local date-time, not null
 293      * @throws DateTimeException if the value of any field is out of range
 294      * @throws DateTimeException if the day-of-month is invalid for the month-year
 295      */
 296     public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) {
 297         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 298         LocalTime time = LocalTime.of(hour, minute);
 299         return new LocalDateTime(date, time);
 300     }
 301 
 302     /**
 303      * Obtains an instance of {@code LocalDateTime} from year, month,
 304      * day, hour, minute and second, setting the nanosecond to zero.
 305      * <p>


 306      * The day must be valid for the year and month, otherwise an exception will be thrown.
 307      * The nanosecond field will be set to zero.
 308      *
 309      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 310      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 311      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 312      * @param hour  the hour-of-day to represent, from 0 to 23
 313      * @param minute  the minute-of-hour to represent, from 0 to 59
 314      * @param second  the second-of-minute to represent, from 0 to 59
 315      * @return the local date-time, not null
 316      * @throws DateTimeException if the value of any field is out of range
 317      * @throws DateTimeException if the day-of-month is invalid for the month-year
 318      */
 319     public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) {
 320         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 321         LocalTime time = LocalTime.of(hour, minute, second);
 322         return new LocalDateTime(date, time);
 323     }
 324 
 325     /**
 326      * Obtains an instance of {@code LocalDateTime} from year, month,
 327      * day, hour, minute, second and nanosecond.
 328      * <p>


 329      * The day must be valid for the year and month, otherwise an exception will be thrown.
 330      *
 331      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 332      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 333      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 334      * @param hour  the hour-of-day to represent, from 0 to 23
 335      * @param minute  the minute-of-hour to represent, from 0 to 59
 336      * @param second  the second-of-minute to represent, from 0 to 59
 337      * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
 338      * @return the local date-time, not null
 339      * @throws DateTimeException if the value of any field is out of range
 340      * @throws DateTimeException if the day-of-month is invalid for the month-year
 341      */
 342     public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) {
 343         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 344         LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond);
 345         return new LocalDateTime(date, time);
 346     }
 347 
 348     /**
 349      * Obtains an instance of {@code LocalDateTime} from a date and time.
 350      *
 351      * @param date  the local date, not null
 352      * @param time  the local time, not null
 353      * @return the local date-time, not null
 354      */
 355     public static LocalDateTime of(LocalDate date, LocalTime time) {
 356         Objects.requireNonNull(date, "date");
 357         Objects.requireNonNull(time, "time");
 358         return new LocalDateTime(date, time);
 359     }
 360 


 375     public static LocalDateTime ofInstant(Instant instant, ZoneId zone) {
 376         Objects.requireNonNull(instant, "instant");
 377         Objects.requireNonNull(zone, "zone");
 378         ZoneRules rules = zone.getRules();
 379         ZoneOffset offset = rules.getOffset(instant);
 380         return ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
 381     }
 382 
 383     /**
 384      * Obtains an instance of {@code LocalDateTime} using seconds from the
 385      * epoch of 1970-01-01T00:00:00Z.
 386      * <p>
 387      * This allows the {@link ChronoField#INSTANT_SECONDS epoch-second} field
 388      * to be converted to a local date-time. This is primarily intended for
 389      * low-level conversions rather than general application usage.
 390      *
 391      * @param epochSecond  the number of seconds from the epoch of 1970-01-01T00:00:00Z
 392      * @param nanoOfSecond  the nanosecond within the second, from 0 to 999,999,999
 393      * @param offset  the zone offset, not null
 394      * @return the local date-time, not null
 395      * @throws DateTimeException if the result exceeds the supported range

 396      */
 397     public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) {
 398         Objects.requireNonNull(offset, "offset");

 399         long localSecond = epochSecond + offset.getTotalSeconds();  // overflow caught later
 400         long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY);
 401         int secsOfDay = (int)Math.floorMod(localSecond, SECONDS_PER_DAY);
 402         LocalDate date = LocalDate.ofEpochDay(localEpochDay);
 403         LocalTime time = LocalTime.ofSecondOfDay(secsOfDay, nanoOfSecond);
 404         return new LocalDateTime(date, time);
 405     }
 406 
 407     //-----------------------------------------------------------------------
 408     /**
 409      * Obtains an instance of {@code LocalDateTime} from a temporal object.
 410      * <p>
 411      * A {@code TemporalAccessor} represents some form of date and time information.
 412      * This factory converts the arbitrary temporal object to an instance of {@code LocalDateTime}.
 413      * <p>
 414      * The conversion extracts and combines {@code LocalDate} and {@code LocalTime}.




 415      * <p>
 416      * This method matches the signature of the functional interface {@link TemporalQuery}
 417      * allowing it to be used as a query via method reference, {@code LocalDateTime::from}.
 418      *
 419      * @param temporal  the temporal object to convert, not null
 420      * @return the local date-time, not null
 421      * @throws DateTimeException if unable to convert to a {@code LocalDateTime}
 422      */
 423     public static LocalDateTime from(TemporalAccessor temporal) {
 424         if (temporal instanceof LocalDateTime) {
 425             return (LocalDateTime) temporal;
 426         } else if (temporal instanceof ZonedDateTime) {
 427             return ((ZonedDateTime) temporal).getDateTime();


 428         }
 429         try {
 430             LocalDate date = LocalDate.from(temporal);
 431             LocalTime time = LocalTime.from(temporal);
 432             return new LocalDateTime(date, time);
 433         } catch (DateTimeException ex) {
 434             throw new DateTimeException("Unable to obtain LocalDateTime from TemporalAccessor: " + temporal.getClass(), ex);
 435         }
 436     }
 437 
 438     //-----------------------------------------------------------------------
 439     /**
 440      * Obtains an instance of {@code LocalDateTime} from a text string such as {@code 2007-12-03T10:15:30}.
 441      * <p>
 442      * The string must represent a valid date-time and is parsed using
 443      * {@link java.time.format.DateTimeFormatters#isoLocalDateTime()}.
 444      *
 445      * @param text  the text to parse such as "2007-12-03T10:15:30", not null
 446      * @return the parsed local date-time, not null
 447      * @throws DateTimeParseException if the text cannot be parsed
 448      */
 449     public static LocalDateTime parse(CharSequence text) {
 450         return parse(text, DateTimeFormatters.isoLocalDateTime());
 451     }
 452 
 453     /**
 454      * Obtains an instance of {@code LocalDateTime} from a text string using a specific formatter.
 455      * <p>
 456      * The text is parsed using the formatter, returning a date-time.
 457      *
 458      * @param text  the text to parse, not null
 459      * @param formatter  the formatter to use, not null
 460      * @return the parsed local date-time, not null
 461      * @throws DateTimeParseException if the text cannot be parsed
 462      */
 463     public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) {
 464         Objects.requireNonNull(formatter, "formatter");
 465         return formatter.parse(text, LocalDateTime::from);
 466     }
 467 
 468     //-----------------------------------------------------------------------
 469     /**
 470      * Constructor.


 518      * <li>{@code HOUR_OF_DAY}
 519      * <li>{@code CLOCK_HOUR_OF_DAY}
 520      * <li>{@code AMPM_OF_DAY}
 521      * <li>{@code DAY_OF_WEEK}
 522      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
 523      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
 524      * <li>{@code DAY_OF_MONTH}
 525      * <li>{@code DAY_OF_YEAR}
 526      * <li>{@code EPOCH_DAY}
 527      * <li>{@code ALIGNED_WEEK_OF_MONTH}
 528      * <li>{@code ALIGNED_WEEK_OF_YEAR}
 529      * <li>{@code MONTH_OF_YEAR}
 530      * <li>{@code EPOCH_MONTH}
 531      * <li>{@code YEAR_OF_ERA}
 532      * <li>{@code YEAR}
 533      * <li>{@code ERA}
 534      * </ul>
 535      * All other {@code ChronoField} instances will return false.
 536      * <p>
 537      * If the field is not a {@code ChronoField}, then the result of this method
 538      * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)}
 539      * passing {@code this} as the argument.
 540      * Whether the field is supported is determined by the field.
 541      *
 542      * @param field  the field to check, null returns false
 543      * @return true if the field is supported on this date-time, false if not
 544      */
 545     @Override
 546     public boolean isSupported(TemporalField field) {
 547         if (field instanceof ChronoField) {
 548             ChronoField f = (ChronoField) field;
 549             return f.isDateField() || f.isTimeField();
 550         }
 551         return field != null && field.doIsSupported(this);
 552     }
 553 
 554     /**
 555      * Gets the range of valid values for the specified field.
 556      * <p>
 557      * The range object expresses the minimum and maximum valid values for a field.
 558      * This date-time is used to enhance the accuracy of the returned range.
 559      * If it is not possible to return the range, because the field is not supported
 560      * or for some other reason, an exception is thrown.
 561      * <p>
 562      * If the field is a {@link ChronoField} then the query is implemented here.
 563      * The {@link #isSupported(TemporalField) supported fields} will return
 564      * appropriate range instances.
 565      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 566      * <p>
 567      * If the field is not a {@code ChronoField}, then the result of this method
 568      * is obtained by invoking {@code TemporalField.doRange(TemporalAccessor)}
 569      * passing {@code this} as the argument.
 570      * Whether the range can be obtained is determined by the field.
 571      *
 572      * @param field  the field to query the range for, not null
 573      * @return the range of valid values for the field, not null
 574      * @throws DateTimeException if the range for the field cannot be obtained
 575      */
 576     @Override
 577     public ValueRange range(TemporalField field) {
 578         if (field instanceof ChronoField) {
 579             ChronoField f = (ChronoField) field;
 580             return (f.isTimeField() ? time.range(field) : date.range(field));
 581         }
 582         return field.doRange(this);
 583     }
 584 
 585     /**
 586      * Gets the value of the specified field from this date-time as an {@code int}.
 587      * <p>
 588      * This queries this date-time for the value for the specified field.
 589      * The returned value will always be within the valid range of values for the field.
 590      * If it is not possible to return the value, because the field is not supported
 591      * or for some other reason, an exception is thrown.
 592      * <p>
 593      * If the field is a {@link ChronoField} then the query is implemented here.
 594      * The {@link #isSupported(TemporalField) supported fields} will return valid
 595      * values based on this date-time, except {@code NANO_OF_DAY}, {@code MICRO_OF_DAY},
 596      * {@code EPOCH_DAY} and {@code EPOCH_MONTH} which are too large to fit in
 597      * an {@code int} and throw a {@code DateTimeException}.
 598      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 599      * <p>
 600      * If the field is not a {@code ChronoField}, then the result of this method
 601      * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
 602      * passing {@code this} as the argument. Whether the value can be obtained,
 603      * and what the value represents, is determined by the field.
 604      *
 605      * @param field  the field to get, not null
 606      * @return the value for the field
 607      * @throws DateTimeException if a value for the field cannot be obtained
 608      * @throws ArithmeticException if numeric overflow occurs
 609      */
 610     @Override
 611     public int get(TemporalField field) {
 612         if (field instanceof ChronoField) {
 613             ChronoField f = (ChronoField) field;
 614             return (f.isTimeField() ? time.get(field) : date.get(field));
 615         }
 616         return ChronoLocalDateTime.super.get(field);
 617     }
 618 
 619     /**
 620      * Gets the value of the specified field from this date-time as a {@code long}.
 621      * <p>
 622      * This queries this date-time for the value for the specified field.
 623      * If it is not possible to return the value, because the field is not supported
 624      * or for some other reason, an exception is thrown.
 625      * <p>
 626      * If the field is a {@link ChronoField} then the query is implemented here.
 627      * The {@link #isSupported(TemporalField) supported fields} will return valid
 628      * values based on this date-time.
 629      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 630      * <p>
 631      * If the field is not a {@code ChronoField}, then the result of this method
 632      * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
 633      * passing {@code this} as the argument. Whether the value can be obtained,
 634      * and what the value represents, is determined by the field.
 635      *
 636      * @param field  the field to get, not null
 637      * @return the value for the field
 638      * @throws DateTimeException if a value for the field cannot be obtained
 639      * @throws ArithmeticException if numeric overflow occurs
 640      */
 641     @Override
 642     public long getLong(TemporalField field) {
 643         if (field instanceof ChronoField) {
 644             ChronoField f = (ChronoField) field;
 645             return (f.isTimeField() ? time.getLong(field) : date.getLong(field));
 646         }
 647         return field.doGet(this);
 648     }
 649 
 650     //-----------------------------------------------------------------------
 651     /**
 652      * Gets the {@code LocalDate} part of this date-time.
 653      * <p>
 654      * This returns a {@code LocalDate} with the same year, month and day
 655      * as this date-time.
 656      *
 657      * @return the date part of this date-time, not null
 658      */
 659     @Override
 660     public LocalDate getDate() {
 661         return date;
 662     }
 663 
 664     /**
 665      * Gets the year field.
 666      * <p>
 667      * This method returns the primitive {@code int} value for the year.
 668      * <p>
 669      * The year returned by this method is proleptic as per {@code get(YEAR)}.
 670      * To obtain the year-of-era, use {@code get(YEAR_OF_ERA}.
 671      *
 672      * @return the year, from MIN_YEAR to MAX_YEAR
 673      */
 674     public int getYear() {
 675         return date.getYear();
 676     }
 677 
 678     /**
 679      * Gets the month-of-year field from 1 to 12.
 680      * <p>
 681      * This method returns the month as an {@code int} from 1 to 12.
 682      * Application code is frequently clearer if the enum {@link Month}
 683      * is used by calling {@link #getMonth()}.
 684      *
 685      * @return the month-of-year, from 1 to 12
 686      * @see #getMonth()
 687      */
 688     public int getMonthValue() {
 689         return date.getMonthValue();
 690     }


 736      * <p>
 737      * Additional information can be obtained from the {@code DayOfWeek}.
 738      * This includes textual names of the values.
 739      *
 740      * @return the day-of-week, not null
 741      */
 742     public DayOfWeek getDayOfWeek() {
 743         return date.getDayOfWeek();
 744     }
 745 
 746     //-----------------------------------------------------------------------
 747     /**
 748      * Gets the {@code LocalTime} part of this date-time.
 749      * <p>
 750      * This returns a {@code LocalTime} with the same hour, minute, second and
 751      * nanosecond as this date-time.
 752      *
 753      * @return the time part of this date-time, not null
 754      */
 755     @Override
 756     public LocalTime getTime() {
 757         return time;
 758     }
 759 
 760     /**
 761      * Gets the hour-of-day field.
 762      *
 763      * @return the hour-of-day, from 0 to 23
 764      */
 765     public int getHour() {
 766         return time.getHour();
 767     }
 768 
 769     /**
 770      * Gets the minute-of-hour field.
 771      *
 772      * @return the minute-of-hour, from 0 to 59
 773      */
 774     public int getMinute() {
 775         return time.getMinute();
 776     }


 780      *
 781      * @return the second-of-minute, from 0 to 59
 782      */
 783     public int getSecond() {
 784         return time.getSecond();
 785     }
 786 
 787     /**
 788      * Gets the nano-of-second field.
 789      *
 790      * @return the nano-of-second, from 0 to 999,999,999
 791      */
 792     public int getNano() {
 793         return time.getNano();
 794     }
 795 
 796     //-----------------------------------------------------------------------
 797     /**
 798      * Returns an adjusted copy of this date-time.
 799      * <p>
 800      * This returns a new {@code LocalDateTime}, based on this one, with the date-time adjusted.
 801      * The adjustment takes place using the specified adjuster strategy object.
 802      * Read the documentation of the adjuster to understand what adjustment will be made.
 803      * <p>
 804      * A simple adjuster might simply set the one of the fields, such as the year field.
 805      * A more complex adjuster might set the date to the last day of the month.
 806      * A selection of common adjustments is provided in {@link java.time.temporal.Adjusters}.
 807      * These include finding the "last day of the month" and "next Wednesday".
 808      * Key date-time classes also implement the {@code TemporalAdjuster} interface,
 809      * such as {@link Month} and {@link java.time.temporal.MonthDay MonthDay}.
 810      * The adjuster is responsible for handling special cases, such as the varying
 811      * lengths of month and leap years.
 812      * <p>
 813      * For example this code returns a date on the last day of July:
 814      * <pre>
 815      *  import static java.time.Month.*;
 816      *  import static java.time.temporal.Adjusters.*;
 817      *
 818      *  result = localDateTime.with(JULY).with(lastDayOfMonth());
 819      * </pre>
 820      * <p>
 821      * The classes {@link LocalDate} and {@link LocalTime} implement {@code TemporalAdjuster},
 822      * thus this method can be used to change the date, time or offset:
 823      * <pre>
 824      *  result = localDateTime.with(date);
 825      *  result = localDateTime.with(time);
 826      * </pre>
 827      * <p>
 828      * The result of this method is obtained by invoking the
 829      * {@link TemporalAdjuster#adjustInto(Temporal)} method on the


 835      * @return a {@code LocalDateTime} based on {@code this} with the adjustment made, not null
 836      * @throws DateTimeException if the adjustment cannot be made
 837      * @throws ArithmeticException if numeric overflow occurs
 838      */
 839     @Override
 840     public LocalDateTime with(TemporalAdjuster adjuster) {
 841         // optimizations
 842         if (adjuster instanceof LocalDate) {
 843             return with((LocalDate) adjuster, time);
 844         } else if (adjuster instanceof LocalTime) {
 845             return with(date, (LocalTime) adjuster);
 846         } else if (adjuster instanceof LocalDateTime) {
 847             return (LocalDateTime) adjuster;
 848         }
 849         return (LocalDateTime) adjuster.adjustInto(this);
 850     }
 851 
 852     /**
 853      * Returns a copy of this date-time with the specified field set to a new value.
 854      * <p>
 855      * This returns a new {@code LocalDateTime}, based on this one, with the value
 856      * for the specified field changed.
 857      * This can be used to change any supported field, such as the year, month or day-of-month.
 858      * If it is not possible to set the value, because the field is not supported or for
 859      * some other reason, an exception is thrown.
 860      * <p>
 861      * In some cases, changing the specified field can cause the resulting date-time to become invalid,
 862      * such as changing the month from 31st January to February would make the day-of-month invalid.
 863      * In cases like this, the field is responsible for resolving the date. Typically it will choose
 864      * the previous valid date, which would be the last valid day of February in this example.
 865      * <p>
 866      * If the field is a {@link ChronoField} then the adjustment is implemented here.
 867      * The {@link #isSupported(TemporalField) supported fields} will behave as per
 868      * the matching method on {@link LocalDate#with(TemporalField, long) LocalDate}
 869      * or {@link LocalTime#with(TemporalField, long) LocalTime}.
 870      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 871      * <p>
 872      * If the field is not a {@code ChronoField}, then the result of this method
 873      * is obtained by invoking {@code TemporalField.doWith(Temporal, long)}
 874      * passing {@code this} as the argument. In this case, the field determines
 875      * whether and how to adjust the instant.
 876      * <p>
 877      * This instance is immutable and unaffected by this method call.
 878      *
 879      * @param field  the field to set in the result, not null
 880      * @param newValue  the new value of the field in the result
 881      * @return a {@code LocalDateTime} based on {@code this} with the specified field set, not null
 882      * @throws DateTimeException if the field cannot be set
 883      * @throws ArithmeticException if numeric overflow occurs
 884      */
 885     @Override
 886     public LocalDateTime with(TemporalField field, long newValue) {
 887         if (field instanceof ChronoField) {
 888             ChronoField f = (ChronoField) field;
 889             if (f.isTimeField()) {
 890                 return with(date, time.with(field, newValue));
 891             } else {
 892                 return with(date.with(field, newValue), time);
 893             }
 894         }
 895         return field.doWith(this, newValue);
 896     }
 897 
 898     //-----------------------------------------------------------------------
 899     /**
 900      * Returns a copy of this {@code LocalDateTime} with the year altered.
 901      * The time does not affect the calculation and will be the same in the result.
 902      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
 903      * <p>
 904      * This instance is immutable and unaffected by this method call.
 905      *
 906      * @param year  the year to set in the result, from MIN_YEAR to MAX_YEAR
 907      * @return a {@code LocalDateTime} based on this date-time with the requested year, not null
 908      * @throws DateTimeException if the year value is invalid
 909      */
 910     public LocalDateTime withYear(int year) {
 911         return with(date.withYear(year), time);
 912     }
 913 
 914     /**
 915      * Returns a copy of this {@code LocalDateTime} with the month-of-year altered.


 918      * <p>
 919      * This instance is immutable and unaffected by this method call.
 920      *
 921      * @param month  the month-of-year to set in the result, from 1 (January) to 12 (December)
 922      * @return a {@code LocalDateTime} based on this date-time with the requested month, not null
 923      * @throws DateTimeException if the month-of-year value is invalid
 924      */
 925     public LocalDateTime withMonth(int month) {
 926         return with(date.withMonth(month), time);
 927     }
 928 
 929     /**
 930      * Returns a copy of this {@code LocalDateTime} with the day-of-month altered.
 931      * If the resulting {@code LocalDateTime} is invalid, an exception is thrown.
 932      * The time does not affect the calculation and will be the same in the result.
 933      * <p>
 934      * This instance is immutable and unaffected by this method call.
 935      *
 936      * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
 937      * @return a {@code LocalDateTime} based on this date-time with the requested day, not null
 938      * @throws DateTimeException if the day-of-month value is invalid
 939      * @throws DateTimeException if the day-of-month is invalid for the month-year
 940      */
 941     public LocalDateTime withDayOfMonth(int dayOfMonth) {
 942         return with(date.withDayOfMonth(dayOfMonth), time);
 943     }
 944 
 945     /**
 946      * Returns a copy of this {@code LocalDateTime} with the day-of-year altered.
 947      * If the resulting {@code LocalDateTime} is invalid, an exception is thrown.
 948      * <p>
 949      * This instance is immutable and unaffected by this method call.
 950      *
 951      * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
 952      * @return a {@code LocalDateTime} based on this date with the requested day, not null
 953      * @throws DateTimeException if the day-of-year value is invalid
 954      * @throws DateTimeException if the day-of-year is invalid for the year
 955      */
 956     public LocalDateTime withDayOfYear(int dayOfYear) {
 957         return with(date.withDayOfYear(dayOfYear), time);
 958     }
 959 
 960     //-----------------------------------------------------------------------
 961     /**
 962      * Returns a copy of this {@code LocalDateTime} with the hour-of-day value altered.
 963      * <p>
 964      * This instance is immutable and unaffected by this method call.
 965      *
 966      * @param hour  the hour-of-day to set in the result, from 0 to 23
 967      * @return a {@code LocalDateTime} based on this date-time with the requested hour, not null
 968      * @throws DateTimeException if the hour value is invalid
 969      */
 970     public LocalDateTime withHour(int hour) {
 971         LocalTime newTime = time.withHour(hour);
 972         return with(date, newTime);
 973     }
 974 


1006      * This instance is immutable and unaffected by this method call.
1007      *
1008      * @param nanoOfSecond  the nano-of-second to set in the result, from 0 to 999,999,999
1009      * @return a {@code LocalDateTime} based on this date-time with the requested nanosecond, not null
1010      * @throws DateTimeException if the nano value is invalid
1011      */
1012     public LocalDateTime withNano(int nanoOfSecond) {
1013         LocalTime newTime = time.withNano(nanoOfSecond);
1014         return with(date, newTime);
1015     }
1016 
1017     //-----------------------------------------------------------------------
1018     /**
1019      * Returns a copy of this {@code LocalDateTime} with the time truncated.
1020      * <p>
1021      * Truncation returns a copy of the original date-time with fields
1022      * smaller than the specified unit set to zero.
1023      * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
1024      * will set the second-of-minute and nano-of-second field to zero.
1025      * <p>
1026      * Not all units are accepted. The {@link ChronoUnit#DAYS days} unit and time
1027      * units with an exact duration can be used, other units throw an exception.


1028      * <p>
1029      * This instance is immutable and unaffected by this method call.
1030      *
1031      * @param unit  the unit to truncate to, not null
1032      * @return a {@code LocalDateTime} based on this date-time with the time truncated, not null
1033      * @throws DateTimeException if unable to truncate
1034      */
1035     public LocalDateTime truncatedTo(TemporalUnit unit) {
1036         return with(date, time.truncatedTo(unit));
1037     }
1038 
1039     //-----------------------------------------------------------------------
1040     /**
1041      * Returns a copy of this date-time with the specified period added.
1042      * <p>
1043      * This method returns a new date-time based on this time with the specified period added.
1044      * The adder is typically {@link Period} but may be any other type implementing
1045      * the {@link TemporalAdder} interface.
1046      * The calculation is delegated to the specified adjuster, which typically calls
1047      * back to {@link #plus(long, TemporalUnit)}.




1048      * <p>
1049      * This instance is immutable and unaffected by this method call.
1050      *
1051      * @param adder  the adder to use, not null
1052      * @return a {@code LocalDateTime} based on this date-time with the addition made, not null
1053      * @throws DateTimeException if the addition cannot be made
1054      * @throws ArithmeticException if numeric overflow occurs
1055      */
1056     @Override
1057     public LocalDateTime plus(TemporalAdder adder) {
1058         return (LocalDateTime) adder.addTo(this);
1059     }
1060 
1061     /**
1062      * Returns a copy of this date-time with the specified period added.




1063      * <p>
1064      * This method returns a new date-time based on this date-time with the specified period added.
1065      * This can be used to add any period that is defined by a unit, for example to add years, months or days.
1066      * The unit is responsible for the details of the calculation, including the resolution
1067      * of any edge cases in the calculation.





1068      * <p>
1069      * This instance is immutable and unaffected by this method call.
1070      *
1071      * @param amountToAdd  the amount of the unit to add to the result, may be negative
1072      * @param unit  the unit of the period to add, not null
1073      * @return a {@code LocalDateTime} based on this date-time with the specified period added, not null
1074      * @throws DateTimeException if the unit cannot be added to this type

1075      */
1076     @Override
1077     public LocalDateTime plus(long amountToAdd, TemporalUnit unit) {
1078         if (unit instanceof ChronoUnit) {
1079             ChronoUnit f = (ChronoUnit) unit;
1080             switch (f) {
1081                 case NANOS: return plusNanos(amountToAdd);
1082                 case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
1083                 case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
1084                 case SECONDS: return plusSeconds(amountToAdd);
1085                 case MINUTES: return plusMinutes(amountToAdd);
1086                 case HOURS: return plusHours(amountToAdd);
1087                 case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12);  // no overflow (256 is multiple of 2)
1088             }
1089             return with(date.plus(amountToAdd, unit), time);
1090         }
1091         return unit.doPlus(this, amountToAdd);
1092     }
1093 
1094     //-----------------------------------------------------------------------
1095     /**
1096      * Returns a copy of this {@code LocalDateTime} with the specified period in years added.
1097      * <p>
1098      * This method adds the specified amount to the years field in three steps:
1099      * <ol>
1100      * <li>Add the input years to the year field</li>
1101      * <li>Check if the resulting date would be invalid</li>
1102      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1103      * </ol>
1104      * <p>
1105      * For example, 2008-02-29 (leap year) plus one year would result in the
1106      * invalid date 2009-02-29 (standard year). Instead of returning an invalid
1107      * result, the last valid day of the month, 2009-02-28, is selected instead.
1108      * <p>
1109      * This instance is immutable and unaffected by this method call.
1110      *
1111      * @param years  the years to add, may be negative


1220      */
1221     public LocalDateTime plusSeconds(long seconds) {
1222         return plusWithOverflow(date, 0, 0, seconds, 0, 1);
1223     }
1224 
1225     /**
1226      * Returns a copy of this {@code LocalDateTime} with the specified period in nanoseconds added.
1227      * <p>
1228      * This instance is immutable and unaffected by this method call.
1229      *
1230      * @param nanos  the nanos to add, may be negative
1231      * @return a {@code LocalDateTime} based on this date-time with the nanoseconds added, not null
1232      * @throws DateTimeException if the result exceeds the supported date range
1233      */
1234     public LocalDateTime plusNanos(long nanos) {
1235         return plusWithOverflow(date, 0, 0, 0, nanos, 1);
1236     }
1237 
1238     //-----------------------------------------------------------------------
1239     /**
1240      * Returns a copy of this date-time with the specified period subtracted.




1241      * <p>
1242      * This method returns a new date-time based on this time with the specified period subtracted.
1243      * The subtractor is typically {@link Period} but may be any other type implementing
1244      * the {@link TemporalSubtractor} interface.
1245      * The calculation is delegated to the specified adjuster, which typically calls
1246      * back to {@link #minus(long, TemporalUnit)}.
1247      * <p>
1248      * This instance is immutable and unaffected by this method call.
1249      *
1250      * @param subtractor  the subtractor to use, not null
1251      * @return a {@code LocalDateTime} based on this date-time with the subtraction made, not null
1252      * @throws DateTimeException if the subtraction cannot be made
1253      * @throws ArithmeticException if numeric overflow occurs
1254      */
1255     @Override
1256     public LocalDateTime minus(TemporalSubtractor subtractor) {
1257         return (LocalDateTime) subtractor.subtractFrom(this);
1258     }
1259 
1260     /**
1261      * Returns a copy of this date-time with the specified period subtracted.
1262      * <p>
1263      * This method returns a new date-time based on this date-time with the specified period subtracted.
1264      * This can be used to subtract any period that is defined by a unit, for example to subtract years, months or days.
1265      * The unit is responsible for the details of the calculation, including the resolution
1266      * of any edge cases in the calculation.


1267      * <p>
1268      * This instance is immutable and unaffected by this method call.
1269      *
1270      * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
1271      * @param unit  the unit of the period to subtract, not null
1272      * @return a {@code LocalDateTime} based on this date-time with the specified period subtracted, not null
1273      * @throws DateTimeException if the unit cannot be added to this type

1274      */
1275     @Override
1276     public LocalDateTime minus(long amountToSubtract, TemporalUnit unit) {
1277         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
1278     }
1279 
1280     //-----------------------------------------------------------------------
1281     /**
1282      * Returns a copy of this {@code LocalDateTime} with the specified period in years subtracted.
1283      * <p>
1284      * This method subtracts the specified amount from the years field in three steps:
1285      * <ol>
1286      * <li>Subtract the input years from the year field</li>
1287      * <li>Check if the resulting date would be invalid</li>
1288      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1289      * </ol>
1290      * <p>
1291      * For example, 2008-02-29 (leap year) minus one year would result in the
1292      * invalid date 2009-02-29 (standard year). Instead of returning an invalid
1293      * result, the last valid day of the month, 2009-02-28, is selected instead.


1455 
1456     //-----------------------------------------------------------------------
1457     /**
1458      * Queries this date-time using the specified query.
1459      * <p>
1460      * This queries this date-time using the specified query strategy object.
1461      * The {@code TemporalQuery} object defines the logic to be used to
1462      * obtain the result. Read the documentation of the query to understand
1463      * what the result of this method will be.
1464      * <p>
1465      * The result of this method is obtained by invoking the
1466      * {@link java.time.temporal.TemporalQuery#queryFrom(TemporalAccessor)} method on the
1467      * specified query passing {@code this} as the argument.
1468      *
1469      * @param <R> the type of the result
1470      * @param query  the query to invoke, not null
1471      * @return the query result, null may be returned (defined by the query)
1472      * @throws DateTimeException if unable to query (defined by the query)
1473      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
1474      */

1475     @Override  // override for Javadoc
1476     public <R> R query(TemporalQuery<R> query) {



1477         return ChronoLocalDateTime.super.query(query);
1478     }
1479 
1480     /**
1481      * Adjusts the specified temporal object to have the same date and time as this object.
1482      * <p>
1483      * This returns a temporal object of the same observable type as the input
1484      * with the date and time changed to be the same as this.
1485      * <p>
1486      * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
1487      * twice, passing {@link ChronoField#EPOCH_DAY} and
1488      * {@link ChronoField#NANO_OF_DAY} as the fields.
1489      * <p>
1490      * In most cases, it is clearer to reverse the calling pattern by using
1491      * {@link Temporal#with(TemporalAdjuster)}:
1492      * <pre>
1493      *   // these two lines are equivalent, but the second approach is recommended
1494      *   temporal = thisLocalDateTime.adjustInto(temporal);
1495      *   temporal = temporal.with(thisLocalDateTime);
1496      * </pre>


1506     public Temporal adjustInto(Temporal temporal) {
1507         return ChronoLocalDateTime.super.adjustInto(temporal);
1508     }
1509 
1510     /**
1511      * Calculates the period between this date-time and another date-time in
1512      * terms of the specified unit.
1513      * <p>
1514      * This calculates the period between two date-times in terms of a single unit.
1515      * The start and end points are {@code this} and the specified date-time.
1516      * The result will be negative if the end is before the start.
1517      * The {@code Temporal} passed to this method must be a {@code LocalDateTime}.
1518      * For example, the period in days between two date-times can be calculated
1519      * using {@code startDateTime.periodUntil(endDateTime, DAYS)}.
1520      * <p>
1521      * The calculation returns a whole number, representing the number of
1522      * complete units between the two date-times.
1523      * For example, the period in months between 2012-06-15T00:00 and 2012-08-14T23:59
1524      * will only be one month as it is one minute short of two months.
1525      * <p>
1526      * This method operates in association with {@link TemporalUnit#between}.
1527      * The result of this method is a {@code long} representing the amount of
1528      * the specified unit. By contrast, the result of {@code between} is an
1529      * object that can be used directly in addition/subtraction:
1530      * <pre>
1531      *   long period = start.periodUntil(end, MONTHS);   // this method
1532      *   dateTime.plus(MONTHS.between(start, end));      // use in plus/minus

1533      * </pre>

1534      * <p>
1535      * The calculation is implemented in this method for {@link ChronoUnit}.
1536      * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
1537      * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS},
1538      * {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES},
1539      * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported.
1540      * Other {@code ChronoUnit} values will throw an exception.
1541      * <p>
1542      * If the unit is not a {@code ChronoUnit}, then the result of this method
1543      * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
1544      * passing {@code this} as the first argument and the input temporal as
1545      * the second argument.
1546      * <p>
1547      * This instance is immutable and unaffected by this method call.
1548      *
1549      * @param endDateTime  the end date-time, which must be a {@code LocalDateTime}, not null
1550      * @param unit  the unit to measure the period in, not null
1551      * @return the amount of the period between this date-time and the end date-time
1552      * @throws DateTimeException if the period cannot be calculated
1553      * @throws ArithmeticException if numeric overflow occurs


1563             ChronoUnit f = (ChronoUnit) unit;
1564             if (f.isTimeUnit()) {
1565                 long amount = date.daysUntil(end.date);
1566                 switch (f) {
1567                     case NANOS: amount = Math.multiplyExact(amount, NANOS_PER_DAY); break;
1568                     case MICROS: amount = Math.multiplyExact(amount, MICROS_PER_DAY); break;
1569                     case MILLIS: amount = Math.multiplyExact(amount, MILLIS_PER_DAY); break;
1570                     case SECONDS: amount = Math.multiplyExact(amount, SECONDS_PER_DAY); break;
1571                     case MINUTES: amount = Math.multiplyExact(amount, MINUTES_PER_DAY); break;
1572                     case HOURS: amount = Math.multiplyExact(amount, HOURS_PER_DAY); break;
1573                     case HALF_DAYS: amount = Math.multiplyExact(amount, 2); break;
1574                 }
1575                 return Math.addExact(amount, time.periodUntil(end.time, unit));
1576             }
1577             LocalDate endDate = end.date;
1578             if (end.time.isBefore(time)) {
1579                 endDate = endDate.minusDays(1);
1580             }
1581             return date.periodUntil(endDate, unit);
1582         }
1583         return unit.between(this, endDateTime).getAmount();
1584     }
1585 
1586     //-----------------------------------------------------------------------
1587     /**
1588      * Returns an offset date-time formed from this date-time and the specified offset.
1589      * <p>
1590      * This combines this date-time with the specified offset to form an {@code OffsetDateTime}.
1591      * All possible combinations of date-time and offset are valid.
1592      * <p>
1593      * This instance is immutable and unaffected by this method call.
1594      *
1595      * @param offset  the offset to combine with, not null
1596      * @return the offset date-time formed from this date-time and the specified offset, not null
1597      */
1598     public OffsetDateTime atOffset(ZoneOffset offset) {
1599         return OffsetDateTime.of(this, offset);
1600     }
1601 
1602     /**
1603      * Returns a zoned date-time formed from this date-time and the specified time-zone.
1604      * <p>
1605      * This creates a zoned date-time matching the input date-time as closely as possible.

1606      * Time-zone rules, such as daylight savings, mean that not every local date-time
1607      * is valid for the specified zone, thus the local date-time may be adjusted.
1608      * <p>
1609      * The local date-time is resolved to a single instant on the time-line.
1610      * This is achieved by finding a valid offset from UTC/Greenwich for the local
1611      * date-time as defined by the {@link ZoneRules rules} of the zone ID.
1612      *<p>
1613      * In most cases, there is only one valid offset for a local date-time.
1614      * In the case of an overlap, where clocks are set back, there are two valid offsets.
1615      * This method uses the earlier offset typically corresponding to "summer".
1616      * <p>
1617      * In the case of a gap, where clocks jump forward, there is no valid offset.
1618      * Instead, the local date-time is adjusted to be later by the length of the gap.
1619      * For a typical one hour daylight savings change, the local date-time will be
1620      * moved one hour later into the offset typically corresponding to "summer".
1621      * <p>
1622      * To obtain the later offset during an overlap, call
1623      * {@link ZonedDateTime#withLaterOffsetAtOverlap()} on the result of this method.
1624      * To throw an exception when there is a gap or overlap, use
1625      * {@link ZonedDateTime#ofStrict(LocalDateTime, ZoneOffset, ZoneId)}.
1626      * <p>
1627      * This instance is immutable and unaffected by this method call.
1628      *
1629      * @param zone  the time-zone to use, not null
1630      * @return the zoned date-time formed from this date-time, not null
1631      */
1632     @Override
1633     public ZonedDateTime atZone(ZoneId zone) {
1634         return ZonedDateTime.of(this, zone);
1635     }
1636 
1637     //-----------------------------------------------------------------------
1638     /**
1639      * Compares this date-time to another date-time.
1640      * <p>
1641      * The comparison is primarily based on the date-time, from earliest to latest.
1642      * It is "consistent with equals", as defined by {@link Comparable}.
1643      * <p>
1644      * If all the date-times being compared are instances of {@code LocalDateTime},
1645      * then the comparison will be entirely based on the date-time.
1646      * If some dates being compared are in different chronologies, then the
1647      * chronology is also considered, see {@link ChronoLocalDateTime#compareTo}.
1648      *
1649      * @param other  the other date-time to compare to, not null
1650      * @return the comparator value, negative if less, positive if greater
1651      */
1652     @Override  // override for Javadoc and performance
1653     public int compareTo(ChronoLocalDateTime<?> other) {
1654         if (other instanceof LocalDateTime) {
1655             return compareTo0((LocalDateTime) other);
1656         }
1657         return ChronoLocalDateTime.super.compareTo(other);
1658     }
1659 
1660     private int compareTo0(LocalDateTime other) {
1661         int cmp = date.compareTo0(other.getDate());
1662         if (cmp == 0) {
1663             cmp = time.compareTo(other.getTime());
1664         }
1665         return cmp;
1666     }
1667 
1668     /**
1669      * Checks if this date-time is after the specified date-time.
1670      * <p>
1671      * This checks to see if this date-time represents a point on the
1672      * local time-line after the other date-time.
1673      * <pre>
1674      *   LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
1675      *   LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
1676      *   a.isAfter(b) == false
1677      *   a.isAfter(a) == false
1678      *   b.isAfter(a) == true
1679      * </pre>
1680      * <p>
1681      * This method only considers the position of the two date-times on the local time-line.
1682      * It does not take into account the chronology, or calendar system.
1683      * This is different from the comparison in {@link #compareTo(ChronoLocalDateTime)},


1793      * <li>{@code yyyy-MM-dd'T'HH:mm}</li>
1794      * <li>{@code yyyy-MM-dd'T'HH:mm:ss}</li>
1795      * <li>{@code yyyy-MM-dd'T'HH:mm:ss.SSS}</li>
1796      * <li>{@code yyyy-MM-dd'T'HH:mm:ss.SSSSSS}</li>
1797      * <li>{@code yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS}</li>
1798      * </ul><p>
1799      * The format used will be the shortest that outputs the full value of
1800      * the time where the omitted parts are implied to be zero.
1801      *
1802      * @return a string representation of this date-time, not null
1803      */
1804     @Override
1805     public String toString() {
1806         return date.toString() + 'T' + time.toString();
1807     }
1808 
1809     /**
1810      * Outputs this date-time as a {@code String} using the formatter.
1811      * <p>
1812      * This date-time will be passed to the formatter
1813      * {@link DateTimeFormatter#print(TemporalAccessor) print method}.
1814      *
1815      * @param formatter  the formatter to use, not null
1816      * @return the formatted date-time string, not null
1817      * @throws DateTimeException if an error occurs during printing
1818      */
1819     @Override  // override for Javadoc
1820     public String toString(DateTimeFormatter formatter) {
1821         return ChronoLocalDateTime.super.toString(formatter);
1822     }
1823 
1824     //-----------------------------------------------------------------------
1825     /**
1826      * Writes the object using a
1827      * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1828      * <pre>
1829      *  out.writeByte(5);  // identifies this as a LocalDateTime
1830      *  // the <a href="../../serialized-form.html#java.time.LocalDate">date</a> excluding the one byte header
1831      *  // the <a href="../../serialized-form.html#java.time.LocalTime">time</a> excluding the one byte header
1832      * </pre>
1833      *




  53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61  */
  62 package java.time;
  63 
  64 import static java.time.LocalTime.HOURS_PER_DAY;
  65 import static java.time.LocalTime.MICROS_PER_DAY;
  66 import static java.time.LocalTime.MILLIS_PER_DAY;
  67 import static java.time.LocalTime.MINUTES_PER_DAY;
  68 import static java.time.LocalTime.NANOS_PER_DAY;
  69 import static java.time.LocalTime.NANOS_PER_HOUR;
  70 import static java.time.LocalTime.NANOS_PER_MINUTE;
  71 import static java.time.LocalTime.NANOS_PER_SECOND;
  72 import static java.time.LocalTime.SECONDS_PER_DAY;
  73 import static java.time.temporal.ChronoField.NANO_OF_SECOND;
  74 
  75 import java.io.DataInput;
  76 import java.io.DataOutput;
  77 import java.io.IOException;
  78 import java.io.InvalidObjectException;
  79 import java.io.ObjectStreamException;
  80 import java.io.Serializable;
  81 import java.time.chrono.ChronoLocalDateTime;
  82 import java.time.chrono.IsoChronology;
  83 import java.time.format.DateTimeFormatter;

  84 import java.time.format.DateTimeParseException;
  85 import java.time.temporal.ChronoField;

  86 import java.time.temporal.ChronoUnit;
  87 import java.time.temporal.Queries;

  88 import java.time.temporal.Temporal;
  89 import java.time.temporal.TemporalAccessor;

  90 import java.time.temporal.TemporalAdjuster;
  91 import java.time.temporal.TemporalAmount;
  92 import java.time.temporal.TemporalField;
  93 import java.time.temporal.TemporalQuery;

  94 import java.time.temporal.TemporalUnit;
  95 import java.time.temporal.ValueRange;
  96 import java.time.zone.ZoneRules;
  97 import java.util.Objects;
  98 
  99 /**
 100  * A date-time without a time-zone in the ISO-8601 calendar system,
 101  * such as {@code 2007-12-03T10:15:30}.
 102  * <p>
 103  * {@code LocalDateTime} is an immutable date-time object that represents a date-time,
 104  * often viewed as year-month-day-hour-minute-second. Other date and time fields,
 105  * such as day-of-year, day-of-week and week-of-year, can also be accessed.
 106  * Time is represented to nanosecond precision.
 107  * For example, the value "2nd October 2007 at 13:45.30.123456789" can be
 108  * stored in a {@code LocalDateTime}.
 109  * <p>
 110  * This class does not store or represent a time-zone.
 111  * Instead, it is a description of the date, as used for birthdays, combined with
 112  * the local time as seen on a wall clock.
 113  * It cannot represent an instant on the time-line without additional information
 114  * such as an offset or time-zone.
 115  * <p>
 116  * The ISO-8601 calendar system is the modern civil calendar system used today
 117  * in most of the world. It is equivalent to the proleptic Gregorian calendar
 118  * system, in which today's rules for leap years are applied for all time.
 119  * For most applications written today, the ISO-8601 rules are entirely suitable.
 120  * However, any application that makes use of historical dates, and requires them
 121  * to be accurate will find the ISO-8601 approach unsuitable.
 122  *
 123  * <h3>Specification for implementors</h3>
 124  * This class is immutable and thread-safe.
 125  *
 126  * @since 1.8
 127  */
 128 public final class LocalDateTime
 129         implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable {
 130 
 131     /**
 132      * The minimum supported {@code LocalDateTime}, '-999999999-01-01T00:00:00'.
 133      * This is the local date-time of midnight at the start of the minimum date.
 134      * This combines {@link LocalDate#MIN} and {@link LocalTime#MIN}.
 135      * This could be used by an application as a "far past" date-time.
 136      */
 137     public static final LocalDateTime MIN = LocalDateTime.of(LocalDate.MIN, LocalTime.MIN);
 138     /**
 139      * The maximum supported {@code LocalDateTime}, '+999999999-12-31T23:59:59.999999999'.
 140      * This is the local date-time just before midnight at the end of the maximum date.
 141      * This combines {@link LocalDate#MAX} and {@link LocalTime#MAX}.
 142      * This could be used by an application as a "far future" date-time.
 143      */
 144     public static final LocalDateTime MAX = LocalDateTime.of(LocalDate.MAX, LocalTime.MAX);
 145 
 146     /**
 147      * Serialization version.
 148      */
 149     private static final long serialVersionUID = 6207766400415563566L;


 194      * <p>
 195      * This will query the specified clock to obtain the current date-time.
 196      * Using this method allows the use of an alternate clock for testing.
 197      * The alternate clock may be introduced using {@link Clock dependency injection}.
 198      *
 199      * @param clock  the clock to use, not null
 200      * @return the current date-time, not null
 201      */
 202     public static LocalDateTime now(Clock clock) {
 203         Objects.requireNonNull(clock, "clock");
 204         final Instant now = clock.instant();  // called once
 205         ZoneOffset offset = clock.getZone().getRules().getOffset(now);
 206         return ofEpochSecond(now.getEpochSecond(), now.getNano(), offset);
 207     }
 208 
 209     //-----------------------------------------------------------------------
 210     /**
 211      * Obtains an instance of {@code LocalDateTime} from year, month,
 212      * day, hour and minute, setting the second and nanosecond to zero.
 213      * <p>
 214      * This returns a {@code LocalDateTime} with the specified year, month,
 215      * day-of-month, hour and minute.
 216      * The day must be valid for the year and month, otherwise an exception will be thrown.
 217      * The second and nanosecond fields will be set to zero.
 218      *
 219      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 220      * @param month  the month-of-year to represent, not null
 221      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 222      * @param hour  the hour-of-day to represent, from 0 to 23
 223      * @param minute  the minute-of-hour to represent, from 0 to 59
 224      * @return the local date-time, not null
 225      * @throws DateTimeException if the value of any field is out of range,
 226      *  or if the day-of-month is invalid for the month-year
 227      */
 228     public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute) {
 229         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 230         LocalTime time = LocalTime.of(hour, minute);
 231         return new LocalDateTime(date, time);
 232     }
 233 
 234     /**
 235      * Obtains an instance of {@code LocalDateTime} from year, month,
 236      * day, hour, minute and second, setting the nanosecond to zero.
 237      * <p>
 238      * This returns a {@code LocalDateTime} with the specified year, month,
 239      * day-of-month, hour, minute and second.
 240      * The day must be valid for the year and month, otherwise an exception will be thrown.
 241      * The nanosecond field will be set to zero.
 242      *
 243      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 244      * @param month  the month-of-year to represent, not null
 245      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 246      * @param hour  the hour-of-day to represent, from 0 to 23
 247      * @param minute  the minute-of-hour to represent, from 0 to 59
 248      * @param second  the second-of-minute to represent, from 0 to 59
 249      * @return the local date-time, not null
 250      * @throws DateTimeException if the value of any field is out of range,
 251      *  or if the day-of-month is invalid for the month-year
 252      */
 253     public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) {
 254         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 255         LocalTime time = LocalTime.of(hour, minute, second);
 256         return new LocalDateTime(date, time);
 257     }
 258 
 259     /**
 260      * Obtains an instance of {@code LocalDateTime} from year, month,
 261      * day, hour, minute, second and nanosecond.
 262      * <p>
 263      * This returns a {@code LocalDateTime} with the specified year, month,
 264      * day-of-month, hour, minute, second and nanosecond.
 265      * The day must be valid for the year and month, otherwise an exception will be thrown.
 266      *
 267      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 268      * @param month  the month-of-year to represent, not null
 269      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 270      * @param hour  the hour-of-day to represent, from 0 to 23
 271      * @param minute  the minute-of-hour to represent, from 0 to 59
 272      * @param second  the second-of-minute to represent, from 0 to 59
 273      * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
 274      * @return the local date-time, not null
 275      * @throws DateTimeException if the value of any field is out of range,
 276      *  or if the day-of-month is invalid for the month-year
 277      */
 278     public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) {
 279         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 280         LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond);
 281         return new LocalDateTime(date, time);
 282     }
 283 
 284     //-----------------------------------------------------------------------
 285     /**
 286      * Obtains an instance of {@code LocalDateTime} from year, month,
 287      * day, hour and minute, setting the second and nanosecond to zero.
 288      * <p>
 289      * This returns a {@code LocalDateTime} with the specified year, month,
 290      * day-of-month, hour and minute.
 291      * The day must be valid for the year and month, otherwise an exception will be thrown.
 292      * The second and nanosecond fields will be set to zero.
 293      *
 294      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 295      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 296      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 297      * @param hour  the hour-of-day to represent, from 0 to 23
 298      * @param minute  the minute-of-hour to represent, from 0 to 59
 299      * @return the local date-time, not null
 300      * @throws DateTimeException if the value of any field is out of range,
 301      *  or if the day-of-month is invalid for the month-year
 302      */
 303     public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) {
 304         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 305         LocalTime time = LocalTime.of(hour, minute);
 306         return new LocalDateTime(date, time);
 307     }
 308 
 309     /**
 310      * Obtains an instance of {@code LocalDateTime} from year, month,
 311      * day, hour, minute and second, setting the nanosecond to zero.
 312      * <p>
 313      * This returns a {@code LocalDateTime} with the specified year, month,
 314      * day-of-month, hour, minute and second.
 315      * The day must be valid for the year and month, otherwise an exception will be thrown.
 316      * The nanosecond field will be set to zero.
 317      *
 318      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 319      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 320      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 321      * @param hour  the hour-of-day to represent, from 0 to 23
 322      * @param minute  the minute-of-hour to represent, from 0 to 59
 323      * @param second  the second-of-minute to represent, from 0 to 59
 324      * @return the local date-time, not null
 325      * @throws DateTimeException if the value of any field is out of range,
 326      *  or if the day-of-month is invalid for the month-year
 327      */
 328     public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) {
 329         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 330         LocalTime time = LocalTime.of(hour, minute, second);
 331         return new LocalDateTime(date, time);
 332     }
 333 
 334     /**
 335      * Obtains an instance of {@code LocalDateTime} from year, month,
 336      * day, hour, minute, second and nanosecond.
 337      * <p>
 338      * This returns a {@code LocalDateTime} with the specified year, month,
 339      * day-of-month, hour, minute, second and nanosecond.
 340      * The day must be valid for the year and month, otherwise an exception will be thrown.
 341      *
 342      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 343      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 344      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 345      * @param hour  the hour-of-day to represent, from 0 to 23
 346      * @param minute  the minute-of-hour to represent, from 0 to 59
 347      * @param second  the second-of-minute to represent, from 0 to 59
 348      * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
 349      * @return the local date-time, not null
 350      * @throws DateTimeException if the value of any field is out of range,
 351      *  or if the day-of-month is invalid for the month-year
 352      */
 353     public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) {
 354         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 355         LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond);
 356         return new LocalDateTime(date, time);
 357     }
 358 
 359     /**
 360      * Obtains an instance of {@code LocalDateTime} from a date and time.
 361      *
 362      * @param date  the local date, not null
 363      * @param time  the local time, not null
 364      * @return the local date-time, not null
 365      */
 366     public static LocalDateTime of(LocalDate date, LocalTime time) {
 367         Objects.requireNonNull(date, "date");
 368         Objects.requireNonNull(time, "time");
 369         return new LocalDateTime(date, time);
 370     }
 371 


 386     public static LocalDateTime ofInstant(Instant instant, ZoneId zone) {
 387         Objects.requireNonNull(instant, "instant");
 388         Objects.requireNonNull(zone, "zone");
 389         ZoneRules rules = zone.getRules();
 390         ZoneOffset offset = rules.getOffset(instant);
 391         return ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
 392     }
 393 
 394     /**
 395      * Obtains an instance of {@code LocalDateTime} using seconds from the
 396      * epoch of 1970-01-01T00:00:00Z.
 397      * <p>
 398      * This allows the {@link ChronoField#INSTANT_SECONDS epoch-second} field
 399      * to be converted to a local date-time. This is primarily intended for
 400      * low-level conversions rather than general application usage.
 401      *
 402      * @param epochSecond  the number of seconds from the epoch of 1970-01-01T00:00:00Z
 403      * @param nanoOfSecond  the nanosecond within the second, from 0 to 999,999,999
 404      * @param offset  the zone offset, not null
 405      * @return the local date-time, not null
 406      * @throws DateTimeException if the result exceeds the supported range,
 407      *  or if the nano-of-second is invalid
 408      */
 409     public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) {
 410         Objects.requireNonNull(offset, "offset");
 411         NANO_OF_SECOND.checkValidValue(nanoOfSecond);
 412         long localSecond = epochSecond + offset.getTotalSeconds();  // overflow caught later
 413         long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY);
 414         int secsOfDay = (int)Math.floorMod(localSecond, SECONDS_PER_DAY);
 415         LocalDate date = LocalDate.ofEpochDay(localEpochDay);
 416         LocalTime time = LocalTime.ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + nanoOfSecond);
 417         return new LocalDateTime(date, time);
 418     }
 419 
 420     //-----------------------------------------------------------------------
 421     /**
 422      * Obtains an instance of {@code LocalDateTime} from a temporal object.
 423      * <p>
 424      * This obtains an offset time based on the specified temporal.
 425      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 426      * which this factory converts to an instance of {@code LocalDateTime}.
 427      * <p>
 428      * The conversion extracts and combines the {@code LocalDate} and the
 429      * {@code LocalTime} from the temporal object.
 430      * Implementations are permitted to perform optimizations such as accessing
 431      * those fields that are equivalent to the relevant objects.
 432      * <p>
 433      * This method matches the signature of the functional interface {@link TemporalQuery}
 434      * allowing it to be used as a query via method reference, {@code LocalDateTime::from}.
 435      *
 436      * @param temporal  the temporal object to convert, not null
 437      * @return the local date-time, not null
 438      * @throws DateTimeException if unable to convert to a {@code LocalDateTime}
 439      */
 440     public static LocalDateTime from(TemporalAccessor temporal) {
 441         if (temporal instanceof LocalDateTime) {
 442             return (LocalDateTime) temporal;
 443         } else if (temporal instanceof ZonedDateTime) {
 444             return ((ZonedDateTime) temporal).toLocalDateTime();
 445         } else if (temporal instanceof OffsetDateTime) {
 446             return ((OffsetDateTime) temporal).toLocalDateTime();
 447         }
 448         try {
 449             LocalDate date = LocalDate.from(temporal);
 450             LocalTime time = LocalTime.from(temporal);
 451             return new LocalDateTime(date, time);
 452         } catch (DateTimeException ex) {
 453             throw new DateTimeException("Unable to obtain LocalDateTime from TemporalAccessor: " + temporal.getClass(), ex);
 454         }
 455     }
 456 
 457     //-----------------------------------------------------------------------
 458     /**
 459      * Obtains an instance of {@code LocalDateTime} from a text string such as {@code 2007-12-03T10:15:30}.
 460      * <p>
 461      * The string must represent a valid date-time and is parsed using
 462      * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME}.
 463      *
 464      * @param text  the text to parse such as "2007-12-03T10:15:30", not null
 465      * @return the parsed local date-time, not null
 466      * @throws DateTimeParseException if the text cannot be parsed
 467      */
 468     public static LocalDateTime parse(CharSequence text) {
 469         return parse(text, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
 470     }
 471 
 472     /**
 473      * Obtains an instance of {@code LocalDateTime} from a text string using a specific formatter.
 474      * <p>
 475      * The text is parsed using the formatter, returning a date-time.
 476      *
 477      * @param text  the text to parse, not null
 478      * @param formatter  the formatter to use, not null
 479      * @return the parsed local date-time, not null
 480      * @throws DateTimeParseException if the text cannot be parsed
 481      */
 482     public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) {
 483         Objects.requireNonNull(formatter, "formatter");
 484         return formatter.parse(text, LocalDateTime::from);
 485     }
 486 
 487     //-----------------------------------------------------------------------
 488     /**
 489      * Constructor.


 537      * <li>{@code HOUR_OF_DAY}
 538      * <li>{@code CLOCK_HOUR_OF_DAY}
 539      * <li>{@code AMPM_OF_DAY}
 540      * <li>{@code DAY_OF_WEEK}
 541      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
 542      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
 543      * <li>{@code DAY_OF_MONTH}
 544      * <li>{@code DAY_OF_YEAR}
 545      * <li>{@code EPOCH_DAY}
 546      * <li>{@code ALIGNED_WEEK_OF_MONTH}
 547      * <li>{@code ALIGNED_WEEK_OF_YEAR}
 548      * <li>{@code MONTH_OF_YEAR}
 549      * <li>{@code EPOCH_MONTH}
 550      * <li>{@code YEAR_OF_ERA}
 551      * <li>{@code YEAR}
 552      * <li>{@code ERA}
 553      * </ul>
 554      * All other {@code ChronoField} instances will return false.
 555      * <p>
 556      * If the field is not a {@code ChronoField}, then the result of this method
 557      * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
 558      * passing {@code this} as the argument.
 559      * Whether the field is supported is determined by the field.
 560      *
 561      * @param field  the field to check, null returns false
 562      * @return true if the field is supported on this date-time, false if not
 563      */
 564     @Override
 565     public boolean isSupported(TemporalField field) {
 566         if (field instanceof ChronoField) {
 567             ChronoField f = (ChronoField) field;
 568             return f.isDateField() || f.isTimeField();
 569         }
 570         return field != null && field.isSupportedBy(this);
 571     }
 572 
 573     /**
 574      * Gets the range of valid values for the specified field.
 575      * <p>
 576      * The range object expresses the minimum and maximum valid values for a field.
 577      * This date-time is used to enhance the accuracy of the returned range.
 578      * If it is not possible to return the range, because the field is not supported
 579      * or for some other reason, an exception is thrown.
 580      * <p>
 581      * If the field is a {@link ChronoField} then the query is implemented here.
 582      * The {@link #isSupported(TemporalField) supported fields} will return
 583      * appropriate range instances.
 584      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 585      * <p>
 586      * If the field is not a {@code ChronoField}, then the result of this method
 587      * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
 588      * passing {@code this} as the argument.
 589      * Whether the range can be obtained is determined by the field.
 590      *
 591      * @param field  the field to query the range for, not null
 592      * @return the range of valid values for the field, not null
 593      * @throws DateTimeException if the range for the field cannot be obtained
 594      */
 595     @Override
 596     public ValueRange range(TemporalField field) {
 597         if (field instanceof ChronoField) {
 598             ChronoField f = (ChronoField) field;
 599             return (f.isTimeField() ? time.range(field) : date.range(field));
 600         }
 601         return field.rangeRefinedBy(this);
 602     }
 603 
 604     /**
 605      * Gets the value of the specified field from this date-time as an {@code int}.
 606      * <p>
 607      * This queries this date-time for the value for the specified field.
 608      * The returned value will always be within the valid range of values for the field.
 609      * If it is not possible to return the value, because the field is not supported
 610      * or for some other reason, an exception is thrown.
 611      * <p>
 612      * If the field is a {@link ChronoField} then the query is implemented here.
 613      * The {@link #isSupported(TemporalField) supported fields} will return valid
 614      * values based on this date-time, except {@code NANO_OF_DAY}, {@code MICRO_OF_DAY},
 615      * {@code EPOCH_DAY} and {@code EPOCH_MONTH} which are too large to fit in
 616      * an {@code int} and throw a {@code DateTimeException}.
 617      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 618      * <p>
 619      * If the field is not a {@code ChronoField}, then the result of this method
 620      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 621      * passing {@code this} as the argument. Whether the value can be obtained,
 622      * and what the value represents, is determined by the field.
 623      *
 624      * @param field  the field to get, not null
 625      * @return the value for the field
 626      * @throws DateTimeException if a value for the field cannot be obtained
 627      * @throws ArithmeticException if numeric overflow occurs
 628      */
 629     @Override
 630     public int get(TemporalField field) {
 631         if (field instanceof ChronoField) {
 632             ChronoField f = (ChronoField) field;
 633             return (f.isTimeField() ? time.get(field) : date.get(field));
 634         }
 635         return ChronoLocalDateTime.super.get(field);
 636     }
 637 
 638     /**
 639      * Gets the value of the specified field from this date-time as a {@code long}.
 640      * <p>
 641      * This queries this date-time for the value for the specified field.
 642      * If it is not possible to return the value, because the field is not supported
 643      * or for some other reason, an exception is thrown.
 644      * <p>
 645      * If the field is a {@link ChronoField} then the query is implemented here.
 646      * The {@link #isSupported(TemporalField) supported fields} will return valid
 647      * values based on this date-time.
 648      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 649      * <p>
 650      * If the field is not a {@code ChronoField}, then the result of this method
 651      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 652      * passing {@code this} as the argument. Whether the value can be obtained,
 653      * and what the value represents, is determined by the field.
 654      *
 655      * @param field  the field to get, not null
 656      * @return the value for the field
 657      * @throws DateTimeException if a value for the field cannot be obtained
 658      * @throws ArithmeticException if numeric overflow occurs
 659      */
 660     @Override
 661     public long getLong(TemporalField field) {
 662         if (field instanceof ChronoField) {
 663             ChronoField f = (ChronoField) field;
 664             return (f.isTimeField() ? time.getLong(field) : date.getLong(field));
 665         }
 666         return field.getFrom(this);
 667     }
 668 
 669     //-----------------------------------------------------------------------
 670     /**
 671      * Gets the {@code LocalDate} part of this date-time.
 672      * <p>
 673      * This returns a {@code LocalDate} with the same year, month and day
 674      * as this date-time.
 675      *
 676      * @return the date part of this date-time, not null
 677      */
 678     @Override
 679     public LocalDate toLocalDate() {
 680         return date;
 681     }
 682 
 683     /**
 684      * Gets the year field.
 685      * <p>
 686      * This method returns the primitive {@code int} value for the year.
 687      * <p>
 688      * The year returned by this method is proleptic as per {@code get(YEAR)}.
 689      * To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}.
 690      *
 691      * @return the year, from MIN_YEAR to MAX_YEAR
 692      */
 693     public int getYear() {
 694         return date.getYear();
 695     }
 696 
 697     /**
 698      * Gets the month-of-year field from 1 to 12.
 699      * <p>
 700      * This method returns the month as an {@code int} from 1 to 12.
 701      * Application code is frequently clearer if the enum {@link Month}
 702      * is used by calling {@link #getMonth()}.
 703      *
 704      * @return the month-of-year, from 1 to 12
 705      * @see #getMonth()
 706      */
 707     public int getMonthValue() {
 708         return date.getMonthValue();
 709     }


 755      * <p>
 756      * Additional information can be obtained from the {@code DayOfWeek}.
 757      * This includes textual names of the values.
 758      *
 759      * @return the day-of-week, not null
 760      */
 761     public DayOfWeek getDayOfWeek() {
 762         return date.getDayOfWeek();
 763     }
 764 
 765     //-----------------------------------------------------------------------
 766     /**
 767      * Gets the {@code LocalTime} part of this date-time.
 768      * <p>
 769      * This returns a {@code LocalTime} with the same hour, minute, second and
 770      * nanosecond as this date-time.
 771      *
 772      * @return the time part of this date-time, not null
 773      */
 774     @Override
 775     public LocalTime toLocalTime() {
 776         return time;
 777     }
 778 
 779     /**
 780      * Gets the hour-of-day field.
 781      *
 782      * @return the hour-of-day, from 0 to 23
 783      */
 784     public int getHour() {
 785         return time.getHour();
 786     }
 787 
 788     /**
 789      * Gets the minute-of-hour field.
 790      *
 791      * @return the minute-of-hour, from 0 to 59
 792      */
 793     public int getMinute() {
 794         return time.getMinute();
 795     }


 799      *
 800      * @return the second-of-minute, from 0 to 59
 801      */
 802     public int getSecond() {
 803         return time.getSecond();
 804     }
 805 
 806     /**
 807      * Gets the nano-of-second field.
 808      *
 809      * @return the nano-of-second, from 0 to 999,999,999
 810      */
 811     public int getNano() {
 812         return time.getNano();
 813     }
 814 
 815     //-----------------------------------------------------------------------
 816     /**
 817      * Returns an adjusted copy of this date-time.
 818      * <p>
 819      * This returns a {@code LocalDateTime}, based on this one, with the date-time adjusted.
 820      * The adjustment takes place using the specified adjuster strategy object.
 821      * Read the documentation of the adjuster to understand what adjustment will be made.
 822      * <p>
 823      * A simple adjuster might simply set the one of the fields, such as the year field.
 824      * A more complex adjuster might set the date to the last day of the month.
 825      * A selection of common adjustments is provided in {@link java.time.temporal.Adjusters}.
 826      * These include finding the "last day of the month" and "next Wednesday".
 827      * Key date-time classes also implement the {@code TemporalAdjuster} interface,
 828      * such as {@link Month} and {@link java.time.MonthDay MonthDay}.
 829      * The adjuster is responsible for handling special cases, such as the varying
 830      * lengths of month and leap years.
 831      * <p>
 832      * For example this code returns a date on the last day of July:
 833      * <pre>
 834      *  import static java.time.Month.*;
 835      *  import static java.time.temporal.Adjusters.*;
 836      *
 837      *  result = localDateTime.with(JULY).with(lastDayOfMonth());
 838      * </pre>
 839      * <p>
 840      * The classes {@link LocalDate} and {@link LocalTime} implement {@code TemporalAdjuster},
 841      * thus this method can be used to change the date, time or offset:
 842      * <pre>
 843      *  result = localDateTime.with(date);
 844      *  result = localDateTime.with(time);
 845      * </pre>
 846      * <p>
 847      * The result of this method is obtained by invoking the
 848      * {@link TemporalAdjuster#adjustInto(Temporal)} method on the


 854      * @return a {@code LocalDateTime} based on {@code this} with the adjustment made, not null
 855      * @throws DateTimeException if the adjustment cannot be made
 856      * @throws ArithmeticException if numeric overflow occurs
 857      */
 858     @Override
 859     public LocalDateTime with(TemporalAdjuster adjuster) {
 860         // optimizations
 861         if (adjuster instanceof LocalDate) {
 862             return with((LocalDate) adjuster, time);
 863         } else if (adjuster instanceof LocalTime) {
 864             return with(date, (LocalTime) adjuster);
 865         } else if (adjuster instanceof LocalDateTime) {
 866             return (LocalDateTime) adjuster;
 867         }
 868         return (LocalDateTime) adjuster.adjustInto(this);
 869     }
 870 
 871     /**
 872      * Returns a copy of this date-time with the specified field set to a new value.
 873      * <p>
 874      * This returns a {@code LocalDateTime}, based on this one, with the value
 875      * for the specified field changed.
 876      * This can be used to change any supported field, such as the year, month or day-of-month.
 877      * If it is not possible to set the value, because the field is not supported or for
 878      * some other reason, an exception is thrown.
 879      * <p>
 880      * In some cases, changing the specified field can cause the resulting date-time to become invalid,
 881      * such as changing the month from 31st January to February would make the day-of-month invalid.
 882      * In cases like this, the field is responsible for resolving the date. Typically it will choose
 883      * the previous valid date, which would be the last valid day of February in this example.
 884      * <p>
 885      * If the field is a {@link ChronoField} then the adjustment is implemented here.
 886      * The {@link #isSupported(TemporalField) supported fields} will behave as per
 887      * the matching method on {@link LocalDate#with(TemporalField, long) LocalDate}
 888      * or {@link LocalTime#with(TemporalField, long) LocalTime}.
 889      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 890      * <p>
 891      * If the field is not a {@code ChronoField}, then the result of this method
 892      * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
 893      * passing {@code this} as the argument. In this case, the field determines
 894      * whether and how to adjust the instant.
 895      * <p>
 896      * This instance is immutable and unaffected by this method call.
 897      *
 898      * @param field  the field to set in the result, not null
 899      * @param newValue  the new value of the field in the result
 900      * @return a {@code LocalDateTime} based on {@code this} with the specified field set, not null
 901      * @throws DateTimeException if the field cannot be set
 902      * @throws ArithmeticException if numeric overflow occurs
 903      */
 904     @Override
 905     public LocalDateTime with(TemporalField field, long newValue) {
 906         if (field instanceof ChronoField) {
 907             ChronoField f = (ChronoField) field;
 908             if (f.isTimeField()) {
 909                 return with(date, time.with(field, newValue));
 910             } else {
 911                 return with(date.with(field, newValue), time);
 912             }
 913         }
 914         return field.adjustInto(this, newValue);
 915     }
 916 
 917     //-----------------------------------------------------------------------
 918     /**
 919      * Returns a copy of this {@code LocalDateTime} with the year altered.
 920      * The time does not affect the calculation and will be the same in the result.
 921      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
 922      * <p>
 923      * This instance is immutable and unaffected by this method call.
 924      *
 925      * @param year  the year to set in the result, from MIN_YEAR to MAX_YEAR
 926      * @return a {@code LocalDateTime} based on this date-time with the requested year, not null
 927      * @throws DateTimeException if the year value is invalid
 928      */
 929     public LocalDateTime withYear(int year) {
 930         return with(date.withYear(year), time);
 931     }
 932 
 933     /**
 934      * Returns a copy of this {@code LocalDateTime} with the month-of-year altered.


 937      * <p>
 938      * This instance is immutable and unaffected by this method call.
 939      *
 940      * @param month  the month-of-year to set in the result, from 1 (January) to 12 (December)
 941      * @return a {@code LocalDateTime} based on this date-time with the requested month, not null
 942      * @throws DateTimeException if the month-of-year value is invalid
 943      */
 944     public LocalDateTime withMonth(int month) {
 945         return with(date.withMonth(month), time);
 946     }
 947 
 948     /**
 949      * Returns a copy of this {@code LocalDateTime} with the day-of-month altered.
 950      * If the resulting {@code LocalDateTime} is invalid, an exception is thrown.
 951      * The time does not affect the calculation and will be the same in the result.
 952      * <p>
 953      * This instance is immutable and unaffected by this method call.
 954      *
 955      * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
 956      * @return a {@code LocalDateTime} based on this date-time with the requested day, not null
 957      * @throws DateTimeException if the day-of-month value is invalid,
 958      *  or if the day-of-month is invalid for the month-year
 959      */
 960     public LocalDateTime withDayOfMonth(int dayOfMonth) {
 961         return with(date.withDayOfMonth(dayOfMonth), time);
 962     }
 963 
 964     /**
 965      * Returns a copy of this {@code LocalDateTime} with the day-of-year altered.
 966      * If the resulting {@code LocalDateTime} is invalid, an exception is thrown.
 967      * <p>
 968      * This instance is immutable and unaffected by this method call.
 969      *
 970      * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
 971      * @return a {@code LocalDateTime} based on this date with the requested day, not null
 972      * @throws DateTimeException if the day-of-year value is invalid,
 973      *  or if the day-of-year is invalid for the year
 974      */
 975     public LocalDateTime withDayOfYear(int dayOfYear) {
 976         return with(date.withDayOfYear(dayOfYear), time);
 977     }
 978 
 979     //-----------------------------------------------------------------------
 980     /**
 981      * Returns a copy of this {@code LocalDateTime} with the hour-of-day value altered.
 982      * <p>
 983      * This instance is immutable and unaffected by this method call.
 984      *
 985      * @param hour  the hour-of-day to set in the result, from 0 to 23
 986      * @return a {@code LocalDateTime} based on this date-time with the requested hour, not null
 987      * @throws DateTimeException if the hour value is invalid
 988      */
 989     public LocalDateTime withHour(int hour) {
 990         LocalTime newTime = time.withHour(hour);
 991         return with(date, newTime);
 992     }
 993 


1025      * This instance is immutable and unaffected by this method call.
1026      *
1027      * @param nanoOfSecond  the nano-of-second to set in the result, from 0 to 999,999,999
1028      * @return a {@code LocalDateTime} based on this date-time with the requested nanosecond, not null
1029      * @throws DateTimeException if the nano value is invalid
1030      */
1031     public LocalDateTime withNano(int nanoOfSecond) {
1032         LocalTime newTime = time.withNano(nanoOfSecond);
1033         return with(date, newTime);
1034     }
1035 
1036     //-----------------------------------------------------------------------
1037     /**
1038      * Returns a copy of this {@code LocalDateTime} with the time truncated.
1039      * <p>
1040      * Truncation returns a copy of the original date-time with fields
1041      * smaller than the specified unit set to zero.
1042      * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
1043      * will set the second-of-minute and nano-of-second field to zero.
1044      * <p>
1045      * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
1046      * that divides into the length of a standard day without remainder.
1047      * This includes all supplied time units on {@link ChronoUnit} and
1048      * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
1049      * <p>
1050      * This instance is immutable and unaffected by this method call.
1051      *
1052      * @param unit  the unit to truncate to, not null
1053      * @return a {@code LocalDateTime} based on this date-time with the time truncated, not null
1054      * @throws DateTimeException if unable to truncate
1055      */
1056     public LocalDateTime truncatedTo(TemporalUnit unit) {
1057         return with(date, time.truncatedTo(unit));
1058     }
1059 
1060     //-----------------------------------------------------------------------
1061     /**
1062      * Returns a copy of this date-time with the specified amount added.
1063      * <p>
1064      * This returns a {@code LocalDateTime}, based on this one, with the specified amount added.
1065      * The amount is typically {@link Period} or {@link Duration} but may be
1066      * any other type implementing the {@link TemporalAmount} interface.
1067      * <p>
1068      * The calculation is delegated to the amount object by calling
1069      * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
1070      * to implement the addition in any way it wishes, however it typically
1071      * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
1072      * of the amount implementation to determine if it can be successfully added.
1073      * <p>
1074      * This instance is immutable and unaffected by this method call.
1075      *
1076      * @param amountToAdd  the amount to add, not null
1077      * @return a {@code LocalDateTime} based on this date-time with the addition made, not null
1078      * @throws DateTimeException if the addition cannot be made
1079      * @throws ArithmeticException if numeric overflow occurs
1080      */
1081     @Override
1082     public LocalDateTime plus(TemporalAmount amountToAdd) {
1083         return (LocalDateTime) amountToAdd.addTo(this);
1084     }
1085 
1086     /**
1087      * Returns a copy of this date-time with the specified amount added.
1088      * <p>
1089      * This returns a {@code LocalDateTime}, based on this one, with the amount
1090      * in terms of the unit added. If it is not possible to add the amount, because the
1091      * unit is not supported or for some other reason, an exception is thrown.
1092      * <p>
1093      * If the field is a {@link ChronoUnit} then the addition is implemented here.
1094      * Date units are added as per {@link LocalDate#plus(long, TemporalUnit)}.
1095      * Time units are added as per {@link LocalTime#plus(long, TemporalUnit)} with
1096      * any overflow in days added equivalent to using {@link #plusDays(long)}.
1097      * <p>
1098      * If the field is not a {@code ChronoUnit}, then the result of this method
1099      * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
1100      * passing {@code this} as the argument. In this case, the unit determines
1101      * whether and how to perform the addition.
1102      * <p>
1103      * This instance is immutable and unaffected by this method call.
1104      *
1105      * @param amountToAdd  the amount of the unit to add to the result, may be negative
1106      * @param unit  the unit of the amount to add, not null
1107      * @return a {@code LocalDateTime} based on this date-time with the specified amount added, not null
1108      * @throws DateTimeException if the addition cannot be made
1109      * @throws ArithmeticException if numeric overflow occurs
1110      */
1111     @Override
1112     public LocalDateTime plus(long amountToAdd, TemporalUnit unit) {
1113         if (unit instanceof ChronoUnit) {
1114             ChronoUnit f = (ChronoUnit) unit;
1115             switch (f) {
1116                 case NANOS: return plusNanos(amountToAdd);
1117                 case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
1118                 case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
1119                 case SECONDS: return plusSeconds(amountToAdd);
1120                 case MINUTES: return plusMinutes(amountToAdd);
1121                 case HOURS: return plusHours(amountToAdd);
1122                 case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12);  // no overflow (256 is multiple of 2)
1123             }
1124             return with(date.plus(amountToAdd, unit), time);
1125         }
1126         return unit.addTo(this, amountToAdd);
1127     }
1128 
1129     //-----------------------------------------------------------------------
1130     /**
1131      * Returns a copy of this {@code LocalDateTime} with the specified period in years added.
1132      * <p>
1133      * This method adds the specified amount to the years field in three steps:
1134      * <ol>
1135      * <li>Add the input years to the year field</li>
1136      * <li>Check if the resulting date would be invalid</li>
1137      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1138      * </ol>
1139      * <p>
1140      * For example, 2008-02-29 (leap year) plus one year would result in the
1141      * invalid date 2009-02-29 (standard year). Instead of returning an invalid
1142      * result, the last valid day of the month, 2009-02-28, is selected instead.
1143      * <p>
1144      * This instance is immutable and unaffected by this method call.
1145      *
1146      * @param years  the years to add, may be negative


1255      */
1256     public LocalDateTime plusSeconds(long seconds) {
1257         return plusWithOverflow(date, 0, 0, seconds, 0, 1);
1258     }
1259 
1260     /**
1261      * Returns a copy of this {@code LocalDateTime} with the specified period in nanoseconds added.
1262      * <p>
1263      * This instance is immutable and unaffected by this method call.
1264      *
1265      * @param nanos  the nanos to add, may be negative
1266      * @return a {@code LocalDateTime} based on this date-time with the nanoseconds added, not null
1267      * @throws DateTimeException if the result exceeds the supported date range
1268      */
1269     public LocalDateTime plusNanos(long nanos) {
1270         return plusWithOverflow(date, 0, 0, 0, nanos, 1);
1271     }
1272 
1273     //-----------------------------------------------------------------------
1274     /**
1275      * Returns a copy of this date-time with the specified amount subtracted.
1276      * <p>
1277      * This returns a {@code LocalDateTime}, based on this one, with the specified amount subtracted.
1278      * The amount is typically {@link Period} or {@link Duration} but may be
1279      * any other type implementing the {@link TemporalAmount} interface.
1280      * <p>
1281      * The calculation is delegated to the amount object by calling
1282      * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
1283      * to implement the subtraction in any way it wishes, however it typically
1284      * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
1285      * of the amount implementation to determine if it can be successfully subtracted.
1286      * <p>
1287      * This instance is immutable and unaffected by this method call.
1288      *
1289      * @param amountToSubtract  the amount to subtract, not null
1290      * @return a {@code LocalDateTime} based on this date-time with the subtraction made, not null
1291      * @throws DateTimeException if the subtraction cannot be made
1292      * @throws ArithmeticException if numeric overflow occurs
1293      */
1294     @Override
1295     public LocalDateTime minus(TemporalAmount amountToSubtract) {
1296         return (LocalDateTime) amountToSubtract.subtractFrom(this);
1297     }
1298 
1299     /**
1300      * Returns a copy of this date-time with the specified amount subtracted.
1301      * <p>
1302      * This returns a {@code LocalDateTime}, based on this one, with the amount
1303      * in terms of the unit subtracted. If it is not possible to subtract the amount,
1304      * because the unit is not supported or for some other reason, an exception is thrown.
1305      * <p>
1306      * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
1307      * See that method for a full description of how addition, and thus subtraction, works.
1308      * <p>
1309      * This instance is immutable and unaffected by this method call.
1310      *
1311      * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
1312      * @param unit  the unit of the amount to subtract, not null
1313      * @return a {@code LocalDateTime} based on this date-time with the specified amount subtracted, not null
1314      * @throws DateTimeException if the subtraction cannot be made
1315      * @throws ArithmeticException if numeric overflow occurs
1316      */
1317     @Override
1318     public LocalDateTime minus(long amountToSubtract, TemporalUnit unit) {
1319         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
1320     }
1321 
1322     //-----------------------------------------------------------------------
1323     /**
1324      * Returns a copy of this {@code LocalDateTime} with the specified period in years subtracted.
1325      * <p>
1326      * This method subtracts the specified amount from the years field in three steps:
1327      * <ol>
1328      * <li>Subtract the input years from the year field</li>
1329      * <li>Check if the resulting date would be invalid</li>
1330      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1331      * </ol>
1332      * <p>
1333      * For example, 2008-02-29 (leap year) minus one year would result in the
1334      * invalid date 2009-02-29 (standard year). Instead of returning an invalid
1335      * result, the last valid day of the month, 2009-02-28, is selected instead.


1497 
1498     //-----------------------------------------------------------------------
1499     /**
1500      * Queries this date-time using the specified query.
1501      * <p>
1502      * This queries this date-time using the specified query strategy object.
1503      * The {@code TemporalQuery} object defines the logic to be used to
1504      * obtain the result. Read the documentation of the query to understand
1505      * what the result of this method will be.
1506      * <p>
1507      * The result of this method is obtained by invoking the
1508      * {@link java.time.temporal.TemporalQuery#queryFrom(TemporalAccessor)} method on the
1509      * specified query passing {@code this} as the argument.
1510      *
1511      * @param <R> the type of the result
1512      * @param query  the query to invoke, not null
1513      * @return the query result, null may be returned (defined by the query)
1514      * @throws DateTimeException if unable to query (defined by the query)
1515      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
1516      */
1517     @SuppressWarnings("unchecked")
1518     @Override  // override for Javadoc
1519     public <R> R query(TemporalQuery<R> query) {
1520         if (query == Queries.localDate()) {
1521             return (R) date;
1522         }
1523         return ChronoLocalDateTime.super.query(query);
1524     }
1525 
1526     /**
1527      * Adjusts the specified temporal object to have the same date and time as this object.
1528      * <p>
1529      * This returns a temporal object of the same observable type as the input
1530      * with the date and time changed to be the same as this.
1531      * <p>
1532      * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
1533      * twice, passing {@link ChronoField#EPOCH_DAY} and
1534      * {@link ChronoField#NANO_OF_DAY} as the fields.
1535      * <p>
1536      * In most cases, it is clearer to reverse the calling pattern by using
1537      * {@link Temporal#with(TemporalAdjuster)}:
1538      * <pre>
1539      *   // these two lines are equivalent, but the second approach is recommended
1540      *   temporal = thisLocalDateTime.adjustInto(temporal);
1541      *   temporal = temporal.with(thisLocalDateTime);
1542      * </pre>


1552     public Temporal adjustInto(Temporal temporal) {
1553         return ChronoLocalDateTime.super.adjustInto(temporal);
1554     }
1555 
1556     /**
1557      * Calculates the period between this date-time and another date-time in
1558      * terms of the specified unit.
1559      * <p>
1560      * This calculates the period between two date-times in terms of a single unit.
1561      * The start and end points are {@code this} and the specified date-time.
1562      * The result will be negative if the end is before the start.
1563      * The {@code Temporal} passed to this method must be a {@code LocalDateTime}.
1564      * For example, the period in days between two date-times can be calculated
1565      * using {@code startDateTime.periodUntil(endDateTime, DAYS)}.
1566      * <p>
1567      * The calculation returns a whole number, representing the number of
1568      * complete units between the two date-times.
1569      * For example, the period in months between 2012-06-15T00:00 and 2012-08-14T23:59
1570      * will only be one month as it is one minute short of two months.
1571      * <p>
1572      * There are two equivalent ways of using this method.
1573      * The first is to invoke this method.
1574      * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:

1575      * <pre>
1576      *   // these two lines are equivalent
1577      *   amount = start.periodUntil(end, MONTHS);
1578      *   amount = MONTHS.between(start, end);
1579      * </pre>
1580      * The choice should be made based on which makes the code more readable.
1581      * <p>
1582      * The calculation is implemented in this method for {@link ChronoUnit}.
1583      * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
1584      * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS},
1585      * {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES},
1586      * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported.
1587      * Other {@code ChronoUnit} values will throw an exception.
1588      * <p>
1589      * If the unit is not a {@code ChronoUnit}, then the result of this method
1590      * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
1591      * passing {@code this} as the first argument and the input temporal as
1592      * the second argument.
1593      * <p>
1594      * This instance is immutable and unaffected by this method call.
1595      *
1596      * @param endDateTime  the end date-time, which must be a {@code LocalDateTime}, not null
1597      * @param unit  the unit to measure the period in, not null
1598      * @return the amount of the period between this date-time and the end date-time
1599      * @throws DateTimeException if the period cannot be calculated
1600      * @throws ArithmeticException if numeric overflow occurs


1610             ChronoUnit f = (ChronoUnit) unit;
1611             if (f.isTimeUnit()) {
1612                 long amount = date.daysUntil(end.date);
1613                 switch (f) {
1614                     case NANOS: amount = Math.multiplyExact(amount, NANOS_PER_DAY); break;
1615                     case MICROS: amount = Math.multiplyExact(amount, MICROS_PER_DAY); break;
1616                     case MILLIS: amount = Math.multiplyExact(amount, MILLIS_PER_DAY); break;
1617                     case SECONDS: amount = Math.multiplyExact(amount, SECONDS_PER_DAY); break;
1618                     case MINUTES: amount = Math.multiplyExact(amount, MINUTES_PER_DAY); break;
1619                     case HOURS: amount = Math.multiplyExact(amount, HOURS_PER_DAY); break;
1620                     case HALF_DAYS: amount = Math.multiplyExact(amount, 2); break;
1621                 }
1622                 return Math.addExact(amount, time.periodUntil(end.time, unit));
1623             }
1624             LocalDate endDate = end.date;
1625             if (end.time.isBefore(time)) {
1626                 endDate = endDate.minusDays(1);
1627             }
1628             return date.periodUntil(endDate, unit);
1629         }
1630         return unit.between(this, endDateTime);
1631     }
1632 
1633     //-----------------------------------------------------------------------
1634     /**
1635      * Combines this time with a date to create an {@code OffsetTime}.
1636      * <p>
1637      * This returns an {@code OffsetTime} formed from this time at the specified offset.
1638      * All possible combinations of date-time and offset are valid.


1639      *
1640      * @param offset  the offset to combine with, not null
1641      * @return the offset date-time formed from this date-time and the specified offset, not null
1642      */
1643     public OffsetDateTime atOffset(ZoneOffset offset) {
1644         return OffsetDateTime.of(this, offset);
1645     }
1646 
1647     /**
1648      * Combines this time with a time-zone to create a {@code ZonedDateTime}.
1649      * <p>
1650      * This returns a {@code ZonedDateTime} formed from this date-time at the
1651      * specified time-zone. The result will match this date-time as closely as possible.
1652      * Time-zone rules, such as daylight savings, mean that not every local date-time
1653      * is valid for the specified zone, thus the local date-time may be adjusted.
1654      * <p>
1655      * The local date-time is resolved to a single instant on the time-line.
1656      * This is achieved by finding a valid offset from UTC/Greenwich for the local
1657      * date-time as defined by the {@link ZoneRules rules} of the zone ID.
1658      *<p>
1659      * In most cases, there is only one valid offset for a local date-time.
1660      * In the case of an overlap, where clocks are set back, there are two valid offsets.
1661      * This method uses the earlier offset typically corresponding to "summer".
1662      * <p>
1663      * In the case of a gap, where clocks jump forward, there is no valid offset.
1664      * Instead, the local date-time is adjusted to be later by the length of the gap.
1665      * For a typical one hour daylight savings change, the local date-time will be
1666      * moved one hour later into the offset typically corresponding to "summer".
1667      * <p>
1668      * To obtain the later offset during an overlap, call
1669      * {@link ZonedDateTime#withLaterOffsetAtOverlap()} on the result of this method.
1670      * To throw an exception when there is a gap or overlap, use
1671      * {@link ZonedDateTime#ofStrict(LocalDateTime, ZoneOffset, ZoneId)}.


1672      *
1673      * @param zone  the time-zone to use, not null
1674      * @return the zoned date-time formed from this date-time, not null
1675      */
1676     @Override
1677     public ZonedDateTime atZone(ZoneId zone) {
1678         return ZonedDateTime.of(this, zone);
1679     }
1680 
1681     //-----------------------------------------------------------------------
1682     /**
1683      * Compares this date-time to another date-time.
1684      * <p>
1685      * The comparison is primarily based on the date-time, from earliest to latest.
1686      * It is "consistent with equals", as defined by {@link Comparable}.
1687      * <p>
1688      * If all the date-times being compared are instances of {@code LocalDateTime},
1689      * then the comparison will be entirely based on the date-time.
1690      * If some dates being compared are in different chronologies, then the
1691      * chronology is also considered, see {@link ChronoLocalDateTime#compareTo}.
1692      *
1693      * @param other  the other date-time to compare to, not null
1694      * @return the comparator value, negative if less, positive if greater
1695      */
1696     @Override  // override for Javadoc and performance
1697     public int compareTo(ChronoLocalDateTime<?> other) {
1698         if (other instanceof LocalDateTime) {
1699             return compareTo0((LocalDateTime) other);
1700         }
1701         return ChronoLocalDateTime.super.compareTo(other);
1702     }
1703 
1704     private int compareTo0(LocalDateTime other) {
1705         int cmp = date.compareTo0(other.toLocalDate());
1706         if (cmp == 0) {
1707             cmp = time.compareTo(other.toLocalTime());
1708         }
1709         return cmp;
1710     }
1711 
1712     /**
1713      * Checks if this date-time is after the specified date-time.
1714      * <p>
1715      * This checks to see if this date-time represents a point on the
1716      * local time-line after the other date-time.
1717      * <pre>
1718      *   LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
1719      *   LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
1720      *   a.isAfter(b) == false
1721      *   a.isAfter(a) == false
1722      *   b.isAfter(a) == true
1723      * </pre>
1724      * <p>
1725      * This method only considers the position of the two date-times on the local time-line.
1726      * It does not take into account the chronology, or calendar system.
1727      * This is different from the comparison in {@link #compareTo(ChronoLocalDateTime)},


1837      * <li>{@code yyyy-MM-dd'T'HH:mm}</li>
1838      * <li>{@code yyyy-MM-dd'T'HH:mm:ss}</li>
1839      * <li>{@code yyyy-MM-dd'T'HH:mm:ss.SSS}</li>
1840      * <li>{@code yyyy-MM-dd'T'HH:mm:ss.SSSSSS}</li>
1841      * <li>{@code yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS}</li>
1842      * </ul><p>
1843      * The format used will be the shortest that outputs the full value of
1844      * the time where the omitted parts are implied to be zero.
1845      *
1846      * @return a string representation of this date-time, not null
1847      */
1848     @Override
1849     public String toString() {
1850         return date.toString() + 'T' + time.toString();
1851     }
1852 
1853     /**
1854      * Outputs this date-time as a {@code String} using the formatter.
1855      * <p>
1856      * This date-time will be passed to the formatter
1857      * {@link DateTimeFormatter#format(TemporalAccessor) format method}.
1858      *
1859      * @param formatter  the formatter to use, not null
1860      * @return the formatted date-time string, not null
1861      * @throws DateTimeException if an error occurs during printing
1862      */
1863     @Override  // override for Javadoc
1864     public String toString(DateTimeFormatter formatter) {
1865         return ChronoLocalDateTime.super.toString(formatter);
1866     }
1867 
1868     //-----------------------------------------------------------------------
1869     /**
1870      * Writes the object using a
1871      * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1872      * <pre>
1873      *  out.writeByte(5);  // identifies this as a LocalDateTime
1874      *  // the <a href="../../serialized-form.html#java.time.LocalDate">date</a> excluding the one byte header
1875      *  // the <a href="../../serialized-form.html#java.time.LocalTime">time</a> excluding the one byte header
1876      * </pre>
1877      *