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