1 /*
   2  * Copyright (c) 2012, 2015, 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.temporal.ChronoField.HOUR_OF_DAY;
  65 import static java.time.temporal.ChronoField.MICRO_OF_DAY;
  66 import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
  67 import static java.time.temporal.ChronoField.NANO_OF_DAY;
  68 import static java.time.temporal.ChronoField.NANO_OF_SECOND;
  69 import static java.time.temporal.ChronoField.SECOND_OF_DAY;
  70 import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
  71 import static java.time.temporal.ChronoUnit.NANOS;
  72 
  73 import java.io.DataInput;
  74 import java.io.DataOutput;
  75 import java.io.IOException;
  76 import java.io.InvalidObjectException;
  77 import java.io.ObjectInputStream;
  78 import java.io.Serializable;
  79 import java.time.format.DateTimeFormatter;
  80 import java.time.format.DateTimeParseException;
  81 import java.time.temporal.ChronoField;
  82 import java.time.temporal.ChronoUnit;
  83 import java.time.temporal.Temporal;
  84 import java.time.temporal.TemporalAccessor;
  85 import java.time.temporal.TemporalAdjuster;
  86 import java.time.temporal.TemporalAmount;
  87 import java.time.temporal.TemporalField;
  88 import java.time.temporal.TemporalQueries;
  89 import java.time.temporal.TemporalQuery;
  90 import java.time.temporal.TemporalUnit;
  91 import java.time.temporal.UnsupportedTemporalTypeException;
  92 import java.time.temporal.ValueRange;
  93 import java.util.Objects;
  94 
  95 /**
  96  * A time without a time-zone in the ISO-8601 calendar system,
  97  * such as {@code 10:15:30}.
  98  * <p>
  99  * {@code LocalTime} is an immutable date-time object that represents a time,
 100  * often viewed as hour-minute-second.
 101  * Time is represented to nanosecond precision.
 102  * For example, the value "13:45.30.123456789" can be stored in a {@code LocalTime}.
 103  * <p>
 104  * This class does not store or represent a date or time-zone.
 105  * Instead, it is a description of the local time as seen on a wall clock.
 106  * It cannot represent an instant on the time-line without additional information
 107  * such as an offset or time-zone.
 108  * <p>
 109  * The ISO-8601 calendar system is the modern civil calendar system used today
 110  * in most of the world. This API assumes that all calendar systems use the same
 111  * representation, this class, for time-of-day.
 112  *
 113  * <p>
 114  * This is a <a href="{@docRoot}/java/lang/doc-files/ValueBased.html">value-based</a>
 115  * class; use of identity-sensitive operations (including reference equality
 116  * ({@code ==}), identity hash code, or synchronization) on instances of
 117  * {@code LocalTime} may have unpredictable results and should be avoided.
 118  * The {@code equals} method should be used for comparisons.
 119  *
 120  * @implSpec
 121  * This class is immutable and thread-safe.
 122  *
 123  * @since 1.8
 124  */
 125 public final class LocalTime
 126         implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable {
 127 
 128     /**
 129      * The minimum supported {@code LocalTime}, '00:00'.
 130      * This is the time of midnight at the start of the day.
 131      */
 132     public static final LocalTime MIN;
 133     /**
 134      * The maximum supported {@code LocalTime}, '23:59:59.999999999'.
 135      * This is the time just before midnight at the end of the day.
 136      */
 137     public static final LocalTime MAX;
 138     /**
 139      * The time of midnight at the start of the day, '00:00'.
 140      */
 141     public static final LocalTime MIDNIGHT;
 142     /**
 143      * The time of noon in the middle of the day, '12:00'.
 144      */
 145     public static final LocalTime NOON;
 146     /**
 147      * Constants for the local time of each hour.
 148      */
 149     private static final LocalTime[] HOURS = new LocalTime[24];
 150     static {
 151         for (int i = 0; i < HOURS.length; i++) {
 152             HOURS[i] = new LocalTime(i, 0, 0, 0);
 153         }
 154         MIDNIGHT = HOURS[0];
 155         NOON = HOURS[12];
 156         MIN = HOURS[0];
 157         MAX = new LocalTime(23, 59, 59, 999_999_999);
 158     }
 159 
 160     /**
 161      * Hours per day.
 162      */
 163     static final int HOURS_PER_DAY = 24;
 164     /**
 165      * Minutes per hour.
 166      */
 167     static final int MINUTES_PER_HOUR = 60;
 168     /**
 169      * Minutes per day.
 170      */
 171     static final int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY;
 172     /**
 173      * Seconds per minute.
 174      */
 175     static final int SECONDS_PER_MINUTE = 60;
 176     /**
 177      * Seconds per hour.
 178      */
 179     static final int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR;
 180     /**
 181      * Seconds per day.
 182      */
 183     static final int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY;
 184     /**
 185      * Milliseconds per day.
 186      */
 187     static final long MILLIS_PER_DAY = SECONDS_PER_DAY * 1000L;
 188     /**
 189      * Microseconds per day.
 190      */
 191     static final long MICROS_PER_DAY = SECONDS_PER_DAY * 1000_000L;
 192     /**
 193      * Nanos per millisecond.
 194      */
 195     static final long NANOS_PER_MILLI = 1000_000L;
 196     /**
 197      * Nanos per second.
 198      */
 199     static final long NANOS_PER_SECOND =  1000_000_000L;
 200     /**
 201      * Nanos per minute.
 202      */
 203     static final long NANOS_PER_MINUTE = NANOS_PER_SECOND * SECONDS_PER_MINUTE;
 204     /**
 205      * Nanos per hour.
 206      */
 207     static final long NANOS_PER_HOUR = NANOS_PER_MINUTE * MINUTES_PER_HOUR;
 208     /**
 209      * Nanos per day.
 210      */
 211     static final long NANOS_PER_DAY = NANOS_PER_HOUR * HOURS_PER_DAY;
 212 
 213     /**
 214      * Serialization version.
 215      */
 216     private static final long serialVersionUID = 6414437269572265201L;
 217 
 218     /**
 219      * The hour.
 220      */
 221     private final byte hour;
 222     /**
 223      * The minute.
 224      */
 225     private final byte minute;
 226     /**
 227      * The second.
 228      */
 229     private final byte second;
 230     /**
 231      * The nanosecond.
 232      */
 233     private final int nano;
 234 
 235     //-----------------------------------------------------------------------
 236     /**
 237      * Obtains the current time from the system clock in the default time-zone.
 238      * <p>
 239      * This will query the {@link Clock#systemDefaultZone() system clock} in the default
 240      * time-zone to obtain the current time.
 241      * <p>
 242      * Using this method will prevent the ability to use an alternate clock for testing
 243      * because the clock is hard-coded.
 244      *
 245      * @return the current time using the system clock and default time-zone, not null
 246      */
 247     public static LocalTime now() {
 248         return now(Clock.systemDefaultZone());
 249     }
 250 
 251     /**
 252      * Obtains the current time from the system clock in the specified time-zone.
 253      * <p>
 254      * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current time.
 255      * Specifying the time-zone avoids dependence on the default time-zone.
 256      * <p>
 257      * Using this method will prevent the ability to use an alternate clock for testing
 258      * because the clock is hard-coded.
 259      *
 260      * @param zone  the zone ID to use, not null
 261      * @return the current time using the system clock, not null
 262      */
 263     public static LocalTime now(ZoneId zone) {
 264         return now(Clock.system(zone));
 265     }
 266 
 267     /**
 268      * Obtains the current time from the specified clock.
 269      * <p>
 270      * This will query the specified clock to obtain the current time.
 271      * Using this method allows the use of an alternate clock for testing.
 272      * The alternate clock may be introduced using {@link Clock dependency injection}.
 273      *
 274      * @param clock  the clock to use, not null
 275      * @return the current time, not null
 276      */
 277     public static LocalTime now(Clock clock) {
 278         Objects.requireNonNull(clock, "clock");
 279         final Instant now = clock.instant();  // called once
 280         return ofInstant(now, clock.getZone());
 281     }
 282 
 283     //-----------------------------------------------------------------------
 284     /**
 285      * Obtains an instance of {@code LocalTime} from an hour and minute.
 286      * <p>
 287      * This returns a {@code LocalTime} with the specified hour and minute.
 288      * The second and nanosecond fields will be set to zero.
 289      *
 290      * @param hour  the hour-of-day to represent, from 0 to 23
 291      * @param minute  the minute-of-hour to represent, from 0 to 59
 292      * @return the local time, not null
 293      * @throws DateTimeException if the value of any field is out of range
 294      */
 295     public static LocalTime of(int hour, int minute) {
 296         HOUR_OF_DAY.checkValidValue(hour);
 297         if (minute == 0) {
 298             return HOURS[hour];  // for performance
 299         }
 300         MINUTE_OF_HOUR.checkValidValue(minute);
 301         return new LocalTime(hour, minute, 0, 0);
 302     }
 303 
 304     /**
 305      * Obtains an instance of {@code LocalTime} from an hour, minute and second.
 306      * <p>
 307      * This returns a {@code LocalTime} with the specified hour, minute and second.
 308      * The nanosecond field will be set to zero.
 309      *
 310      * @param hour  the hour-of-day to represent, from 0 to 23
 311      * @param minute  the minute-of-hour to represent, from 0 to 59
 312      * @param second  the second-of-minute to represent, from 0 to 59
 313      * @return the local time, not null
 314      * @throws DateTimeException if the value of any field is out of range
 315      */
 316     public static LocalTime of(int hour, int minute, int second) {
 317         HOUR_OF_DAY.checkValidValue(hour);
 318         if ((minute | second) == 0) {
 319             return HOURS[hour];  // for performance
 320         }
 321         MINUTE_OF_HOUR.checkValidValue(minute);
 322         SECOND_OF_MINUTE.checkValidValue(second);
 323         return new LocalTime(hour, minute, second, 0);
 324     }
 325 
 326     /**
 327      * Obtains an instance of {@code LocalTime} from an hour, minute, second and nanosecond.
 328      * <p>
 329      * This returns a {@code LocalTime} with the specified hour, minute, second and nanosecond.
 330      *
 331      * @param hour  the hour-of-day to represent, from 0 to 23
 332      * @param minute  the minute-of-hour to represent, from 0 to 59
 333      * @param second  the second-of-minute to represent, from 0 to 59
 334      * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
 335      * @return the local time, not null
 336      * @throws DateTimeException if the value of any field is out of range
 337      */
 338     public static LocalTime of(int hour, int minute, int second, int nanoOfSecond) {
 339         HOUR_OF_DAY.checkValidValue(hour);
 340         MINUTE_OF_HOUR.checkValidValue(minute);
 341         SECOND_OF_MINUTE.checkValidValue(second);
 342         NANO_OF_SECOND.checkValidValue(nanoOfSecond);
 343         return create(hour, minute, second, nanoOfSecond);
 344     }
 345 
 346     /**
 347      * Obtains an instance of {@code LocalTime} from an {@code Instant} and zone ID.
 348      * <p>
 349      * This creates a local time based on the specified instant.
 350      * First, the offset from UTC/Greenwich is obtained using the zone ID and instant,
 351      * which is simple as there is only one valid offset for each instant.
 352      * Then, the instant and offset are used to calculate the local time.
 353      *
 354      * @param instant  the instant to create the time from, not null
 355      * @param zone  the time-zone, which may be an offset, not null
 356      * @return the local time, not null
 357      */
 358      public static LocalTime ofInstant(Instant instant, ZoneId zone) {
 359          Objects.requireNonNull(instant, "instant");
 360          Objects.requireNonNull(zone, "zone");
 361          ZoneOffset offset = zone.getRules().getOffset(instant);
 362          long localSecond = instant.getEpochSecond() + offset.getTotalSeconds();
 363          int secsOfDay = (int) Math.floorMod(localSecond, SECONDS_PER_DAY);
 364          return ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + instant.getNano());
 365      }
 366 
 367     //-----------------------------------------------------------------------
 368     /**
 369      * Obtains an instance of {@code LocalTime} from a second-of-day value.
 370      * <p>
 371      * This returns a {@code LocalTime} with the specified second-of-day.
 372      * The nanosecond field will be set to zero.
 373      *
 374      * @param secondOfDay  the second-of-day, from {@code 0} to {@code 24 * 60 * 60 - 1}
 375      * @return the local time, not null
 376      * @throws DateTimeException if the second-of-day value is invalid
 377      */
 378     public static LocalTime ofSecondOfDay(long secondOfDay) {
 379         SECOND_OF_DAY.checkValidValue(secondOfDay);
 380         int hours = (int) (secondOfDay / SECONDS_PER_HOUR);
 381         secondOfDay -= hours * SECONDS_PER_HOUR;
 382         int minutes = (int) (secondOfDay / SECONDS_PER_MINUTE);
 383         secondOfDay -= minutes * SECONDS_PER_MINUTE;
 384         return create(hours, minutes, (int) secondOfDay, 0);
 385     }
 386 
 387     /**
 388      * Obtains an instance of {@code LocalTime} from a nanos-of-day value.
 389      * <p>
 390      * This returns a {@code LocalTime} with the specified nanosecond-of-day.
 391      *
 392      * @param nanoOfDay  the nano of day, from {@code 0} to {@code 24 * 60 * 60 * 1,000,000,000 - 1}
 393      * @return the local time, not null
 394      * @throws DateTimeException if the nanos of day value is invalid
 395      */
 396     public static LocalTime ofNanoOfDay(long nanoOfDay) {
 397         NANO_OF_DAY.checkValidValue(nanoOfDay);
 398         int hours = (int) (nanoOfDay / NANOS_PER_HOUR);
 399         nanoOfDay -= hours * NANOS_PER_HOUR;
 400         int minutes = (int) (nanoOfDay / NANOS_PER_MINUTE);
 401         nanoOfDay -= minutes * NANOS_PER_MINUTE;
 402         int seconds = (int) (nanoOfDay / NANOS_PER_SECOND);
 403         nanoOfDay -= seconds * NANOS_PER_SECOND;
 404         return create(hours, minutes, seconds, (int) nanoOfDay);
 405     }
 406 
 407     //-----------------------------------------------------------------------
 408     /**
 409      * Obtains an instance of {@code LocalTime} from a temporal object.
 410      * <p>
 411      * This obtains a local time based on the specified temporal.
 412      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 413      * which this factory converts to an instance of {@code LocalTime}.
 414      * <p>
 415      * The conversion uses the {@link TemporalQueries#localTime()} query, which relies
 416      * on extracting the {@link ChronoField#NANO_OF_DAY NANO_OF_DAY} field.
 417      * <p>
 418      * This method matches the signature of the functional interface {@link TemporalQuery}
 419      * allowing it to be used as a query via method reference, {@code LocalTime::from}.
 420      *
 421      * @param temporal  the temporal object to convert, not null
 422      * @return the local time, not null
 423      * @throws DateTimeException if unable to convert to a {@code LocalTime}
 424      */
 425     public static LocalTime from(TemporalAccessor temporal) {
 426         Objects.requireNonNull(temporal, "temporal");
 427         LocalTime time = temporal.query(TemporalQueries.localTime());
 428         if (time == null) {
 429             throw new DateTimeException("Unable to obtain LocalTime from TemporalAccessor: " +
 430                     temporal + " of type " + temporal.getClass().getName());
 431         }
 432         return time;
 433     }
 434 
 435     //-----------------------------------------------------------------------
 436     /**
 437      * Obtains an instance of {@code LocalTime} from a text string such as {@code 10:15}.
 438      * <p>
 439      * The string must represent a valid time and is parsed using
 440      * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_TIME}.
 441      *
 442      * @param text  the text to parse such as "10:15:30", not null
 443      * @return the parsed local time, not null
 444      * @throws DateTimeParseException if the text cannot be parsed
 445      */
 446     public static LocalTime parse(CharSequence text) {
 447         return parse(text, DateTimeFormatter.ISO_LOCAL_TIME);
 448     }
 449 
 450     /**
 451      * Obtains an instance of {@code LocalTime} from a text string using a specific formatter.
 452      * <p>
 453      * The text is parsed using the formatter, returning a time.
 454      *
 455      * @param text  the text to parse, not null
 456      * @param formatter  the formatter to use, not null
 457      * @return the parsed local time, not null
 458      * @throws DateTimeParseException if the text cannot be parsed
 459      */
 460     public static LocalTime parse(CharSequence text, DateTimeFormatter formatter) {
 461         Objects.requireNonNull(formatter, "formatter");
 462         return formatter.parse(text, LocalTime::from);
 463     }
 464 
 465     //-----------------------------------------------------------------------
 466     /**
 467      * Creates a local time from the hour, minute, second and nanosecond fields.
 468      * <p>
 469      * This factory may return a cached value, but applications must not rely on this.
 470      *
 471      * @param hour  the hour-of-day to represent, validated from 0 to 23
 472      * @param minute  the minute-of-hour to represent, validated from 0 to 59
 473      * @param second  the second-of-minute to represent, validated from 0 to 59
 474      * @param nanoOfSecond  the nano-of-second to represent, validated from 0 to 999,999,999
 475      * @return the local time, not null
 476      */
 477     private static LocalTime create(int hour, int minute, int second, int nanoOfSecond) {
 478         if ((minute | second | nanoOfSecond) == 0) {
 479             return HOURS[hour];
 480         }
 481         return new LocalTime(hour, minute, second, nanoOfSecond);
 482     }
 483 
 484     /**
 485      * Constructor, previously validated.
 486      *
 487      * @param hour  the hour-of-day to represent, validated from 0 to 23
 488      * @param minute  the minute-of-hour to represent, validated from 0 to 59
 489      * @param second  the second-of-minute to represent, validated from 0 to 59
 490      * @param nanoOfSecond  the nano-of-second to represent, validated from 0 to 999,999,999
 491      */
 492     private LocalTime(int hour, int minute, int second, int nanoOfSecond) {
 493         this.hour = (byte) hour;
 494         this.minute = (byte) minute;
 495         this.second = (byte) second;
 496         this.nano = nanoOfSecond;
 497     }
 498 
 499     //-----------------------------------------------------------------------
 500     /**
 501      * Checks if the specified field is supported.
 502      * <p>
 503      * This checks if this time can be queried for the specified field.
 504      * If false, then calling the {@link #range(TemporalField) range},
 505      * {@link #get(TemporalField) get} and {@link #with(TemporalField, long)}
 506      * methods will throw an exception.
 507      * <p>
 508      * If the field is a {@link ChronoField} then the query is implemented here.
 509      * The supported fields are:
 510      * <ul>
 511      * <li>{@code NANO_OF_SECOND}
 512      * <li>{@code NANO_OF_DAY}
 513      * <li>{@code MICRO_OF_SECOND}
 514      * <li>{@code MICRO_OF_DAY}
 515      * <li>{@code MILLI_OF_SECOND}
 516      * <li>{@code MILLI_OF_DAY}
 517      * <li>{@code SECOND_OF_MINUTE}
 518      * <li>{@code SECOND_OF_DAY}
 519      * <li>{@code MINUTE_OF_HOUR}
 520      * <li>{@code MINUTE_OF_DAY}
 521      * <li>{@code HOUR_OF_AMPM}
 522      * <li>{@code CLOCK_HOUR_OF_AMPM}
 523      * <li>{@code HOUR_OF_DAY}
 524      * <li>{@code CLOCK_HOUR_OF_DAY}
 525      * <li>{@code AMPM_OF_DAY}
 526      * </ul>
 527      * All other {@code ChronoField} instances will return false.
 528      * <p>
 529      * If the field is not a {@code ChronoField}, then the result of this method
 530      * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
 531      * passing {@code this} as the argument.
 532      * Whether the field is supported is determined by the field.
 533      *
 534      * @param field  the field to check, null returns false
 535      * @return true if the field is supported on this time, false if not
 536      */
 537     @Override
 538     public boolean isSupported(TemporalField field) {
 539         if (field instanceof ChronoField) {
 540             return field.isTimeBased();
 541         }
 542         return field != null && field.isSupportedBy(this);
 543     }
 544 
 545     /**
 546      * Checks if the specified unit is supported.
 547      * <p>
 548      * This checks if the specified unit can be added to, or subtracted from, this time.
 549      * If false, then calling the {@link #plus(long, TemporalUnit)} and
 550      * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
 551      * <p>
 552      * If the unit is a {@link ChronoUnit} then the query is implemented here.
 553      * The supported units are:
 554      * <ul>
 555      * <li>{@code NANOS}
 556      * <li>{@code MICROS}
 557      * <li>{@code MILLIS}
 558      * <li>{@code SECONDS}
 559      * <li>{@code MINUTES}
 560      * <li>{@code HOURS}
 561      * <li>{@code HALF_DAYS}
 562      * </ul>
 563      * All other {@code ChronoUnit} instances will return false.
 564      * <p>
 565      * If the unit is not a {@code ChronoUnit}, then the result of this method
 566      * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
 567      * passing {@code this} as the argument.
 568      * Whether the unit is supported is determined by the unit.
 569      *
 570      * @param unit  the unit to check, null returns false
 571      * @return true if the unit can be added/subtracted, false if not
 572      */
 573     @Override  // override for Javadoc
 574     public boolean isSupported(TemporalUnit unit) {
 575         if (unit instanceof ChronoUnit) {
 576             return unit.isTimeBased();
 577         }
 578         return unit != null && unit.isSupportedBy(this);
 579     }
 580 
 581     //-----------------------------------------------------------------------
 582     /**
 583      * Gets the range of valid values for the specified field.
 584      * <p>
 585      * The range object expresses the minimum and maximum valid values for a field.
 586      * This time is used to enhance the accuracy of the returned range.
 587      * If it is not possible to return the range, because the field is not supported
 588      * or for some other reason, an exception is thrown.
 589      * <p>
 590      * If the field is a {@link ChronoField} then the query is implemented here.
 591      * The {@link #isSupported(TemporalField) supported fields} will return
 592      * appropriate range instances.
 593      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 594      * <p>
 595      * If the field is not a {@code ChronoField}, then the result of this method
 596      * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
 597      * passing {@code this} as the argument.
 598      * Whether the range can be obtained is determined by the field.
 599      *
 600      * @param field  the field to query the range for, not null
 601      * @return the range of valid values for the field, not null
 602      * @throws DateTimeException if the range for the field cannot be obtained
 603      * @throws UnsupportedTemporalTypeException if the field is not supported
 604      */
 605     @Override  // override for Javadoc
 606     public ValueRange range(TemporalField field) {
 607         return Temporal.super.range(field);
 608     }
 609 
 610     /**
 611      * Gets the value of the specified field from this time as an {@code int}.
 612      * <p>
 613      * This queries this time for the value of the specified field.
 614      * The returned value will always be within the valid range of values for the field.
 615      * If it is not possible to return the value, because the field is not supported
 616      * or for some other reason, an exception is thrown.
 617      * <p>
 618      * If the field is a {@link ChronoField} then the query is implemented here.
 619      * The {@link #isSupported(TemporalField) supported fields} will return valid
 620      * values based on this time, except {@code NANO_OF_DAY} and {@code MICRO_OF_DAY}
 621      * which are too large to fit in an {@code int} and throw a {@code UnsupportedTemporalTypeException}.
 622      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 623      * <p>
 624      * If the field is not a {@code ChronoField}, then the result of this method
 625      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 626      * passing {@code this} as the argument. Whether the value can be obtained,
 627      * and what the value represents, is determined by the field.
 628      *
 629      * @param field  the field to get, not null
 630      * @return the value for the field
 631      * @throws DateTimeException if a value for the field cannot be obtained or
 632      *         the value is outside the range of valid values for the field
 633      * @throws UnsupportedTemporalTypeException if the field is not supported or
 634      *         the range of values exceeds an {@code int}
 635      * @throws ArithmeticException if numeric overflow occurs
 636      */
 637     @Override  // override for Javadoc and performance
 638     public int get(TemporalField field) {
 639         if (field instanceof ChronoField) {
 640             return get0(field);
 641         }
 642         return Temporal.super.get(field);
 643     }
 644 
 645     /**
 646      * Gets the value of the specified field from this time as a {@code long}.
 647      * <p>
 648      * This queries this time for the value of the specified field.
 649      * If it is not possible to return the value, because the field is not supported
 650      * or for some other reason, an exception is thrown.
 651      * <p>
 652      * If the field is a {@link ChronoField} then the query is implemented here.
 653      * The {@link #isSupported(TemporalField) supported fields} will return valid
 654      * values based on this time.
 655      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 656      * <p>
 657      * If the field is not a {@code ChronoField}, then the result of this method
 658      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 659      * passing {@code this} as the argument. Whether the value can be obtained,
 660      * and what the value represents, is determined by the field.
 661      *
 662      * @param field  the field to get, not null
 663      * @return the value for the field
 664      * @throws DateTimeException if a value for the field cannot be obtained
 665      * @throws UnsupportedTemporalTypeException if the field is not supported
 666      * @throws ArithmeticException if numeric overflow occurs
 667      */
 668     @Override
 669     public long getLong(TemporalField field) {
 670         if (field instanceof ChronoField) {
 671             if (field == NANO_OF_DAY) {
 672                 return toNanoOfDay();
 673             }
 674             if (field == MICRO_OF_DAY) {
 675                 return toNanoOfDay() / 1000;
 676             }
 677             return get0(field);
 678         }
 679         return field.getFrom(this);
 680     }
 681 
 682     private int get0(TemporalField field) {
 683         switch ((ChronoField) field) {
 684             case NANO_OF_SECOND: return nano;
 685             case NANO_OF_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'NanoOfDay' for get() method, use getLong() instead");
 686             case MICRO_OF_SECOND: return nano / 1000;
 687             case MICRO_OF_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'MicroOfDay' for get() method, use getLong() instead");
 688             case MILLI_OF_SECOND: return nano / 1000_000;
 689             case MILLI_OF_DAY: return (int) (toNanoOfDay() / 1000_000);
 690             case SECOND_OF_MINUTE: return second;
 691             case SECOND_OF_DAY: return toSecondOfDay();
 692             case MINUTE_OF_HOUR: return minute;
 693             case MINUTE_OF_DAY: return hour * 60 + minute;
 694             case HOUR_OF_AMPM: return hour % 12;
 695             case CLOCK_HOUR_OF_AMPM: int ham = hour % 12; return (ham % 12 == 0 ? 12 : ham);
 696             case HOUR_OF_DAY: return hour;
 697             case CLOCK_HOUR_OF_DAY: return (hour == 0 ? 24 : hour);
 698             case AMPM_OF_DAY: return hour / 12;
 699         }
 700         throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
 701     }
 702 
 703     //-----------------------------------------------------------------------
 704     /**
 705      * Gets the hour-of-day field.
 706      *
 707      * @return the hour-of-day, from 0 to 23
 708      */
 709     public int getHour() {
 710         return hour;
 711     }
 712 
 713     /**
 714      * Gets the minute-of-hour field.
 715      *
 716      * @return the minute-of-hour, from 0 to 59
 717      */
 718     public int getMinute() {
 719         return minute;
 720     }
 721 
 722     /**
 723      * Gets the second-of-minute field.
 724      *
 725      * @return the second-of-minute, from 0 to 59
 726      */
 727     public int getSecond() {
 728         return second;
 729     }
 730 
 731     /**
 732      * Gets the nano-of-second field.
 733      *
 734      * @return the nano-of-second, from 0 to 999,999,999
 735      */
 736     public int getNano() {
 737         return nano;
 738     }
 739 
 740     //-----------------------------------------------------------------------
 741     /**
 742      * Returns an adjusted copy of this time.
 743      * <p>
 744      * This returns a {@code LocalTime}, based on this one, with the time adjusted.
 745      * The adjustment takes place using the specified adjuster strategy object.
 746      * Read the documentation of the adjuster to understand what adjustment will be made.
 747      * <p>
 748      * A simple adjuster might simply set the one of the fields, such as the hour field.
 749      * A more complex adjuster might set the time to the last hour of the day.
 750      * <p>
 751      * The result of this method is obtained by invoking the
 752      * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
 753      * specified adjuster passing {@code this} as the argument.
 754      * <p>
 755      * This instance is immutable and unaffected by this method call.
 756      *
 757      * @param adjuster the adjuster to use, not null
 758      * @return a {@code LocalTime} based on {@code this} with the adjustment made, not null
 759      * @throws DateTimeException if the adjustment cannot be made
 760      * @throws ArithmeticException if numeric overflow occurs
 761      */
 762     @Override
 763     public LocalTime with(TemporalAdjuster adjuster) {
 764         // optimizations
 765         if (adjuster instanceof LocalTime) {
 766             return (LocalTime) adjuster;
 767         }
 768         return (LocalTime) adjuster.adjustInto(this);
 769     }
 770 
 771     /**
 772      * Returns a copy of this time with the specified field set to a new value.
 773      * <p>
 774      * This returns a {@code LocalTime}, based on this one, with the value
 775      * for the specified field changed.
 776      * This can be used to change any supported field, such as the hour, minute or second.
 777      * If it is not possible to set the value, because the field is not supported or for
 778      * some other reason, an exception is thrown.
 779      * <p>
 780      * If the field is a {@link ChronoField} then the adjustment is implemented here.
 781      * The supported fields behave as follows:
 782      * <ul>
 783      * <li>{@code NANO_OF_SECOND} -
 784      *  Returns a {@code LocalTime} with the specified nano-of-second.
 785      *  The hour, minute and second will be unchanged.
 786      * <li>{@code NANO_OF_DAY} -
 787      *  Returns a {@code LocalTime} with the specified nano-of-day.
 788      *  This completely replaces the time and is equivalent to {@link #ofNanoOfDay(long)}.
 789      * <li>{@code MICRO_OF_SECOND} -
 790      *  Returns a {@code LocalTime} with the nano-of-second replaced by the specified
 791      *  micro-of-second multiplied by 1,000.
 792      *  The hour, minute and second will be unchanged.
 793      * <li>{@code MICRO_OF_DAY} -
 794      *  Returns a {@code LocalTime} with the specified micro-of-day.
 795      *  This completely replaces the time and is equivalent to using {@link #ofNanoOfDay(long)}
 796      *  with the micro-of-day multiplied by 1,000.
 797      * <li>{@code MILLI_OF_SECOND} -
 798      *  Returns a {@code LocalTime} with the nano-of-second replaced by the specified
 799      *  milli-of-second multiplied by 1,000,000.
 800      *  The hour, minute and second will be unchanged.
 801      * <li>{@code MILLI_OF_DAY} -
 802      *  Returns a {@code LocalTime} with the specified milli-of-day.
 803      *  This completely replaces the time and is equivalent to using {@link #ofNanoOfDay(long)}
 804      *  with the milli-of-day multiplied by 1,000,000.
 805      * <li>{@code SECOND_OF_MINUTE} -
 806      *  Returns a {@code LocalTime} with the specified second-of-minute.
 807      *  The hour, minute and nano-of-second will be unchanged.
 808      * <li>{@code SECOND_OF_DAY} -
 809      *  Returns a {@code LocalTime} with the specified second-of-day.
 810      *  The nano-of-second will be unchanged.
 811      * <li>{@code MINUTE_OF_HOUR} -
 812      *  Returns a {@code LocalTime} with the specified minute-of-hour.
 813      *  The hour, second-of-minute and nano-of-second will be unchanged.
 814      * <li>{@code MINUTE_OF_DAY} -
 815      *  Returns a {@code LocalTime} with the specified minute-of-day.
 816      *  The second-of-minute and nano-of-second will be unchanged.
 817      * <li>{@code HOUR_OF_AMPM} -
 818      *  Returns a {@code LocalTime} with the specified hour-of-am-pm.
 819      *  The AM/PM, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
 820      * <li>{@code CLOCK_HOUR_OF_AMPM} -
 821      *  Returns a {@code LocalTime} with the specified clock-hour-of-am-pm.
 822      *  The AM/PM, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
 823      * <li>{@code HOUR_OF_DAY} -
 824      *  Returns a {@code LocalTime} with the specified hour-of-day.
 825      *  The minute-of-hour, second-of-minute and nano-of-second will be unchanged.
 826      * <li>{@code CLOCK_HOUR_OF_DAY} -
 827      *  Returns a {@code LocalTime} with the specified clock-hour-of-day.
 828      *  The minute-of-hour, second-of-minute and nano-of-second will be unchanged.
 829      * <li>{@code AMPM_OF_DAY} -
 830      *  Returns a {@code LocalTime} with the specified AM/PM.
 831      *  The hour-of-am-pm, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
 832      * </ul>
 833      * <p>
 834      * In all cases, if the new value is outside the valid range of values for the field
 835      * then a {@code DateTimeException} will be thrown.
 836      * <p>
 837      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 838      * <p>
 839      * If the field is not a {@code ChronoField}, then the result of this method
 840      * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
 841      * passing {@code this} as the argument. In this case, the field determines
 842      * whether and how to adjust the instant.
 843      * <p>
 844      * This instance is immutable and unaffected by this method call.
 845      *
 846      * @param field  the field to set in the result, not null
 847      * @param newValue  the new value of the field in the result
 848      * @return a {@code LocalTime} based on {@code this} with the specified field set, not null
 849      * @throws DateTimeException if the field cannot be set
 850      * @throws UnsupportedTemporalTypeException if the field is not supported
 851      * @throws ArithmeticException if numeric overflow occurs
 852      */
 853     @Override
 854     public LocalTime with(TemporalField field, long newValue) {
 855         if (field instanceof ChronoField) {
 856             ChronoField f = (ChronoField) field;
 857             f.checkValidValue(newValue);
 858             switch (f) {
 859                 case NANO_OF_SECOND: return withNano((int) newValue);
 860                 case NANO_OF_DAY: return LocalTime.ofNanoOfDay(newValue);
 861                 case MICRO_OF_SECOND: return withNano((int) newValue * 1000);
 862                 case MICRO_OF_DAY: return LocalTime.ofNanoOfDay(newValue * 1000);
 863                 case MILLI_OF_SECOND: return withNano((int) newValue * 1000_000);
 864                 case MILLI_OF_DAY: return LocalTime.ofNanoOfDay(newValue * 1000_000);
 865                 case SECOND_OF_MINUTE: return withSecond((int) newValue);
 866                 case SECOND_OF_DAY: return plusSeconds(newValue - toSecondOfDay());
 867                 case MINUTE_OF_HOUR: return withMinute((int) newValue);
 868                 case MINUTE_OF_DAY: return plusMinutes(newValue - (hour * 60 + minute));
 869                 case HOUR_OF_AMPM: return plusHours(newValue - (hour % 12));
 870                 case CLOCK_HOUR_OF_AMPM: return plusHours((newValue == 12 ? 0 : newValue) - (hour % 12));
 871                 case HOUR_OF_DAY: return withHour((int) newValue);
 872                 case CLOCK_HOUR_OF_DAY: return withHour((int) (newValue == 24 ? 0 : newValue));
 873                 case AMPM_OF_DAY: return plusHours((newValue - (hour / 12)) * 12);
 874             }
 875             throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
 876         }
 877         return field.adjustInto(this, newValue);
 878     }
 879 
 880     //-----------------------------------------------------------------------
 881     /**
 882      * Returns a copy of this {@code LocalTime} with the hour-of-day altered.
 883      * <p>
 884      * This instance is immutable and unaffected by this method call.
 885      *
 886      * @param hour  the hour-of-day to set in the result, from 0 to 23
 887      * @return a {@code LocalTime} based on this time with the requested hour, not null
 888      * @throws DateTimeException if the hour value is invalid
 889      */
 890     public LocalTime withHour(int hour) {
 891         if (this.hour == hour) {
 892             return this;
 893         }
 894         HOUR_OF_DAY.checkValidValue(hour);
 895         return create(hour, minute, second, nano);
 896     }
 897 
 898     /**
 899      * Returns a copy of this {@code LocalTime} with the minute-of-hour altered.
 900      * <p>
 901      * This instance is immutable and unaffected by this method call.
 902      *
 903      * @param minute  the minute-of-hour to set in the result, from 0 to 59
 904      * @return a {@code LocalTime} based on this time with the requested minute, not null
 905      * @throws DateTimeException if the minute value is invalid
 906      */
 907     public LocalTime withMinute(int minute) {
 908         if (this.minute == minute) {
 909             return this;
 910         }
 911         MINUTE_OF_HOUR.checkValidValue(minute);
 912         return create(hour, minute, second, nano);
 913     }
 914 
 915     /**
 916      * Returns a copy of this {@code LocalTime} with the second-of-minute altered.
 917      * <p>
 918      * This instance is immutable and unaffected by this method call.
 919      *
 920      * @param second  the second-of-minute to set in the result, from 0 to 59
 921      * @return a {@code LocalTime} based on this time with the requested second, not null
 922      * @throws DateTimeException if the second value is invalid
 923      */
 924     public LocalTime withSecond(int second) {
 925         if (this.second == second) {
 926             return this;
 927         }
 928         SECOND_OF_MINUTE.checkValidValue(second);
 929         return create(hour, minute, second, nano);
 930     }
 931 
 932     /**
 933      * Returns a copy of this {@code LocalTime} with the nano-of-second altered.
 934      * <p>
 935      * This instance is immutable and unaffected by this method call.
 936      *
 937      * @param nanoOfSecond  the nano-of-second to set in the result, from 0 to 999,999,999
 938      * @return a {@code LocalTime} based on this time with the requested nanosecond, not null
 939      * @throws DateTimeException if the nanos value is invalid
 940      */
 941     public LocalTime withNano(int nanoOfSecond) {
 942         if (this.nano == nanoOfSecond) {
 943             return this;
 944         }
 945         NANO_OF_SECOND.checkValidValue(nanoOfSecond);
 946         return create(hour, minute, second, nanoOfSecond);
 947     }
 948 
 949     //-----------------------------------------------------------------------
 950     /**
 951      * Returns a copy of this {@code LocalTime} with the time truncated.
 952      * <p>
 953      * Truncation returns a copy of the original time with fields
 954      * smaller than the specified unit set to zero.
 955      * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
 956      * will set the second-of-minute and nano-of-second field to zero.
 957      * <p>
 958      * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 959      * that divides into the length of a standard day without remainder.
 960      * This includes all supplied time units on {@link ChronoUnit} and
 961      * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 962      * <p>
 963      * This instance is immutable and unaffected by this method call.
 964      *
 965      * @param unit  the unit to truncate to, not null
 966      * @return a {@code LocalTime} based on this time with the time truncated, not null
 967      * @throws DateTimeException if unable to truncate
 968      * @throws UnsupportedTemporalTypeException if the unit is not supported
 969      */
 970     public LocalTime truncatedTo(TemporalUnit unit) {
 971         if (unit == ChronoUnit.NANOS) {
 972             return this;
 973         }
 974         Duration unitDur = unit.getDuration();
 975         if (unitDur.getSeconds() > SECONDS_PER_DAY) {
 976             throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
 977         }
 978         long dur = unitDur.toNanos();
 979         if ((NANOS_PER_DAY % dur) != 0) {
 980             throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
 981         }
 982         long nod = toNanoOfDay();
 983         return ofNanoOfDay((nod / dur) * dur);
 984     }
 985 
 986     //-----------------------------------------------------------------------
 987     /**
 988      * Returns a copy of this time with the specified amount added.
 989      * <p>
 990      * This returns a {@code LocalTime}, based on this one, with the specified amount added.
 991      * The amount is typically {@link Duration} but may be any other type implementing
 992      * the {@link TemporalAmount} interface.
 993      * <p>
 994      * The calculation is delegated to the amount object by calling
 995      * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
 996      * to implement the addition in any way it wishes, however it typically
 997      * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
 998      * of the amount implementation to determine if it can be successfully added.
 999      * <p>
1000      * This instance is immutable and unaffected by this method call.
1001      *
1002      * @param amountToAdd  the amount to add, not null
1003      * @return a {@code LocalTime} based on this time with the addition made, not null
1004      * @throws DateTimeException if the addition cannot be made
1005      * @throws ArithmeticException if numeric overflow occurs
1006      */
1007     @Override
1008     public LocalTime plus(TemporalAmount amountToAdd) {
1009         return (LocalTime) amountToAdd.addTo(this);
1010     }
1011 
1012     /**
1013      * Returns a copy of this time with the specified amount added.
1014      * <p>
1015      * This returns a {@code LocalTime}, based on this one, with the amount
1016      * in terms of the unit added. If it is not possible to add the amount, because the
1017      * unit is not supported or for some other reason, an exception is thrown.
1018      * <p>
1019      * If the field is a {@link ChronoUnit} then the addition is implemented here.
1020      * The supported fields behave as follows:
1021      * <ul>
1022      * <li>{@code NANOS} -
1023      *  Returns a {@code LocalTime} with the specified number of nanoseconds added.
1024      *  This is equivalent to {@link #plusNanos(long)}.
1025      * <li>{@code MICROS} -
1026      *  Returns a {@code LocalTime} with the specified number of microseconds added.
1027      *  This is equivalent to {@link #plusNanos(long)} with the amount
1028      *  multiplied by 1,000.
1029      * <li>{@code MILLIS} -
1030      *  Returns a {@code LocalTime} with the specified number of milliseconds added.
1031      *  This is equivalent to {@link #plusNanos(long)} with the amount
1032      *  multiplied by 1,000,000.
1033      * <li>{@code SECONDS} -
1034      *  Returns a {@code LocalTime} with the specified number of seconds added.
1035      *  This is equivalent to {@link #plusSeconds(long)}.
1036      * <li>{@code MINUTES} -
1037      *  Returns a {@code LocalTime} with the specified number of minutes added.
1038      *  This is equivalent to {@link #plusMinutes(long)}.
1039      * <li>{@code HOURS} -
1040      *  Returns a {@code LocalTime} with the specified number of hours added.
1041      *  This is equivalent to {@link #plusHours(long)}.
1042      * <li>{@code HALF_DAYS} -
1043      *  Returns a {@code LocalTime} with the specified number of half-days added.
1044      *  This is equivalent to {@link #plusHours(long)} with the amount
1045      *  multiplied by 12.
1046      * </ul>
1047      * <p>
1048      * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}.
1049      * <p>
1050      * If the field is not a {@code ChronoUnit}, then the result of this method
1051      * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
1052      * passing {@code this} as the argument. In this case, the unit determines
1053      * whether and how to perform the addition.
1054      * <p>
1055      * This instance is immutable and unaffected by this method call.
1056      *
1057      * @param amountToAdd  the amount of the unit to add to the result, may be negative
1058      * @param unit  the unit of the amount to add, not null
1059      * @return a {@code LocalTime} based on this time with the specified amount added, not null
1060      * @throws DateTimeException if the addition cannot be made
1061      * @throws UnsupportedTemporalTypeException if the unit is not supported
1062      * @throws ArithmeticException if numeric overflow occurs
1063      */
1064     @Override
1065     public LocalTime plus(long amountToAdd, TemporalUnit unit) {
1066         if (unit instanceof ChronoUnit) {
1067             switch ((ChronoUnit) unit) {
1068                 case NANOS: return plusNanos(amountToAdd);
1069                 case MICROS: return plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
1070                 case MILLIS: return plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
1071                 case SECONDS: return plusSeconds(amountToAdd);
1072                 case MINUTES: return plusMinutes(amountToAdd);
1073                 case HOURS: return plusHours(amountToAdd);
1074                 case HALF_DAYS: return plusHours((amountToAdd % 2) * 12);
1075             }
1076             throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
1077         }
1078         return unit.addTo(this, amountToAdd);
1079     }
1080 
1081     //-----------------------------------------------------------------------
1082     /**
1083      * Returns a copy of this {@code LocalTime} with the specified number of hours added.
1084      * <p>
1085      * This adds the specified number of hours to this time, returning a new time.
1086      * The calculation wraps around midnight.
1087      * <p>
1088      * This instance is immutable and unaffected by this method call.
1089      *
1090      * @param hoursToAdd  the hours to add, may be negative
1091      * @return a {@code LocalTime} based on this time with the hours added, not null
1092      */
1093     public LocalTime plusHours(long hoursToAdd) {
1094         if (hoursToAdd == 0) {
1095             return this;
1096         }
1097         int newHour = ((int) (hoursToAdd % HOURS_PER_DAY) + hour + HOURS_PER_DAY) % HOURS_PER_DAY;
1098         return create(newHour, minute, second, nano);
1099     }
1100 
1101     /**
1102      * Returns a copy of this {@code LocalTime} with the specified number of minutes added.
1103      * <p>
1104      * This adds the specified number of minutes to this time, returning a new time.
1105      * The calculation wraps around midnight.
1106      * <p>
1107      * This instance is immutable and unaffected by this method call.
1108      *
1109      * @param minutesToAdd  the minutes to add, may be negative
1110      * @return a {@code LocalTime} based on this time with the minutes added, not null
1111      */
1112     public LocalTime plusMinutes(long minutesToAdd) {
1113         if (minutesToAdd == 0) {
1114             return this;
1115         }
1116         int mofd = hour * MINUTES_PER_HOUR + minute;
1117         int newMofd = ((int) (minutesToAdd % MINUTES_PER_DAY) + mofd + MINUTES_PER_DAY) % MINUTES_PER_DAY;
1118         if (mofd == newMofd) {
1119             return this;
1120         }
1121         int newHour = newMofd / MINUTES_PER_HOUR;
1122         int newMinute = newMofd % MINUTES_PER_HOUR;
1123         return create(newHour, newMinute, second, nano);
1124     }
1125 
1126     /**
1127      * Returns a copy of this {@code LocalTime} with the specified number of seconds added.
1128      * <p>
1129      * This adds the specified number of seconds to this time, returning a new time.
1130      * The calculation wraps around midnight.
1131      * <p>
1132      * This instance is immutable and unaffected by this method call.
1133      *
1134      * @param secondstoAdd  the seconds to add, may be negative
1135      * @return a {@code LocalTime} based on this time with the seconds added, not null
1136      */
1137     public LocalTime plusSeconds(long secondstoAdd) {
1138         if (secondstoAdd == 0) {
1139             return this;
1140         }
1141         int sofd = hour * SECONDS_PER_HOUR +
1142                     minute * SECONDS_PER_MINUTE + second;
1143         int newSofd = ((int) (secondstoAdd % SECONDS_PER_DAY) + sofd + SECONDS_PER_DAY) % SECONDS_PER_DAY;
1144         if (sofd == newSofd) {
1145             return this;
1146         }
1147         int newHour = newSofd / SECONDS_PER_HOUR;
1148         int newMinute = (newSofd / SECONDS_PER_MINUTE) % MINUTES_PER_HOUR;
1149         int newSecond = newSofd % SECONDS_PER_MINUTE;
1150         return create(newHour, newMinute, newSecond, nano);
1151     }
1152 
1153     /**
1154      * Returns a copy of this {@code LocalTime} with the specified number of nanoseconds added.
1155      * <p>
1156      * This adds the specified number of nanoseconds to this time, returning a new time.
1157      * The calculation wraps around midnight.
1158      * <p>
1159      * This instance is immutable and unaffected by this method call.
1160      *
1161      * @param nanosToAdd  the nanos to add, may be negative
1162      * @return a {@code LocalTime} based on this time with the nanoseconds added, not null
1163      */
1164     public LocalTime plusNanos(long nanosToAdd) {
1165         if (nanosToAdd == 0) {
1166             return this;
1167         }
1168         long nofd = toNanoOfDay();
1169         long newNofd = ((nanosToAdd % NANOS_PER_DAY) + nofd + NANOS_PER_DAY) % NANOS_PER_DAY;
1170         if (nofd == newNofd) {
1171             return this;
1172         }
1173         int newHour = (int) (newNofd / NANOS_PER_HOUR);
1174         int newMinute = (int) ((newNofd / NANOS_PER_MINUTE) % MINUTES_PER_HOUR);
1175         int newSecond = (int) ((newNofd / NANOS_PER_SECOND) % SECONDS_PER_MINUTE);
1176         int newNano = (int) (newNofd % NANOS_PER_SECOND);
1177         return create(newHour, newMinute, newSecond, newNano);
1178     }
1179 
1180     //-----------------------------------------------------------------------
1181     /**
1182      * Returns a copy of this time with the specified amount subtracted.
1183      * <p>
1184      * This returns a {@code LocalTime}, based on this one, with the specified amount subtracted.
1185      * The amount is typically {@link Duration} but may be any other type implementing
1186      * the {@link TemporalAmount} interface.
1187      * <p>
1188      * The calculation is delegated to the amount object by calling
1189      * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
1190      * to implement the subtraction in any way it wishes, however it typically
1191      * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
1192      * of the amount implementation to determine if it can be successfully subtracted.
1193      * <p>
1194      * This instance is immutable and unaffected by this method call.
1195      *
1196      * @param amountToSubtract  the amount to subtract, not null
1197      * @return a {@code LocalTime} based on this time with the subtraction made, not null
1198      * @throws DateTimeException if the subtraction cannot be made
1199      * @throws ArithmeticException if numeric overflow occurs
1200      */
1201     @Override
1202     public LocalTime minus(TemporalAmount amountToSubtract) {
1203         return (LocalTime) amountToSubtract.subtractFrom(this);
1204     }
1205 
1206     /**
1207      * Returns a copy of this time with the specified amount subtracted.
1208      * <p>
1209      * This returns a {@code LocalTime}, based on this one, with the amount
1210      * in terms of the unit subtracted. If it is not possible to subtract the amount,
1211      * because the unit is not supported or for some other reason, an exception is thrown.
1212      * <p>
1213      * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
1214      * See that method for a full description of how addition, and thus subtraction, works.
1215      * <p>
1216      * This instance is immutable and unaffected by this method call.
1217      *
1218      * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
1219      * @param unit  the unit of the amount to subtract, not null
1220      * @return a {@code LocalTime} based on this time with the specified amount subtracted, not null
1221      * @throws DateTimeException if the subtraction cannot be made
1222      * @throws UnsupportedTemporalTypeException if the unit is not supported
1223      * @throws ArithmeticException if numeric overflow occurs
1224      */
1225     @Override
1226     public LocalTime minus(long amountToSubtract, TemporalUnit unit) {
1227         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
1228     }
1229 
1230     //-----------------------------------------------------------------------
1231     /**
1232      * Returns a copy of this {@code LocalTime} with the specified number of hours subtracted.
1233      * <p>
1234      * This subtracts the specified number of hours from this time, returning a new time.
1235      * The calculation wraps around midnight.
1236      * <p>
1237      * This instance is immutable and unaffected by this method call.
1238      *
1239      * @param hoursToSubtract  the hours to subtract, may be negative
1240      * @return a {@code LocalTime} based on this time with the hours subtracted, not null
1241      */
1242     public LocalTime minusHours(long hoursToSubtract) {
1243         return plusHours(-(hoursToSubtract % HOURS_PER_DAY));
1244     }
1245 
1246     /**
1247      * Returns a copy of this {@code LocalTime} with the specified number of minutes subtracted.
1248      * <p>
1249      * This subtracts the specified number of minutes from this time, returning a new time.
1250      * The calculation wraps around midnight.
1251      * <p>
1252      * This instance is immutable and unaffected by this method call.
1253      *
1254      * @param minutesToSubtract  the minutes to subtract, may be negative
1255      * @return a {@code LocalTime} based on this time with the minutes subtracted, not null
1256      */
1257     public LocalTime minusMinutes(long minutesToSubtract) {
1258         return plusMinutes(-(minutesToSubtract % MINUTES_PER_DAY));
1259     }
1260 
1261     /**
1262      * Returns a copy of this {@code LocalTime} with the specified number of seconds subtracted.
1263      * <p>
1264      * This subtracts the specified number of seconds from this time, returning a new time.
1265      * The calculation wraps around midnight.
1266      * <p>
1267      * This instance is immutable and unaffected by this method call.
1268      *
1269      * @param secondsToSubtract  the seconds to subtract, may be negative
1270      * @return a {@code LocalTime} based on this time with the seconds subtracted, not null
1271      */
1272     public LocalTime minusSeconds(long secondsToSubtract) {
1273         return plusSeconds(-(secondsToSubtract % SECONDS_PER_DAY));
1274     }
1275 
1276     /**
1277      * Returns a copy of this {@code LocalTime} with the specified number of nanoseconds subtracted.
1278      * <p>
1279      * This subtracts the specified number of nanoseconds from this time, returning a new time.
1280      * The calculation wraps around midnight.
1281      * <p>
1282      * This instance is immutable and unaffected by this method call.
1283      *
1284      * @param nanosToSubtract  the nanos to subtract, may be negative
1285      * @return a {@code LocalTime} based on this time with the nanoseconds subtracted, not null
1286      */
1287     public LocalTime minusNanos(long nanosToSubtract) {
1288         return plusNanos(-(nanosToSubtract % NANOS_PER_DAY));
1289     }
1290 
1291     //-----------------------------------------------------------------------
1292     /**
1293      * Queries this time using the specified query.
1294      * <p>
1295      * This queries this time using the specified query strategy object.
1296      * The {@code TemporalQuery} object defines the logic to be used to
1297      * obtain the result. Read the documentation of the query to understand
1298      * what the result of this method will be.
1299      * <p>
1300      * The result of this method is obtained by invoking the
1301      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
1302      * specified query passing {@code this} as the argument.
1303      *
1304      * @param <R> the type of the result
1305      * @param query  the query to invoke, not null
1306      * @return the query result, null may be returned (defined by the query)
1307      * @throws DateTimeException if unable to query (defined by the query)
1308      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
1309      */
1310     @SuppressWarnings("unchecked")
1311     @Override
1312     public <R> R query(TemporalQuery<R> query) {
1313         if (query == TemporalQueries.chronology() || query == TemporalQueries.zoneId() ||
1314                 query == TemporalQueries.zone() || query == TemporalQueries.offset()) {
1315             return null;
1316         } else if (query == TemporalQueries.localTime()) {
1317             return (R) this;
1318         } else if (query == TemporalQueries.localDate()) {
1319             return null;
1320         } else if (query == TemporalQueries.precision()) {
1321             return (R) NANOS;
1322         }
1323         // inline TemporalAccessor.super.query(query) as an optimization
1324         // non-JDK classes are not permitted to make this optimization
1325         return query.queryFrom(this);
1326     }
1327 
1328     /**
1329      * Adjusts the specified temporal object to have the same time as this object.
1330      * <p>
1331      * This returns a temporal object of the same observable type as the input
1332      * with the time changed to be the same as this.
1333      * <p>
1334      * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
1335      * passing {@link ChronoField#NANO_OF_DAY} as the field.
1336      * <p>
1337      * In most cases, it is clearer to reverse the calling pattern by using
1338      * {@link Temporal#with(TemporalAdjuster)}:
1339      * <pre>
1340      *   // these two lines are equivalent, but the second approach is recommended
1341      *   temporal = thisLocalTime.adjustInto(temporal);
1342      *   temporal = temporal.with(thisLocalTime);
1343      * </pre>
1344      * <p>
1345      * This instance is immutable and unaffected by this method call.
1346      *
1347      * @param temporal  the target object to be adjusted, not null
1348      * @return the adjusted object, not null
1349      * @throws DateTimeException if unable to make the adjustment
1350      * @throws ArithmeticException if numeric overflow occurs
1351      */
1352     @Override
1353     public Temporal adjustInto(Temporal temporal) {
1354         return temporal.with(NANO_OF_DAY, toNanoOfDay());
1355     }
1356 
1357     /**
1358      * Calculates the amount of time until another time in terms of the specified unit.
1359      * <p>
1360      * This calculates the amount of time between two {@code LocalTime}
1361      * objects in terms of a single {@code TemporalUnit}.
1362      * The start and end points are {@code this} and the specified time.
1363      * The result will be negative if the end is before the start.
1364      * The {@code Temporal} passed to this method is converted to a
1365      * {@code LocalTime} using {@link #from(TemporalAccessor)}.
1366      * For example, the amount in hours between two times can be calculated
1367      * using {@code startTime.until(endTime, HOURS)}.
1368      * <p>
1369      * The calculation returns a whole number, representing the number of
1370      * complete units between the two times.
1371      * For example, the amount in hours between 11:30 and 13:29 will only
1372      * be one hour as it is one minute short of two hours.
1373      * <p>
1374      * There are two equivalent ways of using this method.
1375      * The first is to invoke this method.
1376      * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
1377      * <pre>
1378      *   // these two lines are equivalent
1379      *   amount = start.until(end, MINUTES);
1380      *   amount = MINUTES.between(start, end);
1381      * </pre>
1382      * The choice should be made based on which makes the code more readable.
1383      * <p>
1384      * The calculation is implemented in this method for {@link ChronoUnit}.
1385      * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
1386      * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS} are supported.
1387      * Other {@code ChronoUnit} values will throw an exception.
1388      * <p>
1389      * If the unit is not a {@code ChronoUnit}, then the result of this method
1390      * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
1391      * passing {@code this} as the first argument and the converted input temporal
1392      * as the second argument.
1393      * <p>
1394      * This instance is immutable and unaffected by this method call.
1395      *
1396      * @param endExclusive  the end time, exclusive, which is converted to a {@code LocalTime}, not null
1397      * @param unit  the unit to measure the amount in, not null
1398      * @return the amount of time between this time and the end time
1399      * @throws DateTimeException if the amount cannot be calculated, or the end
1400      *  temporal cannot be converted to a {@code LocalTime}
1401      * @throws UnsupportedTemporalTypeException if the unit is not supported
1402      * @throws ArithmeticException if numeric overflow occurs
1403      */
1404     @Override
1405     public long until(Temporal endExclusive, TemporalUnit unit) {
1406         LocalTime end = LocalTime.from(endExclusive);
1407         if (unit instanceof ChronoUnit) {
1408             long nanosUntil = end.toNanoOfDay() - toNanoOfDay();  // no overflow
1409             switch ((ChronoUnit) unit) {
1410                 case NANOS: return nanosUntil;
1411                 case MICROS: return nanosUntil / 1000;
1412                 case MILLIS: return nanosUntil / 1000_000;
1413                 case SECONDS: return nanosUntil / NANOS_PER_SECOND;
1414                 case MINUTES: return nanosUntil / NANOS_PER_MINUTE;
1415                 case HOURS: return nanosUntil / NANOS_PER_HOUR;
1416                 case HALF_DAYS: return nanosUntil / (12 * NANOS_PER_HOUR);
1417             }
1418             throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
1419         }
1420         return unit.between(this, end);
1421     }
1422 
1423     /**
1424      * Formats this time using the specified formatter.
1425      * <p>
1426      * This time will be passed to the formatter to produce a string.
1427      *
1428      * @param formatter  the formatter to use, not null
1429      * @return the formatted time string, not null
1430      * @throws DateTimeException if an error occurs during printing
1431      */
1432     public String format(DateTimeFormatter formatter) {
1433         Objects.requireNonNull(formatter, "formatter");
1434         return formatter.format(this);
1435     }
1436 
1437     //-----------------------------------------------------------------------
1438     /**
1439      * Combines this time with a date to create a {@code LocalDateTime}.
1440      * <p>
1441      * This returns a {@code LocalDateTime} formed from this time at the specified date.
1442      * All possible combinations of date and time are valid.
1443      *
1444      * @param date  the date to combine with, not null
1445      * @return the local date-time formed from this time and the specified date, not null
1446      */
1447     public LocalDateTime atDate(LocalDate date) {
1448         return LocalDateTime.of(date, this);
1449     }
1450 
1451     /**
1452      * Combines this time with an offset to create an {@code OffsetTime}.
1453      * <p>
1454      * This returns an {@code OffsetTime} formed from this time at the specified offset.
1455      * All possible combinations of time and offset are valid.
1456      *
1457      * @param offset  the offset to combine with, not null
1458      * @return the offset time formed from this time and the specified offset, not null
1459      */
1460     public OffsetTime atOffset(ZoneOffset offset) {
1461         return OffsetTime.of(this, offset);
1462     }
1463 
1464     //-----------------------------------------------------------------------
1465     /**
1466      * Extracts the time as seconds of day,
1467      * from {@code 0} to {@code 24 * 60 * 60 - 1}.
1468      *
1469      * @return the second-of-day equivalent to this time
1470      */
1471     public int toSecondOfDay() {
1472         int total = hour * SECONDS_PER_HOUR;
1473         total += minute * SECONDS_PER_MINUTE;
1474         total += second;
1475         return total;
1476     }
1477 
1478     /**
1479      * Extracts the time as nanos of day,
1480      * from {@code 0} to {@code 24 * 60 * 60 * 1,000,000,000 - 1}.
1481      *
1482      * @return the nano of day equivalent to this time
1483      */
1484     public long toNanoOfDay() {
1485         long total = hour * NANOS_PER_HOUR;
1486         total += minute * NANOS_PER_MINUTE;
1487         total += second * NANOS_PER_SECOND;
1488         total += nano;
1489         return total;
1490     }
1491     /**
1492      * Converts this {@code LocalTime} to the number of seconds since the epoch
1493      * of 1970-01-01T00:00:00Z.
1494      * <p>
1495      * This returns the number of seconds from the epoch for this {@link LocalTime}.
1496      *
1497      * @param offset the zone offset, not null
1498      * @return the number of seconds since the epoch of 1970-01-01T00:00:00Z, may be negative
1499      */
1500     public int toEpochSecond(ZoneOffset offset) {
1501         Objects.requireNonNull(offset, "offset");
1502         int secs = toSecondOfDay();
1503         secs -= offset.getTotalSeconds();
1504         return secs;
1505     }
1506 
1507     //-----------------------------------------------------------------------
1508     /**
1509      * Compares this time to another time.
1510      * <p>
1511      * The comparison is based on the time-line position of the local times within a day.
1512      * It is "consistent with equals", as defined by {@link Comparable}.
1513      *
1514      * @param other  the other time to compare to, not null
1515      * @return the comparator value, negative if less, positive if greater
1516      */
1517     @Override
1518     public int compareTo(LocalTime other) {
1519         int cmp = Integer.compare(hour, other.hour);
1520         if (cmp == 0) {
1521             cmp = Integer.compare(minute, other.minute);
1522             if (cmp == 0) {
1523                 cmp = Integer.compare(second, other.second);
1524                 if (cmp == 0) {
1525                     cmp = Integer.compare(nano, other.nano);
1526                 }
1527             }
1528         }
1529         return cmp;
1530     }
1531 
1532     /**
1533      * Checks if this time is after the specified time.
1534      * <p>
1535      * The comparison is based on the time-line position of the time within a day.
1536      *
1537      * @param other  the other time to compare to, not null
1538      * @return true if this is after the specified time
1539      */
1540     public boolean isAfter(LocalTime other) {
1541         return compareTo(other) > 0;
1542     }
1543 
1544     /**
1545      * Checks if this time is before the specified time.
1546      * <p>
1547      * The comparison is based on the time-line position of the time within a day.
1548      *
1549      * @param other  the other time to compare to, not null
1550      * @return true if this point is before the specified time
1551      */
1552     public boolean isBefore(LocalTime other) {
1553         return compareTo(other) < 0;
1554     }
1555 
1556     //-----------------------------------------------------------------------
1557     /**
1558      * Checks if this time is equal to another time.
1559      * <p>
1560      * The comparison is based on the time-line position of the time within a day.
1561      * <p>
1562      * Only objects of type {@code LocalTime} are compared, other types return false.
1563      * To compare the date of two {@code TemporalAccessor} instances, use
1564      * {@link ChronoField#NANO_OF_DAY} as a comparator.
1565      *
1566      * @param obj  the object to check, null returns false
1567      * @return true if this is equal to the other time
1568      */
1569     @Override
1570     public boolean equals(Object obj) {
1571         if (this == obj) {
1572             return true;
1573         }
1574         if (obj instanceof LocalTime) {
1575             LocalTime other = (LocalTime) obj;
1576             return hour == other.hour && minute == other.minute &&
1577                     second == other.second && nano == other.nano;
1578         }
1579         return false;
1580     }
1581 
1582     /**
1583      * A hash code for this time.
1584      *
1585      * @return a suitable hash code
1586      */
1587     @Override
1588     public int hashCode() {
1589         long nod = toNanoOfDay();
1590         return (int) (nod ^ (nod >>> 32));
1591     }
1592 
1593     //-----------------------------------------------------------------------
1594     /**
1595      * Outputs this time as a {@code String}, such as {@code 10:15}.
1596      * <p>
1597      * The output will be one of the following ISO-8601 formats:
1598      * <ul>
1599      * <li>{@code HH:mm}</li>
1600      * <li>{@code HH:mm:ss}</li>
1601      * <li>{@code HH:mm:ss.SSS}</li>
1602      * <li>{@code HH:mm:ss.SSSSSS}</li>
1603      * <li>{@code HH:mm:ss.SSSSSSSSS}</li>
1604      * </ul>
1605      * The format used will be the shortest that outputs the full value of
1606      * the time where the omitted parts are implied to be zero.
1607      *
1608      * @return a string representation of this time, not null
1609      */
1610     @Override
1611     public String toString() {
1612         StringBuilder buf = new StringBuilder(18);
1613         int hourValue = hour;
1614         int minuteValue = minute;
1615         int secondValue = second;
1616         int nanoValue = nano;
1617         buf.append(hourValue < 10 ? "0" : "").append(hourValue)
1618             .append(minuteValue < 10 ? ":0" : ":").append(minuteValue);
1619         if (secondValue > 0 || nanoValue > 0) {
1620             buf.append(secondValue < 10 ? ":0" : ":").append(secondValue);
1621             if (nanoValue > 0) {
1622                 buf.append('.');
1623                 if (nanoValue % 1000_000 == 0) {
1624                     buf.append(Integer.toString((nanoValue / 1000_000) + 1000).substring(1));
1625                 } else if (nanoValue % 1000 == 0) {
1626                     buf.append(Integer.toString((nanoValue / 1000) + 1000_000).substring(1));
1627                 } else {
1628                     buf.append(Integer.toString((nanoValue) + 1000_000_000).substring(1));
1629                 }
1630             }
1631         }
1632         return buf.toString();
1633     }
1634 
1635     //-----------------------------------------------------------------------
1636     /**
1637      * Writes the object using a
1638      * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1639      * @serialData
1640      * A twos-complement value indicates the remaining values are not in the stream
1641      * and should be set to zero.
1642      * <pre>
1643      *  out.writeByte(4);  // identifies a LocalTime
1644      *  if (nano == 0) {
1645      *    if (second == 0) {
1646      *      if (minute == 0) {
1647      *        out.writeByte(~hour);
1648      *      } else {
1649      *        out.writeByte(hour);
1650      *        out.writeByte(~minute);
1651      *      }
1652      *    } else {
1653      *      out.writeByte(hour);
1654      *      out.writeByte(minute);
1655      *      out.writeByte(~second);
1656      *    }
1657      *  } else {
1658      *    out.writeByte(hour);
1659      *    out.writeByte(minute);
1660      *    out.writeByte(second);
1661      *    out.writeInt(nano);
1662      *  }
1663      * </pre>
1664      *
1665      * @return the instance of {@code Ser}, not null
1666      */
1667     private Object writeReplace() {
1668         return new Ser(Ser.LOCAL_TIME_TYPE, this);
1669     }
1670 
1671     /**
1672      * Defend against malicious streams.
1673      *
1674      * @param s the stream to read
1675      * @throws InvalidObjectException always
1676      */
1677     private void readObject(ObjectInputStream s) throws InvalidObjectException {
1678         throw new InvalidObjectException("Deserialization via serialization delegate");
1679     }
1680 
1681     void writeExternal(DataOutput out) throws IOException {
1682         if (nano == 0) {
1683             if (second == 0) {
1684                 if (minute == 0) {
1685                     out.writeByte(~hour);
1686                 } else {
1687                     out.writeByte(hour);
1688                     out.writeByte(~minute);
1689                 }
1690             } else {
1691                 out.writeByte(hour);
1692                 out.writeByte(minute);
1693                 out.writeByte(~second);
1694             }
1695         } else {
1696             out.writeByte(hour);
1697             out.writeByte(minute);
1698             out.writeByte(second);
1699             out.writeInt(nano);
1700         }
1701     }
1702 
1703     static LocalTime readExternal(DataInput in) throws IOException {
1704         int hour = in.readByte();
1705         int minute = 0;
1706         int second = 0;
1707         int nano = 0;
1708         if (hour < 0) {
1709             hour = ~hour;
1710         } else {
1711             minute = in.readByte();
1712             if (minute < 0) {
1713                 minute = ~minute;
1714             } else {
1715                 second = in.readByte();
1716                 if (second < 0) {
1717                     second = ~second;
1718                 } else {
1719                     nano = in.readInt();
1720                 }
1721             }
1722         }
1723         return LocalTime.of(hour, minute, second, nano);
1724     }
1725 
1726 }