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