src/share/classes/java/time/LocalTime.java

Print this page




  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.ObjectStreamException;
  78 import java.io.Serializable;
  79 import java.time.format.DateTimeBuilder;
  80 import java.time.format.DateTimeFormatter;
  81 import java.time.format.DateTimeFormatters;
  82 import java.time.format.DateTimeParseException;
  83 import java.time.temporal.ChronoField;
  84 import java.time.temporal.ChronoLocalDateTime;
  85 import java.time.temporal.ChronoUnit;
  86 import java.time.temporal.ChronoZonedDateTime;
  87 import java.time.temporal.OffsetTime;
  88 import java.time.temporal.Queries;
  89 import java.time.temporal.Temporal;
  90 import java.time.temporal.TemporalAccessor;
  91 import java.time.temporal.TemporalAdder;
  92 import java.time.temporal.TemporalAdjuster;

  93 import java.time.temporal.TemporalField;
  94 import java.time.temporal.TemporalQuery;
  95 import java.time.temporal.TemporalSubtractor;
  96 import java.time.temporal.TemporalUnit;
  97 import java.time.temporal.ValueRange;
  98 import java.util.Objects;
  99 
 100 /**
 101  * A time without time-zone in the ISO-8601 calendar system,
 102  * such as {@code 10:15:30}.
 103  * <p>
 104  * {@code LocalTime} is an immutable date-time object that represents a time,
 105  * often viewed as hour-minute-second.
 106  * Time is represented to nanosecond precision.
 107  * For example, the value "13:45.30.123456789" can be stored in a {@code LocalTime}.
 108  * <p>
 109  * It does not store or represent a date or time-zone.
 110  * Instead, it is a description of the local time as seen on a wall clock.
 111  * It cannot represent an instant on the time-line without additional information
 112  * such as an offset or time-zone.
 113  * <p>
 114  * The ISO-8601 calendar system is the modern civil calendar system used today
 115  * in most of the world. This API assumes that all calendar systems use the same
 116  * representation, this class, for time-of-day.
 117  *
 118  * <h3>Specification for implementors</h3>
 119  * This class is immutable and thread-safe.
 120  *
 121  * @since 1.8
 122  */
 123 public final class LocalTime
 124         implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable {
 125 
 126     /**
 127      * The minimum supported {@code LocalTime}, '00:00'.
 128      * This is the time of midnight at the start of the day.
 129      */
 130     public static final LocalTime MIN;
 131     /**
 132      * The minimum supported {@code LocalTime}, '23:59:59.999999999'.
 133      * This is the time just before midnight at the end of the day.
 134      */
 135     public static final LocalTime MAX;
 136     /**
 137      * The time of midnight at the start of the day, '00:00'.
 138      */
 139     public static final LocalTime MIDNIGHT;
 140     /**
 141      * The time of noon in the middle of the day, '12:00'.
 142      */
 143     public static final LocalTime NOON;
 144     /**
 145      * Constants for the local time of each hour.
 146      */
 147     private static final LocalTime[] HOURS = new LocalTime[24];
 148     static {
 149         for (int i = 0; i < HOURS.length; i++) {
 150             HOURS[i] = new LocalTime(i, 0, 0, 0);
 151         }
 152         MIDNIGHT = HOURS[0];


 256      */
 257     public static LocalTime now(ZoneId zone) {
 258         return now(Clock.system(zone));
 259     }
 260 
 261     /**
 262      * Obtains the current time from the specified clock.
 263      * <p>
 264      * This will query the specified clock to obtain the current time.
 265      * Using this method allows the use of an alternate clock for testing.
 266      * The alternate clock may be introduced using {@link Clock dependency injection}.
 267      *
 268      * @param clock  the clock to use, not null
 269      * @return the current time, not null
 270      */
 271     public static LocalTime now(Clock clock) {
 272         Objects.requireNonNull(clock, "clock");
 273         // inline OffsetTime factory to avoid creating object and InstantProvider checks
 274         final Instant now = clock.instant();  // called once
 275         ZoneOffset offset = clock.getZone().getRules().getOffset(now);
 276         long secsOfDay = now.getEpochSecond() % SECONDS_PER_DAY;
 277         secsOfDay = (secsOfDay + offset.getTotalSeconds()) % SECONDS_PER_DAY;
 278         if (secsOfDay < 0) {
 279             secsOfDay += SECONDS_PER_DAY;
 280         }
 281         return LocalTime.ofSecondOfDay(secsOfDay, now.getNano());
 282     }
 283 
 284     //------------------------get-----------------------------------------------
 285     /**
 286      * Obtains an instance of {@code LocalTime} from an hour and minute.
 287      * <p>
 288      * The second and nanosecond fields will be set to zero by this factory method.
 289      * <p>
 290      * This factory may return a cached value, but applications must not rely on this.
 291      *
 292      * @param hour  the hour-of-day to represent, from 0 to 23
 293      * @param minute  the minute-of-hour to represent, from 0 to 59
 294      * @return the local time, not null
 295      * @throws DateTimeException if the value of any field is out of range
 296      */
 297     public static LocalTime of(int hour, int minute) {
 298         HOUR_OF_DAY.checkValidValue(hour);
 299         if (minute == 0) {
 300             return HOURS[hour];  // for performance
 301         }
 302         MINUTE_OF_HOUR.checkValidValue(minute);
 303         return new LocalTime(hour, minute, 0, 0);
 304     }
 305 
 306     /**
 307      * Obtains an instance of {@code LocalTime} from an hour, minute and second.
 308      * <p>
 309      * The nanosecond field will be set to zero by this factory method.
 310      * <p>
 311      * This factory may return a cached value, but applications must not rely on this.
 312      *
 313      * @param hour  the hour-of-day to represent, from 0 to 23
 314      * @param minute  the minute-of-hour to represent, from 0 to 59
 315      * @param second  the second-of-minute to represent, from 0 to 59
 316      * @return the local time, not null
 317      * @throws DateTimeException if the value of any field is out of range
 318      */
 319     public static LocalTime of(int hour, int minute, int second) {
 320         HOUR_OF_DAY.checkValidValue(hour);
 321         if ((minute | second) == 0) {
 322             return HOURS[hour];  // for performance
 323         }
 324         MINUTE_OF_HOUR.checkValidValue(minute);
 325         SECOND_OF_MINUTE.checkValidValue(second);
 326         return new LocalTime(hour, minute, second, 0);
 327     }
 328 
 329     /**
 330      * Obtains an instance of {@code LocalTime} from an hour, minute, second and nanosecond.
 331      * <p>
 332      * This factory may return a cached value, but applications must not rely on this.
 333      *
 334      * @param hour  the hour-of-day to represent, from 0 to 23
 335      * @param minute  the minute-of-hour to represent, from 0 to 59
 336      * @param second  the second-of-minute to represent, from 0 to 59
 337      * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
 338      * @return the local time, not null
 339      * @throws DateTimeException if the value of any field is out of range
 340      */
 341     public static LocalTime of(int hour, int minute, int second, int nanoOfSecond) {
 342         HOUR_OF_DAY.checkValidValue(hour);
 343         MINUTE_OF_HOUR.checkValidValue(minute);
 344         SECOND_OF_MINUTE.checkValidValue(second);
 345         NANO_OF_SECOND.checkValidValue(nanoOfSecond);
 346         return create(hour, minute, second, nanoOfSecond);
 347     }
 348 
 349     //-----------------------------------------------------------------------
 350     /**
 351      * Obtains an instance of {@code LocalTime} from a second-of-day value.
 352      * <p>
 353      * This factory may return a cached value, but applications must not rely on this.

 354      *
 355      * @param secondOfDay  the second-of-day, from {@code 0} to {@code 24 * 60 * 60 - 1}
 356      * @return the local time, not null
 357      * @throws DateTimeException if the second-of-day value is invalid
 358      */
 359     public static LocalTime ofSecondOfDay(long secondOfDay) {
 360         SECOND_OF_DAY.checkValidValue(secondOfDay);
 361         int hours = (int) (secondOfDay / SECONDS_PER_HOUR);
 362         secondOfDay -= hours * SECONDS_PER_HOUR;
 363         int minutes = (int) (secondOfDay / SECONDS_PER_MINUTE);
 364         secondOfDay -= minutes * SECONDS_PER_MINUTE;
 365         return create(hours, minutes, (int) secondOfDay, 0);
 366     }
 367 
 368     /**
 369      * Obtains an instance of {@code LocalTime} from a second-of-day value, with
 370      * associated nanos of second.
 371      * <p>
 372      * This factory may return a cached value, but applications must not rely on this.
 373      *
 374      * @param secondOfDay  the second-of-day, from {@code 0} to {@code 24 * 60 * 60 - 1}
 375      * @param nanoOfSecond  the nano-of-second, from 0 to 999,999,999
 376      * @return the local time, not null
 377      * @throws DateTimeException if the either input value is invalid
 378      */
 379     public static LocalTime ofSecondOfDay(long secondOfDay, int nanoOfSecond) {
 380         SECOND_OF_DAY.checkValidValue(secondOfDay);
 381         NANO_OF_SECOND.checkValidValue(nanoOfSecond);
 382         int hours = (int) (secondOfDay / SECONDS_PER_HOUR);
 383         secondOfDay -= hours * SECONDS_PER_HOUR;
 384         int minutes = (int) (secondOfDay / SECONDS_PER_MINUTE);
 385         secondOfDay -= minutes * SECONDS_PER_MINUTE;
 386         return create(hours, minutes, (int) secondOfDay, nanoOfSecond);
 387     }
 388 
 389     /**
 390      * Obtains an instance of {@code LocalTime} from a nanos-of-day value.
 391      * <p>
 392      * This factory may return a cached value, but applications must not rely on this.
 393      *
 394      * @param nanoOfDay  the nano of day, from {@code 0} to {@code 24 * 60 * 60 * 1,000,000,000 - 1}
 395      * @return the local time, not null
 396      * @throws DateTimeException if the nanos of day value is invalid
 397      */
 398     public static LocalTime ofNanoOfDay(long nanoOfDay) {
 399         NANO_OF_DAY.checkValidValue(nanoOfDay);
 400         int hours = (int) (nanoOfDay / NANOS_PER_HOUR);
 401         nanoOfDay -= hours * NANOS_PER_HOUR;
 402         int minutes = (int) (nanoOfDay / NANOS_PER_MINUTE);
 403         nanoOfDay -= minutes * NANOS_PER_MINUTE;
 404         int seconds = (int) (nanoOfDay / NANOS_PER_SECOND);
 405         nanoOfDay -= seconds * NANOS_PER_SECOND;
 406         return create(hours, minutes, seconds, (int) nanoOfDay);
 407     }
 408 
 409     //-----------------------------------------------------------------------
 410     /**
 411      * Obtains an instance of {@code LocalTime} from a temporal object.
 412      * <p>
 413      * A {@code TemporalAccessor} represents some form of date and time information.
 414      * This factory converts the arbitrary temporal object to an instance of {@code LocalTime}.

 415      * <p>
 416      * The conversion extracts 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 in queries 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         if (temporal instanceof LocalTime) {
 427             return (LocalTime) temporal;
 428         } else if (temporal instanceof ChronoLocalDateTime) {
 429             return ((ChronoLocalDateTime) temporal).getTime();
 430         } else if (temporal instanceof ChronoZonedDateTime) {
 431             return ((ChronoZonedDateTime) temporal).getTime();
 432         }
 433         // handle builder as a special case
 434         if (temporal instanceof DateTimeBuilder) {
 435             DateTimeBuilder builder = (DateTimeBuilder) temporal;
 436             LocalTime time = builder.extract(LocalTime.class);
 437             if (time != null) {
 438                 return time;
 439             }
 440         }
 441         try {
 442             return ofNanoOfDay(temporal.getLong(NANO_OF_DAY));
 443         } catch (DateTimeException ex) {
 444             throw new DateTimeException("Unable to obtain LocalTime from TemporalAccessor: " + temporal.getClass(), ex);
 445         }

 446     }
 447 
 448     //-----------------------------------------------------------------------
 449     /**
 450      * Obtains an instance of {@code LocalTime} from a text string such as {@code 10:15}.
 451      * <p>
 452      * The string must represent a valid time and is parsed using
 453      * {@link java.time.format.DateTimeFormatters#isoLocalTime()}.
 454      *
 455      * @param text the text to parse such as "10:15:30", not null
 456      * @return the parsed local time, not null
 457      * @throws DateTimeParseException if the text cannot be parsed
 458      */
 459     public static LocalTime parse(CharSequence text) {
 460         return parse(text, DateTimeFormatters.isoLocalTime());
 461     }
 462 
 463     /**
 464      * Obtains an instance of {@code LocalTime} from a text string using a specific formatter.
 465      * <p>
 466      * The text is parsed using the formatter, returning a time.
 467      *
 468      * @param text  the text to parse, not null
 469      * @param formatter  the formatter to use, not null
 470      * @return the parsed local time, not null
 471      * @throws DateTimeParseException if the text cannot be parsed
 472      */
 473     public static LocalTime parse(CharSequence text, DateTimeFormatter formatter) {
 474         Objects.requireNonNull(formatter, "formatter");
 475         return formatter.parse(text, LocalTime::from);
 476     }
 477 
 478     //-----------------------------------------------------------------------
 479     /**
 480      * Creates a local time from the hour, minute, second and nanosecond fields.


 522      * <ul>
 523      * <li>{@code NANO_OF_SECOND}
 524      * <li>{@code NANO_OF_DAY}
 525      * <li>{@code MICRO_OF_SECOND}
 526      * <li>{@code MICRO_OF_DAY}
 527      * <li>{@code MILLI_OF_SECOND}
 528      * <li>{@code MILLI_OF_DAY}
 529      * <li>{@code SECOND_OF_MINUTE}
 530      * <li>{@code SECOND_OF_DAY}
 531      * <li>{@code MINUTE_OF_HOUR}
 532      * <li>{@code MINUTE_OF_DAY}
 533      * <li>{@code HOUR_OF_AMPM}
 534      * <li>{@code CLOCK_HOUR_OF_AMPM}
 535      * <li>{@code HOUR_OF_DAY}
 536      * <li>{@code CLOCK_HOUR_OF_DAY}
 537      * <li>{@code AMPM_OF_DAY}
 538      * </ul>
 539      * All other {@code ChronoField} instances will return false.
 540      * <p>
 541      * If the field is not a {@code ChronoField}, then the result of this method
 542      * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)}
 543      * passing {@code this} as the argument.
 544      * Whether the field is supported is determined by the field.
 545      *
 546      * @param field  the field to check, null returns false
 547      * @return true if the field is supported on this time, false if not
 548      */
 549     @Override
 550     public boolean isSupported(TemporalField field) {
 551         if (field instanceof ChronoField) {
 552             return ((ChronoField) field).isTimeField();
 553         }
 554         return field != null && field.doIsSupported(this);
 555     }
 556 
 557     /**
 558      * Gets the range of valid values for the specified field.
 559      * <p>
 560      * The range object expresses the minimum and maximum valid values for a field.
 561      * This time is used to enhance the accuracy of the returned range.
 562      * If it is not possible to return the range, because the field is not supported
 563      * or for some other reason, an exception is thrown.
 564      * <p>
 565      * If the field is a {@link ChronoField} then the query is implemented here.
 566      * The {@link #isSupported(TemporalField) supported fields} will return
 567      * appropriate range instances.
 568      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 569      * <p>
 570      * If the field is not a {@code ChronoField}, then the result of this method
 571      * is obtained by invoking {@code TemporalField.doRange(TemporalAccessor)}
 572      * passing {@code this} as the argument.
 573      * Whether the range can be obtained is determined by the field.
 574      *
 575      * @param field  the field to query the range for, not null
 576      * @return the range of valid values for the field, not null
 577      * @throws DateTimeException if the range for the field cannot be obtained
 578      */
 579     @Override  // override for Javadoc
 580     public ValueRange range(TemporalField field) {
 581         return Temporal.super.range(field);
 582     }
 583 
 584     /**
 585      * Gets the value of the specified field from this time as an {@code int}.
 586      * <p>
 587      * This queries this time for the value for the specified field.
 588      * The returned value will always be within the valid range of values for the field.
 589      * If it is not possible to return the value, because the field is not supported
 590      * or for some other reason, an exception is thrown.
 591      * <p>
 592      * If the field is a {@link ChronoField} then the query is implemented here.
 593      * The {@link #isSupported(TemporalField) supported fields} will return valid
 594      * values based on this time, except {@code NANO_OF_DAY} and {@code MICRO_OF_DAY}
 595      * which are too large to fit in an {@code int} and throw a {@code DateTimeException}.
 596      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 597      * <p>
 598      * If the field is not a {@code ChronoField}, then the result of this method
 599      * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
 600      * passing {@code this} as the argument. Whether the value can be obtained,
 601      * and what the value represents, is determined by the field.
 602      *
 603      * @param field  the field to get, not null
 604      * @return the value for the field
 605      * @throws DateTimeException if a value for the field cannot be obtained
 606      * @throws ArithmeticException if numeric overflow occurs
 607      */
 608     @Override  // override for Javadoc and performance
 609     public int get(TemporalField field) {
 610         if (field instanceof ChronoField) {
 611             return get0(field);
 612         }
 613         return Temporal.super.get(field);
 614     }
 615 
 616     /**
 617      * Gets the value of the specified field from this time as a {@code long}.
 618      * <p>
 619      * This queries this time for the value for the specified field.
 620      * If it is not possible to return the value, because the field is not supported
 621      * or for some other reason, an exception is thrown.
 622      * <p>
 623      * If the field is a {@link ChronoField} then the query is implemented here.
 624      * The {@link #isSupported(TemporalField) supported fields} will return valid
 625      * values based on this time.
 626      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 627      * <p>
 628      * If the field is not a {@code ChronoField}, then the result of this method
 629      * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
 630      * passing {@code this} as the argument. Whether the value can be obtained,
 631      * and what the value represents, is determined by the field.
 632      *
 633      * @param field  the field to get, not null
 634      * @return the value for the field
 635      * @throws DateTimeException if a value for the field cannot be obtained
 636      * @throws ArithmeticException if numeric overflow occurs
 637      */
 638     @Override
 639     public long getLong(TemporalField field) {
 640         if (field instanceof ChronoField) {
 641             if (field == NANO_OF_DAY) {
 642                 return toNanoOfDay();
 643             }
 644             if (field == MICRO_OF_DAY) {
 645                 return toNanoOfDay() / 1000;
 646             }
 647             return get0(field);
 648         }
 649         return field.doGet(this);
 650     }
 651 
 652     private int get0(TemporalField field) {
 653         switch ((ChronoField) field) {
 654             case NANO_OF_SECOND: return nano;
 655             case NANO_OF_DAY: throw new DateTimeException("Field too large for an int: " + field);
 656             case MICRO_OF_SECOND: return nano / 1000;
 657             case MICRO_OF_DAY: throw new DateTimeException("Field too large for an int: " + field);
 658             case MILLI_OF_SECOND: return nano / 1000_000;
 659             case MILLI_OF_DAY: return (int) (toNanoOfDay() / 1000_000);
 660             case SECOND_OF_MINUTE: return second;
 661             case SECOND_OF_DAY: return toSecondOfDay();
 662             case MINUTE_OF_HOUR: return minute;
 663             case MINUTE_OF_DAY: return hour * 60 + minute;
 664             case HOUR_OF_AMPM: return hour % 12;
 665             case CLOCK_HOUR_OF_AMPM: int ham = hour % 12; return (ham % 12 == 0 ? 12 : ham);
 666             case HOUR_OF_DAY: return hour;
 667             case CLOCK_HOUR_OF_DAY: return (hour == 0 ? 24 : hour);
 668             case AMPM_OF_DAY: return hour / 12;
 669         }


 694      *
 695      * @return the second-of-minute, from 0 to 59
 696      */
 697     public int getSecond() {
 698         return second;
 699     }
 700 
 701     /**
 702      * Gets the nano-of-second field.
 703      *
 704      * @return the nano-of-second, from 0 to 999,999,999
 705      */
 706     public int getNano() {
 707         return nano;
 708     }
 709 
 710     //-----------------------------------------------------------------------
 711     /**
 712      * Returns an adjusted copy of this time.
 713      * <p>
 714      * This returns a new {@code LocalTime}, based on this one, with the time adjusted.
 715      * The adjustment takes place using the specified adjuster strategy object.
 716      * Read the documentation of the adjuster to understand what adjustment will be made.
 717      * <p>
 718      * A simple adjuster might simply set the one of the fields, such as the hour field.
 719      * A more complex adjuster might set the time to the last hour of the day.
 720      * <p>
 721      * The result of this method is obtained by invoking the
 722      * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
 723      * specified adjuster passing {@code this} as the argument.
 724      * <p>
 725      * This instance is immutable and unaffected by this method call.
 726      *
 727      * @param adjuster the adjuster to use, not null
 728      * @return a {@code LocalTime} based on {@code this} with the adjustment made, not null
 729      * @throws DateTimeException if the adjustment cannot be made
 730      * @throws ArithmeticException if numeric overflow occurs
 731      */
 732     @Override
 733     public LocalTime with(TemporalAdjuster adjuster) {
 734         // optimizations
 735         if (adjuster instanceof LocalTime) {
 736             return (LocalTime) adjuster;
 737         }
 738         return (LocalTime) adjuster.adjustInto(this);
 739     }
 740 
 741     /**
 742      * Returns a copy of this time with the specified field set to a new value.
 743      * <p>
 744      * This returns a new {@code LocalTime}, based on this one, with the value
 745      * for the specified field changed.
 746      * This can be used to change any supported field, such as the hour, minute or second.
 747      * If it is not possible to set the value, because the field is not supported or for
 748      * some other reason, an exception is thrown.
 749      * <p>
 750      * If the field is a {@link ChronoField} then the adjustment is implemented here.
 751      * The supported fields behave as follows:
 752      * <ul>
 753      * <li>{@code NANO_OF_SECOND} -
 754      *  Returns a {@code LocalTime} with the specified nano-of-second.
 755      *  The hour, minute and second will be unchanged.
 756      * <li>{@code NANO_OF_DAY} -
 757      *  Returns a {@code LocalTime} with the specified nano-of-day.
 758      *  This completely replaces the time and is equivalent to {@link #ofNanoOfDay(long)}.
 759      * <li>{@code MICRO_OF_SECOND} -
 760      *  Returns a {@code LocalTime} with the nano-of-second replaced by the specified
 761      *  micro-of-second multiplied by 1,000.
 762      *  The hour, minute and second will be unchanged.
 763      * <li>{@code MICRO_OF_DAY} -
 764      *  Returns a {@code LocalTime} with the specified micro-of-day.


 790      * <li>{@code CLOCK_HOUR_OF_AMPM} -
 791      *  Returns a {@code LocalTime} with the specified clock-hour-of-am-pm.
 792      *  The AM/PM, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
 793      * <li>{@code HOUR_OF_DAY} -
 794      *  Returns a {@code LocalTime} with the specified hour-of-day.
 795      *  The minute-of-hour, second-of-minute and nano-of-second will be unchanged.
 796      * <li>{@code CLOCK_HOUR_OF_DAY} -
 797      *  Returns a {@code LocalTime} with the specified clock-hour-of-day.
 798      *  The minute-of-hour, second-of-minute and nano-of-second will be unchanged.
 799      * <li>{@code AMPM_OF_DAY} -
 800      *  Returns a {@code LocalTime} with the specified AM/PM.
 801      *  The hour-of-am-pm, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
 802      * </ul>
 803      * <p>
 804      * In all cases, if the new value is outside the valid range of values for the field
 805      * then a {@code DateTimeException} will be thrown.
 806      * <p>
 807      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 808      * <p>
 809      * If the field is not a {@code ChronoField}, then the result of this method
 810      * is obtained by invoking {@code TemporalField.doWith(Temporal, long)}
 811      * passing {@code this} as the argument. In this case, the field determines
 812      * whether and how to adjust the instant.
 813      * <p>
 814      * This instance is immutable and unaffected by this method call.
 815      *
 816      * @param field  the field to set in the result, not null
 817      * @param newValue  the new value of the field in the result
 818      * @return a {@code LocalTime} based on {@code this} with the specified field set, not null
 819      * @throws DateTimeException if the field cannot be set
 820      * @throws ArithmeticException if numeric overflow occurs
 821      */
 822     @Override
 823     public LocalTime with(TemporalField field, long newValue) {
 824         if (field instanceof ChronoField) {
 825             ChronoField f = (ChronoField) field;
 826             f.checkValidValue(newValue);
 827             switch (f) {
 828                 case NANO_OF_SECOND: return withNano((int) newValue);
 829                 case NANO_OF_DAY: return LocalTime.ofNanoOfDay(newValue);
 830                 case MICRO_OF_SECOND: return withNano((int) newValue * 1000);
 831                 case MICRO_OF_DAY: return plusNanos((newValue - toNanoOfDay() / 1000) * 1000);
 832                 case MILLI_OF_SECOND: return withNano((int) newValue * 1000_000);
 833                 case MILLI_OF_DAY: return plusNanos((newValue - toNanoOfDay() / 1000_000) * 1000_000);
 834                 case SECOND_OF_MINUTE: return withSecond((int) newValue);
 835                 case SECOND_OF_DAY: return plusSeconds(newValue - toSecondOfDay());
 836                 case MINUTE_OF_HOUR: return withMinute((int) newValue);
 837                 case MINUTE_OF_DAY: return plusMinutes(newValue - (hour * 60 + minute));
 838                 case HOUR_OF_AMPM: return plusHours(newValue - (hour % 12));
 839                 case CLOCK_HOUR_OF_AMPM: return plusHours((newValue == 12 ? 0 : newValue) - (hour % 12));
 840                 case HOUR_OF_DAY: return withHour((int) newValue);
 841                 case CLOCK_HOUR_OF_DAY: return withHour((int) (newValue == 24 ? 0 : newValue));
 842                 case AMPM_OF_DAY: return plusHours((newValue - (hour / 12)) * 12);
 843             }
 844             throw new DateTimeException("Unsupported field: " + field.getName());
 845         }
 846         return field.doWith(this, newValue);
 847     }
 848 
 849     //-----------------------------------------------------------------------
 850     /**
 851      * Returns a copy of this {@code LocalTime} with the hour-of-day value altered.
 852      * <p>
 853      * This instance is immutable and unaffected by this method call.
 854      *
 855      * @param hour  the hour-of-day to set in the result, from 0 to 23
 856      * @return a {@code LocalTime} based on this time with the requested hour, not null
 857      * @throws DateTimeException if the hour value is invalid
 858      */
 859     public LocalTime withHour(int hour) {
 860         if (this.hour == hour) {
 861             return this;
 862         }
 863         HOUR_OF_DAY.checkValidValue(hour);
 864         return create(hour, minute, second, nano);
 865     }
 866 


 907      * @return a {@code LocalTime} based on this time with the requested nanosecond, not null
 908      * @throws DateTimeException if the nanos value is invalid
 909      */
 910     public LocalTime withNano(int nanoOfSecond) {
 911         if (this.nano == nanoOfSecond) {
 912             return this;
 913         }
 914         NANO_OF_SECOND.checkValidValue(nanoOfSecond);
 915         return create(hour, minute, second, nanoOfSecond);
 916     }
 917 
 918     //-----------------------------------------------------------------------
 919     /**
 920      * Returns a copy of this {@code LocalTime} with the time truncated.
 921      * <p>
 922      * Truncating the time returns a copy of the original time with fields
 923      * smaller than the specified unit set to zero.
 924      * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
 925      * will set the second-of-minute and nano-of-second field to zero.
 926      * <p>
 927      * Not all units are accepted. The {@link ChronoUnit#DAYS days} unit and time
 928      * units with an exact duration can be used, other units throw an exception.


 929      * <p>
 930      * This instance is immutable and unaffected by this method call.
 931      *
 932      * @param unit  the unit to truncate to, not null
 933      * @return a {@code LocalTime} based on this time with the time truncated, not null
 934      * @throws DateTimeException if unable to truncate
 935      */
 936     public LocalTime truncatedTo(TemporalUnit unit) {
 937         if (unit == ChronoUnit.NANOS) {
 938             return this;
 939         } else if (unit == ChronoUnit.DAYS) {
 940             return MIDNIGHT;
 941         } else if (unit.isDurationEstimated()) {
 942             throw new DateTimeException("Unit must not have an estimated duration");
 943         }
 944         long nod = toNanoOfDay();
 945         long dur = unit.getDuration().toNanos();
 946         if (dur >= NANOS_PER_DAY) {
 947             throw new DateTimeException("Unit must not be a date unit");



 948         }
 949         nod = (nod / dur) * dur;
 950         return ofNanoOfDay(nod);
 951     }
 952 
 953     //-----------------------------------------------------------------------
 954     /**
 955      * Returns a copy of this date with the specified period added.
 956      * <p>
 957      * This method returns a new time based on this time with the specified period added.
 958      * The adder is typically {@link Period} but may be any other type implementing
 959      * the {@link TemporalAdder} interface.
 960      * The calculation is delegated to the specified adjuster, which typically calls
 961      * back to {@link #plus(long, TemporalUnit)}.




 962      * <p>
 963      * This instance is immutable and unaffected by this method call.
 964      *
 965      * @param adder  the adder to use, not null
 966      * @return a {@code LocalTime} based on this time with the addition made, not null
 967      * @throws DateTimeException if the addition cannot be made
 968      * @throws ArithmeticException if numeric overflow occurs
 969      */
 970     @Override
 971     public LocalTime plus(TemporalAdder adder) {
 972         return (LocalTime) adder.addTo(this);
 973     }
 974 
 975     /**
 976      * Returns a copy of this time with the specified period added.
 977      * <p>
 978      * This method returns a new time based on this time with the specified period added.
 979      * This can be used to add any period that is defined by a unit, for example to add hours, minutes or seconds.
 980      * The unit is responsible for the details of the calculation, including the resolution
 981      * of any edge cases in the calculation.






































 982      * <p>
 983      * This instance is immutable and unaffected by this method call.
 984      *
 985      * @param amountToAdd  the amount of the unit to add to the result, may be negative
 986      * @param unit  the unit of the period to add, not null
 987      * @return a {@code LocalTime} based on this time with the specified period added, not null
 988      * @throws DateTimeException if the unit cannot be added to this type

 989      */
 990     @Override
 991     public LocalTime plus(long amountToAdd, TemporalUnit unit) {
 992         if (unit instanceof ChronoUnit) {
 993             ChronoUnit f = (ChronoUnit) unit;
 994             switch (f) {
 995                 case NANOS: return plusNanos(amountToAdd);
 996                 case MICROS: return plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
 997                 case MILLIS: return plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
 998                 case SECONDS: return plusSeconds(amountToAdd);
 999                 case MINUTES: return plusMinutes(amountToAdd);
1000                 case HOURS: return plusHours(amountToAdd);
1001                 case HALF_DAYS: return plusHours((amountToAdd % 2) * 12);
1002                 case DAYS: return this;
1003             }
1004             throw new DateTimeException("Unsupported unit: " + unit.getName());
1005         }
1006         return unit.doPlus(this, amountToAdd);
1007     }
1008 
1009     //-----------------------------------------------------------------------
1010     /**
1011      * Returns a copy of this {@code LocalTime} with the specified period in hours added.
1012      * <p>
1013      * This adds the specified number of hours to this time, returning a new time.
1014      * The calculation wraps around midnight.
1015      * <p>
1016      * This instance is immutable and unaffected by this method call.
1017      *
1018      * @param hoursToAdd  the hours to add, may be negative
1019      * @return a {@code LocalTime} based on this time with the hours added, not null
1020      */
1021     public LocalTime plusHours(long hoursToAdd) {
1022         if (hoursToAdd == 0) {
1023             return this;
1024         }
1025         int newHour = ((int) (hoursToAdd % HOURS_PER_DAY) + hour + HOURS_PER_DAY) % HOURS_PER_DAY;
1026         return create(newHour, minute, second, nano);


1090      * @return a {@code LocalTime} based on this time with the nanoseconds added, not null
1091      */
1092     public LocalTime plusNanos(long nanosToAdd) {
1093         if (nanosToAdd == 0) {
1094             return this;
1095         }
1096         long nofd = toNanoOfDay();
1097         long newNofd = ((nanosToAdd % NANOS_PER_DAY) + nofd + NANOS_PER_DAY) % NANOS_PER_DAY;
1098         if (nofd == newNofd) {
1099             return this;
1100         }
1101         int newHour = (int) (newNofd / NANOS_PER_HOUR);
1102         int newMinute = (int) ((newNofd / NANOS_PER_MINUTE) % MINUTES_PER_HOUR);
1103         int newSecond = (int) ((newNofd / NANOS_PER_SECOND) % SECONDS_PER_MINUTE);
1104         int newNano = (int) (newNofd % NANOS_PER_SECOND);
1105         return create(newHour, newMinute, newSecond, newNano);
1106     }
1107 
1108     //-----------------------------------------------------------------------
1109     /**
1110      * Returns a copy of this time with the specified period subtracted.
1111      * <p>
1112      * This method returns a new time based on this time with the specified period subtracted.
1113      * The subtractor is typically {@link Period} but may be any other type implementing
1114      * the {@link TemporalSubtractor} interface.
1115      * The calculation is delegated to the specified adjuster, which typically calls
1116      * back to {@link #minus(long, TemporalUnit)}.




1117      * <p>
1118      * This instance is immutable and unaffected by this method call.
1119      *
1120      * @param subtractor  the subtractor to use, not null
1121      * @return a {@code LocalTime} based on this time with the subtraction made, not null
1122      * @throws DateTimeException if the subtraction cannot be made
1123      * @throws ArithmeticException if numeric overflow occurs
1124      */
1125     @Override
1126     public LocalTime minus(TemporalSubtractor subtractor) {
1127         return (LocalTime) subtractor.subtractFrom(this);
1128     }
1129 
1130     /**
1131      * Returns a copy of this time with the specified period subtracted.




1132      * <p>
1133      * This method returns a new time based on this time with the specified period subtracted.
1134      * This can be used to subtract any period that is defined by a unit, for example to subtract hours, minutes or seconds.
1135      * The unit is responsible for the details of the calculation, including the resolution
1136      * of any edge cases in the calculation.
1137      * <p>
1138      * This instance is immutable and unaffected by this method call.
1139      *
1140      * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
1141      * @param unit  the unit of the period to subtract, not null
1142      * @return a {@code LocalTime} based on this time with the specified period subtracted, not null
1143      * @throws DateTimeException if the unit cannot be added to this type

1144      */
1145     @Override
1146     public LocalTime minus(long amountToSubtract, TemporalUnit unit) {
1147         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
1148     }
1149 
1150     //-----------------------------------------------------------------------
1151     /**
1152      * Returns a copy of this {@code LocalTime} with the specified period in hours subtracted.
1153      * <p>
1154      * This subtracts the specified number of hours from this time, returning a new time.
1155      * The calculation wraps around midnight.
1156      * <p>
1157      * This instance is immutable and unaffected by this method call.
1158      *
1159      * @param hoursToSubtract  the hours to subtract, may be negative
1160      * @return a {@code LocalTime} based on this time with the hours subtracted, not null
1161      */
1162     public LocalTime minusHours(long hoursToSubtract) {
1163         return plusHours(-(hoursToSubtract % HOURS_PER_DAY));


1213      * Queries this time using the specified query.
1214      * <p>
1215      * This queries this time using the specified query strategy object.
1216      * The {@code TemporalQuery} object defines the logic to be used to
1217      * obtain the result. Read the documentation of the query to understand
1218      * what the result of this method will be.
1219      * <p>
1220      * The result of this method is obtained by invoking the
1221      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
1222      * specified query passing {@code this} as the argument.
1223      *
1224      * @param <R> the type of the result
1225      * @param query  the query to invoke, not null
1226      * @return the query result, null may be returned (defined by the query)
1227      * @throws DateTimeException if unable to query (defined by the query)
1228      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
1229      */
1230     @SuppressWarnings("unchecked")
1231     @Override
1232     public <R> R query(TemporalQuery<R> query) {
1233         if (query == Queries.precision()) {






1234             return (R) NANOS;
1235         }
1236         // inline TemporalAccessor.super.query(query) as an optimization
1237         if (query == Queries.chrono() || query == Queries.zoneId() || query == Queries.zone() || query == Queries.offset()) {
1238             return null;
1239         }
1240         return query.queryFrom(this);
1241     }
1242 
1243     /**
1244      * Adjusts the specified temporal object to have the same time as this object.
1245      * <p>
1246      * This returns a temporal object of the same observable type as the input
1247      * with the time changed to be the same as this.
1248      * <p>
1249      * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
1250      * passing {@link ChronoField#NANO_OF_DAY} as the field.
1251      * <p>
1252      * In most cases, it is clearer to reverse the calling pattern by using
1253      * {@link Temporal#with(TemporalAdjuster)}:
1254      * <pre>
1255      *   // these two lines are equivalent, but the second approach is recommended
1256      *   temporal = thisLocalTime.adjustInto(temporal);
1257      *   temporal = temporal.with(thisLocalTime);
1258      * </pre>
1259      * <p>


1268     public Temporal adjustInto(Temporal temporal) {
1269         return temporal.with(NANO_OF_DAY, toNanoOfDay());
1270     }
1271 
1272     /**
1273      * Calculates the period between this time and another time in
1274      * terms of the specified unit.
1275      * <p>
1276      * This calculates the period between two times in terms of a single unit.
1277      * The start and end points are {@code this} and the specified time.
1278      * The result will be negative if the end is before the start.
1279      * The {@code Temporal} passed to this method must be a {@code LocalTime}.
1280      * For example, the period in hours between two times can be calculated
1281      * using {@code startTime.periodUntil(endTime, HOURS)}.
1282      * <p>
1283      * The calculation returns a whole number, representing the number of
1284      * complete units between the two times.
1285      * For example, the period in hours between 11:30 and 13:29 will only
1286      * be one hour as it is one minute short of two hours.
1287      * <p>
1288      * This method operates in association with {@link TemporalUnit#between}.
1289      * The result of this method is a {@code long} representing the amount of
1290      * the specified unit. By contrast, the result of {@code between} is an
1291      * object that can be used directly in addition/subtraction:
1292      * <pre>
1293      *   long period = start.periodUntil(end, HOURS);   // this method
1294      *   dateTime.plus(HOURS.between(start, end));      // use in plus/minus

1295      * </pre>

1296      * <p>
1297      * The calculation is implemented in this method for {@link ChronoUnit}.
1298      * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
1299      * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS} are supported.
1300      * Other {@code ChronoUnit} values will throw an exception.
1301      * <p>
1302      * If the unit is not a {@code ChronoUnit}, then the result of this method
1303      * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
1304      * passing {@code this} as the first argument and the input temporal as
1305      * the second argument.
1306      * <p>
1307      * This instance is immutable and unaffected by this method call.
1308      *
1309      * @param endTime  the end time, which must be a {@code LocalTime}, not null
1310      * @param unit  the unit to measure the period in, not null
1311      * @return the amount of the period between this time and the end time
1312      * @throws DateTimeException if the period cannot be calculated
1313      * @throws ArithmeticException if numeric overflow occurs
1314      */
1315     @Override
1316     public long periodUntil(Temporal endTime, TemporalUnit unit) {
1317         if (endTime instanceof LocalTime == false) {
1318             Objects.requireNonNull(endTime, "endTime");
1319             throw new DateTimeException("Unable to calculate period between objects of two different types");
1320         }
1321         LocalTime end = (LocalTime) endTime;
1322         if (unit instanceof ChronoUnit) {
1323             long nanosUntil = end.toNanoOfDay() - toNanoOfDay();  // no overflow
1324             switch ((ChronoUnit) unit) {
1325                 case NANOS: return nanosUntil;
1326                 case MICROS: return nanosUntil / 1000;
1327                 case MILLIS: return nanosUntil / 1000_000;
1328                 case SECONDS: return nanosUntil / NANOS_PER_SECOND;
1329                 case MINUTES: return nanosUntil / NANOS_PER_MINUTE;
1330                 case HOURS: return nanosUntil / NANOS_PER_HOUR;
1331                 case HALF_DAYS: return nanosUntil / (12 * NANOS_PER_HOUR);
1332             }
1333             throw new DateTimeException("Unsupported unit: " + unit.getName());
1334         }
1335         return unit.between(this, endTime).getAmount();
1336     }
1337 
1338     //-----------------------------------------------------------------------
1339     /**
1340      * Returns a local date-time formed from this time at the specified date.
1341      * <p>
1342      * This combines this time with the specified date to form a {@code LocalDateTime}.
1343      * All possible combinations of date and time are valid.
1344      * <p>
1345      * This instance is immutable and unaffected by this method call.
1346      *
1347      * @param date  the date to combine with, not null
1348      * @return the local date-time formed from this time and the specified date, not null
1349      */
1350     public LocalDateTime atDate(LocalDate date) {
1351         return LocalDateTime.of(date, this);
1352     }
1353 
1354     /**
1355      * Returns an offset time formed from this time and the specified offset.
1356      * <p>
1357      * This combines this time with the specified offset to form an {@code OffsetTime}.
1358      * All possible combinations of time and offset are valid.
1359      * <p>
1360      * This instance is immutable and unaffected by this method call.
1361      *
1362      * @param offset  the offset to combine with, not null
1363      * @return the offset time formed from this time and the specified offset, not null
1364      */
1365     public OffsetTime atOffset(ZoneOffset offset) {
1366         return OffsetTime.of(this, offset);
1367     }
1368 
1369     //-----------------------------------------------------------------------
1370     /**
1371      * Extracts the time as seconds of day,
1372      * from {@code 0} to {@code 24 * 60 * 60 - 1}.
1373      *
1374      * @return the second-of-day equivalent to this time
1375      */
1376     public int toSecondOfDay() {
1377         int total = hour * SECONDS_PER_HOUR;
1378         total += minute * SECONDS_PER_MINUTE;
1379         total += second;
1380         return total;


1512         if (secondValue > 0 || nanoValue > 0) {
1513             buf.append(secondValue < 10 ? ":0" : ":").append(secondValue);
1514             if (nanoValue > 0) {
1515                 buf.append('.');
1516                 if (nanoValue % 1000_000 == 0) {
1517                     buf.append(Integer.toString((nanoValue / 1000_000) + 1000).substring(1));
1518                 } else if (nanoValue % 1000 == 0) {
1519                     buf.append(Integer.toString((nanoValue / 1000) + 1000_000).substring(1));
1520                 } else {
1521                     buf.append(Integer.toString((nanoValue) + 1000_000_000).substring(1));
1522                 }
1523             }
1524         }
1525         return buf.toString();
1526     }
1527 
1528     /**
1529      * Outputs this time as a {@code String} using the formatter.
1530      * <p>
1531      * This time will be passed to the formatter
1532      * {@link DateTimeFormatter#print(TemporalAccessor) print method}.
1533      *
1534      * @param formatter  the formatter to use, not null
1535      * @return the formatted time string, not null
1536      * @throws DateTimeException if an error occurs during printing
1537      */
1538     public String toString(DateTimeFormatter formatter) {
1539         Objects.requireNonNull(formatter, "formatter");
1540         return formatter.print(this);
1541     }
1542 
1543     //-----------------------------------------------------------------------
1544     /**
1545      * Writes the object using a
1546      * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1547      * <pre>
1548      *  out.writeByte(4);  // identifies this as a LocalTime
1549      *  if (nano == 0) {
1550      *    if (second == 0) {
1551      *      if (minute == 0) {
1552      *        out.writeByte(~hour);
1553      *      } else {
1554      *        out.writeByte(hour);
1555      *        out.writeByte(~minute);
1556      *      }
1557      *    } else {
1558      *      out.writeByte(hour);
1559      *      out.writeByte(minute);
1560      *      out.writeByte(~second);




  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.ObjectStreamException;
  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.Queries;
  84 import java.time.temporal.Temporal;
  85 import java.time.temporal.TemporalAccessor;

  86 import java.time.temporal.TemporalAdjuster;
  87 import java.time.temporal.TemporalAmount;
  88 import java.time.temporal.TemporalField;
  89 import java.time.temporal.TemporalQuery;

  90 import java.time.temporal.TemporalUnit;
  91 import java.time.temporal.ValueRange;
  92 import java.util.Objects;
  93 
  94 /**
  95  * A time without time-zone in the ISO-8601 calendar system,
  96  * such as {@code 10:15:30}.
  97  * <p>
  98  * {@code LocalTime} is an immutable date-time object that represents a time,
  99  * often viewed as hour-minute-second.
 100  * Time is represented to nanosecond precision.
 101  * For example, the value "13:45.30.123456789" can be stored in a {@code LocalTime}.
 102  * <p>
 103  * It does not store or represent a date or time-zone.
 104  * Instead, it is a description of the local time as seen on a wall clock.
 105  * It cannot represent an instant on the time-line without additional information
 106  * such as an offset or time-zone.
 107  * <p>
 108  * The ISO-8601 calendar system is the modern civil calendar system used today
 109  * in most of the world. This API assumes that all calendar systems use the same
 110  * representation, this class, for time-of-day.
 111  *
 112  * <h3>Specification for implementors</h3>
 113  * This class is immutable and thread-safe.
 114  *
 115  * @since 1.8
 116  */
 117 public final class LocalTime
 118         implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable {
 119 
 120     /**
 121      * The minimum supported {@code LocalTime}, '00:00'.
 122      * This is the time of midnight at the start of the day.
 123      */
 124     public static final LocalTime MIN;
 125     /**
 126      * The maximum supported {@code LocalTime}, '23:59:59.999999999'.
 127      * This is the time just before midnight at the end of the day.
 128      */
 129     public static final LocalTime MAX;
 130     /**
 131      * The time of midnight at the start of the day, '00:00'.
 132      */
 133     public static final LocalTime MIDNIGHT;
 134     /**
 135      * The time of noon in the middle of the day, '12:00'.
 136      */
 137     public static final LocalTime NOON;
 138     /**
 139      * Constants for the local time of each hour.
 140      */
 141     private static final LocalTime[] HOURS = new LocalTime[24];
 142     static {
 143         for (int i = 0; i < HOURS.length; i++) {
 144             HOURS[i] = new LocalTime(i, 0, 0, 0);
 145         }
 146         MIDNIGHT = HOURS[0];


 250      */
 251     public static LocalTime now(ZoneId zone) {
 252         return now(Clock.system(zone));
 253     }
 254 
 255     /**
 256      * Obtains the current time from the specified clock.
 257      * <p>
 258      * This will query the specified clock to obtain the current time.
 259      * Using this method allows the use of an alternate clock for testing.
 260      * The alternate clock may be introduced using {@link Clock dependency injection}.
 261      *
 262      * @param clock  the clock to use, not null
 263      * @return the current time, not null
 264      */
 265     public static LocalTime now(Clock clock) {
 266         Objects.requireNonNull(clock, "clock");
 267         // inline OffsetTime factory to avoid creating object and InstantProvider checks
 268         final Instant now = clock.instant();  // called once
 269         ZoneOffset offset = clock.getZone().getRules().getOffset(now);
 270         long localSecond = now.getEpochSecond() + offset.getTotalSeconds();  // overflow caught later
 271         int secsOfDay = (int) Math.floorMod(localSecond, SECONDS_PER_DAY);
 272         return ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + now.getNano());



 273     }
 274 
 275     //------------------------get-----------------------------------------------
 276     /**
 277      * Obtains an instance of {@code LocalTime} from an hour and minute.
 278      * <p>
 279      * This returns a {@code LocalTime} with the specified hour and minute.
 280      * The second and nanosecond fields will be set to zero.

 281      *
 282      * @param hour  the hour-of-day to represent, from 0 to 23
 283      * @param minute  the minute-of-hour to represent, from 0 to 59
 284      * @return the local time, not null
 285      * @throws DateTimeException if the value of any field is out of range
 286      */
 287     public static LocalTime of(int hour, int minute) {
 288         HOUR_OF_DAY.checkValidValue(hour);
 289         if (minute == 0) {
 290             return HOURS[hour];  // for performance
 291         }
 292         MINUTE_OF_HOUR.checkValidValue(minute);
 293         return new LocalTime(hour, minute, 0, 0);
 294     }
 295 
 296     /**
 297      * Obtains an instance of {@code LocalTime} from an hour, minute and second.
 298      * <p>
 299      * This returns a {@code LocalTime} with the specified hour, minute and second.
 300      * The nanosecond field will be set to zero.

 301      *
 302      * @param hour  the hour-of-day to represent, from 0 to 23
 303      * @param minute  the minute-of-hour to represent, from 0 to 59
 304      * @param second  the second-of-minute to represent, from 0 to 59
 305      * @return the local time, not null
 306      * @throws DateTimeException if the value of any field is out of range
 307      */
 308     public static LocalTime of(int hour, int minute, int second) {
 309         HOUR_OF_DAY.checkValidValue(hour);
 310         if ((minute | second) == 0) {
 311             return HOURS[hour];  // for performance
 312         }
 313         MINUTE_OF_HOUR.checkValidValue(minute);
 314         SECOND_OF_MINUTE.checkValidValue(second);
 315         return new LocalTime(hour, minute, second, 0);
 316     }
 317 
 318     /**
 319      * Obtains an instance of {@code LocalTime} from an hour, minute, second and nanosecond.
 320      * <p>
 321      * This returns a {@code LocalTime} with the specified hour, minute, second and nanosecond.
 322      *
 323      * @param hour  the hour-of-day to represent, from 0 to 23
 324      * @param minute  the minute-of-hour to represent, from 0 to 59
 325      * @param second  the second-of-minute to represent, from 0 to 59
 326      * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
 327      * @return the local time, not null
 328      * @throws DateTimeException if the value of any field is out of range
 329      */
 330     public static LocalTime of(int hour, int minute, int second, int nanoOfSecond) {
 331         HOUR_OF_DAY.checkValidValue(hour);
 332         MINUTE_OF_HOUR.checkValidValue(minute);
 333         SECOND_OF_MINUTE.checkValidValue(second);
 334         NANO_OF_SECOND.checkValidValue(nanoOfSecond);
 335         return create(hour, minute, second, nanoOfSecond);
 336     }
 337 
 338     //-----------------------------------------------------------------------
 339     /**
 340      * Obtains an instance of {@code LocalTime} from a second-of-day value.
 341      * <p>
 342      * This returns a {@code LocalTime} with the specified second-of-day.
 343      * The nanosecond field will be set to zero.
 344      *
 345      * @param secondOfDay  the second-of-day, from {@code 0} to {@code 24 * 60 * 60 - 1}
 346      * @return the local time, not null
 347      * @throws DateTimeException if the second-of-day value is invalid
 348      */
 349     public static LocalTime ofSecondOfDay(long secondOfDay) {
 350         SECOND_OF_DAY.checkValidValue(secondOfDay);
 351         int hours = (int) (secondOfDay / SECONDS_PER_HOUR);
 352         secondOfDay -= hours * SECONDS_PER_HOUR;
 353         int minutes = (int) (secondOfDay / SECONDS_PER_MINUTE);
 354         secondOfDay -= minutes * SECONDS_PER_MINUTE;
 355         return create(hours, minutes, (int) secondOfDay, 0);
 356     }
 357 
 358     /**





















 359      * Obtains an instance of {@code LocalTime} from a nanos-of-day value.
 360      * <p>
 361      * This returns a {@code LocalTime} with the specified nanosecond-of-day.
 362      *
 363      * @param nanoOfDay  the nano of day, from {@code 0} to {@code 24 * 60 * 60 * 1,000,000,000 - 1}
 364      * @return the local time, not null
 365      * @throws DateTimeException if the nanos of day value is invalid
 366      */
 367     public static LocalTime ofNanoOfDay(long nanoOfDay) {
 368         NANO_OF_DAY.checkValidValue(nanoOfDay);
 369         int hours = (int) (nanoOfDay / NANOS_PER_HOUR);
 370         nanoOfDay -= hours * NANOS_PER_HOUR;
 371         int minutes = (int) (nanoOfDay / NANOS_PER_MINUTE);
 372         nanoOfDay -= minutes * NANOS_PER_MINUTE;
 373         int seconds = (int) (nanoOfDay / NANOS_PER_SECOND);
 374         nanoOfDay -= seconds * NANOS_PER_SECOND;
 375         return create(hours, minutes, seconds, (int) nanoOfDay);
 376     }
 377 
 378     //-----------------------------------------------------------------------
 379     /**
 380      * Obtains an instance of {@code LocalTime} from a temporal object.
 381      * <p>
 382      * This obtains a local time based on the specified temporal.
 383      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
 384      * which this factory converts to an instance of {@code LocalTime}.
 385      * <p>
 386      * The conversion uses the {@link Queries#localTime()} query, which relies
 387      * on extracting the {@link ChronoField#NANO_OF_DAY NANO_OF_DAY} field.
 388      * <p>
 389      * This method matches the signature of the functional interface {@link TemporalQuery}
 390      * allowing it to be used in queries via method reference, {@code LocalTime::from}.
 391      *
 392      * @param temporal  the temporal object to convert, not null
 393      * @return the local time, not null
 394      * @throws DateTimeException if unable to convert to a {@code LocalTime}
 395      */
 396     public static LocalTime from(TemporalAccessor temporal) {
 397         LocalTime time = temporal.query(Queries.localTime());
 398         if (time == null) {
 399             throw new DateTimeException("Unable to obtain LocalTime from TemporalAccessor: " + temporal.getClass());
















 400         }
 401         return time;
 402     }
 403 
 404     //-----------------------------------------------------------------------
 405     /**
 406      * Obtains an instance of {@code LocalTime} from a text string such as {@code 10:15}.
 407      * <p>
 408      * The string must represent a valid time and is parsed using
 409      * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_TIME}.
 410      *
 411      * @param text the text to parse such as "10:15:30", not null
 412      * @return the parsed local time, not null
 413      * @throws DateTimeParseException if the text cannot be parsed
 414      */
 415     public static LocalTime parse(CharSequence text) {
 416         return parse(text, DateTimeFormatter.ISO_LOCAL_TIME);
 417     }
 418 
 419     /**
 420      * Obtains an instance of {@code LocalTime} from a text string using a specific formatter.
 421      * <p>
 422      * The text is parsed using the formatter, returning a time.
 423      *
 424      * @param text  the text to parse, not null
 425      * @param formatter  the formatter to use, not null
 426      * @return the parsed local time, not null
 427      * @throws DateTimeParseException if the text cannot be parsed
 428      */
 429     public static LocalTime parse(CharSequence text, DateTimeFormatter formatter) {
 430         Objects.requireNonNull(formatter, "formatter");
 431         return formatter.parse(text, LocalTime::from);
 432     }
 433 
 434     //-----------------------------------------------------------------------
 435     /**
 436      * Creates a local time from the hour, minute, second and nanosecond fields.


 478      * <ul>
 479      * <li>{@code NANO_OF_SECOND}
 480      * <li>{@code NANO_OF_DAY}
 481      * <li>{@code MICRO_OF_SECOND}
 482      * <li>{@code MICRO_OF_DAY}
 483      * <li>{@code MILLI_OF_SECOND}
 484      * <li>{@code MILLI_OF_DAY}
 485      * <li>{@code SECOND_OF_MINUTE}
 486      * <li>{@code SECOND_OF_DAY}
 487      * <li>{@code MINUTE_OF_HOUR}
 488      * <li>{@code MINUTE_OF_DAY}
 489      * <li>{@code HOUR_OF_AMPM}
 490      * <li>{@code CLOCK_HOUR_OF_AMPM}
 491      * <li>{@code HOUR_OF_DAY}
 492      * <li>{@code CLOCK_HOUR_OF_DAY}
 493      * <li>{@code AMPM_OF_DAY}
 494      * </ul>
 495      * All other {@code ChronoField} instances will return false.
 496      * <p>
 497      * If the field is not a {@code ChronoField}, then the result of this method
 498      * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
 499      * passing {@code this} as the argument.
 500      * Whether the field is supported is determined by the field.
 501      *
 502      * @param field  the field to check, null returns false
 503      * @return true if the field is supported on this time, false if not
 504      */
 505     @Override
 506     public boolean isSupported(TemporalField field) {
 507         if (field instanceof ChronoField) {
 508             return ((ChronoField) field).isTimeField();
 509         }
 510         return field != null && field.isSupportedBy(this);
 511     }
 512 
 513     /**
 514      * Gets the range of valid values for the specified field.
 515      * <p>
 516      * The range object expresses the minimum and maximum valid values for a field.
 517      * This time is used to enhance the accuracy of the returned range.
 518      * If it is not possible to return the range, because the field is not supported
 519      * or for some other reason, an exception is thrown.
 520      * <p>
 521      * If the field is a {@link ChronoField} then the query is implemented here.
 522      * The {@link #isSupported(TemporalField) supported fields} will return
 523      * appropriate range instances.
 524      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 525      * <p>
 526      * If the field is not a {@code ChronoField}, then the result of this method
 527      * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
 528      * passing {@code this} as the argument.
 529      * Whether the range can be obtained is determined by the field.
 530      *
 531      * @param field  the field to query the range for, not null
 532      * @return the range of valid values for the field, not null
 533      * @throws DateTimeException if the range for the field cannot be obtained
 534      */
 535     @Override  // override for Javadoc
 536     public ValueRange range(TemporalField field) {
 537         return Temporal.super.range(field);
 538     }
 539 
 540     /**
 541      * Gets the value of the specified field from this time as an {@code int}.
 542      * <p>
 543      * This queries this time for the value for the specified field.
 544      * The returned value will always be within the valid range of values for the field.
 545      * If it is not possible to return the value, because the field is not supported
 546      * or for some other reason, an exception is thrown.
 547      * <p>
 548      * If the field is a {@link ChronoField} then the query is implemented here.
 549      * The {@link #isSupported(TemporalField) supported fields} will return valid
 550      * values based on this time, except {@code NANO_OF_DAY} and {@code MICRO_OF_DAY}
 551      * which are too large to fit in an {@code int} and throw a {@code DateTimeException}.
 552      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 553      * <p>
 554      * If the field is not a {@code ChronoField}, then the result of this method
 555      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 556      * passing {@code this} as the argument. Whether the value can be obtained,
 557      * and what the value represents, is determined by the field.
 558      *
 559      * @param field  the field to get, not null
 560      * @return the value for the field
 561      * @throws DateTimeException if a value for the field cannot be obtained
 562      * @throws ArithmeticException if numeric overflow occurs
 563      */
 564     @Override  // override for Javadoc and performance
 565     public int get(TemporalField field) {
 566         if (field instanceof ChronoField) {
 567             return get0(field);
 568         }
 569         return Temporal.super.get(field);
 570     }
 571 
 572     /**
 573      * Gets the value of the specified field from this time as a {@code long}.
 574      * <p>
 575      * This queries this time for the value for the specified field.
 576      * If it is not possible to return the value, because the field is not supported
 577      * or for some other reason, an exception is thrown.
 578      * <p>
 579      * If the field is a {@link ChronoField} then the query is implemented here.
 580      * The {@link #isSupported(TemporalField) supported fields} will return valid
 581      * values based on this time.
 582      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 583      * <p>
 584      * If the field is not a {@code ChronoField}, then the result of this method
 585      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
 586      * passing {@code this} as the argument. Whether the value can be obtained,
 587      * and what the value represents, is determined by the field.
 588      *
 589      * @param field  the field to get, not null
 590      * @return the value for the field
 591      * @throws DateTimeException if a value for the field cannot be obtained
 592      * @throws ArithmeticException if numeric overflow occurs
 593      */
 594     @Override
 595     public long getLong(TemporalField field) {
 596         if (field instanceof ChronoField) {
 597             if (field == NANO_OF_DAY) {
 598                 return toNanoOfDay();
 599             }
 600             if (field == MICRO_OF_DAY) {
 601                 return toNanoOfDay() / 1000;
 602             }
 603             return get0(field);
 604         }
 605         return field.getFrom(this);
 606     }
 607 
 608     private int get0(TemporalField field) {
 609         switch ((ChronoField) field) {
 610             case NANO_OF_SECOND: return nano;
 611             case NANO_OF_DAY: throw new DateTimeException("Field too large for an int: " + field);
 612             case MICRO_OF_SECOND: return nano / 1000;
 613             case MICRO_OF_DAY: throw new DateTimeException("Field too large for an int: " + field);
 614             case MILLI_OF_SECOND: return nano / 1000_000;
 615             case MILLI_OF_DAY: return (int) (toNanoOfDay() / 1000_000);
 616             case SECOND_OF_MINUTE: return second;
 617             case SECOND_OF_DAY: return toSecondOfDay();
 618             case MINUTE_OF_HOUR: return minute;
 619             case MINUTE_OF_DAY: return hour * 60 + minute;
 620             case HOUR_OF_AMPM: return hour % 12;
 621             case CLOCK_HOUR_OF_AMPM: int ham = hour % 12; return (ham % 12 == 0 ? 12 : ham);
 622             case HOUR_OF_DAY: return hour;
 623             case CLOCK_HOUR_OF_DAY: return (hour == 0 ? 24 : hour);
 624             case AMPM_OF_DAY: return hour / 12;
 625         }


 650      *
 651      * @return the second-of-minute, from 0 to 59
 652      */
 653     public int getSecond() {
 654         return second;
 655     }
 656 
 657     /**
 658      * Gets the nano-of-second field.
 659      *
 660      * @return the nano-of-second, from 0 to 999,999,999
 661      */
 662     public int getNano() {
 663         return nano;
 664     }
 665 
 666     //-----------------------------------------------------------------------
 667     /**
 668      * Returns an adjusted copy of this time.
 669      * <p>
 670      * This returns a {@code LocalTime}, based on this one, with the time adjusted.
 671      * The adjustment takes place using the specified adjuster strategy object.
 672      * Read the documentation of the adjuster to understand what adjustment will be made.
 673      * <p>
 674      * A simple adjuster might simply set the one of the fields, such as the hour field.
 675      * A more complex adjuster might set the time to the last hour of the day.
 676      * <p>
 677      * The result of this method is obtained by invoking the
 678      * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
 679      * specified adjuster passing {@code this} as the argument.
 680      * <p>
 681      * This instance is immutable and unaffected by this method call.
 682      *
 683      * @param adjuster the adjuster to use, not null
 684      * @return a {@code LocalTime} based on {@code this} with the adjustment made, not null
 685      * @throws DateTimeException if the adjustment cannot be made
 686      * @throws ArithmeticException if numeric overflow occurs
 687      */
 688     @Override
 689     public LocalTime with(TemporalAdjuster adjuster) {
 690         // optimizations
 691         if (adjuster instanceof LocalTime) {
 692             return (LocalTime) adjuster;
 693         }
 694         return (LocalTime) adjuster.adjustInto(this);
 695     }
 696 
 697     /**
 698      * Returns a copy of this time with the specified field set to a new value.
 699      * <p>
 700      * This returns a {@code LocalTime}, based on this one, with the value
 701      * for the specified field changed.
 702      * This can be used to change any supported field, such as the hour, minute or second.
 703      * If it is not possible to set the value, because the field is not supported or for
 704      * some other reason, an exception is thrown.
 705      * <p>
 706      * If the field is a {@link ChronoField} then the adjustment is implemented here.
 707      * The supported fields behave as follows:
 708      * <ul>
 709      * <li>{@code NANO_OF_SECOND} -
 710      *  Returns a {@code LocalTime} with the specified nano-of-second.
 711      *  The hour, minute and second will be unchanged.
 712      * <li>{@code NANO_OF_DAY} -
 713      *  Returns a {@code LocalTime} with the specified nano-of-day.
 714      *  This completely replaces the time and is equivalent to {@link #ofNanoOfDay(long)}.
 715      * <li>{@code MICRO_OF_SECOND} -
 716      *  Returns a {@code LocalTime} with the nano-of-second replaced by the specified
 717      *  micro-of-second multiplied by 1,000.
 718      *  The hour, minute and second will be unchanged.
 719      * <li>{@code MICRO_OF_DAY} -
 720      *  Returns a {@code LocalTime} with the specified micro-of-day.


 746      * <li>{@code CLOCK_HOUR_OF_AMPM} -
 747      *  Returns a {@code LocalTime} with the specified clock-hour-of-am-pm.
 748      *  The AM/PM, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
 749      * <li>{@code HOUR_OF_DAY} -
 750      *  Returns a {@code LocalTime} with the specified hour-of-day.
 751      *  The minute-of-hour, second-of-minute and nano-of-second will be unchanged.
 752      * <li>{@code CLOCK_HOUR_OF_DAY} -
 753      *  Returns a {@code LocalTime} with the specified clock-hour-of-day.
 754      *  The minute-of-hour, second-of-minute and nano-of-second will be unchanged.
 755      * <li>{@code AMPM_OF_DAY} -
 756      *  Returns a {@code LocalTime} with the specified AM/PM.
 757      *  The hour-of-am-pm, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
 758      * </ul>
 759      * <p>
 760      * In all cases, if the new value is outside the valid range of values for the field
 761      * then a {@code DateTimeException} will be thrown.
 762      * <p>
 763      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 764      * <p>
 765      * If the field is not a {@code ChronoField}, then the result of this method
 766      * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
 767      * passing {@code this} as the argument. In this case, the field determines
 768      * whether and how to adjust the instant.
 769      * <p>
 770      * This instance is immutable and unaffected by this method call.
 771      *
 772      * @param field  the field to set in the result, not null
 773      * @param newValue  the new value of the field in the result
 774      * @return a {@code LocalTime} based on {@code this} with the specified field set, not null
 775      * @throws DateTimeException if the field cannot be set
 776      * @throws ArithmeticException if numeric overflow occurs
 777      */
 778     @Override
 779     public LocalTime with(TemporalField field, long newValue) {
 780         if (field instanceof ChronoField) {
 781             ChronoField f = (ChronoField) field;
 782             f.checkValidValue(newValue);
 783             switch (f) {
 784                 case NANO_OF_SECOND: return withNano((int) newValue);
 785                 case NANO_OF_DAY: return LocalTime.ofNanoOfDay(newValue);
 786                 case MICRO_OF_SECOND: return withNano((int) newValue * 1000);
 787                 case MICRO_OF_DAY: return plusNanos((newValue - toNanoOfDay() / 1000) * 1000);
 788                 case MILLI_OF_SECOND: return withNano((int) newValue * 1000_000);
 789                 case MILLI_OF_DAY: return plusNanos((newValue - toNanoOfDay() / 1000_000) * 1000_000);
 790                 case SECOND_OF_MINUTE: return withSecond((int) newValue);
 791                 case SECOND_OF_DAY: return plusSeconds(newValue - toSecondOfDay());
 792                 case MINUTE_OF_HOUR: return withMinute((int) newValue);
 793                 case MINUTE_OF_DAY: return plusMinutes(newValue - (hour * 60 + minute));
 794                 case HOUR_OF_AMPM: return plusHours(newValue - (hour % 12));
 795                 case CLOCK_HOUR_OF_AMPM: return plusHours((newValue == 12 ? 0 : newValue) - (hour % 12));
 796                 case HOUR_OF_DAY: return withHour((int) newValue);
 797                 case CLOCK_HOUR_OF_DAY: return withHour((int) (newValue == 24 ? 0 : newValue));
 798                 case AMPM_OF_DAY: return plusHours((newValue - (hour / 12)) * 12);
 799             }
 800             throw new DateTimeException("Unsupported field: " + field.getName());
 801         }
 802         return field.adjustInto(this, newValue);
 803     }
 804 
 805     //-----------------------------------------------------------------------
 806     /**
 807      * Returns a copy of this {@code LocalTime} with the hour-of-day value altered.
 808      * <p>
 809      * This instance is immutable and unaffected by this method call.
 810      *
 811      * @param hour  the hour-of-day to set in the result, from 0 to 23
 812      * @return a {@code LocalTime} based on this time with the requested hour, not null
 813      * @throws DateTimeException if the hour value is invalid
 814      */
 815     public LocalTime withHour(int hour) {
 816         if (this.hour == hour) {
 817             return this;
 818         }
 819         HOUR_OF_DAY.checkValidValue(hour);
 820         return create(hour, minute, second, nano);
 821     }
 822 


 863      * @return a {@code LocalTime} based on this time with the requested nanosecond, not null
 864      * @throws DateTimeException if the nanos value is invalid
 865      */
 866     public LocalTime withNano(int nanoOfSecond) {
 867         if (this.nano == nanoOfSecond) {
 868             return this;
 869         }
 870         NANO_OF_SECOND.checkValidValue(nanoOfSecond);
 871         return create(hour, minute, second, nanoOfSecond);
 872     }
 873 
 874     //-----------------------------------------------------------------------
 875     /**
 876      * Returns a copy of this {@code LocalTime} with the time truncated.
 877      * <p>
 878      * Truncating the time returns a copy of the original time with fields
 879      * smaller than the specified unit set to zero.
 880      * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
 881      * will set the second-of-minute and nano-of-second field to zero.
 882      * <p>
 883      * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 884      * that divides into the length of a standard day without remainder.
 885      * This includes all supplied time units on {@link ChronoUnit} and
 886      * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
 887      * <p>
 888      * This instance is immutable and unaffected by this method call.
 889      *
 890      * @param unit  the unit to truncate to, not null
 891      * @return a {@code LocalTime} based on this time with the time truncated, not null
 892      * @throws DateTimeException if unable to truncate
 893      */
 894     public LocalTime truncatedTo(TemporalUnit unit) {
 895         if (unit == ChronoUnit.NANOS) {
 896             return this;




 897         }
 898         Duration unitDur = unit.getDuration();
 899         if (unitDur.getSeconds() > SECONDS_PER_DAY) {
 900             throw new DateTimeException("Unit is too large to be used for truncation");
 901         }
 902         long dur = unitDur.toNanos();
 903         if ((NANOS_PER_DAY % dur) != 0) {
 904             throw new DateTimeException("Unit must divide into a standard day without remainder");
 905         }
 906         long nod = toNanoOfDay();
 907         return ofNanoOfDay((nod / dur) * dur);
 908     }
 909 
 910     //-----------------------------------------------------------------------
 911     /**
 912      * Returns a copy of this time with the specified amount added.
 913      * <p>
 914      * This returns a {@code LocalTime}, based on this one, with the specified amount added.
 915      * The amount is typically {@link Duration} but may be any other type implementing
 916      * the {@link TemporalAmount} interface.
 917      * <p>
 918      * The calculation is delegated to the amount object by calling
 919      * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
 920      * to implement the addition in any way it wishes, however it typically
 921      * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
 922      * of the amount implementation to determine if it can be successfully added.
 923      * <p>
 924      * This instance is immutable and unaffected by this method call.
 925      *
 926      * @param amountToAdd  the amount to add, not null
 927      * @return a {@code LocalTime} based on this time with the addition made, not null
 928      * @throws DateTimeException if the addition cannot be made
 929      * @throws ArithmeticException if numeric overflow occurs
 930      */
 931     @Override
 932     public LocalTime plus(TemporalAmount amountToAdd) {
 933         return (LocalTime) amountToAdd.addTo(this);
 934     }
 935 
 936     /**
 937      * Returns a copy of this time with the specified amount added.
 938      * <p>
 939      * This returns a {@code LocalTime}, based on this one, with the amount
 940      * in terms of the unit added. If it is not possible to add the amount, because the
 941      * unit is not supported or for some other reason, an exception is thrown.
 942      * <p>
 943      * If the field is a {@link ChronoUnit} then the addition is implemented here.
 944      * The supported fields behave as follows:
 945      * <ul>
 946      * <li>{@code NANOS} -
 947      *  Returns a {@code LocalTime} with the specified number of nanoseconds added.
 948      *  This is equivalent to {@link #plusNanos(long)}.
 949      * <li>{@code MICROS} -
 950      *  Returns a {@code LocalTime} with the specified number of microseconds added.
 951      *  This is equivalent to {@link #plusNanos(long)} with the amount
 952      *  multiplied by 1,000.
 953      * <li>{@code MILLIS} -
 954      *  Returns a {@code LocalTime} with the specified number of milliseconds added.
 955      *  This is equivalent to {@link #plusNanos(long)} with the amount
 956      *  multiplied by 1,000,000.
 957      * <li>{@code SECONDS} -
 958      *  Returns a {@code LocalTime} with the specified number of seconds added.
 959      *  This is equivalent to {@link #plusSeconds(long)}.
 960      * <li>{@code MINUTES} -
 961      *  Returns a {@code LocalTime} with the specified number of minutes added.
 962      *  This is equivalent to {@link #plusMinutes(long)}.
 963      * <li>{@code HOURS} -
 964      *  Returns a {@code LocalTime} with the specified number of hours added.
 965      *  This is equivalent to {@link #plusHours(long)}.
 966      * <li>{@code HALF_DAYS} -
 967      *  Returns a {@code LocalTime} with the specified number of half-days added.
 968      *  This is equivalent to {@link #plusHours(long)} with the amount
 969      *  multiplied by 12.
 970      * <li>{@code DAYS} -
 971      *  Returns a {@code LocalTime} with the specified number of days added.
 972      *  This returns {@code this} time.
 973      * </ul>
 974      * <p>
 975      * All other {@code ChronoUnit} instances will throw a {@code DateTimeException}.
 976      * <p>
 977      * If the field is not a {@code ChronoUnit}, then the result of this method
 978      * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
 979      * passing {@code this} as the argument. In this case, the unit determines
 980      * whether and how to perform the addition.
 981      * <p>
 982      * This instance is immutable and unaffected by this method call.
 983      *
 984      * @param amountToAdd  the amount of the unit to add to the result, may be negative
 985      * @param unit  the unit of the amount to add, not null
 986      * @return a {@code LocalTime} based on this time with the specified amount added, not null
 987      * @throws DateTimeException if the addition cannot be made
 988      * @throws ArithmeticException if numeric overflow occurs
 989      */
 990     @Override
 991     public LocalTime plus(long amountToAdd, TemporalUnit unit) {
 992         if (unit instanceof ChronoUnit) {
 993             ChronoUnit f = (ChronoUnit) unit;
 994             switch (f) {
 995                 case NANOS: return plusNanos(amountToAdd);
 996                 case MICROS: return plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
 997                 case MILLIS: return plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
 998                 case SECONDS: return plusSeconds(amountToAdd);
 999                 case MINUTES: return plusMinutes(amountToAdd);
1000                 case HOURS: return plusHours(amountToAdd);
1001                 case HALF_DAYS: return plusHours((amountToAdd % 2) * 12);
1002                 case DAYS: return this;
1003             }
1004             throw new DateTimeException("Unsupported unit: " + unit.getName());
1005         }
1006         return unit.addTo(this, amountToAdd);
1007     }
1008 
1009     //-----------------------------------------------------------------------
1010     /**
1011      * Returns a copy of this {@code LocalTime} with the specified period in hours added.
1012      * <p>
1013      * This adds the specified number of hours to this time, returning a new time.
1014      * The calculation wraps around midnight.
1015      * <p>
1016      * This instance is immutable and unaffected by this method call.
1017      *
1018      * @param hoursToAdd  the hours to add, may be negative
1019      * @return a {@code LocalTime} based on this time with the hours added, not null
1020      */
1021     public LocalTime plusHours(long hoursToAdd) {
1022         if (hoursToAdd == 0) {
1023             return this;
1024         }
1025         int newHour = ((int) (hoursToAdd % HOURS_PER_DAY) + hour + HOURS_PER_DAY) % HOURS_PER_DAY;
1026         return create(newHour, minute, second, nano);


1090      * @return a {@code LocalTime} based on this time with the nanoseconds added, not null
1091      */
1092     public LocalTime plusNanos(long nanosToAdd) {
1093         if (nanosToAdd == 0) {
1094             return this;
1095         }
1096         long nofd = toNanoOfDay();
1097         long newNofd = ((nanosToAdd % NANOS_PER_DAY) + nofd + NANOS_PER_DAY) % NANOS_PER_DAY;
1098         if (nofd == newNofd) {
1099             return this;
1100         }
1101         int newHour = (int) (newNofd / NANOS_PER_HOUR);
1102         int newMinute = (int) ((newNofd / NANOS_PER_MINUTE) % MINUTES_PER_HOUR);
1103         int newSecond = (int) ((newNofd / NANOS_PER_SECOND) % SECONDS_PER_MINUTE);
1104         int newNano = (int) (newNofd % NANOS_PER_SECOND);
1105         return create(newHour, newMinute, newSecond, newNano);
1106     }
1107 
1108     //-----------------------------------------------------------------------
1109     /**
1110      * Returns a copy of this time with the specified amount subtracted.
1111      * <p>
1112      * This returns a {@code LocalTime}, based on this one, with the specified amount subtracted.
1113      * The amount is typically {@link Duration} but may be any other type implementing
1114      * the {@link TemporalAmount} interface.
1115      * <p>
1116      * The calculation is delegated to the amount object by calling
1117      * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
1118      * to implement the subtraction in any way it wishes, however it typically
1119      * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
1120      * of the amount implementation to determine if it can be successfully subtracted.
1121      * <p>
1122      * This instance is immutable and unaffected by this method call.
1123      *
1124      * @param amountToSubtract  the amount to subtract, not null
1125      * @return a {@code LocalTime} based on this time with the subtraction made, not null
1126      * @throws DateTimeException if the subtraction cannot be made
1127      * @throws ArithmeticException if numeric overflow occurs
1128      */
1129     @Override
1130     public LocalTime minus(TemporalAmount amountToSubtract) {
1131         return (LocalTime) amountToSubtract.subtractFrom(this);
1132     }
1133 
1134     /**
1135      * Returns a copy of this time with the specified amount subtracted.
1136      * <p>
1137      * This returns a {@code LocalTime}, based on this one, with the amount
1138      * in terms of the unit subtracted. If it is not possible to subtract the amount,
1139      * because the unit is not supported or for some other reason, an exception is thrown.
1140      * <p>
1141      * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
1142      * See that method for a full description of how addition, and thus subtraction, works.


1143      * <p>
1144      * This instance is immutable and unaffected by this method call.
1145      *
1146      * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
1147      * @param unit  the unit of the amount to subtract, not null
1148      * @return a {@code LocalTime} based on this time with the specified amount subtracted, not null
1149      * @throws DateTimeException if the subtraction cannot be made
1150      * @throws ArithmeticException if numeric overflow occurs
1151      */
1152     @Override
1153     public LocalTime minus(long amountToSubtract, TemporalUnit unit) {
1154         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
1155     }
1156 
1157     //-----------------------------------------------------------------------
1158     /**
1159      * Returns a copy of this {@code LocalTime} with the specified period in hours subtracted.
1160      * <p>
1161      * This subtracts the specified number of hours from this time, returning a new time.
1162      * The calculation wraps around midnight.
1163      * <p>
1164      * This instance is immutable and unaffected by this method call.
1165      *
1166      * @param hoursToSubtract  the hours to subtract, may be negative
1167      * @return a {@code LocalTime} based on this time with the hours subtracted, not null
1168      */
1169     public LocalTime minusHours(long hoursToSubtract) {
1170         return plusHours(-(hoursToSubtract % HOURS_PER_DAY));


1220      * Queries this time using the specified query.
1221      * <p>
1222      * This queries this time using the specified query strategy object.
1223      * The {@code TemporalQuery} object defines the logic to be used to
1224      * obtain the result. Read the documentation of the query to understand
1225      * what the result of this method will be.
1226      * <p>
1227      * The result of this method is obtained by invoking the
1228      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
1229      * specified query passing {@code this} as the argument.
1230      *
1231      * @param <R> the type of the result
1232      * @param query  the query to invoke, not null
1233      * @return the query result, null may be returned (defined by the query)
1234      * @throws DateTimeException if unable to query (defined by the query)
1235      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
1236      */
1237     @SuppressWarnings("unchecked")
1238     @Override
1239     public <R> R query(TemporalQuery<R> query) {
1240         if (query == Queries.chronology() || query == Queries.zoneId() || query == Queries.zone() || query == Queries.offset()) {
1241             return null;
1242         } else if (query == Queries.localTime()) {
1243             return (R) this;
1244         } else if (query == Queries.localDate()) {
1245             return null;
1246         } else if (query == Queries.precision()) {
1247             return (R) NANOS;
1248         }
1249         // inline TemporalAccessor.super.query(query) as an optimization
1250         // non-JDK classes are not permitted to make this optimization


1251         return query.queryFrom(this);
1252     }
1253 
1254     /**
1255      * Adjusts the specified temporal object to have the same time as this object.
1256      * <p>
1257      * This returns a temporal object of the same observable type as the input
1258      * with the time changed to be the same as this.
1259      * <p>
1260      * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
1261      * passing {@link ChronoField#NANO_OF_DAY} as the field.
1262      * <p>
1263      * In most cases, it is clearer to reverse the calling pattern by using
1264      * {@link Temporal#with(TemporalAdjuster)}:
1265      * <pre>
1266      *   // these two lines are equivalent, but the second approach is recommended
1267      *   temporal = thisLocalTime.adjustInto(temporal);
1268      *   temporal = temporal.with(thisLocalTime);
1269      * </pre>
1270      * <p>


1279     public Temporal adjustInto(Temporal temporal) {
1280         return temporal.with(NANO_OF_DAY, toNanoOfDay());
1281     }
1282 
1283     /**
1284      * Calculates the period between this time and another time in
1285      * terms of the specified unit.
1286      * <p>
1287      * This calculates the period between two times in terms of a single unit.
1288      * The start and end points are {@code this} and the specified time.
1289      * The result will be negative if the end is before the start.
1290      * The {@code Temporal} passed to this method must be a {@code LocalTime}.
1291      * For example, the period in hours between two times can be calculated
1292      * using {@code startTime.periodUntil(endTime, HOURS)}.
1293      * <p>
1294      * The calculation returns a whole number, representing the number of
1295      * complete units between the two times.
1296      * For example, the period in hours between 11:30 and 13:29 will only
1297      * be one hour as it is one minute short of two hours.
1298      * <p>
1299      * There are two equivalent ways of using this method.
1300      * The first is to invoke this method.
1301      * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:

1302      * <pre>
1303      *   // these two lines are equivalent
1304      *   amount = start.periodUntil(end, MINUTES);
1305      *   amount = MINUTES.between(start, end);
1306      * </pre>
1307      * The choice should be made based on which makes the code more readable.
1308      * <p>
1309      * The calculation is implemented in this method for {@link ChronoUnit}.
1310      * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
1311      * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS} are supported.
1312      * Other {@code ChronoUnit} values will throw an exception.
1313      * <p>
1314      * If the unit is not a {@code ChronoUnit}, then the result of this method
1315      * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
1316      * passing {@code this} as the first argument and the input temporal as
1317      * the second argument.
1318      * <p>
1319      * This instance is immutable and unaffected by this method call.
1320      *
1321      * @param endTime  the end time, which must be a {@code LocalTime}, not null
1322      * @param unit  the unit to measure the period in, not null
1323      * @return the amount of the period between this time and the end time
1324      * @throws DateTimeException if the period cannot be calculated
1325      * @throws ArithmeticException if numeric overflow occurs
1326      */
1327     @Override
1328     public long periodUntil(Temporal endTime, TemporalUnit unit) {
1329         if (endTime instanceof LocalTime == false) {
1330             Objects.requireNonNull(endTime, "endTime");
1331             throw new DateTimeException("Unable to calculate period between objects of two different types");
1332         }
1333         LocalTime end = (LocalTime) endTime;
1334         if (unit instanceof ChronoUnit) {
1335             long nanosUntil = end.toNanoOfDay() - toNanoOfDay();  // no overflow
1336             switch ((ChronoUnit) unit) {
1337                 case NANOS: return nanosUntil;
1338                 case MICROS: return nanosUntil / 1000;
1339                 case MILLIS: return nanosUntil / 1000_000;
1340                 case SECONDS: return nanosUntil / NANOS_PER_SECOND;
1341                 case MINUTES: return nanosUntil / NANOS_PER_MINUTE;
1342                 case HOURS: return nanosUntil / NANOS_PER_HOUR;
1343                 case HALF_DAYS: return nanosUntil / (12 * NANOS_PER_HOUR);
1344             }
1345             throw new DateTimeException("Unsupported unit: " + unit.getName());
1346         }
1347         return unit.between(this, endTime);
1348     }
1349 
1350     //-----------------------------------------------------------------------
1351     /**
1352      * Combines this time with a date to create a {@code LocalDateTime}.
1353      * <p>
1354      * This returns a {@code LocalDateTime} formed from this time at the specified date.
1355      * All possible combinations of date and time are valid.


1356      *
1357      * @param date  the date to combine with, not null
1358      * @return the local date-time formed from this time and the specified date, not null
1359      */
1360     public LocalDateTime atDate(LocalDate date) {
1361         return LocalDateTime.of(date, this);
1362     }
1363 
1364     /**
1365      * Combines this time with a date to create an {@code OffsetTime}.
1366      * <p>
1367      * This returns an {@code OffsetTime} formed from this time at the specified offset.
1368      * All possible combinations of time and offset are valid.


1369      *
1370      * @param offset  the offset to combine with, not null
1371      * @return the offset time formed from this time and the specified offset, not null
1372      */
1373     public OffsetTime atOffset(ZoneOffset offset) {
1374         return OffsetTime.of(this, offset);
1375     }
1376 
1377     //-----------------------------------------------------------------------
1378     /**
1379      * Extracts the time as seconds of day,
1380      * from {@code 0} to {@code 24 * 60 * 60 - 1}.
1381      *
1382      * @return the second-of-day equivalent to this time
1383      */
1384     public int toSecondOfDay() {
1385         int total = hour * SECONDS_PER_HOUR;
1386         total += minute * SECONDS_PER_MINUTE;
1387         total += second;
1388         return total;


1520         if (secondValue > 0 || nanoValue > 0) {
1521             buf.append(secondValue < 10 ? ":0" : ":").append(secondValue);
1522             if (nanoValue > 0) {
1523                 buf.append('.');
1524                 if (nanoValue % 1000_000 == 0) {
1525                     buf.append(Integer.toString((nanoValue / 1000_000) + 1000).substring(1));
1526                 } else if (nanoValue % 1000 == 0) {
1527                     buf.append(Integer.toString((nanoValue / 1000) + 1000_000).substring(1));
1528                 } else {
1529                     buf.append(Integer.toString((nanoValue) + 1000_000_000).substring(1));
1530                 }
1531             }
1532         }
1533         return buf.toString();
1534     }
1535 
1536     /**
1537      * Outputs this time as a {@code String} using the formatter.
1538      * <p>
1539      * This time will be passed to the formatter
1540      * {@link DateTimeFormatter#format(TemporalAccessor) format method}.
1541      *
1542      * @param formatter  the formatter to use, not null
1543      * @return the formatted time string, not null
1544      * @throws DateTimeException if an error occurs during printing
1545      */
1546     public String toString(DateTimeFormatter formatter) {
1547         Objects.requireNonNull(formatter, "formatter");
1548         return formatter.format(this);
1549     }
1550 
1551     //-----------------------------------------------------------------------
1552     /**
1553      * Writes the object using a
1554      * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1555      * <pre>
1556      *  out.writeByte(4);  // identifies this as a LocalTime
1557      *  if (nano == 0) {
1558      *    if (second == 0) {
1559      *      if (minute == 0) {
1560      *        out.writeByte(~hour);
1561      *      } else {
1562      *        out.writeByte(hour);
1563      *        out.writeByte(~minute);
1564      *      }
1565      *    } else {
1566      *      out.writeByte(hour);
1567      *      out.writeByte(minute);
1568      *      out.writeByte(~second);