1 /*
   2  * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * This file is available under and governed by the GNU General Public
  28  * License version 2 only, as published by the Free Software Foundation.
  29  * However, the following notice accompanied the original version of this
  30  * file:
  31  *
  32  * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
  33  *
  34  * All rights reserved.
  35  *
  36  * Redistribution and use in source and binary forms, with or without
  37  * modification, are permitted provided that the following conditions are met:
  38  *
  39  *  * Redistributions of source code must retain the above copyright notice,
  40  *    this list of conditions and the following disclaimer.
  41  *
  42  *  * Redistributions in binary form must reproduce the above copyright notice,
  43  *    this list of conditions and the following disclaimer in the documentation
  44  *    and/or other materials provided with the distribution.
  45  *
  46  *  * Neither the name of JSR-310 nor the names of its contributors
  47  *    may be used to endorse or promote products derived from this software
  48  *    without specific prior written permission.
  49  *
  50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  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.ObjectInputStream;
  80 import java.io.Serializable;
  81 import java.time.chrono.ChronoLocalDateTime;
  82 import java.time.format.DateTimeFormatter;
  83 import java.time.format.DateTimeParseException;
  84 import java.time.temporal.ChronoField;
  85 import java.time.temporal.ChronoUnit;
  86 import java.time.temporal.Temporal;
  87 import java.time.temporal.TemporalAccessor;
  88 import java.time.temporal.TemporalAdjuster;
  89 import java.time.temporal.TemporalAmount;
  90 import java.time.temporal.TemporalField;
  91 import java.time.temporal.TemporalQueries;
  92 import java.time.temporal.TemporalQuery;
  93 import java.time.temporal.TemporalUnit;
  94 import java.time.temporal.UnsupportedTemporalTypeException;
  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  * <p>
 123  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 124  * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
 125  * as interchangeable and should not use instances for synchronization, mutexes, or
 126  * with {@linkplain java.lang.ref.Reference object references}.
 127  *
 128  * <div class="preview-block">
 129  *      <div class="preview-comment">
 130  *          When preview features are enabled, {@code LocalDateTime} is a {@linkplain Class#isValue value class}.
 131  *          Use of value class instances for synchronization, mutexes, or with
 132  *          {@linkplain java.lang.ref.Reference object references} result in
 133  *          {@link IdentityException}.
 134  *      </div>
 135  * </div>
 136  *
 137  * @implSpec
 138  * This class is immutable and thread-safe.
 139  *
 140  * @since 1.8
 141  */
 142 @jdk.internal.ValueBased
 143 @jdk.internal.MigratedValueClass
 144 public final class LocalDateTime
 145         implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable {
 146 
 147     /**
 148      * The minimum supported {@code LocalDateTime}, '-999999999-01-01T00:00:00'.
 149      * This is the local date-time of midnight at the start of the minimum date.
 150      * This combines {@link LocalDate#MIN} and {@link LocalTime#MIN}.
 151      * This could be used by an application as a "far past" date-time.
 152      */
 153     public static final LocalDateTime MIN = LocalDateTime.of(LocalDate.MIN, LocalTime.MIN);
 154     /**
 155      * The maximum supported {@code LocalDateTime}, '+999999999-12-31T23:59:59.999999999'.
 156      * This is the local date-time just before midnight at the end of the maximum date.
 157      * This combines {@link LocalDate#MAX} and {@link LocalTime#MAX}.
 158      * This could be used by an application as a "far future" date-time.
 159      */
 160     public static final LocalDateTime MAX = LocalDateTime.of(LocalDate.MAX, LocalTime.MAX);
 161 
 162     /**
 163      * Serialization version.
 164      */
 165     @java.io.Serial
 166     private static final long serialVersionUID = 6207766400415563566L;
 167 
 168     /**
 169      * The date part.
 170      */
 171     private final LocalDate date;
 172     /**
 173      * The time part.
 174      */
 175     private final LocalTime time;
 176 
 177     //-----------------------------------------------------------------------
 178     /**
 179      * Obtains the current date-time from the system clock in the default time-zone.
 180      * <p>
 181      * This will query the {@link Clock#systemDefaultZone() system clock} in the default
 182      * time-zone to obtain the current date-time.
 183      * <p>
 184      * Using this method will prevent the ability to use an alternate clock for testing
 185      * because the clock is hard-coded.
 186      *
 187      * @return the current date-time using the system clock and default time-zone, not null
 188      */
 189     public static LocalDateTime now() {
 190         return now(Clock.systemDefaultZone());
 191     }
 192 
 193     /**
 194      * Obtains the current date-time from the system clock in the specified time-zone.
 195      * <p>
 196      * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date-time.
 197      * Specifying the time-zone avoids dependence on the default time-zone.
 198      * <p>
 199      * Using this method will prevent the ability to use an alternate clock for testing
 200      * because the clock is hard-coded.
 201      *
 202      * @param zone  the zone ID to use, not null
 203      * @return the current date-time using the system clock, not null
 204      */
 205     public static LocalDateTime now(ZoneId zone) {
 206         return now(Clock.system(zone));
 207     }
 208 
 209     /**
 210      * Obtains the current date-time from the specified clock.
 211      * <p>
 212      * This will query the specified clock to obtain the current date-time.
 213      * Using this method allows the use of an alternate clock for testing.
 214      * The alternate clock may be introduced using {@link Clock dependency injection}.
 215      *
 216      * @param clock  the clock to use, not null
 217      * @return the current date-time, not null
 218      */
 219     public static LocalDateTime now(Clock clock) {
 220         Objects.requireNonNull(clock, "clock");
 221         final Instant now = clock.instant();  // called once
 222         ZoneOffset offset = clock.getZone().getRules().getOffset(now);
 223         return ofEpochSecond(now.getEpochSecond(), now.getNano(), offset);
 224     }
 225 
 226     //-----------------------------------------------------------------------
 227     /**
 228      * Obtains an instance of {@code LocalDateTime} from year, month,
 229      * day, hour and minute, setting the second and nanosecond to zero.
 230      * <p>
 231      * This returns a {@code LocalDateTime} with the specified year, month,
 232      * day-of-month, hour and minute.
 233      * The day must be valid for the year and month, otherwise an exception will be thrown.
 234      * The second and nanosecond fields will be set to zero.
 235      *
 236      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 237      * @param month  the month-of-year to represent, not null
 238      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 239      * @param hour  the hour-of-day to represent, from 0 to 23
 240      * @param minute  the minute-of-hour to represent, from 0 to 59
 241      * @return the local date-time, not null
 242      * @throws DateTimeException if the value of any field is out of range,
 243      *  or if the day-of-month is invalid for the month-year
 244      */
 245     public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute) {
 246         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 247         LocalTime time = LocalTime.of(hour, minute);
 248         return new LocalDateTime(date, time);
 249     }
 250 
 251     /**
 252      * Obtains an instance of {@code LocalDateTime} from year, month,
 253      * day, hour, minute and second, setting the nanosecond to zero.
 254      * <p>
 255      * This returns a {@code LocalDateTime} with the specified year, month,
 256      * day-of-month, hour, minute and second.
 257      * The day must be valid for the year and month, otherwise an exception will be thrown.
 258      * The nanosecond field will be set to zero.
 259      *
 260      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 261      * @param month  the month-of-year to represent, not null
 262      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 263      * @param hour  the hour-of-day to represent, from 0 to 23
 264      * @param minute  the minute-of-hour to represent, from 0 to 59
 265      * @param second  the second-of-minute to represent, from 0 to 59
 266      * @return the local date-time, not null
 267      * @throws DateTimeException if the value of any field is out of range,
 268      *  or if the day-of-month is invalid for the month-year
 269      */
 270     public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) {
 271         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 272         LocalTime time = LocalTime.of(hour, minute, second);
 273         return new LocalDateTime(date, time);
 274     }
 275 
 276     /**
 277      * Obtains an instance of {@code LocalDateTime} from year, month,
 278      * day, hour, minute, second and nanosecond.
 279      * <p>
 280      * This returns a {@code LocalDateTime} with the specified year, month,
 281      * day-of-month, hour, minute, second and nanosecond.
 282      * The day must be valid for the year and month, otherwise an exception will be thrown.
 283      *
 284      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 285      * @param month  the month-of-year to represent, not null
 286      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 287      * @param hour  the hour-of-day to represent, from 0 to 23
 288      * @param minute  the minute-of-hour to represent, from 0 to 59
 289      * @param second  the second-of-minute to represent, from 0 to 59
 290      * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
 291      * @return the local date-time, not null
 292      * @throws DateTimeException if the value of any field is out of range,
 293      *  or if the day-of-month is invalid for the month-year
 294      */
 295     public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) {
 296         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 297         LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond);
 298         return new LocalDateTime(date, time);
 299     }
 300 
 301     //-----------------------------------------------------------------------
 302     /**
 303      * Obtains an instance of {@code LocalDateTime} from year, month,
 304      * day, hour and minute, setting the second and nanosecond to zero.
 305      * <p>
 306      * This returns a {@code LocalDateTime} with the specified year, month,
 307      * day-of-month, hour and minute.
 308      * The day must be valid for the year and month, otherwise an exception will be thrown.
 309      * The second and nanosecond fields will be set to zero.
 310      *
 311      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 312      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 313      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 314      * @param hour  the hour-of-day to represent, from 0 to 23
 315      * @param minute  the minute-of-hour to represent, from 0 to 59
 316      * @return the local date-time, not null
 317      * @throws DateTimeException if the value of any field is out of range,
 318      *  or if the day-of-month is invalid for the month-year
 319      */
 320     public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) {
 321         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 322         LocalTime time = LocalTime.of(hour, minute);
 323         return new LocalDateTime(date, time);
 324     }
 325 
 326     /**
 327      * Obtains an instance of {@code LocalDateTime} from year, month,
 328      * day, hour, minute and second, setting the nanosecond to zero.
 329      * <p>
 330      * This returns a {@code LocalDateTime} with the specified year, month,
 331      * day-of-month, hour, minute and second.
 332      * The day must be valid for the year and month, otherwise an exception will be thrown.
 333      * The nanosecond field will be set to zero.
 334      *
 335      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 336      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 337      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 338      * @param hour  the hour-of-day to represent, from 0 to 23
 339      * @param minute  the minute-of-hour to represent, from 0 to 59
 340      * @param second  the second-of-minute to represent, from 0 to 59
 341      * @return the local date-time, not null
 342      * @throws DateTimeException if the value of any field is out of range,
 343      *  or if the day-of-month is invalid for the month-year
 344      */
 345     public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) {
 346         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 347         LocalTime time = LocalTime.of(hour, minute, second);
 348         return new LocalDateTime(date, time);
 349     }
 350 
 351     /**
 352      * Obtains an instance of {@code LocalDateTime} from year, month,
 353      * day, hour, minute, second and nanosecond.
 354      * <p>
 355      * This returns a {@code LocalDateTime} with the specified year, month,
 356      * day-of-month, hour, minute, second and nanosecond.
 357      * The day must be valid for the year and month, otherwise an exception will be thrown.
 358      *
 359      * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
 360      * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
 361      * @param dayOfMonth  the day-of-month to represent, from 1 to 31
 362      * @param hour  the hour-of-day to represent, from 0 to 23
 363      * @param minute  the minute-of-hour to represent, from 0 to 59
 364      * @param second  the second-of-minute to represent, from 0 to 59
 365      * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
 366      * @return the local date-time, not null
 367      * @throws DateTimeException if the value of any field is out of range,
 368      *  or if the day-of-month is invalid for the month-year
 369      */
 370     public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) {
 371         LocalDate date = LocalDate.of(year, month, dayOfMonth);
 372         LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond);
 373         return new LocalDateTime(date, time);
 374     }
 375 
 376     /**
 377      * Obtains an instance of {@code LocalDateTime} from a date and time.
 378      *
 379      * @param date  the local date, not null
 380      * @param time  the local time, not null
 381      * @return the local date-time, not null
 382      */
 383     public static LocalDateTime of(LocalDate date, LocalTime time) {
 384         Objects.requireNonNull(date, "date");
 385         Objects.requireNonNull(time, "time");
 386         return new LocalDateTime(date, time);
 387     }
 388 
 389     //-------------------------------------------------------------------------
 390     /**
 391      * Obtains an instance of {@code LocalDateTime} from an {@code Instant} and zone ID.
 392      * <p>
 393      * This creates a local date-time based on the specified instant.
 394      * First, the offset from UTC/Greenwich is obtained using the zone ID and instant,
 395      * which is simple as there is only one valid offset for each instant.
 396      * Then, the instant and offset are used to calculate the local date-time.
 397      *
 398      * @param instant  the instant to create the date-time from, not null
 399      * @param zone  the time-zone, which may be an offset, not null
 400      * @return the local date-time, not null
 401      * @throws DateTimeException if the result exceeds the supported range
 402      */
 403     public static LocalDateTime ofInstant(Instant instant, ZoneId zone) {
 404         Objects.requireNonNull(instant, "instant");
 405         Objects.requireNonNull(zone, "zone");
 406         ZoneRules rules = zone.getRules();
 407         ZoneOffset offset = rules.getOffset(instant);
 408         return ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
 409     }
 410 
 411     /**
 412      * Obtains an instance of {@code LocalDateTime} using seconds from the
 413      * epoch of 1970-01-01T00:00:00Z.
 414      * <p>
 415      * This allows the {@link ChronoField#INSTANT_SECONDS epoch-second} field
 416      * to be converted to a local date-time. This is primarily intended for
 417      * low-level conversions rather than general application usage.
 418      *
 419      * @param epochSecond  the number of seconds from the epoch of 1970-01-01T00:00:00Z
 420      * @param nanoOfSecond  the nanosecond within the second, from 0 to 999,999,999
 421      * @param offset  the zone offset, not null
 422      * @return the local date-time, not null
 423      * @throws DateTimeException if the result exceeds the supported range,
 424      *  or if the nano-of-second is invalid
 425      */
 426     public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) {
 427         Objects.requireNonNull(offset, "offset");
 428         NANO_OF_SECOND.checkValidValue(nanoOfSecond);
 429         long localSecond = epochSecond + offset.getTotalSeconds();  // overflow caught later
 430         long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY);
 431         int secsOfDay = Math.floorMod(localSecond, SECONDS_PER_DAY);
 432         LocalDate date = LocalDate.ofEpochDay(localEpochDay);
 433         LocalTime time = LocalTime.ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + nanoOfSecond);
 434         return new LocalDateTime(date, time);
 435     }
 436 
 437     //-----------------------------------------------------------------------
 438     /**
 439      * Obtains an instance of {@code LocalDateTime} from a temporal object.
 440      * <p>
 441      * This obtains a local date-time based on the specified temporal.
 442      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 443      * which this factory converts to an instance of {@code LocalDateTime}.
 444      * <p>
 445      * The conversion extracts and combines the {@code LocalDate} and the
 446      * {@code LocalTime} from the temporal object.
 447      * Implementations are permitted to perform optimizations such as accessing
 448      * those fields that are equivalent to the relevant objects.
 449      * <p>
 450      * This method matches the signature of the functional interface {@link TemporalQuery}
 451      * allowing it to be used as a query via method reference, {@code LocalDateTime::from}.
 452      *
 453      * @param temporal  the temporal object to convert, not null
 454      * @return the local date-time, not null
 455      * @throws DateTimeException if unable to convert to a {@code LocalDateTime}
 456      */
 457     public static LocalDateTime from(TemporalAccessor temporal) {
 458         if (temporal instanceof LocalDateTime) {
 459             return (LocalDateTime) temporal;
 460         } else if (temporal instanceof ZonedDateTime) {
 461             return ((ZonedDateTime) temporal).toLocalDateTime();
 462         } else if (temporal instanceof OffsetDateTime) {
 463             return ((OffsetDateTime) temporal).toLocalDateTime();
 464         }
 465         try {
 466             LocalDate date = LocalDate.from(temporal);
 467             LocalTime time = LocalTime.from(temporal);
 468             return new LocalDateTime(date, time);
 469         } catch (DateTimeException ex) {
 470             throw new DateTimeException("Unable to obtain LocalDateTime from TemporalAccessor: " +
 471                     temporal + " of type " + temporal.getClass().getName(), ex);
 472         }
 473     }
 474 
 475     //-----------------------------------------------------------------------
 476     /**
 477      * Obtains an instance of {@code LocalDateTime} from a text string such as {@code 2007-12-03T10:15:30}.
 478      * <p>
 479      * The string must represent a valid date-time and is parsed using
 480      * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME}.
 481      *
 482      * @param text  the text to parse such as "2007-12-03T10:15:30", not null
 483      * @return the parsed local date-time, not null
 484      * @throws DateTimeParseException if the text cannot be parsed
 485      */
 486     public static LocalDateTime parse(CharSequence text) {
 487         return parse(text, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
 488     }
 489 
 490     /**
 491      * Obtains an instance of {@code LocalDateTime} from a text string using a specific formatter.
 492      * <p>
 493      * The text is parsed using the formatter, returning a date-time.
 494      *
 495      * @param text  the text to parse, not null
 496      * @param formatter  the formatter to use, not null
 497      * @return the parsed local date-time, not null
 498      * @throws DateTimeParseException if the text cannot be parsed
 499      */
 500     public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) {
 501         Objects.requireNonNull(formatter, "formatter");
 502         return formatter.parse(text, LocalDateTime::from);
 503     }
 504 
 505     //-----------------------------------------------------------------------
 506     /**
 507      * Constructor.
 508      *
 509      * @param date  the date part of the date-time, validated not null
 510      * @param time  the time part of the date-time, validated not null
 511      */
 512     private LocalDateTime(LocalDate date, LocalTime time) {
 513         this.date = date;
 514         this.time = time;
 515     }
 516 
 517     /**
 518      * Returns a copy of this date-time with the new date and time, checking
 519      * to see if a new object is in fact required.
 520      *
 521      * @param newDate  the date of the new date-time, not null
 522      * @param newTime  the time of the new date-time, not null
 523      * @return the date-time, not null
 524      */
 525     private LocalDateTime with(LocalDate newDate, LocalTime newTime) {
 526         if (date == newDate && time == newTime) {
 527             return this;
 528         }
 529         return new LocalDateTime(newDate, newTime);
 530     }
 531 
 532     //-----------------------------------------------------------------------
 533     /**
 534      * Checks if the specified field is supported.
 535      * <p>
 536      * This checks if this date-time can be queried for the specified field.
 537      * If false, then calling the {@link #range(TemporalField) range},
 538      * {@link #get(TemporalField) get} and {@link #with(TemporalField, long)}
 539      * methods will throw an exception.
 540      * <p>
 541      * If the field is a {@link ChronoField} then the query is implemented here.
 542      * The supported fields are:
 543      * <ul>
 544      * <li>{@code NANO_OF_SECOND}
 545      * <li>{@code NANO_OF_DAY}
 546      * <li>{@code MICRO_OF_SECOND}
 547      * <li>{@code MICRO_OF_DAY}
 548      * <li>{@code MILLI_OF_SECOND}
 549      * <li>{@code MILLI_OF_DAY}
 550      * <li>{@code SECOND_OF_MINUTE}
 551      * <li>{@code SECOND_OF_DAY}
 552      * <li>{@code MINUTE_OF_HOUR}
 553      * <li>{@code MINUTE_OF_DAY}
 554      * <li>{@code HOUR_OF_AMPM}
 555      * <li>{@code CLOCK_HOUR_OF_AMPM}
 556      * <li>{@code HOUR_OF_DAY}
 557      * <li>{@code CLOCK_HOUR_OF_DAY}
 558      * <li>{@code AMPM_OF_DAY}
 559      * <li>{@code DAY_OF_WEEK}
 560      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
 561      * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
 562      * <li>{@code DAY_OF_MONTH}
 563      * <li>{@code DAY_OF_YEAR}
 564      * <li>{@code EPOCH_DAY}
 565      * <li>{@code ALIGNED_WEEK_OF_MONTH}
 566      * <li>{@code ALIGNED_WEEK_OF_YEAR}
 567      * <li>{@code MONTH_OF_YEAR}
 568      * <li>{@code PROLEPTIC_MONTH}
 569      * <li>{@code YEAR_OF_ERA}
 570      * <li>{@code YEAR}
 571      * <li>{@code ERA}
 572      * </ul>
 573      * All other {@code ChronoField} instances will return false.
 574      * <p>
 575      * If the field is not a {@code ChronoField}, then the result of this method
 576      * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
 577      * passing {@code this} as the argument.
 578      * Whether the field is supported is determined by the field.
 579      *
 580      * @param field  the field to check, null returns false
 581      * @return true if the field is supported on this date-time, false if not
 582      */
 583     @Override
 584     public boolean isSupported(TemporalField field) {
 585         if (field instanceof ChronoField chronoField) {
 586             return chronoField.isDateBased() || chronoField.isTimeBased();
 587         }
 588         return field != null && field.isSupportedBy(this);
 589     }
 590 
 591     /**
 592      * Checks if the specified unit is supported.
 593      * <p>
 594      * This checks if the specified unit can be added to, or subtracted from, this date-time.
 595      * If false, then calling the {@link #plus(long, TemporalUnit)} and
 596      * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
 597      * <p>
 598      * If the unit is a {@link ChronoUnit} then the query is implemented here.
 599      * The supported units are:
 600      * <ul>
 601      * <li>{@code NANOS}
 602      * <li>{@code MICROS}
 603      * <li>{@code MILLIS}
 604      * <li>{@code SECONDS}
 605      * <li>{@code MINUTES}
 606      * <li>{@code HOURS}
 607      * <li>{@code HALF_DAYS}
 608      * <li>{@code DAYS}
 609      * <li>{@code WEEKS}
 610      * <li>{@code MONTHS}
 611      * <li>{@code YEARS}
 612      * <li>{@code DECADES}
 613      * <li>{@code CENTURIES}
 614      * <li>{@code MILLENNIA}
 615      * <li>{@code ERAS}
 616      * </ul>
 617      * All other {@code ChronoUnit} instances will return false.
 618      * <p>
 619      * If the unit is not a {@code ChronoUnit}, then the result of this method
 620      * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
 621      * passing {@code this} as the argument.
 622      * Whether the unit is supported is determined by the unit.
 623      *
 624      * @param unit  the unit to check, null returns false
 625      * @return true if the unit can be added/subtracted, false if not
 626      */
 627     @Override  // override for Javadoc
 628     public boolean isSupported(TemporalUnit unit) {
 629         return ChronoLocalDateTime.super.isSupported(unit);
 630     }
 631 
 632     //-----------------------------------------------------------------------
 633     /**
 634      * Gets the range of valid values for the specified field.
 635      * <p>
 636      * The range object expresses the minimum and maximum valid values for a field.
 637      * This date-time is used to enhance the accuracy of the returned range.
 638      * If it is not possible to return the range, because the field is not supported
 639      * or for some other reason, an exception is thrown.
 640      * <p>
 641      * If the field is a {@link ChronoField} then the query is implemented here.
 642      * The {@link #isSupported(TemporalField) supported fields} will return
 643      * appropriate range instances.
 644      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 645      * <p>
 646      * If the field is not a {@code ChronoField}, then the result of this method
 647      * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
 648      * passing {@code this} as the argument.
 649      * Whether the range can be obtained is determined by the field.
 650      *
 651      * @param field  the field to query the range for, not null
 652      * @return the range of valid values for the field, not null
 653      * @throws DateTimeException if the range for the field cannot be obtained
 654      * @throws UnsupportedTemporalTypeException if the field is not supported
 655      */
 656     @Override
 657     public ValueRange range(TemporalField field) {
 658         if (field instanceof ChronoField chronoField) {
 659             return (chronoField.isTimeBased() ? time.range(field) : date.range(field));
 660         }
 661         return field.rangeRefinedBy(this);
 662     }
 663 
 664     /**
 665      * Gets the value of the specified field from this date-time as an {@code int}.
 666      * <p>
 667      * This queries this date-time for the value of the specified field.
 668      * The returned value will always be within the valid range of values for the field.
 669      * If it is not possible to return the value, because the field is not supported
 670      * or for some other reason, an exception is thrown.
 671      * <p>
 672      * If the field is a {@link ChronoField} then the query is implemented here.
 673      * The {@link #isSupported(TemporalField) supported fields} will return valid
 674      * values based on this date-time, except {@code NANO_OF_DAY}, {@code MICRO_OF_DAY},
 675      * {@code EPOCH_DAY} and {@code PROLEPTIC_MONTH} which are too large to fit in
 676      * an {@code int} and throw an {@code UnsupportedTemporalTypeException}.
 677      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 678      * <p>
 679      * If the field is not a {@code ChronoField}, then the result of this method
 680      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 681      * passing {@code this} as the argument. Whether the value can be obtained,
 682      * and what the value represents, is determined by the field.
 683      *
 684      * @param field  the field to get, not null
 685      * @return the value for the field
 686      * @throws DateTimeException if a value for the field cannot be obtained or
 687      *         the value is outside the range of valid values for the field
 688      * @throws UnsupportedTemporalTypeException if the field is not supported or
 689      *         the range of values exceeds an {@code int}
 690      * @throws ArithmeticException if numeric overflow occurs
 691      */
 692     @Override
 693     public int get(TemporalField field) {
 694         if (field instanceof ChronoField chronoField) {
 695             return (chronoField.isTimeBased() ? time.get(field) : date.get(field));
 696         }
 697         return ChronoLocalDateTime.super.get(field);
 698     }
 699 
 700     /**
 701      * Gets the value of the specified field from this date-time as a {@code long}.
 702      * <p>
 703      * This queries this date-time for the value of the specified field.
 704      * If it is not possible to return the value, because the field is not supported
 705      * or for some other reason, an exception is thrown.
 706      * <p>
 707      * If the field is a {@link ChronoField} then the query is implemented here.
 708      * The {@link #isSupported(TemporalField) supported fields} will return valid
 709      * values based on this date-time.
 710      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 711      * <p>
 712      * If the field is not a {@code ChronoField}, then the result of this method
 713      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 714      * passing {@code this} as the argument. Whether the value can be obtained,
 715      * and what the value represents, is determined by the field.
 716      *
 717      * @param field  the field to get, not null
 718      * @return the value for the field
 719      * @throws DateTimeException if a value for the field cannot be obtained
 720      * @throws UnsupportedTemporalTypeException if the field is not supported
 721      * @throws ArithmeticException if numeric overflow occurs
 722      */
 723     @Override
 724     public long getLong(TemporalField field) {
 725         if (field instanceof ChronoField chronoField) {
 726             return (chronoField.isTimeBased() ? time.getLong(field) : date.getLong(field));
 727         }
 728         return field.getFrom(this);
 729     }
 730 
 731     //-----------------------------------------------------------------------
 732     /**
 733      * Gets the {@code LocalDate} part of this date-time.
 734      * <p>
 735      * This returns a {@code LocalDate} with the same year, month and day
 736      * as this date-time.
 737      *
 738      * @return the date part of this date-time, not null
 739      */
 740     @Override
 741     public LocalDate toLocalDate() {
 742         return date;
 743     }
 744 
 745     /**
 746      * Gets the year field.
 747      * <p>
 748      * This method returns the primitive {@code int} value for the year.
 749      * <p>
 750      * The year returned by this method is proleptic as per {@code get(YEAR)}.
 751      * To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}.
 752      *
 753      * @return the year, from MIN_YEAR to MAX_YEAR
 754      */
 755     public int getYear() {
 756         return date.getYear();
 757     }
 758 
 759     /**
 760      * Gets the month-of-year field from 1 to 12.
 761      * <p>
 762      * This method returns the month as an {@code int} from 1 to 12.
 763      * Application code is frequently clearer if the enum {@link Month}
 764      * is used by calling {@link #getMonth()}.
 765      *
 766      * @return the month-of-year, from 1 to 12
 767      * @see #getMonth()
 768      */
 769     public int getMonthValue() {
 770         return date.getMonthValue();
 771     }
 772 
 773     /**
 774      * Gets the month-of-year field using the {@code Month} enum.
 775      * <p>
 776      * This method returns the enum {@link Month} for the month.
 777      * This avoids confusion as to what {@code int} values mean.
 778      * If you need access to the primitive {@code int} value then the enum
 779      * provides the {@link Month#getValue() int value}.
 780      *
 781      * @return the month-of-year, not null
 782      * @see #getMonthValue()
 783      */
 784     public Month getMonth() {
 785         return date.getMonth();
 786     }
 787 
 788     /**
 789      * Gets the day-of-month field.
 790      * <p>
 791      * This method returns the primitive {@code int} value for the day-of-month.
 792      *
 793      * @return the day-of-month, from 1 to 31
 794      */
 795     public int getDayOfMonth() {
 796         return date.getDayOfMonth();
 797     }
 798 
 799     /**
 800      * Gets the day-of-year field.
 801      * <p>
 802      * This method returns the primitive {@code int} value for the day-of-year.
 803      *
 804      * @return the day-of-year, from 1 to 365, or 366 in a leap year
 805      */
 806     public int getDayOfYear() {
 807         return date.getDayOfYear();
 808     }
 809 
 810     /**
 811      * Gets the day-of-week field, which is an enum {@code DayOfWeek}.
 812      * <p>
 813      * This method returns the enum {@link DayOfWeek} for the day-of-week.
 814      * This avoids confusion as to what {@code int} values mean.
 815      * If you need access to the primitive {@code int} value then the enum
 816      * provides the {@link DayOfWeek#getValue() int value}.
 817      * <p>
 818      * Additional information can be obtained from the {@code DayOfWeek}.
 819      * This includes textual names of the values.
 820      *
 821      * @return the day-of-week, not null
 822      */
 823     public DayOfWeek getDayOfWeek() {
 824         return date.getDayOfWeek();
 825     }
 826 
 827     //-----------------------------------------------------------------------
 828     /**
 829      * Gets the {@code LocalTime} part of this date-time.
 830      * <p>
 831      * This returns a {@code LocalTime} with the same hour, minute, second and
 832      * nanosecond as this date-time.
 833      *
 834      * @return the time part of this date-time, not null
 835      */
 836     @Override
 837     public LocalTime toLocalTime() {
 838         return time;
 839     }
 840 
 841     /**
 842      * Gets the hour-of-day field.
 843      *
 844      * @return the hour-of-day, from 0 to 23
 845      */
 846     public int getHour() {
 847         return time.getHour();
 848     }
 849 
 850     /**
 851      * Gets the minute-of-hour field.
 852      *
 853      * @return the minute-of-hour, from 0 to 59
 854      */
 855     public int getMinute() {
 856         return time.getMinute();
 857     }
 858 
 859     /**
 860      * Gets the second-of-minute field.
 861      *
 862      * @return the second-of-minute, from 0 to 59
 863      */
 864     public int getSecond() {
 865         return time.getSecond();
 866     }
 867 
 868     /**
 869      * Gets the nano-of-second field.
 870      *
 871      * @return the nano-of-second, from 0 to 999,999,999
 872      */
 873     public int getNano() {
 874         return time.getNano();
 875     }
 876 
 877     //-----------------------------------------------------------------------
 878     /**
 879      * Returns an adjusted copy of this date-time.
 880      * <p>
 881      * This returns a {@code LocalDateTime}, based on this one, with the date-time adjusted.
 882      * The adjustment takes place using the specified adjuster strategy object.
 883      * Read the documentation of the adjuster to understand what adjustment will be made.
 884      * <p>
 885      * A simple adjuster might simply set the one of the fields, such as the year field.
 886      * A more complex adjuster might set the date to the last day of the month.
 887      * <p>
 888      * A selection of common adjustments is provided in
 889      * {@link java.time.temporal.TemporalAdjusters TemporalAdjusters}.
 890      * These include finding the "last day of the month" and "next Wednesday".
 891      * Key date-time classes also implement the {@code TemporalAdjuster} interface,
 892      * such as {@link Month} and {@link java.time.MonthDay MonthDay}.
 893      * The adjuster is responsible for handling special cases, such as the varying
 894      * lengths of month and leap years.
 895      * <p>
 896      * For example this code returns a date on the last day of July:
 897      * <pre>
 898      *  import static java.time.Month.*;
 899      *  import static java.time.temporal.TemporalAdjusters.*;
 900      *
 901      *  result = localDateTime.with(JULY).with(lastDayOfMonth());
 902      * </pre>
 903      * <p>
 904      * The classes {@link LocalDate} and {@link LocalTime} implement {@code TemporalAdjuster},
 905      * thus this method can be used to change the date, time or offset:
 906      * <pre>
 907      *  result = localDateTime.with(date);
 908      *  result = localDateTime.with(time);
 909      * </pre>
 910      * <p>
 911      * The result of this method is obtained by invoking the
 912      * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
 913      * specified adjuster passing {@code this} as the argument.
 914      * <p>
 915      * This instance is immutable and unaffected by this method call.
 916      *
 917      * @param adjuster the adjuster to use, not null
 918      * @return a {@code LocalDateTime} based on {@code this} with the adjustment made, not null
 919      * @throws DateTimeException if the adjustment cannot be made
 920      * @throws ArithmeticException if numeric overflow occurs
 921      */
 922     @Override
 923     public LocalDateTime with(TemporalAdjuster adjuster) {
 924         // optimizations
 925         if (adjuster instanceof LocalDate) {
 926             return with((LocalDate) adjuster, time);
 927         } else if (adjuster instanceof LocalTime) {
 928             return with(date, (LocalTime) adjuster);
 929         } else if (adjuster instanceof LocalDateTime) {
 930             return (LocalDateTime) adjuster;
 931         }
 932         return (LocalDateTime) adjuster.adjustInto(this);
 933     }
 934 
 935     /**
 936      * Returns a copy of this date-time with the specified field set to a new value.
 937      * <p>
 938      * This returns a {@code LocalDateTime}, based on this one, with the value
 939      * for the specified field changed.
 940      * This can be used to change any supported field, such as the year, month or day-of-month.
 941      * If it is not possible to set the value, because the field is not supported or for
 942      * some other reason, an exception is thrown.
 943      * <p>
 944      * In some cases, changing the specified field can cause the resulting date-time to become invalid,
 945      * such as changing the month from 31st January to February would make the day-of-month invalid.
 946      * In cases like this, the field is responsible for resolving the date. Typically it will choose
 947      * the previous valid date, which would be the last valid day of February in this example.
 948      * <p>
 949      * If the field is a {@link ChronoField} then the adjustment is implemented here.
 950      * The {@link #isSupported(TemporalField) supported fields} will behave as per
 951      * the matching method on {@link LocalDate#with(TemporalField, long) LocalDate}
 952      * or {@link LocalTime#with(TemporalField, long) LocalTime}.
 953      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 954      * <p>
 955      * If the field is not a {@code ChronoField}, then the result of this method
 956      * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
 957      * passing {@code this} as the argument. In this case, the field determines
 958      * whether and how to adjust the instant.
 959      * <p>
 960      * This instance is immutable and unaffected by this method call.
 961      *
 962      * @param field  the field to set in the result, not null
 963      * @param newValue  the new value of the field in the result
 964      * @return a {@code LocalDateTime} based on {@code this} with the specified field set, not null
 965      * @throws DateTimeException if the field cannot be set
 966      * @throws UnsupportedTemporalTypeException if the field is not supported
 967      * @throws ArithmeticException if numeric overflow occurs
 968      */
 969     @Override
 970     public LocalDateTime with(TemporalField field, long newValue) {
 971         if (field instanceof ChronoField chronoField) {
 972             if (chronoField.isTimeBased()) {
 973                 return with(date, time.with(field, newValue));
 974             } else {
 975                 return with(date.with(field, newValue), time);
 976             }
 977         }
 978         return field.adjustInto(this, newValue);
 979     }
 980 
 981     //-----------------------------------------------------------------------
 982     /**
 983      * Returns a copy of this {@code LocalDateTime} with the year altered.
 984      * <p>
 985      * The time does not affect the calculation and will be the same in the result.
 986      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
 987      * <p>
 988      * This instance is immutable and unaffected by this method call.
 989      *
 990      * @param year  the year to set in the result, from MIN_YEAR to MAX_YEAR
 991      * @return a {@code LocalDateTime} based on this date-time with the requested year, not null
 992      * @throws DateTimeException if the year value is invalid
 993      */
 994     public LocalDateTime withYear(int year) {
 995         return with(date.withYear(year), time);
 996     }
 997 
 998     /**
 999      * Returns a copy of this {@code LocalDateTime} with the month-of-year altered.
1000      * <p>
1001      * The time does not affect the calculation and will be the same in the result.
1002      * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
1003      * <p>
1004      * This instance is immutable and unaffected by this method call.
1005      *
1006      * @param month  the month-of-year to set in the result, from 1 (January) to 12 (December)
1007      * @return a {@code LocalDateTime} based on this date-time with the requested month, not null
1008      * @throws DateTimeException if the month-of-year value is invalid
1009      */
1010     public LocalDateTime withMonth(int month) {
1011         return with(date.withMonth(month), time);
1012     }
1013 
1014     /**
1015      * Returns a copy of this {@code LocalDateTime} with the day-of-month altered.
1016      * <p>
1017      * If the resulting date-time is invalid, an exception is thrown.
1018      * The time does not affect the calculation and will be the same in the result.
1019      * <p>
1020      * This instance is immutable and unaffected by this method call.
1021      *
1022      * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
1023      * @return a {@code LocalDateTime} based on this date-time with the requested day, not null
1024      * @throws DateTimeException if the day-of-month value is invalid,
1025      *  or if the day-of-month is invalid for the month-year
1026      */
1027     public LocalDateTime withDayOfMonth(int dayOfMonth) {
1028         return with(date.withDayOfMonth(dayOfMonth), time);
1029     }
1030 
1031     /**
1032      * Returns a copy of this {@code LocalDateTime} with the day-of-year altered.
1033      * <p>
1034      * If the resulting date-time is invalid, an exception is thrown.
1035      * <p>
1036      * This instance is immutable and unaffected by this method call.
1037      *
1038      * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
1039      * @return a {@code LocalDateTime} based on this date with the requested day, not null
1040      * @throws DateTimeException if the day-of-year value is invalid,
1041      *  or if the day-of-year is invalid for the year
1042      */
1043     public LocalDateTime withDayOfYear(int dayOfYear) {
1044         return with(date.withDayOfYear(dayOfYear), time);
1045     }
1046 
1047     //-----------------------------------------------------------------------
1048     /**
1049      * Returns a copy of this {@code LocalDateTime} with the hour-of-day altered.
1050      * <p>
1051      * This instance is immutable and unaffected by this method call.
1052      *
1053      * @param hour  the hour-of-day to set in the result, from 0 to 23
1054      * @return a {@code LocalDateTime} based on this date-time with the requested hour, not null
1055      * @throws DateTimeException if the hour value is invalid
1056      */
1057     public LocalDateTime withHour(int hour) {
1058         LocalTime newTime = time.withHour(hour);
1059         return with(date, newTime);
1060     }
1061 
1062     /**
1063      * Returns a copy of this {@code LocalDateTime} with the minute-of-hour altered.
1064      * <p>
1065      * This instance is immutable and unaffected by this method call.
1066      *
1067      * @param minute  the minute-of-hour to set in the result, from 0 to 59
1068      * @return a {@code LocalDateTime} based on this date-time with the requested minute, not null
1069      * @throws DateTimeException if the minute value is invalid
1070      */
1071     public LocalDateTime withMinute(int minute) {
1072         LocalTime newTime = time.withMinute(minute);
1073         return with(date, newTime);
1074     }
1075 
1076     /**
1077      * Returns a copy of this {@code LocalDateTime} with the second-of-minute altered.
1078      * <p>
1079      * This instance is immutable and unaffected by this method call.
1080      *
1081      * @param second  the second-of-minute to set in the result, from 0 to 59
1082      * @return a {@code LocalDateTime} based on this date-time with the requested second, not null
1083      * @throws DateTimeException if the second value is invalid
1084      */
1085     public LocalDateTime withSecond(int second) {
1086         LocalTime newTime = time.withSecond(second);
1087         return with(date, newTime);
1088     }
1089 
1090     /**
1091      * Returns a copy of this {@code LocalDateTime} with the nano-of-second altered.
1092      * <p>
1093      * This instance is immutable and unaffected by this method call.
1094      *
1095      * @param nanoOfSecond  the nano-of-second to set in the result, from 0 to 999,999,999
1096      * @return a {@code LocalDateTime} based on this date-time with the requested nanosecond, not null
1097      * @throws DateTimeException if the nano value is invalid
1098      */
1099     public LocalDateTime withNano(int nanoOfSecond) {
1100         LocalTime newTime = time.withNano(nanoOfSecond);
1101         return with(date, newTime);
1102     }
1103 
1104     //-----------------------------------------------------------------------
1105     /**
1106      * Returns a copy of this {@code LocalDateTime} with the time truncated.
1107      * <p>
1108      * Truncation returns a copy of the original date-time with fields
1109      * smaller than the specified unit set to zero.
1110      * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
1111      * will set the second-of-minute and nano-of-second field to zero.
1112      * <p>
1113      * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
1114      * that divides into the length of a standard day without remainder.
1115      * This includes all supplied time units on {@link ChronoUnit} and
1116      * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
1117      * <p>
1118      * This instance is immutable and unaffected by this method call.
1119      *
1120      * @param unit  the unit to truncate to, not null
1121      * @return a {@code LocalDateTime} based on this date-time with the time truncated, not null
1122      * @throws DateTimeException if unable to truncate
1123      * @throws UnsupportedTemporalTypeException if the unit is not supported
1124      */
1125     public LocalDateTime truncatedTo(TemporalUnit unit) {
1126         return with(date, time.truncatedTo(unit));
1127     }
1128 
1129     //-----------------------------------------------------------------------
1130     /**
1131      * Returns a copy of this date-time with the specified amount added.
1132      * <p>
1133      * This returns a {@code LocalDateTime}, based on this one, with the specified amount added.
1134      * The amount is typically {@link Period} or {@link Duration} but may be
1135      * any other type implementing the {@link TemporalAmount} interface.
1136      * <p>
1137      * The calculation is delegated to the amount object by calling
1138      * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
1139      * to implement the addition in any way it wishes, however it typically
1140      * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
1141      * of the amount implementation to determine if it can be successfully added.
1142      * <p>
1143      * This instance is immutable and unaffected by this method call.
1144      *
1145      * @param amountToAdd  the amount to add, not null
1146      * @return a {@code LocalDateTime} based on this date-time with the addition made, not null
1147      * @throws DateTimeException if the addition cannot be made
1148      * @throws ArithmeticException if numeric overflow occurs
1149      */
1150     @Override
1151     public LocalDateTime plus(TemporalAmount amountToAdd) {
1152         if (amountToAdd instanceof Period periodToAdd) {
1153             return with(date.plus(periodToAdd), time);
1154         }
1155         Objects.requireNonNull(amountToAdd, "amountToAdd");
1156         return (LocalDateTime) amountToAdd.addTo(this);
1157     }
1158 
1159     /**
1160      * Returns a copy of this date-time with the specified amount added.
1161      * <p>
1162      * This returns a {@code LocalDateTime}, based on this one, with the amount
1163      * in terms of the unit added. If it is not possible to add the amount, because the
1164      * unit is not supported or for some other reason, an exception is thrown.
1165      * <p>
1166      * If the field is a {@link ChronoUnit} then the addition is implemented here.
1167      * Date units are added as per {@link LocalDate#plus(long, TemporalUnit)}.
1168      * Time units are added as per {@link LocalTime#plus(long, TemporalUnit)} with
1169      * any overflow in days added equivalent to using {@link #plusDays(long)}.
1170      * <p>
1171      * If the field is not a {@code ChronoUnit}, then the result of this method
1172      * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
1173      * passing {@code this} as the argument. In this case, the unit determines
1174      * whether and how to perform the addition.
1175      * <p>
1176      * This instance is immutable and unaffected by this method call.
1177      *
1178      * @param amountToAdd  the amount of the unit to add to the result, may be negative
1179      * @param unit  the unit of the amount to add, not null
1180      * @return a {@code LocalDateTime} based on this date-time with the specified amount added, not null
1181      * @throws DateTimeException if the addition cannot be made
1182      * @throws UnsupportedTemporalTypeException if the unit is not supported
1183      * @throws ArithmeticException if numeric overflow occurs
1184      */
1185     @Override
1186     public LocalDateTime plus(long amountToAdd, TemporalUnit unit) {
1187         if (unit instanceof ChronoUnit chronoUnit) {
1188             return switch (chronoUnit) {
1189                 case NANOS     -> plusNanos(amountToAdd);
1190                 case MICROS    -> plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
1191                 case MILLIS    -> plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
1192                 case SECONDS   -> plusSeconds(amountToAdd);
1193                 case MINUTES   -> plusMinutes(amountToAdd);
1194                 case HOURS     -> plusHours(amountToAdd);
1195                 case HALF_DAYS -> plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is multiple of 2)
1196                 default -> with(date.plus(amountToAdd, unit), time);
1197             };
1198         }
1199         return unit.addTo(this, amountToAdd);
1200     }
1201 
1202     //-----------------------------------------------------------------------
1203     /**
1204      * Returns a copy of this {@code LocalDateTime} with the specified number of years added.
1205      * <p>
1206      * This method adds the specified amount to the years field in three steps:
1207      * <ol>
1208      * <li>Add the input years to the year field</li>
1209      * <li>Check if the resulting date would be invalid</li>
1210      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1211      * </ol>
1212      * <p>
1213      * For example, 2008-02-29 (leap year) plus one year would result in the
1214      * invalid date 2009-02-29 (standard year). Instead of returning an invalid
1215      * result, the last valid day of the month, 2009-02-28, is selected instead.
1216      * <p>
1217      * This instance is immutable and unaffected by this method call.
1218      *
1219      * @param years  the years to add, may be negative
1220      * @return a {@code LocalDateTime} based on this date-time with the years added, not null
1221      * @throws DateTimeException if the result exceeds the supported date range
1222      */
1223     public LocalDateTime plusYears(long years) {
1224         LocalDate newDate = date.plusYears(years);
1225         return with(newDate, time);
1226     }
1227 
1228     /**
1229      * Returns a copy of this {@code LocalDateTime} with the specified number of months added.
1230      * <p>
1231      * This method adds the specified amount to the months field in three steps:
1232      * <ol>
1233      * <li>Add the input months to the month-of-year field</li>
1234      * <li>Check if the resulting date would be invalid</li>
1235      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1236      * </ol>
1237      * <p>
1238      * For example, 2007-03-31 plus one month would result in the invalid date
1239      * 2007-04-31. Instead of returning an invalid result, the last valid day
1240      * of the month, 2007-04-30, is selected instead.
1241      * <p>
1242      * This instance is immutable and unaffected by this method call.
1243      *
1244      * @param months  the months to add, may be negative
1245      * @return a {@code LocalDateTime} based on this date-time with the months added, not null
1246      * @throws DateTimeException if the result exceeds the supported date range
1247      */
1248     public LocalDateTime plusMonths(long months) {
1249         LocalDate newDate = date.plusMonths(months);
1250         return with(newDate, time);
1251     }
1252 
1253     /**
1254      * Returns a copy of this {@code LocalDateTime} with the specified number of weeks added.
1255      * <p>
1256      * This method adds the specified amount in weeks to the days field incrementing
1257      * the month and year fields as necessary to ensure the result remains valid.
1258      * The result is only invalid if the maximum/minimum year is exceeded.
1259      * <p>
1260      * For example, 2008-12-31 plus one week would result in 2009-01-07.
1261      * <p>
1262      * This instance is immutable and unaffected by this method call.
1263      *
1264      * @param weeks  the weeks to add, may be negative
1265      * @return a {@code LocalDateTime} based on this date-time with the weeks added, not null
1266      * @throws DateTimeException if the result exceeds the supported date range
1267      */
1268     public LocalDateTime plusWeeks(long weeks) {
1269         LocalDate newDate = date.plusWeeks(weeks);
1270         return with(newDate, time);
1271     }
1272 
1273     /**
1274      * Returns a copy of this {@code LocalDateTime} with the specified number of days added.
1275      * <p>
1276      * This method adds the specified amount to the days field incrementing the
1277      * month and year fields as necessary to ensure the result remains valid.
1278      * The result is only invalid if the maximum/minimum year is exceeded.
1279      * <p>
1280      * For example, 2008-12-31 plus one day would result in 2009-01-01.
1281      * <p>
1282      * This instance is immutable and unaffected by this method call.
1283      *
1284      * @param days  the days to add, may be negative
1285      * @return a {@code LocalDateTime} based on this date-time with the days added, not null
1286      * @throws DateTimeException if the result exceeds the supported date range
1287      */
1288     public LocalDateTime plusDays(long days) {
1289         LocalDate newDate = date.plusDays(days);
1290         return with(newDate, time);
1291     }
1292 
1293     //-----------------------------------------------------------------------
1294     /**
1295      * Returns a copy of this {@code LocalDateTime} with the specified number of hours added.
1296      * <p>
1297      * This instance is immutable and unaffected by this method call.
1298      *
1299      * @param hours  the hours to add, may be negative
1300      * @return a {@code LocalDateTime} based on this date-time with the hours added, not null
1301      * @throws DateTimeException if the result exceeds the supported date range
1302      */
1303     public LocalDateTime plusHours(long hours) {
1304         return plusWithOverflow(date, hours, 0, 0, 0, 1);
1305     }
1306 
1307     /**
1308      * Returns a copy of this {@code LocalDateTime} with the specified number of minutes added.
1309      * <p>
1310      * This instance is immutable and unaffected by this method call.
1311      *
1312      * @param minutes  the minutes to add, may be negative
1313      * @return a {@code LocalDateTime} based on this date-time with the minutes added, not null
1314      * @throws DateTimeException if the result exceeds the supported date range
1315      */
1316     public LocalDateTime plusMinutes(long minutes) {
1317         return plusWithOverflow(date, 0, minutes, 0, 0, 1);
1318     }
1319 
1320     /**
1321      * Returns a copy of this {@code LocalDateTime} with the specified number of seconds added.
1322      * <p>
1323      * This instance is immutable and unaffected by this method call.
1324      *
1325      * @param seconds  the seconds to add, may be negative
1326      * @return a {@code LocalDateTime} based on this date-time with the seconds added, not null
1327      * @throws DateTimeException if the result exceeds the supported date range
1328      */
1329     public LocalDateTime plusSeconds(long seconds) {
1330         return plusWithOverflow(date, 0, 0, seconds, 0, 1);
1331     }
1332 
1333     /**
1334      * Returns a copy of this {@code LocalDateTime} with the specified number of nanoseconds added.
1335      * <p>
1336      * This instance is immutable and unaffected by this method call.
1337      *
1338      * @param nanos  the nanos to add, may be negative
1339      * @return a {@code LocalDateTime} based on this date-time with the nanoseconds added, not null
1340      * @throws DateTimeException if the result exceeds the supported date range
1341      */
1342     public LocalDateTime plusNanos(long nanos) {
1343         return plusWithOverflow(date, 0, 0, 0, nanos, 1);
1344     }
1345 
1346     //-----------------------------------------------------------------------
1347     /**
1348      * Returns a copy of this date-time with the specified amount subtracted.
1349      * <p>
1350      * This returns a {@code LocalDateTime}, based on this one, with the specified amount subtracted.
1351      * The amount is typically {@link Period} or {@link Duration} but may be
1352      * any other type implementing the {@link TemporalAmount} interface.
1353      * <p>
1354      * The calculation is delegated to the amount object by calling
1355      * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
1356      * to implement the subtraction in any way it wishes, however it typically
1357      * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
1358      * of the amount implementation to determine if it can be successfully subtracted.
1359      * <p>
1360      * This instance is immutable and unaffected by this method call.
1361      *
1362      * @param amountToSubtract  the amount to subtract, not null
1363      * @return a {@code LocalDateTime} based on this date-time with the subtraction made, not null
1364      * @throws DateTimeException if the subtraction cannot be made
1365      * @throws ArithmeticException if numeric overflow occurs
1366      */
1367     @Override
1368     public LocalDateTime minus(TemporalAmount amountToSubtract) {
1369         if (amountToSubtract instanceof Period periodToSubtract) {
1370             return with(date.minus(periodToSubtract), time);
1371         }
1372         Objects.requireNonNull(amountToSubtract, "amountToSubtract");
1373         return (LocalDateTime) amountToSubtract.subtractFrom(this);
1374     }
1375 
1376     /**
1377      * Returns a copy of this date-time with the specified amount subtracted.
1378      * <p>
1379      * This returns a {@code LocalDateTime}, based on this one, with the amount
1380      * in terms of the unit subtracted. If it is not possible to subtract the amount,
1381      * because the unit is not supported or for some other reason, an exception is thrown.
1382      * <p>
1383      * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
1384      * See that method for a full description of how addition, and thus subtraction, works.
1385      * <p>
1386      * This instance is immutable and unaffected by this method call.
1387      *
1388      * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
1389      * @param unit  the unit of the amount to subtract, not null
1390      * @return a {@code LocalDateTime} based on this date-time with the specified amount subtracted, not null
1391      * @throws DateTimeException if the subtraction cannot be made
1392      * @throws UnsupportedTemporalTypeException if the unit is not supported
1393      * @throws ArithmeticException if numeric overflow occurs
1394      */
1395     @Override
1396     public LocalDateTime minus(long amountToSubtract, TemporalUnit unit) {
1397         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
1398     }
1399 
1400     //-----------------------------------------------------------------------
1401     /**
1402      * Returns a copy of this {@code LocalDateTime} with the specified number of years subtracted.
1403      * <p>
1404      * This method subtracts the specified amount from the years field in three steps:
1405      * <ol>
1406      * <li>Subtract the input years from the year field</li>
1407      * <li>Check if the resulting date would be invalid</li>
1408      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1409      * </ol>
1410      * <p>
1411      * For example, 2008-02-29 (leap year) minus one year would result in the
1412      * invalid date 2007-02-29 (standard year). Instead of returning an invalid
1413      * result, the last valid day of the month, 2007-02-28, is selected instead.
1414      * <p>
1415      * This instance is immutable and unaffected by this method call.
1416      *
1417      * @param years  the years to subtract, may be negative
1418      * @return a {@code LocalDateTime} based on this date-time with the years subtracted, not null
1419      * @throws DateTimeException if the result exceeds the supported date range
1420      */
1421     public LocalDateTime minusYears(long years) {
1422         return (years == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-years));
1423     }
1424 
1425     /**
1426      * Returns a copy of this {@code LocalDateTime} with the specified number of months subtracted.
1427      * <p>
1428      * This method subtracts the specified amount from the months field in three steps:
1429      * <ol>
1430      * <li>Subtract the input months from the month-of-year field</li>
1431      * <li>Check if the resulting date would be invalid</li>
1432      * <li>Adjust the day-of-month to the last valid day if necessary</li>
1433      * </ol>
1434      * <p>
1435      * For example, 2007-03-31 minus one month would result in the invalid date
1436      * 2007-02-31. Instead of returning an invalid result, the last valid day
1437      * of the month, 2007-02-28, is selected instead.
1438      * <p>
1439      * This instance is immutable and unaffected by this method call.
1440      *
1441      * @param months  the months to subtract, may be negative
1442      * @return a {@code LocalDateTime} based on this date-time with the months subtracted, not null
1443      * @throws DateTimeException if the result exceeds the supported date range
1444      */
1445     public LocalDateTime minusMonths(long months) {
1446         return (months == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-months));
1447     }
1448 
1449     /**
1450      * Returns a copy of this {@code LocalDateTime} with the specified number of weeks subtracted.
1451      * <p>
1452      * This method subtracts the specified amount in weeks from the days field decrementing
1453      * the month and year fields as necessary to ensure the result remains valid.
1454      * The result is only invalid if the maximum/minimum year is exceeded.
1455      * <p>
1456      * For example, 2009-01-07 minus one week would result in 2008-12-31.
1457      * <p>
1458      * This instance is immutable and unaffected by this method call.
1459      *
1460      * @param weeks  the weeks to subtract, may be negative
1461      * @return a {@code LocalDateTime} based on this date-time with the weeks subtracted, not null
1462      * @throws DateTimeException if the result exceeds the supported date range
1463      */
1464     public LocalDateTime minusWeeks(long weeks) {
1465         return (weeks == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeks));
1466     }
1467 
1468     /**
1469      * Returns a copy of this {@code LocalDateTime} with the specified number of days subtracted.
1470      * <p>
1471      * This method subtracts the specified amount from the days field decrementing the
1472      * month and year fields as necessary to ensure the result remains valid.
1473      * The result is only invalid if the maximum/minimum year is exceeded.
1474      * <p>
1475      * For example, 2009-01-01 minus one day would result in 2008-12-31.
1476      * <p>
1477      * This instance is immutable and unaffected by this method call.
1478      *
1479      * @param days  the days to subtract, may be negative
1480      * @return a {@code LocalDateTime} based on this date-time with the days subtracted, not null
1481      * @throws DateTimeException if the result exceeds the supported date range
1482      */
1483     public LocalDateTime minusDays(long days) {
1484         return (days == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-days));
1485     }
1486 
1487     //-----------------------------------------------------------------------
1488     /**
1489      * Returns a copy of this {@code LocalDateTime} with the specified number of hours subtracted.
1490      * <p>
1491      * This instance is immutable and unaffected by this method call.
1492      *
1493      * @param hours  the hours to subtract, may be negative
1494      * @return a {@code LocalDateTime} based on this date-time with the hours subtracted, not null
1495      * @throws DateTimeException if the result exceeds the supported date range
1496      */
1497     public LocalDateTime minusHours(long hours) {
1498         return plusWithOverflow(date, hours, 0, 0, 0, -1);
1499    }
1500 
1501     /**
1502      * Returns a copy of this {@code LocalDateTime} with the specified number of minutes subtracted.
1503      * <p>
1504      * This instance is immutable and unaffected by this method call.
1505      *
1506      * @param minutes  the minutes to subtract, may be negative
1507      * @return a {@code LocalDateTime} based on this date-time with the minutes subtracted, not null
1508      * @throws DateTimeException if the result exceeds the supported date range
1509      */
1510     public LocalDateTime minusMinutes(long minutes) {
1511         return plusWithOverflow(date, 0, minutes, 0, 0, -1);
1512     }
1513 
1514     /**
1515      * Returns a copy of this {@code LocalDateTime} with the specified number of seconds subtracted.
1516      * <p>
1517      * This instance is immutable and unaffected by this method call.
1518      *
1519      * @param seconds  the seconds to subtract, may be negative
1520      * @return a {@code LocalDateTime} based on this date-time with the seconds subtracted, not null
1521      * @throws DateTimeException if the result exceeds the supported date range
1522      */
1523     public LocalDateTime minusSeconds(long seconds) {
1524         return plusWithOverflow(date, 0, 0, seconds, 0, -1);
1525     }
1526 
1527     /**
1528      * Returns a copy of this {@code LocalDateTime} with the specified number of nanoseconds subtracted.
1529      * <p>
1530      * This instance is immutable and unaffected by this method call.
1531      *
1532      * @param nanos  the nanos to subtract, may be negative
1533      * @return a {@code LocalDateTime} based on this date-time with the nanoseconds subtracted, not null
1534      * @throws DateTimeException if the result exceeds the supported date range
1535      */
1536     public LocalDateTime minusNanos(long nanos) {
1537         return plusWithOverflow(date, 0, 0, 0, nanos, -1);
1538     }
1539 
1540     //-----------------------------------------------------------------------
1541     /**
1542      * Returns a copy of this {@code LocalDateTime} with the specified period added.
1543      * <p>
1544      * This instance is immutable and unaffected by this method call.
1545      *
1546      * @param newDate  the new date to base the calculation on, not null
1547      * @param hours  the hours to add, may be negative
1548      * @param minutes the minutes to add, may be negative
1549      * @param seconds the seconds to add, may be negative
1550      * @param nanos the nanos to add, may be negative
1551      * @param sign  the sign to determine add or subtract
1552      * @return the combined result, not null
1553      */
1554     private LocalDateTime plusWithOverflow(LocalDate newDate, long hours, long minutes, long seconds, long nanos, int sign) {
1555         // 9223372036854775808 long, 2147483648 int
1556         if ((hours | minutes | seconds | nanos) == 0) {
1557             return with(newDate, time);
1558         }
1559         long totDays = nanos / NANOS_PER_DAY +             //   max/24*60*60*1B
1560                 seconds / SECONDS_PER_DAY +                //   max/24*60*60
1561                 minutes / MINUTES_PER_DAY +                //   max/24*60
1562                 hours / HOURS_PER_DAY;                     //   max/24
1563         totDays *= sign;                                   // total max*0.4237...
1564         long totNanos = nanos % NANOS_PER_DAY +                    //   max  86400000000000
1565                 (seconds % SECONDS_PER_DAY) * NANOS_PER_SECOND +   //   max  86400000000000
1566                 (minutes % MINUTES_PER_DAY) * NANOS_PER_MINUTE +   //   max  86400000000000
1567                 (hours % HOURS_PER_DAY) * NANOS_PER_HOUR;          //   max  86400000000000
1568         long curNoD = time.toNanoOfDay();                       //   max  86400000000000
1569         totNanos = totNanos * sign + curNoD;                    // total 432000000000000
1570         totDays += Math.floorDiv(totNanos, NANOS_PER_DAY);
1571         long newNoD = Math.floorMod(totNanos, NANOS_PER_DAY);
1572         LocalTime newTime = (newNoD == curNoD ? time : LocalTime.ofNanoOfDay(newNoD));
1573         return with(newDate.plusDays(totDays), newTime);
1574     }
1575 
1576     //-----------------------------------------------------------------------
1577     /**
1578      * Queries this date-time using the specified query.
1579      * <p>
1580      * This queries this date-time using the specified query strategy object.
1581      * The {@code TemporalQuery} object defines the logic to be used to
1582      * obtain the result. Read the documentation of the query to understand
1583      * what the result of this method will be.
1584      * <p>
1585      * The result of this method is obtained by invoking the
1586      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
1587      * specified query passing {@code this} as the argument.
1588      *
1589      * @param <R> the type of the result
1590      * @param query  the query to invoke, not null
1591      * @return the query result, null may be returned (defined by the query)
1592      * @throws DateTimeException if unable to query (defined by the query)
1593      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
1594      */
1595     @SuppressWarnings("unchecked")
1596     @Override  // override for Javadoc
1597     public <R> R query(TemporalQuery<R> query) {
1598         if (query == TemporalQueries.localDate()) {
1599             return (R) date;
1600         }
1601         return ChronoLocalDateTime.super.query(query);
1602     }
1603 
1604     /**
1605      * Adjusts the specified temporal object to have the same date and time as this object.
1606      * <p>
1607      * This returns a temporal object of the same observable type as the input
1608      * with the date and time changed to be the same as this.
1609      * <p>
1610      * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
1611      * twice, passing {@link ChronoField#EPOCH_DAY} and
1612      * {@link ChronoField#NANO_OF_DAY} as the fields.
1613      * <p>
1614      * In most cases, it is clearer to reverse the calling pattern by using
1615      * {@link Temporal#with(TemporalAdjuster)}:
1616      * <pre>
1617      *   // these two lines are equivalent, but the second approach is recommended
1618      *   temporal = thisLocalDateTime.adjustInto(temporal);
1619      *   temporal = temporal.with(thisLocalDateTime);
1620      * </pre>
1621      * <p>
1622      * This instance is immutable and unaffected by this method call.
1623      *
1624      * @param temporal  the target object to be adjusted, not null
1625      * @return the adjusted object, not null
1626      * @throws DateTimeException if unable to make the adjustment
1627      * @throws ArithmeticException if numeric overflow occurs
1628      */
1629     @Override  // override for Javadoc
1630     public Temporal adjustInto(Temporal temporal) {
1631         return ChronoLocalDateTime.super.adjustInto(temporal);
1632     }
1633 
1634     /**
1635      * Calculates the amount of time until another date-time in terms of the specified unit.
1636      * <p>
1637      * This calculates the amount of time between two {@code LocalDateTime}
1638      * objects in terms of a single {@code TemporalUnit}.
1639      * The start and end points are {@code this} and the specified date-time.
1640      * The result will be negative if the end is before the start.
1641      * The {@code Temporal} passed to this method is converted to a
1642      * {@code LocalDateTime} using {@link #from(TemporalAccessor)}.
1643      * For example, the amount in days between two date-times can be calculated
1644      * using {@code startDateTime.until(endDateTime, DAYS)}.
1645      * <p>
1646      * The calculation returns a whole number, representing the number of
1647      * complete units between the two date-times.
1648      * For example, the amount in months between 2012-06-15T00:00 and 2012-08-14T23:59
1649      * will only be one month as it is one minute short of two months.
1650      * <p>
1651      * There are two equivalent ways of using this method.
1652      * The first is to invoke this method.
1653      * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
1654      * <pre>
1655      *   // these two lines are equivalent
1656      *   amount = start.until(end, MONTHS);
1657      *   amount = MONTHS.between(start, end);
1658      * </pre>
1659      * The choice should be made based on which makes the code more readable.
1660      * <p>
1661      * The calculation is implemented in this method for {@link ChronoUnit}.
1662      * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
1663      * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS},
1664      * {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES},
1665      * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported.
1666      * Other {@code ChronoUnit} values will throw an exception.
1667      * <p>
1668      * If the unit is not a {@code ChronoUnit}, then the result of this method
1669      * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
1670      * passing {@code this} as the first argument and the converted input temporal
1671      * as the second argument.
1672      * <p>
1673      * This instance is immutable and unaffected by this method call.
1674      *
1675      * @param endExclusive  the end date, exclusive, which is converted to a {@code LocalDateTime}, not null
1676      * @param unit  the unit to measure the amount in, not null
1677      * @return the amount of time between this date-time and the end date-time
1678      * @throws DateTimeException if the amount cannot be calculated, or the end
1679      *  temporal cannot be converted to a {@code LocalDateTime}
1680      * @throws UnsupportedTemporalTypeException if the unit is not supported
1681      * @throws ArithmeticException if numeric overflow occurs
1682      */
1683     @Override
1684     public long until(Temporal endExclusive, TemporalUnit unit) {
1685         LocalDateTime end = LocalDateTime.from(endExclusive);
1686         if (unit instanceof ChronoUnit chronoUnit) {
1687             if (unit.isTimeBased()) {
1688                 long amount = date.daysUntil(end.date);
1689                 if (amount == 0) {
1690                     return time.until(end.time, unit);
1691                 }
1692                 long timePart = end.time.toNanoOfDay() - time.toNanoOfDay();
1693                 if (amount > 0) {
1694                     amount--;  // safe
1695                     timePart += NANOS_PER_DAY;  // safe
1696                 } else {
1697                     amount++;  // safe
1698                     timePart -= NANOS_PER_DAY;  // safe
1699                 }
1700                 switch (chronoUnit) {
1701                     case NANOS:
1702                         amount = Math.multiplyExact(amount, NANOS_PER_DAY);
1703                         break;
1704                     case MICROS:
1705                         amount = Math.multiplyExact(amount, MICROS_PER_DAY);
1706                         timePart = timePart / 1000;
1707                         break;
1708                     case MILLIS:
1709                         amount = Math.multiplyExact(amount, MILLIS_PER_DAY);
1710                         timePart = timePart / 1_000_000;
1711                         break;
1712                     case SECONDS:
1713                         amount = Math.multiplyExact(amount, SECONDS_PER_DAY);
1714                         timePart = timePart / NANOS_PER_SECOND;
1715                         break;
1716                     case MINUTES:
1717                         amount = Math.multiplyExact(amount, MINUTES_PER_DAY);
1718                         timePart = timePart / NANOS_PER_MINUTE;
1719                         break;
1720                     case HOURS:
1721                         amount = Math.multiplyExact(amount, HOURS_PER_DAY);
1722                         timePart = timePart / NANOS_PER_HOUR;
1723                         break;
1724                     case HALF_DAYS:
1725                         amount = Math.multiplyExact(amount, 2);
1726                         timePart = timePart / (NANOS_PER_HOUR * 12);
1727                         break;
1728                 }
1729                 return Math.addExact(amount, timePart);
1730             }
1731             LocalDate endDate = end.date;
1732             if (endDate.isAfter(date) && end.time.isBefore(time)) {
1733                 endDate = endDate.minusDays(1);
1734             } else if (endDate.isBefore(date) && end.time.isAfter(time)) {
1735                 endDate = endDate.plusDays(1);
1736             }
1737             return date.until(endDate, unit);
1738         }
1739         return unit.between(this, end);
1740     }
1741 
1742     /**
1743      * Formats this date-time using the specified formatter.
1744      * <p>
1745      * This date-time will be passed to the formatter to produce a string.
1746      *
1747      * @param formatter  the formatter to use, not null
1748      * @return the formatted date-time string, not null
1749      * @throws DateTimeException if an error occurs during printing
1750      */
1751     @Override  // override for Javadoc and performance
1752     public String format(DateTimeFormatter formatter) {
1753         Objects.requireNonNull(formatter, "formatter");
1754         return formatter.format(this);
1755     }
1756 
1757     //-----------------------------------------------------------------------
1758     /**
1759      * Combines this date-time with an offset to create an {@code OffsetDateTime}.
1760      * <p>
1761      * This returns an {@code OffsetDateTime} formed from this date-time at the specified offset.
1762      * All possible combinations of date-time and offset are valid.
1763      *
1764      * @param offset  the offset to combine with, not null
1765      * @return the offset date-time formed from this date-time and the specified offset, not null
1766      */
1767     public OffsetDateTime atOffset(ZoneOffset offset) {
1768         return OffsetDateTime.of(this, offset);
1769     }
1770 
1771     /**
1772      * Combines this date-time with a time-zone to create a {@code ZonedDateTime}.
1773      * <p>
1774      * This returns a {@code ZonedDateTime} formed from this date-time at the
1775      * specified time-zone. The result will match this date-time as closely as possible.
1776      * Time-zone rules, such as daylight savings, mean that not every local date-time
1777      * is valid for the specified zone, thus the local date-time may be adjusted.
1778      * <p>
1779      * The local date-time is resolved to a single instant on the time-line.
1780      * This is achieved by finding a valid offset from UTC/Greenwich for the local
1781      * date-time as defined by the {@link ZoneRules rules} of the zone ID.
1782      *<p>
1783      * In most cases, there is only one valid offset for a local date-time.
1784      * In the case of an overlap, where clocks are set back, there are two valid offsets.
1785      * This method uses the earlier offset typically corresponding to "summer".
1786      * <p>
1787      * In the case of a gap, where clocks jump forward, there is no valid offset.
1788      * Instead, the local date-time is adjusted to be later by the length of the gap.
1789      * For a typical one hour daylight savings change, the local date-time will be
1790      * moved one hour later into the offset typically corresponding to "summer".
1791      * <p>
1792      * To obtain the later offset during an overlap, call
1793      * {@link ZonedDateTime#withLaterOffsetAtOverlap()} on the result of this method.
1794      * To throw an exception when there is a gap or overlap, use
1795      * {@link ZonedDateTime#ofStrict(LocalDateTime, ZoneOffset, ZoneId)}.
1796      *
1797      * @param zone  the time-zone to use, not null
1798      * @return the zoned date-time formed from this date-time, not null
1799      */
1800     @Override
1801     public ZonedDateTime atZone(ZoneId zone) {
1802         return ZonedDateTime.of(this, zone);
1803     }
1804 
1805     //-----------------------------------------------------------------------
1806     /**
1807      * Compares this date-time to another date-time.
1808      * <p>
1809      * The comparison is primarily based on the date-time, from earliest to latest.
1810      * It is "consistent with equals", as defined by {@link Comparable}.
1811      * <p>
1812      * If all the date-times being compared are instances of {@code LocalDateTime},
1813      * then the comparison will be entirely based on the date-time.
1814      * If some dates being compared are in different chronologies, then the
1815      * chronology is also considered, see {@link ChronoLocalDateTime#compareTo}.
1816      *
1817      * @param other  the other date-time to compare to, not null
1818      * @return the comparator value, that is the comparison of this local date-time with
1819      *          the {@code other} local date-time and this chronology with the {@code other} chronology,
1820      *          in order, returning the first non-zero result, and otherwise returning zero
1821      * @see #isBefore
1822      * @see #isAfter
1823      */
1824     @Override  // override for Javadoc and performance
1825     public int compareTo(ChronoLocalDateTime<?> other) {
1826         if (other instanceof LocalDateTime) {
1827             return compareTo0((LocalDateTime) other);
1828         }
1829         return ChronoLocalDateTime.super.compareTo(other);
1830     }
1831 
1832     private int compareTo0(LocalDateTime other) {
1833         int cmp = date.compareTo0(other.toLocalDate());
1834         if (cmp == 0) {
1835             cmp = time.compareTo(other.toLocalTime());
1836         }
1837         return cmp;
1838     }
1839 
1840     /**
1841      * Checks if this date-time is after the specified date-time.
1842      * <p>
1843      * This checks to see if this date-time represents a point on the
1844      * local time-line after the other date-time.
1845      * <pre>
1846      *   LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
1847      *   LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
1848      *   a.isAfter(b) == false
1849      *   a.isAfter(a) == false
1850      *   b.isAfter(a) == true
1851      * </pre>
1852      * <p>
1853      * This method only considers the position of the two date-times on the local time-line.
1854      * It does not take into account the chronology, or calendar system.
1855      * This is different from the comparison in {@link #compareTo(ChronoLocalDateTime)},
1856      * but is the same approach as {@link ChronoLocalDateTime#timeLineOrder()}.
1857      *
1858      * @param other  the other date-time to compare to, not null
1859      * @return true if this date-time is after the specified date-time
1860      */
1861     @Override  // override for Javadoc and performance
1862     public boolean isAfter(ChronoLocalDateTime<?> other) {
1863         if (other instanceof LocalDateTime) {
1864             return compareTo0((LocalDateTime) other) > 0;
1865         }
1866         return ChronoLocalDateTime.super.isAfter(other);
1867     }
1868 
1869     /**
1870      * Checks if this date-time is before the specified date-time.
1871      * <p>
1872      * This checks to see if this date-time represents a point on the
1873      * local time-line before the other date-time.
1874      * <pre>
1875      *   LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
1876      *   LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
1877      *   a.isBefore(b) == true
1878      *   a.isBefore(a) == false
1879      *   b.isBefore(a) == false
1880      * </pre>
1881      * <p>
1882      * This method only considers the position of the two date-times on the local time-line.
1883      * It does not take into account the chronology, or calendar system.
1884      * This is different from the comparison in {@link #compareTo(ChronoLocalDateTime)},
1885      * but is the same approach as {@link ChronoLocalDateTime#timeLineOrder()}.
1886      *
1887      * @param other  the other date-time to compare to, not null
1888      * @return true if this date-time is before the specified date-time
1889      */
1890     @Override  // override for Javadoc and performance
1891     public boolean isBefore(ChronoLocalDateTime<?> other) {
1892         if (other instanceof LocalDateTime) {
1893             return compareTo0((LocalDateTime) other) < 0;
1894         }
1895         return ChronoLocalDateTime.super.isBefore(other);
1896     }
1897 
1898     /**
1899      * Checks if this date-time is equal to the specified date-time.
1900      * <p>
1901      * This checks to see if this date-time represents the same point on the
1902      * local time-line as the other date-time.
1903      * <pre>
1904      *   LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
1905      *   LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
1906      *   a.isEqual(b) == false
1907      *   a.isEqual(a) == true
1908      *   b.isEqual(a) == false
1909      * </pre>
1910      * <p>
1911      * This method only considers the position of the two date-times on the local time-line.
1912      * It does not take into account the chronology, or calendar system.
1913      * This is different from the comparison in {@link #compareTo(ChronoLocalDateTime)},
1914      * but is the same approach as {@link ChronoLocalDateTime#timeLineOrder()}.
1915      *
1916      * @param other  the other date-time to compare to, not null
1917      * @return true if this date-time is equal to the specified date-time
1918      */
1919     @Override  // override for Javadoc and performance
1920     public boolean isEqual(ChronoLocalDateTime<?> other) {
1921         if (other instanceof LocalDateTime) {
1922             return compareTo0((LocalDateTime) other) == 0;
1923         }
1924         return ChronoLocalDateTime.super.isEqual(other);
1925     }
1926 
1927     //-----------------------------------------------------------------------
1928     /**
1929      * Checks if this date-time is equal to another date-time.
1930      * <p>
1931      * Compares this {@code LocalDateTime} with another ensuring that the date-time is the same.
1932      * Only objects of type {@code LocalDateTime} are compared, other types return false.
1933      *
1934      * @param obj  the object to check, null returns false
1935      * @return true if this is equal to the other date-time
1936      */
1937     @Override
1938     public boolean equals(Object obj) {
1939         if (this == obj) {
1940             return true;
1941         }
1942         return (obj instanceof LocalDateTime other)
1943                 && date.equals(other.date)
1944                 && time.equals(other.time);
1945     }
1946 
1947     /**
1948      * A hash code for this date-time.
1949      *
1950      * @return a suitable hash code
1951      */
1952     @Override
1953     public int hashCode() {
1954         return date.hashCode() ^ time.hashCode();
1955     }
1956 
1957     //-----------------------------------------------------------------------
1958     /**
1959      * Outputs this date-time as a {@code String}, such as {@code 2007-12-03T10:15:30}.
1960      * <p>
1961      * The output will be one of the following ISO-8601 formats:
1962      * <ul>
1963      * <li>{@code uuuu-MM-dd'T'HH:mm}</li>
1964      * <li>{@code uuuu-MM-dd'T'HH:mm:ss}</li>
1965      * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSS}</li>
1966      * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSSSS}</li>
1967      * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS}</li>
1968      * </ul>
1969      * The format used will be the shortest that outputs the full value of
1970      * the time where the omitted parts are implied to be zero.
1971      *
1972      * @return a string representation of this date-time, not null
1973      */
1974     @Override
1975     public String toString() {
1976         var buf = new StringBuilder(29);
1977         formatTo(buf);
1978         return buf.toString();
1979     }
1980 
1981     /**
1982      * Prints the toString result to the given buf, avoiding extra string allocations.
1983      */
1984     void formatTo(StringBuilder buf) {
1985         date.formatTo(buf);
1986         buf.append('T');
1987         time.formatTo(buf);
1988     }
1989 
1990     //-----------------------------------------------------------------------
1991     /**
1992      * Writes the object using a
1993      * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1994      * @serialData
1995      * <pre>
1996      *  out.writeByte(5);  // identifies a LocalDateTime
1997      *  // the <a href="{@docRoot}/serialized-form.html#java.time.LocalDate">date</a> excluding the one byte header
1998      *  // the <a href="{@docRoot}/serialized-form.html#java.time.LocalTime">time</a> excluding the one byte header
1999      * </pre>
2000      *
2001      * @return the instance of {@code Ser}, not null
2002      */
2003     @java.io.Serial
2004     private Object writeReplace() {
2005         return new Ser(Ser.LOCAL_DATE_TIME_TYPE, this);
2006     }
2007 
2008     /**
2009      * Defend against malicious streams.
2010      *
2011      * @param s the stream to read
2012      * @throws InvalidObjectException always
2013      */
2014     @java.io.Serial
2015     private void readObject(ObjectInputStream s) throws InvalidObjectException {
2016         throw new InvalidObjectException("Deserialization via serialization delegate");
2017     }
2018 
2019     void writeExternal(DataOutput out) throws IOException {
2020         date.writeExternal(out);
2021         time.writeExternal(out);
2022     }
2023 
2024     static LocalDateTime readExternal(DataInput in) throws IOException {
2025         LocalDate date = LocalDate.readExternal(in);
2026         LocalTime time = LocalTime.readExternal(in);
2027         return LocalDateTime.of(date, time);
2028     }
2029 
2030 }