Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/time/OffsetTime.java
38829 views
/*1* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos32*33* All rights reserved.34*35* Redistribution and use in source and binary forms, with or without36* modification, are permitted provided that the following conditions are met:37*38* * Redistributions of source code must retain the above copyright notice,39* this list of conditions and the following disclaimer.40*41* * Redistributions in binary form must reproduce the above copyright notice,42* this list of conditions and the following disclaimer in the documentation43* and/or other materials provided with the distribution.44*45* * Neither the name of JSR-310 nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS50* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT51* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR52* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR53* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,54* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,55* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR56* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF57* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING58* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS59* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.60*/61package java.time;6263import static java.time.LocalTime.NANOS_PER_HOUR;64import static java.time.LocalTime.NANOS_PER_MINUTE;65import static java.time.LocalTime.NANOS_PER_SECOND;66import static java.time.LocalTime.SECONDS_PER_DAY;67import static java.time.temporal.ChronoField.NANO_OF_DAY;68import static java.time.temporal.ChronoField.OFFSET_SECONDS;69import static java.time.temporal.ChronoUnit.NANOS;7071import java.io.IOException;72import java.io.ObjectInput;73import java.io.ObjectOutput;74import java.io.InvalidObjectException;75import java.io.ObjectInputStream;76import java.io.Serializable;77import java.time.format.DateTimeFormatter;78import java.time.format.DateTimeParseException;79import java.time.temporal.ChronoField;80import java.time.temporal.ChronoUnit;81import java.time.temporal.Temporal;82import java.time.temporal.TemporalAccessor;83import java.time.temporal.TemporalAdjuster;84import java.time.temporal.TemporalAmount;85import java.time.temporal.TemporalField;86import java.time.temporal.TemporalQueries;87import java.time.temporal.TemporalQuery;88import java.time.temporal.TemporalUnit;89import java.time.temporal.UnsupportedTemporalTypeException;90import java.time.temporal.ValueRange;91import java.time.zone.ZoneRules;92import java.util.Objects;9394/**95* A time with an offset from UTC/Greenwich in the ISO-8601 calendar system,96* such as {@code 10:15:30+01:00}.97* <p>98* {@code OffsetTime} is an immutable date-time object that represents a time, often99* viewed as hour-minute-second-offset.100* This class stores all time fields, to a precision of nanoseconds,101* as well as a zone offset.102* For example, the value "13:45.30.123456789+02:00" can be stored103* in an {@code OffsetTime}.104*105* <p>106* This is a <a href="{@docRoot}/java/lang/doc-files/ValueBased.html">value-based</a>107* class; use of identity-sensitive operations (including reference equality108* ({@code ==}), identity hash code, or synchronization) on instances of109* {@code OffsetTime} may have unpredictable results and should be avoided.110* The {@code equals} method should be used for comparisons.111*112* @implSpec113* This class is immutable and thread-safe.114*115* @since 1.8116*/117public final class OffsetTime118implements Temporal, TemporalAdjuster, Comparable<OffsetTime>, Serializable {119120/**121* The minimum supported {@code OffsetTime}, '00:00:00+18:00'.122* This is the time of midnight at the start of the day in the maximum offset123* (larger offsets are earlier on the time-line).124* This combines {@link LocalTime#MIN} and {@link ZoneOffset#MAX}.125* This could be used by an application as a "far past" date.126*/127public static final OffsetTime MIN = LocalTime.MIN.atOffset(ZoneOffset.MAX);128/**129* The maximum supported {@code OffsetTime}, '23:59:59.999999999-18:00'.130* This is the time just before midnight at the end of the day in the minimum offset131* (larger negative offsets are later on the time-line).132* This combines {@link LocalTime#MAX} and {@link ZoneOffset#MIN}.133* This could be used by an application as a "far future" date.134*/135public static final OffsetTime MAX = LocalTime.MAX.atOffset(ZoneOffset.MIN);136137/**138* Serialization version.139*/140private static final long serialVersionUID = 7264499704384272492L;141142/**143* The local date-time.144*/145private final LocalTime time;146/**147* The offset from UTC/Greenwich.148*/149private final ZoneOffset offset;150151//-----------------------------------------------------------------------152/**153* Obtains the current time from the system clock in the default time-zone.154* <p>155* This will query the {@link Clock#systemDefaultZone() system clock} in the default156* time-zone to obtain the current time.157* The offset will be calculated from the time-zone in the clock.158* <p>159* Using this method will prevent the ability to use an alternate clock for testing160* because the clock is hard-coded.161*162* @return the current time using the system clock and default time-zone, not null163*/164public static OffsetTime now() {165return now(Clock.systemDefaultZone());166}167168/**169* Obtains the current time from the system clock in the specified time-zone.170* <p>171* This will query the {@link Clock#system(ZoneId) system clock} to obtain the current time.172* Specifying the time-zone avoids dependence on the default time-zone.173* The offset will be calculated from the specified time-zone.174* <p>175* Using this method will prevent the ability to use an alternate clock for testing176* because the clock is hard-coded.177*178* @param zone the zone ID to use, not null179* @return the current time using the system clock, not null180*/181public static OffsetTime now(ZoneId zone) {182return now(Clock.system(zone));183}184185/**186* Obtains the current time from the specified clock.187* <p>188* This will query the specified clock to obtain the current time.189* The offset will be calculated from the time-zone in the clock.190* <p>191* Using this method allows the use of an alternate clock for testing.192* The alternate clock may be introduced using {@link Clock dependency injection}.193*194* @param clock the clock to use, not null195* @return the current time, not null196*/197public static OffsetTime now(Clock clock) {198Objects.requireNonNull(clock, "clock");199final Instant now = clock.instant(); // called once200return ofInstant(now, clock.getZone().getRules().getOffset(now));201}202203//-----------------------------------------------------------------------204/**205* Obtains an instance of {@code OffsetTime} from a local time and an offset.206*207* @param time the local time, not null208* @param offset the zone offset, not null209* @return the offset time, not null210*/211public static OffsetTime of(LocalTime time, ZoneOffset offset) {212return new OffsetTime(time, offset);213}214215/**216* Obtains an instance of {@code OffsetTime} from an hour, minute, second and nanosecond.217* <p>218* This creates an offset time with the four specified fields.219* <p>220* This method exists primarily for writing test cases.221* Non test-code will typically use other methods to create an offset time.222* {@code LocalTime} has two additional convenience variants of the223* equivalent factory method taking fewer arguments.224* They are not provided here to reduce the footprint of the API.225*226* @param hour the hour-of-day to represent, from 0 to 23227* @param minute the minute-of-hour to represent, from 0 to 59228* @param second the second-of-minute to represent, from 0 to 59229* @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999230* @param offset the zone offset, not null231* @return the offset time, not null232* @throws DateTimeException if the value of any field is out of range233*/234public static OffsetTime of(int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset) {235return new OffsetTime(LocalTime.of(hour, minute, second, nanoOfSecond), offset);236}237238//-----------------------------------------------------------------------239/**240* Obtains an instance of {@code OffsetTime} from an {@code Instant} and zone ID.241* <p>242* This creates an offset time with the same instant as that specified.243* Finding the offset from UTC/Greenwich is simple as there is only one valid244* offset for each instant.245* <p>246* The date component of the instant is dropped during the conversion.247* This means that the conversion can never fail due to the instant being248* out of the valid range of dates.249*250* @param instant the instant to create the time from, not null251* @param zone the time-zone, which may be an offset, not null252* @return the offset time, not null253*/254public static OffsetTime ofInstant(Instant instant, ZoneId zone) {255Objects.requireNonNull(instant, "instant");256Objects.requireNonNull(zone, "zone");257ZoneRules rules = zone.getRules();258ZoneOffset offset = rules.getOffset(instant);259long localSecond = instant.getEpochSecond() + offset.getTotalSeconds(); // overflow caught later260int secsOfDay = (int) Math.floorMod(localSecond, SECONDS_PER_DAY);261LocalTime time = LocalTime.ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + instant.getNano());262return new OffsetTime(time, offset);263}264265//-----------------------------------------------------------------------266/**267* Obtains an instance of {@code OffsetTime} from a temporal object.268* <p>269* This obtains an offset time based on the specified temporal.270* A {@code TemporalAccessor} represents an arbitrary set of date and time information,271* which this factory converts to an instance of {@code OffsetTime}.272* <p>273* The conversion extracts and combines the {@code ZoneOffset} and the274* {@code LocalTime} from the temporal object.275* Implementations are permitted to perform optimizations such as accessing276* those fields that are equivalent to the relevant objects.277* <p>278* This method matches the signature of the functional interface {@link TemporalQuery}279* allowing it to be used as a query via method reference, {@code OffsetTime::from}.280*281* @param temporal the temporal object to convert, not null282* @return the offset time, not null283* @throws DateTimeException if unable to convert to an {@code OffsetTime}284*/285public static OffsetTime from(TemporalAccessor temporal) {286if (temporal instanceof OffsetTime) {287return (OffsetTime) temporal;288}289try {290LocalTime time = LocalTime.from(temporal);291ZoneOffset offset = ZoneOffset.from(temporal);292return new OffsetTime(time, offset);293} catch (DateTimeException ex) {294throw new DateTimeException("Unable to obtain OffsetTime from TemporalAccessor: " +295temporal + " of type " + temporal.getClass().getName(), ex);296}297}298299//-----------------------------------------------------------------------300/**301* Obtains an instance of {@code OffsetTime} from a text string such as {@code 10:15:30+01:00}.302* <p>303* The string must represent a valid time and is parsed using304* {@link java.time.format.DateTimeFormatter#ISO_OFFSET_TIME}.305*306* @param text the text to parse such as "10:15:30+01:00", not null307* @return the parsed local time, not null308* @throws DateTimeParseException if the text cannot be parsed309*/310public static OffsetTime parse(CharSequence text) {311return parse(text, DateTimeFormatter.ISO_OFFSET_TIME);312}313314/**315* Obtains an instance of {@code OffsetTime} from a text string using a specific formatter.316* <p>317* The text is parsed using the formatter, returning a time.318*319* @param text the text to parse, not null320* @param formatter the formatter to use, not null321* @return the parsed offset time, not null322* @throws DateTimeParseException if the text cannot be parsed323*/324public static OffsetTime parse(CharSequence text, DateTimeFormatter formatter) {325Objects.requireNonNull(formatter, "formatter");326return formatter.parse(text, OffsetTime::from);327}328329//-----------------------------------------------------------------------330/**331* Constructor.332*333* @param time the local time, not null334* @param offset the zone offset, not null335*/336private OffsetTime(LocalTime time, ZoneOffset offset) {337this.time = Objects.requireNonNull(time, "time");338this.offset = Objects.requireNonNull(offset, "offset");339}340341/**342* Returns a new time based on this one, returning {@code this} where possible.343*344* @param time the time to create with, not null345* @param offset the zone offset to create with, not null346*/347private OffsetTime with(LocalTime time, ZoneOffset offset) {348if (this.time == time && this.offset.equals(offset)) {349return this;350}351return new OffsetTime(time, offset);352}353354//-----------------------------------------------------------------------355/**356* Checks if the specified field is supported.357* <p>358* This checks if this time can be queried for the specified field.359* If false, then calling the {@link #range(TemporalField) range},360* {@link #get(TemporalField) get} and {@link #with(TemporalField, long)}361* methods will throw an exception.362* <p>363* If the field is a {@link ChronoField} then the query is implemented here.364* The supported fields are:365* <ul>366* <li>{@code NANO_OF_SECOND}367* <li>{@code NANO_OF_DAY}368* <li>{@code MICRO_OF_SECOND}369* <li>{@code MICRO_OF_DAY}370* <li>{@code MILLI_OF_SECOND}371* <li>{@code MILLI_OF_DAY}372* <li>{@code SECOND_OF_MINUTE}373* <li>{@code SECOND_OF_DAY}374* <li>{@code MINUTE_OF_HOUR}375* <li>{@code MINUTE_OF_DAY}376* <li>{@code HOUR_OF_AMPM}377* <li>{@code CLOCK_HOUR_OF_AMPM}378* <li>{@code HOUR_OF_DAY}379* <li>{@code CLOCK_HOUR_OF_DAY}380* <li>{@code AMPM_OF_DAY}381* <li>{@code OFFSET_SECONDS}382* </ul>383* All other {@code ChronoField} instances will return false.384* <p>385* If the field is not a {@code ChronoField}, then the result of this method386* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}387* passing {@code this} as the argument.388* Whether the field is supported is determined by the field.389*390* @param field the field to check, null returns false391* @return true if the field is supported on this time, false if not392*/393@Override394public boolean isSupported(TemporalField field) {395if (field instanceof ChronoField) {396return field.isTimeBased() || field == OFFSET_SECONDS;397}398return field != null && field.isSupportedBy(this);399}400401/**402* Checks if the specified unit is supported.403* <p>404* This checks if the specified unit can be added to, or subtracted from, this offset-time.405* If false, then calling the {@link #plus(long, TemporalUnit)} and406* {@link #minus(long, TemporalUnit) minus} methods will throw an exception.407* <p>408* If the unit is a {@link ChronoUnit} then the query is implemented here.409* The supported units are:410* <ul>411* <li>{@code NANOS}412* <li>{@code MICROS}413* <li>{@code MILLIS}414* <li>{@code SECONDS}415* <li>{@code MINUTES}416* <li>{@code HOURS}417* <li>{@code HALF_DAYS}418* </ul>419* All other {@code ChronoUnit} instances will return false.420* <p>421* If the unit is not a {@code ChronoUnit}, then the result of this method422* is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}423* passing {@code this} as the argument.424* Whether the unit is supported is determined by the unit.425*426* @param unit the unit to check, null returns false427* @return true if the unit can be added/subtracted, false if not428*/429@Override // override for Javadoc430public boolean isSupported(TemporalUnit unit) {431if (unit instanceof ChronoUnit) {432return unit.isTimeBased();433}434return unit != null && unit.isSupportedBy(this);435}436437//-----------------------------------------------------------------------438/**439* Gets the range of valid values for the specified field.440* <p>441* The range object expresses the minimum and maximum valid values for a field.442* This time is used to enhance the accuracy of the returned range.443* If it is not possible to return the range, because the field is not supported444* or for some other reason, an exception is thrown.445* <p>446* If the field is a {@link ChronoField} then the query is implemented here.447* The {@link #isSupported(TemporalField) supported fields} will return448* appropriate range instances.449* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.450* <p>451* If the field is not a {@code ChronoField}, then the result of this method452* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}453* passing {@code this} as the argument.454* Whether the range can be obtained is determined by the field.455*456* @param field the field to query the range for, not null457* @return the range of valid values for the field, not null458* @throws DateTimeException if the range for the field cannot be obtained459* @throws UnsupportedTemporalTypeException if the field is not supported460*/461@Override462public ValueRange range(TemporalField field) {463if (field instanceof ChronoField) {464if (field == OFFSET_SECONDS) {465return field.range();466}467return time.range(field);468}469return field.rangeRefinedBy(this);470}471472/**473* Gets the value of the specified field from this time as an {@code int}.474* <p>475* This queries this time for the value of the specified field.476* The returned value will always be within the valid range of values for the field.477* If it is not possible to return the value, because the field is not supported478* or for some other reason, an exception is thrown.479* <p>480* If the field is a {@link ChronoField} then the query is implemented here.481* The {@link #isSupported(TemporalField) supported fields} will return valid482* values based on this time, except {@code NANO_OF_DAY} and {@code MICRO_OF_DAY}483* which are too large to fit in an {@code int} and throw a {@code DateTimeException}.484* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.485* <p>486* If the field is not a {@code ChronoField}, then the result of this method487* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}488* passing {@code this} as the argument. Whether the value can be obtained,489* and what the value represents, is determined by the field.490*491* @param field the field to get, not null492* @return the value for the field493* @throws DateTimeException if a value for the field cannot be obtained or494* the value is outside the range of valid values for the field495* @throws UnsupportedTemporalTypeException if the field is not supported or496* the range of values exceeds an {@code int}497* @throws ArithmeticException if numeric overflow occurs498*/499@Override // override for Javadoc500public int get(TemporalField field) {501return Temporal.super.get(field);502}503504/**505* Gets the value of the specified field from this time as a {@code long}.506* <p>507* This queries this time for the value of the specified field.508* If it is not possible to return the value, because the field is not supported509* or for some other reason, an exception is thrown.510* <p>511* If the field is a {@link ChronoField} then the query is implemented here.512* The {@link #isSupported(TemporalField) supported fields} will return valid513* values based on this time.514* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.515* <p>516* If the field is not a {@code ChronoField}, then the result of this method517* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}518* passing {@code this} as the argument. Whether the value can be obtained,519* and what the value represents, is determined by the field.520*521* @param field the field to get, not null522* @return the value for the field523* @throws DateTimeException if a value for the field cannot be obtained524* @throws UnsupportedTemporalTypeException if the field is not supported525* @throws ArithmeticException if numeric overflow occurs526*/527@Override528public long getLong(TemporalField field) {529if (field instanceof ChronoField) {530if (field == OFFSET_SECONDS) {531return offset.getTotalSeconds();532}533return time.getLong(field);534}535return field.getFrom(this);536}537538//-----------------------------------------------------------------------539/**540* Gets the zone offset, such as '+01:00'.541* <p>542* This is the offset of the local time from UTC/Greenwich.543*544* @return the zone offset, not null545*/546public ZoneOffset getOffset() {547return offset;548}549550/**551* Returns a copy of this {@code OffsetTime} with the specified offset ensuring552* that the result has the same local time.553* <p>554* This method returns an object with the same {@code LocalTime} and the specified {@code ZoneOffset}.555* No calculation is needed or performed.556* For example, if this time represents {@code 10:30+02:00} and the offset specified is557* {@code +03:00}, then this method will return {@code 10:30+03:00}.558* <p>559* To take into account the difference between the offsets, and adjust the time fields,560* use {@link #withOffsetSameInstant}.561* <p>562* This instance is immutable and unaffected by this method call.563*564* @param offset the zone offset to change to, not null565* @return an {@code OffsetTime} based on this time with the requested offset, not null566*/567public OffsetTime withOffsetSameLocal(ZoneOffset offset) {568return offset != null && offset.equals(this.offset) ? this : new OffsetTime(time, offset);569}570571/**572* Returns a copy of this {@code OffsetTime} with the specified offset ensuring573* that the result is at the same instant on an implied day.574* <p>575* This method returns an object with the specified {@code ZoneOffset} and a {@code LocalTime}576* adjusted by the difference between the two offsets.577* This will result in the old and new objects representing the same instant on an implied day.578* This is useful for finding the local time in a different offset.579* For example, if this time represents {@code 10:30+02:00} and the offset specified is580* {@code +03:00}, then this method will return {@code 11:30+03:00}.581* <p>582* To change the offset without adjusting the local time use {@link #withOffsetSameLocal}.583* <p>584* This instance is immutable and unaffected by this method call.585*586* @param offset the zone offset to change to, not null587* @return an {@code OffsetTime} based on this time with the requested offset, not null588*/589public OffsetTime withOffsetSameInstant(ZoneOffset offset) {590if (offset.equals(this.offset)) {591return this;592}593int difference = offset.getTotalSeconds() - this.offset.getTotalSeconds();594LocalTime adjusted = time.plusSeconds(difference);595return new OffsetTime(adjusted, offset);596}597598//-----------------------------------------------------------------------599/**600* Gets the {@code LocalTime} part of this date-time.601* <p>602* This returns a {@code LocalTime} with the same hour, minute, second and603* nanosecond as this date-time.604*605* @return the time part of this date-time, not null606*/607public LocalTime toLocalTime() {608return time;609}610611//-----------------------------------------------------------------------612/**613* Gets the hour-of-day field.614*615* @return the hour-of-day, from 0 to 23616*/617public int getHour() {618return time.getHour();619}620621/**622* Gets the minute-of-hour field.623*624* @return the minute-of-hour, from 0 to 59625*/626public int getMinute() {627return time.getMinute();628}629630/**631* Gets the second-of-minute field.632*633* @return the second-of-minute, from 0 to 59634*/635public int getSecond() {636return time.getSecond();637}638639/**640* Gets the nano-of-second field.641*642* @return the nano-of-second, from 0 to 999,999,999643*/644public int getNano() {645return time.getNano();646}647648//-----------------------------------------------------------------------649/**650* Returns an adjusted copy of this time.651* <p>652* This returns an {@code OffsetTime}, based on this one, with the time adjusted.653* The adjustment takes place using the specified adjuster strategy object.654* Read the documentation of the adjuster to understand what adjustment will be made.655* <p>656* A simple adjuster might simply set the one of the fields, such as the hour field.657* A more complex adjuster might set the time to the last hour of the day.658* <p>659* The classes {@link LocalTime} and {@link ZoneOffset} implement {@code TemporalAdjuster},660* thus this method can be used to change the time or offset:661* <pre>662* result = offsetTime.with(time);663* result = offsetTime.with(offset);664* </pre>665* <p>666* The result of this method is obtained by invoking the667* {@link TemporalAdjuster#adjustInto(Temporal)} method on the668* specified adjuster passing {@code this} as the argument.669* <p>670* This instance is immutable and unaffected by this method call.671*672* @param adjuster the adjuster to use, not null673* @return an {@code OffsetTime} based on {@code this} with the adjustment made, not null674* @throws DateTimeException if the adjustment cannot be made675* @throws ArithmeticException if numeric overflow occurs676*/677@Override678public OffsetTime with(TemporalAdjuster adjuster) {679// optimizations680if (adjuster instanceof LocalTime) {681return with((LocalTime) adjuster, offset);682} else if (adjuster instanceof ZoneOffset) {683return with(time, (ZoneOffset) adjuster);684} else if (adjuster instanceof OffsetTime) {685return (OffsetTime) adjuster;686}687return (OffsetTime) adjuster.adjustInto(this);688}689690/**691* Returns a copy of this time with the specified field set to a new value.692* <p>693* This returns an {@code OffsetTime}, based on this one, with the value694* for the specified field changed.695* This can be used to change any supported field, such as the hour, minute or second.696* If it is not possible to set the value, because the field is not supported or for697* some other reason, an exception is thrown.698* <p>699* If the field is a {@link ChronoField} then the adjustment is implemented here.700* <p>701* The {@code OFFSET_SECONDS} field will return a time with the specified offset.702* The local time is unaltered. If the new offset value is outside the valid range703* then a {@code DateTimeException} will be thrown.704* <p>705* The other {@link #isSupported(TemporalField) supported fields} will behave as per706* the matching method on {@link LocalTime#with(TemporalField, long)} LocalTime}.707* In this case, the offset is not part of the calculation and will be unchanged.708* <p>709* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.710* <p>711* If the field is not a {@code ChronoField}, then the result of this method712* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}713* passing {@code this} as the argument. In this case, the field determines714* whether and how to adjust the instant.715* <p>716* This instance is immutable and unaffected by this method call.717*718* @param field the field to set in the result, not null719* @param newValue the new value of the field in the result720* @return an {@code OffsetTime} based on {@code this} with the specified field set, not null721* @throws DateTimeException if the field cannot be set722* @throws UnsupportedTemporalTypeException if the field is not supported723* @throws ArithmeticException if numeric overflow occurs724*/725@Override726public OffsetTime with(TemporalField field, long newValue) {727if (field instanceof ChronoField) {728if (field == OFFSET_SECONDS) {729ChronoField f = (ChronoField) field;730return with(time, ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue)));731}732return with(time.with(field, newValue), offset);733}734return field.adjustInto(this, newValue);735}736737//-----------------------------------------------------------------------738/**739* Returns a copy of this {@code OffsetTime} with the hour-of-day altered.740* <p>741* The offset does not affect the calculation and will be the same in the result.742* <p>743* This instance is immutable and unaffected by this method call.744*745* @param hour the hour-of-day to set in the result, from 0 to 23746* @return an {@code OffsetTime} based on this time with the requested hour, not null747* @throws DateTimeException if the hour value is invalid748*/749public OffsetTime withHour(int hour) {750return with(time.withHour(hour), offset);751}752753/**754* Returns a copy of this {@code OffsetTime} with the minute-of-hour altered.755* <p>756* The offset does not affect the calculation and will be the same in the result.757* <p>758* This instance is immutable and unaffected by this method call.759*760* @param minute the minute-of-hour to set in the result, from 0 to 59761* @return an {@code OffsetTime} based on this time with the requested minute, not null762* @throws DateTimeException if the minute value is invalid763*/764public OffsetTime withMinute(int minute) {765return with(time.withMinute(minute), offset);766}767768/**769* Returns a copy of this {@code OffsetTime} with the second-of-minute altered.770* <p>771* The offset does not affect the calculation and will be the same in the result.772* <p>773* This instance is immutable and unaffected by this method call.774*775* @param second the second-of-minute to set in the result, from 0 to 59776* @return an {@code OffsetTime} based on this time with the requested second, not null777* @throws DateTimeException if the second value is invalid778*/779public OffsetTime withSecond(int second) {780return with(time.withSecond(second), offset);781}782783/**784* Returns a copy of this {@code OffsetTime} with the nano-of-second altered.785* <p>786* The offset does not affect the calculation and will be the same in the result.787* <p>788* This instance is immutable and unaffected by this method call.789*790* @param nanoOfSecond the nano-of-second to set in the result, from 0 to 999,999,999791* @return an {@code OffsetTime} based on this time with the requested nanosecond, not null792* @throws DateTimeException if the nanos value is invalid793*/794public OffsetTime withNano(int nanoOfSecond) {795return with(time.withNano(nanoOfSecond), offset);796}797798//-----------------------------------------------------------------------799/**800* Returns a copy of this {@code OffsetTime} with the time truncated.801* <p>802* Truncation returns a copy of the original time with fields803* smaller than the specified unit set to zero.804* For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit805* will set the second-of-minute and nano-of-second field to zero.806* <p>807* The unit must have a {@linkplain TemporalUnit#getDuration() duration}808* that divides into the length of a standard day without remainder.809* This includes all supplied time units on {@link ChronoUnit} and810* {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.811* <p>812* The offset does not affect the calculation and will be the same in the result.813* <p>814* This instance is immutable and unaffected by this method call.815*816* @param unit the unit to truncate to, not null817* @return an {@code OffsetTime} based on this time with the time truncated, not null818* @throws DateTimeException if unable to truncate819* @throws UnsupportedTemporalTypeException if the unit is not supported820*/821public OffsetTime truncatedTo(TemporalUnit unit) {822return with(time.truncatedTo(unit), offset);823}824825//-----------------------------------------------------------------------826/**827* Returns a copy of this time with the specified amount added.828* <p>829* This returns an {@code OffsetTime}, based on this one, with the specified amount added.830* The amount is typically {@link Duration} but may be any other type implementing831* the {@link TemporalAmount} interface.832* <p>833* The calculation is delegated to the amount object by calling834* {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free835* to implement the addition in any way it wishes, however it typically836* calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation837* of the amount implementation to determine if it can be successfully added.838* <p>839* This instance is immutable and unaffected by this method call.840*841* @param amountToAdd the amount to add, not null842* @return an {@code OffsetTime} based on this time with the addition made, not null843* @throws DateTimeException if the addition cannot be made844* @throws ArithmeticException if numeric overflow occurs845*/846@Override847public OffsetTime plus(TemporalAmount amountToAdd) {848return (OffsetTime) amountToAdd.addTo(this);849}850851/**852* Returns a copy of this time with the specified amount added.853* <p>854* This returns an {@code OffsetTime}, based on this one, with the amount855* in terms of the unit added. If it is not possible to add the amount, because the856* unit is not supported or for some other reason, an exception is thrown.857* <p>858* If the field is a {@link ChronoUnit} then the addition is implemented by859* {@link LocalTime#plus(long, TemporalUnit)}.860* The offset is not part of the calculation and will be unchanged in the result.861* <p>862* If the field is not a {@code ChronoUnit}, then the result of this method863* is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}864* passing {@code this} as the argument. In this case, the unit determines865* whether and how to perform the addition.866* <p>867* This instance is immutable and unaffected by this method call.868*869* @param amountToAdd the amount of the unit to add to the result, may be negative870* @param unit the unit of the amount to add, not null871* @return an {@code OffsetTime} based on this time with the specified amount added, not null872* @throws DateTimeException if the addition cannot be made873* @throws UnsupportedTemporalTypeException if the unit is not supported874* @throws ArithmeticException if numeric overflow occurs875*/876@Override877public OffsetTime plus(long amountToAdd, TemporalUnit unit) {878if (unit instanceof ChronoUnit) {879return with(time.plus(amountToAdd, unit), offset);880}881return unit.addTo(this, amountToAdd);882}883884//-----------------------------------------------------------------------885/**886* Returns a copy of this {@code OffsetTime} with the specified number of hours added.887* <p>888* This adds the specified number of hours to this time, returning a new time.889* The calculation wraps around midnight.890* <p>891* This instance is immutable and unaffected by this method call.892*893* @param hours the hours to add, may be negative894* @return an {@code OffsetTime} based on this time with the hours added, not null895*/896public OffsetTime plusHours(long hours) {897return with(time.plusHours(hours), offset);898}899900/**901* Returns a copy of this {@code OffsetTime} with the specified number of minutes added.902* <p>903* This adds the specified number of minutes to this time, returning a new time.904* The calculation wraps around midnight.905* <p>906* This instance is immutable and unaffected by this method call.907*908* @param minutes the minutes to add, may be negative909* @return an {@code OffsetTime} based on this time with the minutes added, not null910*/911public OffsetTime plusMinutes(long minutes) {912return with(time.plusMinutes(minutes), offset);913}914915/**916* Returns a copy of this {@code OffsetTime} with the specified number of seconds added.917* <p>918* This adds the specified number of seconds to this time, returning a new time.919* The calculation wraps around midnight.920* <p>921* This instance is immutable and unaffected by this method call.922*923* @param seconds the seconds to add, may be negative924* @return an {@code OffsetTime} based on this time with the seconds added, not null925*/926public OffsetTime plusSeconds(long seconds) {927return with(time.plusSeconds(seconds), offset);928}929930/**931* Returns a copy of this {@code OffsetTime} with the specified number of nanoseconds added.932* <p>933* This adds the specified number of nanoseconds to this time, returning a new time.934* The calculation wraps around midnight.935* <p>936* This instance is immutable and unaffected by this method call.937*938* @param nanos the nanos to add, may be negative939* @return an {@code OffsetTime} based on this time with the nanoseconds added, not null940*/941public OffsetTime plusNanos(long nanos) {942return with(time.plusNanos(nanos), offset);943}944945//-----------------------------------------------------------------------946/**947* Returns a copy of this time with the specified amount subtracted.948* <p>949* This returns an {@code OffsetTime}, based on this one, with the specified amount subtracted.950* The amount is typically {@link Duration} but may be any other type implementing951* the {@link TemporalAmount} interface.952* <p>953* The calculation is delegated to the amount object by calling954* {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free955* to implement the subtraction in any way it wishes, however it typically956* calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation957* of the amount implementation to determine if it can be successfully subtracted.958* <p>959* This instance is immutable and unaffected by this method call.960*961* @param amountToSubtract the amount to subtract, not null962* @return an {@code OffsetTime} based on this time with the subtraction made, not null963* @throws DateTimeException if the subtraction cannot be made964* @throws ArithmeticException if numeric overflow occurs965*/966@Override967public OffsetTime minus(TemporalAmount amountToSubtract) {968return (OffsetTime) amountToSubtract.subtractFrom(this);969}970971/**972* Returns a copy of this time with the specified amount subtracted.973* <p>974* This returns an {@code OffsetTime}, based on this one, with the amount975* in terms of the unit subtracted. If it is not possible to subtract the amount,976* because the unit is not supported or for some other reason, an exception is thrown.977* <p>978* This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.979* See that method for a full description of how addition, and thus subtraction, works.980* <p>981* This instance is immutable and unaffected by this method call.982*983* @param amountToSubtract the amount of the unit to subtract from the result, may be negative984* @param unit the unit of the amount to subtract, not null985* @return an {@code OffsetTime} based on this time with the specified amount subtracted, not null986* @throws DateTimeException if the subtraction cannot be made987* @throws UnsupportedTemporalTypeException if the unit is not supported988* @throws ArithmeticException if numeric overflow occurs989*/990@Override991public OffsetTime minus(long amountToSubtract, TemporalUnit unit) {992return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));993}994995//-----------------------------------------------------------------------996/**997* Returns a copy of this {@code OffsetTime} with the specified number of hours subtracted.998* <p>999* This subtracts the specified number of hours from this time, returning a new time.1000* The calculation wraps around midnight.1001* <p>1002* This instance is immutable and unaffected by this method call.1003*1004* @param hours the hours to subtract, may be negative1005* @return an {@code OffsetTime} based on this time with the hours subtracted, not null1006*/1007public OffsetTime minusHours(long hours) {1008return with(time.minusHours(hours), offset);1009}10101011/**1012* Returns a copy of this {@code OffsetTime} with the specified number of minutes subtracted.1013* <p>1014* This subtracts the specified number of minutes from this time, returning a new time.1015* The calculation wraps around midnight.1016* <p>1017* This instance is immutable and unaffected by this method call.1018*1019* @param minutes the minutes to subtract, may be negative1020* @return an {@code OffsetTime} based on this time with the minutes subtracted, not null1021*/1022public OffsetTime minusMinutes(long minutes) {1023return with(time.minusMinutes(minutes), offset);1024}10251026/**1027* Returns a copy of this {@code OffsetTime} with the specified number of seconds subtracted.1028* <p>1029* This subtracts the specified number of seconds from this time, returning a new time.1030* The calculation wraps around midnight.1031* <p>1032* This instance is immutable and unaffected by this method call.1033*1034* @param seconds the seconds to subtract, may be negative1035* @return an {@code OffsetTime} based on this time with the seconds subtracted, not null1036*/1037public OffsetTime minusSeconds(long seconds) {1038return with(time.minusSeconds(seconds), offset);1039}10401041/**1042* Returns a copy of this {@code OffsetTime} with the specified number of nanoseconds subtracted.1043* <p>1044* This subtracts the specified number of nanoseconds from this time, returning a new time.1045* The calculation wraps around midnight.1046* <p>1047* This instance is immutable and unaffected by this method call.1048*1049* @param nanos the nanos to subtract, may be negative1050* @return an {@code OffsetTime} based on this time with the nanoseconds subtracted, not null1051*/1052public OffsetTime minusNanos(long nanos) {1053return with(time.minusNanos(nanos), offset);1054}10551056//-----------------------------------------------------------------------1057/**1058* Queries this time using the specified query.1059* <p>1060* This queries this time using the specified query strategy object.1061* The {@code TemporalQuery} object defines the logic to be used to1062* obtain the result. Read the documentation of the query to understand1063* what the result of this method will be.1064* <p>1065* The result of this method is obtained by invoking the1066* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the1067* specified query passing {@code this} as the argument.1068*1069* @param <R> the type of the result1070* @param query the query to invoke, not null1071* @return the query result, null may be returned (defined by the query)1072* @throws DateTimeException if unable to query (defined by the query)1073* @throws ArithmeticException if numeric overflow occurs (defined by the query)1074*/1075@SuppressWarnings("unchecked")1076@Override1077public <R> R query(TemporalQuery<R> query) {1078if (query == TemporalQueries.offset() || query == TemporalQueries.zone()) {1079return (R) offset;1080} else if (query == TemporalQueries.zoneId() | query == TemporalQueries.chronology() || query == TemporalQueries.localDate()) {1081return null;1082} else if (query == TemporalQueries.localTime()) {1083return (R) time;1084} else if (query == TemporalQueries.precision()) {1085return (R) NANOS;1086}1087// inline TemporalAccessor.super.query(query) as an optimization1088// non-JDK classes are not permitted to make this optimization1089return query.queryFrom(this);1090}10911092/**1093* Adjusts the specified temporal object to have the same offset and time1094* as this object.1095* <p>1096* This returns a temporal object of the same observable type as the input1097* with the offset and time changed to be the same as this.1098* <p>1099* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}1100* twice, passing {@link ChronoField#NANO_OF_DAY} and1101* {@link ChronoField#OFFSET_SECONDS} as the fields.1102* <p>1103* In most cases, it is clearer to reverse the calling pattern by using1104* {@link Temporal#with(TemporalAdjuster)}:1105* <pre>1106* // these two lines are equivalent, but the second approach is recommended1107* temporal = thisOffsetTime.adjustInto(temporal);1108* temporal = temporal.with(thisOffsetTime);1109* </pre>1110* <p>1111* This instance is immutable and unaffected by this method call.1112*1113* @param temporal the target object to be adjusted, not null1114* @return the adjusted object, not null1115* @throws DateTimeException if unable to make the adjustment1116* @throws ArithmeticException if numeric overflow occurs1117*/1118@Override1119public Temporal adjustInto(Temporal temporal) {1120return temporal1121.with(NANO_OF_DAY, time.toNanoOfDay())1122.with(OFFSET_SECONDS, offset.getTotalSeconds());1123}11241125/**1126* Calculates the amount of time until another time in terms of the specified unit.1127* <p>1128* This calculates the amount of time between two {@code OffsetTime}1129* objects in terms of a single {@code TemporalUnit}.1130* The start and end points are {@code this} and the specified time.1131* The result will be negative if the end is before the start.1132* For example, the amount in hours between two times can be calculated1133* using {@code startTime.until(endTime, HOURS)}.1134* <p>1135* The {@code Temporal} passed to this method is converted to a1136* {@code OffsetTime} using {@link #from(TemporalAccessor)}.1137* If the offset differs between the two times, then the specified1138* end time is normalized to have the same offset as this time.1139* <p>1140* The calculation returns a whole number, representing the number of1141* complete units between the two times.1142* For example, the amount in hours between 11:30Z and 13:29Z will only1143* be one hour as it is one minute short of two hours.1144* <p>1145* There are two equivalent ways of using this method.1146* The first is to invoke this method.1147* The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:1148* <pre>1149* // these two lines are equivalent1150* amount = start.until(end, MINUTES);1151* amount = MINUTES.between(start, end);1152* </pre>1153* The choice should be made based on which makes the code more readable.1154* <p>1155* The calculation is implemented in this method for {@link ChronoUnit}.1156* The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},1157* {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS} are supported.1158* Other {@code ChronoUnit} values will throw an exception.1159* <p>1160* If the unit is not a {@code ChronoUnit}, then the result of this method1161* is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}1162* passing {@code this} as the first argument and the converted input temporal1163* as the second argument.1164* <p>1165* This instance is immutable and unaffected by this method call.1166*1167* @param endExclusive the end time, exclusive, which is converted to an {@code OffsetTime}, not null1168* @param unit the unit to measure the amount in, not null1169* @return the amount of time between this time and the end time1170* @throws DateTimeException if the amount cannot be calculated, or the end1171* temporal cannot be converted to an {@code OffsetTime}1172* @throws UnsupportedTemporalTypeException if the unit is not supported1173* @throws ArithmeticException if numeric overflow occurs1174*/1175@Override1176public long until(Temporal endExclusive, TemporalUnit unit) {1177OffsetTime end = OffsetTime.from(endExclusive);1178if (unit instanceof ChronoUnit) {1179long nanosUntil = end.toEpochNano() - toEpochNano(); // no overflow1180switch ((ChronoUnit) unit) {1181case NANOS: return nanosUntil;1182case MICROS: return nanosUntil / 1000;1183case MILLIS: return nanosUntil / 1000_000;1184case SECONDS: return nanosUntil / NANOS_PER_SECOND;1185case MINUTES: return nanosUntil / NANOS_PER_MINUTE;1186case HOURS: return nanosUntil / NANOS_PER_HOUR;1187case HALF_DAYS: return nanosUntil / (12 * NANOS_PER_HOUR);1188}1189throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);1190}1191return unit.between(this, end);1192}11931194/**1195* Formats this time using the specified formatter.1196* <p>1197* This time will be passed to the formatter to produce a string.1198*1199* @param formatter the formatter to use, not null1200* @return the formatted time string, not null1201* @throws DateTimeException if an error occurs during printing1202*/1203public String format(DateTimeFormatter formatter) {1204Objects.requireNonNull(formatter, "formatter");1205return formatter.format(this);1206}12071208//-----------------------------------------------------------------------1209/**1210* Combines this time with a date to create an {@code OffsetDateTime}.1211* <p>1212* This returns an {@code OffsetDateTime} formed from this time and the specified date.1213* All possible combinations of date and time are valid.1214*1215* @param date the date to combine with, not null1216* @return the offset date-time formed from this time and the specified date, not null1217*/1218public OffsetDateTime atDate(LocalDate date) {1219return OffsetDateTime.of(date, time, offset);1220}12211222//-----------------------------------------------------------------------1223/**1224* Converts this time to epoch nanos based on 1970-01-01Z.1225*1226* @return the epoch nanos value1227*/1228private long toEpochNano() {1229long nod = time.toNanoOfDay();1230long offsetNanos = offset.getTotalSeconds() * NANOS_PER_SECOND;1231return nod - offsetNanos;1232}12331234//-----------------------------------------------------------------------1235/**1236* Compares this {@code OffsetTime} to another time.1237* <p>1238* The comparison is based first on the UTC equivalent instant, then on the local time.1239* It is "consistent with equals", as defined by {@link Comparable}.1240* <p>1241* For example, the following is the comparator order:1242* <ol>1243* <li>{@code 10:30+01:00}</li>1244* <li>{@code 11:00+01:00}</li>1245* <li>{@code 12:00+02:00}</li>1246* <li>{@code 11:30+01:00}</li>1247* <li>{@code 12:00+01:00}</li>1248* <li>{@code 12:30+01:00}</li>1249* </ol>1250* Values #2 and #3 represent the same instant on the time-line.1251* When two values represent the same instant, the local time is compared1252* to distinguish them. This step is needed to make the ordering1253* consistent with {@code equals()}.1254* <p>1255* To compare the underlying local time of two {@code TemporalAccessor} instances,1256* use {@link ChronoField#NANO_OF_DAY} as a comparator.1257*1258* @param other the other time to compare to, not null1259* @return the comparator value, negative if less, positive if greater1260* @throws NullPointerException if {@code other} is null1261*/1262@Override1263public int compareTo(OffsetTime other) {1264if (offset.equals(other.offset)) {1265return time.compareTo(other.time);1266}1267int compare = Long.compare(toEpochNano(), other.toEpochNano());1268if (compare == 0) {1269compare = time.compareTo(other.time);1270}1271return compare;1272}12731274//-----------------------------------------------------------------------1275/**1276* Checks if the instant of this {@code OffsetTime} is after that of the1277* specified time applying both times to a common date.1278* <p>1279* This method differs from the comparison in {@link #compareTo} in that it1280* only compares the instant of the time. This is equivalent to converting both1281* times to an instant using the same date and comparing the instants.1282*1283* @param other the other time to compare to, not null1284* @return true if this is after the instant of the specified time1285*/1286public boolean isAfter(OffsetTime other) {1287return toEpochNano() > other.toEpochNano();1288}12891290/**1291* Checks if the instant of this {@code OffsetTime} is before that of the1292* specified time applying both times to a common date.1293* <p>1294* This method differs from the comparison in {@link #compareTo} in that it1295* only compares the instant of the time. This is equivalent to converting both1296* times to an instant using the same date and comparing the instants.1297*1298* @param other the other time to compare to, not null1299* @return true if this is before the instant of the specified time1300*/1301public boolean isBefore(OffsetTime other) {1302return toEpochNano() < other.toEpochNano();1303}13041305/**1306* Checks if the instant of this {@code OffsetTime} is equal to that of the1307* specified time applying both times to a common date.1308* <p>1309* This method differs from the comparison in {@link #compareTo} and {@link #equals}1310* in that it only compares the instant of the time. This is equivalent to converting both1311* times to an instant using the same date and comparing the instants.1312*1313* @param other the other time to compare to, not null1314* @return true if this is equal to the instant of the specified time1315*/1316public boolean isEqual(OffsetTime other) {1317return toEpochNano() == other.toEpochNano();1318}13191320//-----------------------------------------------------------------------1321/**1322* Checks if this time is equal to another time.1323* <p>1324* The comparison is based on the local-time and the offset.1325* To compare for the same instant on the time-line, use {@link #isEqual(OffsetTime)}.1326* <p>1327* Only objects of type {@code OffsetTime} are compared, other types return false.1328* To compare the underlying local time of two {@code TemporalAccessor} instances,1329* use {@link ChronoField#NANO_OF_DAY} as a comparator.1330*1331* @param obj the object to check, null returns false1332* @return true if this is equal to the other time1333*/1334@Override1335public boolean equals(Object obj) {1336if (this == obj) {1337return true;1338}1339if (obj instanceof OffsetTime) {1340OffsetTime other = (OffsetTime) obj;1341return time.equals(other.time) && offset.equals(other.offset);1342}1343return false;1344}13451346/**1347* A hash code for this time.1348*1349* @return a suitable hash code1350*/1351@Override1352public int hashCode() {1353return time.hashCode() ^ offset.hashCode();1354}13551356//-----------------------------------------------------------------------1357/**1358* Outputs this time as a {@code String}, such as {@code 10:15:30+01:00}.1359* <p>1360* The output will be one of the following ISO-8601 formats:1361* <ul>1362* <li>{@code HH:mmXXXXX}</li>1363* <li>{@code HH:mm:ssXXXXX}</li>1364* <li>{@code HH:mm:ss.SSSXXXXX}</li>1365* <li>{@code HH:mm:ss.SSSSSSXXXXX}</li>1366* <li>{@code HH:mm:ss.SSSSSSSSSXXXXX}</li>1367* </ul>1368* The format used will be the shortest that outputs the full value of1369* the time where the omitted parts are implied to be zero.1370*1371* @return a string representation of this time, not null1372*/1373@Override1374public String toString() {1375return time.toString() + offset.toString();1376}13771378//-----------------------------------------------------------------------1379/**1380* Writes the object using a1381* <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.1382* @serialData1383* <pre>1384* out.writeByte(9); // identifies an OffsetTime1385* // the <a href="../../serialized-form.html#java.time.LocalTime">time</a> excluding the one byte header1386* // the <a href="../../serialized-form.html#java.time.ZoneOffset">offset</a> excluding the one byte header1387* </pre>1388*1389* @return the instance of {@code Ser}, not null1390*/1391private Object writeReplace() {1392return new Ser(Ser.OFFSET_TIME_TYPE, this);1393}13941395/**1396* Defend against malicious streams.1397*1398* @param s the stream to read1399* @throws InvalidObjectException always1400*/1401private void readObject(ObjectInputStream s) throws InvalidObjectException {1402throw new InvalidObjectException("Deserialization via serialization delegate");1403}14041405void writeExternal(ObjectOutput out) throws IOException {1406time.writeExternal(out);1407offset.writeExternal(out);1408}14091410static OffsetTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException {1411LocalTime time = LocalTime.readExternal(in);1412ZoneOffset offset = ZoneOffset.readExternal(in);1413return OffsetTime.of(time, offset);1414}14151416}141714181419