author | mduigou |
Tue, 05 Nov 2013 19:44:41 -0800 | |
changeset 21615 | 0231a565a5b7 |
parent 21331 | 8ee181e7e48b |
child 22081 | 86eb26ff8f2b |
permissions | -rw-r--r-- |
15658 | 1 |
/* |
2 |
* Copyright (c) 2012, 2013, 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.LocalTime.NANOS_PER_HOUR; |
|
65 |
import static java.time.LocalTime.NANOS_PER_MINUTE; |
|
66 |
import static java.time.LocalTime.NANOS_PER_SECOND; |
|
67 |
import static java.time.LocalTime.SECONDS_PER_DAY; |
|
68 |
import static java.time.temporal.ChronoField.NANO_OF_DAY; |
|
69 |
import static java.time.temporal.ChronoField.OFFSET_SECONDS; |
|
70 |
import static java.time.temporal.ChronoUnit.NANOS; |
|
71 |
||
72 |
import java.io.IOException; |
|
73 |
import java.io.ObjectInput; |
|
74 |
import java.io.ObjectOutput; |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
75 |
import java.io.InvalidObjectException; |
15658 | 76 |
import java.io.Serializable; |
77 |
import java.time.format.DateTimeFormatter; |
|
78 |
import java.time.format.DateTimeParseException; |
|
79 |
import java.time.temporal.ChronoField; |
|
80 |
import java.time.temporal.ChronoUnit; |
|
81 |
import java.time.temporal.Temporal; |
|
82 |
import java.time.temporal.TemporalAccessor; |
|
83 |
import java.time.temporal.TemporalAdjuster; |
|
84 |
import java.time.temporal.TemporalAmount; |
|
85 |
import java.time.temporal.TemporalField; |
|
20795 | 86 |
import java.time.temporal.TemporalQueries; |
15658 | 87 |
import java.time.temporal.TemporalQuery; |
88 |
import java.time.temporal.TemporalUnit; |
|
16852 | 89 |
import java.time.temporal.UnsupportedTemporalTypeException; |
15658 | 90 |
import java.time.temporal.ValueRange; |
91 |
import java.time.zone.ZoneRules; |
|
92 |
import java.util.Objects; |
|
93 |
||
94 |
/** |
|
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, often |
|
99 |
* 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 stored |
|
103 |
* in an {@code OffsetTime}. |
|
104 |
* |
|
17474 | 105 |
* @implSpec |
15658 | 106 |
* This class is immutable and thread-safe. |
107 |
* |
|
108 |
* @since 1.8 |
|
109 |
*/ |
|
110 |
public final class OffsetTime |
|
111 |
implements Temporal, TemporalAdjuster, Comparable<OffsetTime>, Serializable { |
|
112 |
||
113 |
/** |
|
114 |
* The minimum supported {@code OffsetTime}, '00:00:00+18:00'. |
|
115 |
* This is the time of midnight at the start of the day in the maximum offset |
|
116 |
* (larger offsets are earlier on the time-line). |
|
117 |
* This combines {@link LocalTime#MIN} and {@link ZoneOffset#MAX}. |
|
118 |
* This could be used by an application as a "far past" date. |
|
119 |
*/ |
|
120 |
public static final OffsetTime MIN = LocalTime.MIN.atOffset(ZoneOffset.MAX); |
|
121 |
/** |
|
122 |
* The maximum supported {@code OffsetTime}, '23:59:59.999999999-18:00'. |
|
123 |
* This is the time just before midnight at the end of the day in the minimum offset |
|
124 |
* (larger negative offsets are later on the time-line). |
|
125 |
* This combines {@link LocalTime#MAX} and {@link ZoneOffset#MIN}. |
|
126 |
* This could be used by an application as a "far future" date. |
|
127 |
*/ |
|
128 |
public static final OffsetTime MAX = LocalTime.MAX.atOffset(ZoneOffset.MIN); |
|
129 |
||
130 |
/** |
|
131 |
* Serialization version. |
|
132 |
*/ |
|
133 |
private static final long serialVersionUID = 7264499704384272492L; |
|
134 |
||
135 |
/** |
|
136 |
* The local date-time. |
|
137 |
*/ |
|
138 |
private final LocalTime time; |
|
139 |
/** |
|
140 |
* The offset from UTC/Greenwich. |
|
141 |
*/ |
|
142 |
private final ZoneOffset offset; |
|
143 |
||
144 |
//----------------------------------------------------------------------- |
|
145 |
/** |
|
146 |
* Obtains the current time from the system clock in the default time-zone. |
|
147 |
* <p> |
|
148 |
* This will query the {@link java.time.Clock#systemDefaultZone() system clock} in the default |
|
149 |
* time-zone to obtain the current time. |
|
150 |
* The offset will be calculated from the time-zone in the clock. |
|
151 |
* <p> |
|
152 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
153 |
* because the clock is hard-coded. |
|
154 |
* |
|
155 |
* @return the current time using the system clock, not null |
|
156 |
*/ |
|
157 |
public static OffsetTime now() { |
|
158 |
return now(Clock.systemDefaultZone()); |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Obtains the current time from the system clock in the specified time-zone. |
|
163 |
* <p> |
|
164 |
* This will query the {@link Clock#system(java.time.ZoneId) system clock} to obtain the current time. |
|
165 |
* Specifying the time-zone avoids dependence on the default time-zone. |
|
166 |
* The offset will be calculated from the specified time-zone. |
|
167 |
* <p> |
|
168 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
169 |
* because the clock is hard-coded. |
|
170 |
* |
|
171 |
* @param zone the zone ID to use, not null |
|
172 |
* @return the current time using the system clock, not null |
|
173 |
*/ |
|
174 |
public static OffsetTime now(ZoneId zone) { |
|
175 |
return now(Clock.system(zone)); |
|
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* Obtains the current time from the specified clock. |
|
180 |
* <p> |
|
181 |
* This will query the specified clock to obtain the current time. |
|
182 |
* The offset will be calculated from the time-zone in the clock. |
|
183 |
* <p> |
|
184 |
* Using this method allows the use of an alternate clock for testing. |
|
185 |
* The alternate clock may be introduced using {@link Clock dependency injection}. |
|
186 |
* |
|
187 |
* @param clock the clock to use, not null |
|
188 |
* @return the current time, not null |
|
189 |
*/ |
|
190 |
public static OffsetTime now(Clock clock) { |
|
191 |
Objects.requireNonNull(clock, "clock"); |
|
192 |
final Instant now = clock.instant(); // called once |
|
193 |
return ofInstant(now, clock.getZone().getRules().getOffset(now)); |
|
194 |
} |
|
195 |
||
196 |
//----------------------------------------------------------------------- |
|
197 |
/** |
|
198 |
* Obtains an instance of {@code OffsetTime} from a local time and an offset. |
|
199 |
* |
|
200 |
* @param time the local time, not null |
|
201 |
* @param offset the zone offset, not null |
|
202 |
* @return the offset time, not null |
|
203 |
*/ |
|
204 |
public static OffsetTime of(LocalTime time, ZoneOffset offset) { |
|
205 |
return new OffsetTime(time, offset); |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
* Obtains an instance of {@code OffsetTime} from an hour, minute, second and nanosecond. |
|
210 |
* <p> |
|
211 |
* This creates an offset time with the four specified fields. |
|
212 |
* <p> |
|
213 |
* This method exists primarily for writing test cases. |
|
214 |
* Non test-code will typically use other methods to create an offset time. |
|
215 |
* {@code LocalTime} has two additional convenience variants of the |
|
216 |
* equivalent factory method taking fewer arguments. |
|
217 |
* They are not provided here to reduce the footprint of the API. |
|
218 |
* |
|
219 |
* @param hour the hour-of-day to represent, from 0 to 23 |
|
220 |
* @param minute the minute-of-hour to represent, from 0 to 59 |
|
221 |
* @param second the second-of-minute to represent, from 0 to 59 |
|
222 |
* @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 |
|
223 |
* @param offset the zone offset, not null |
|
224 |
* @return the offset time, not null |
|
225 |
* @throws DateTimeException if the value of any field is out of range |
|
226 |
*/ |
|
227 |
public static OffsetTime of(int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset) { |
|
228 |
return new OffsetTime(LocalTime.of(hour, minute, second, nanoOfSecond), offset); |
|
229 |
} |
|
230 |
||
231 |
//----------------------------------------------------------------------- |
|
232 |
/** |
|
233 |
* Obtains an instance of {@code OffsetTime} from an {@code Instant} and zone ID. |
|
234 |
* <p> |
|
235 |
* This creates an offset time with the same instant as that specified. |
|
236 |
* Finding the offset from UTC/Greenwich is simple as there is only one valid |
|
237 |
* offset for each instant. |
|
238 |
* <p> |
|
239 |
* The date component of the instant is dropped during the conversion. |
|
240 |
* This means that the conversion can never fail due to the instant being |
|
241 |
* out of the valid range of dates. |
|
242 |
* |
|
243 |
* @param instant the instant to create the time from, not null |
|
244 |
* @param zone the time-zone, which may be an offset, not null |
|
245 |
* @return the offset time, not null |
|
246 |
*/ |
|
247 |
public static OffsetTime ofInstant(Instant instant, ZoneId zone) { |
|
248 |
Objects.requireNonNull(instant, "instant"); |
|
249 |
Objects.requireNonNull(zone, "zone"); |
|
250 |
ZoneRules rules = zone.getRules(); |
|
251 |
ZoneOffset offset = rules.getOffset(instant); |
|
252 |
long localSecond = instant.getEpochSecond() + offset.getTotalSeconds(); // overflow caught later |
|
253 |
int secsOfDay = (int) Math.floorMod(localSecond, SECONDS_PER_DAY); |
|
254 |
LocalTime time = LocalTime.ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + instant.getNano()); |
|
255 |
return new OffsetTime(time, offset); |
|
256 |
} |
|
257 |
||
258 |
//----------------------------------------------------------------------- |
|
259 |
/** |
|
260 |
* Obtains an instance of {@code OffsetTime} from a temporal object. |
|
261 |
* <p> |
|
262 |
* This obtains an offset time based on the specified temporal. |
|
263 |
* A {@code TemporalAccessor} represents an arbitrary set of date and time information, |
|
264 |
* which this factory converts to an instance of {@code OffsetTime}. |
|
265 |
* <p> |
|
266 |
* The conversion extracts and combines the {@code ZoneOffset} and the |
|
267 |
* {@code LocalTime} from the temporal object. |
|
268 |
* Implementations are permitted to perform optimizations such as accessing |
|
269 |
* those fields that are equivalent to the relevant objects. |
|
270 |
* <p> |
|
271 |
* This method matches the signature of the functional interface {@link TemporalQuery} |
|
272 |
* allowing it to be used in queries via method reference, {@code OffsetTime::from}. |
|
273 |
* |
|
274 |
* @param temporal the temporal object to convert, not null |
|
275 |
* @return the offset time, not null |
|
276 |
* @throws DateTimeException if unable to convert to an {@code OffsetTime} |
|
277 |
*/ |
|
278 |
public static OffsetTime from(TemporalAccessor temporal) { |
|
279 |
if (temporal instanceof OffsetTime) { |
|
280 |
return (OffsetTime) temporal; |
|
281 |
} |
|
282 |
try { |
|
283 |
LocalTime time = LocalTime.from(temporal); |
|
284 |
ZoneOffset offset = ZoneOffset.from(temporal); |
|
285 |
return new OffsetTime(time, offset); |
|
286 |
} catch (DateTimeException ex) { |
|
20747 | 287 |
throw new DateTimeException("Unable to obtain OffsetTime from TemporalAccessor: " + |
288 |
temporal + " of type " + temporal.getClass().getName(), ex); |
|
15658 | 289 |
} |
290 |
} |
|
291 |
||
292 |
//----------------------------------------------------------------------- |
|
293 |
/** |
|
294 |
* Obtains an instance of {@code OffsetTime} from a text string such as {@code 10:15:30+01:00}. |
|
295 |
* <p> |
|
296 |
* The string must represent a valid time and is parsed using |
|
297 |
* {@link java.time.format.DateTimeFormatter#ISO_OFFSET_TIME}. |
|
298 |
* |
|
299 |
* @param text the text to parse such as "10:15:30+01:00", not null |
|
300 |
* @return the parsed local time, not null |
|
301 |
* @throws DateTimeParseException if the text cannot be parsed |
|
302 |
*/ |
|
303 |
public static OffsetTime parse(CharSequence text) { |
|
304 |
return parse(text, DateTimeFormatter.ISO_OFFSET_TIME); |
|
305 |
} |
|
306 |
||
307 |
/** |
|
308 |
* Obtains an instance of {@code OffsetTime} from a text string using a specific formatter. |
|
309 |
* <p> |
|
310 |
* The text is parsed using the formatter, returning a time. |
|
311 |
* |
|
312 |
* @param text the text to parse, not null |
|
313 |
* @param formatter the formatter to use, not null |
|
314 |
* @return the parsed offset time, not null |
|
315 |
* @throws DateTimeParseException if the text cannot be parsed |
|
316 |
*/ |
|
317 |
public static OffsetTime parse(CharSequence text, DateTimeFormatter formatter) { |
|
318 |
Objects.requireNonNull(formatter, "formatter"); |
|
319 |
return formatter.parse(text, OffsetTime::from); |
|
320 |
} |
|
321 |
||
322 |
//----------------------------------------------------------------------- |
|
323 |
/** |
|
324 |
* Constructor. |
|
325 |
* |
|
326 |
* @param time the local time, not null |
|
327 |
* @param offset the zone offset, not null |
|
328 |
*/ |
|
329 |
private OffsetTime(LocalTime time, ZoneOffset offset) { |
|
330 |
this.time = Objects.requireNonNull(time, "time"); |
|
331 |
this.offset = Objects.requireNonNull(offset, "offset"); |
|
332 |
} |
|
333 |
||
334 |
/** |
|
335 |
* Returns a new time based on this one, returning {@code this} where possible. |
|
336 |
* |
|
337 |
* @param time the time to create with, not null |
|
338 |
* @param offset the zone offset to create with, not null |
|
339 |
*/ |
|
340 |
private OffsetTime with(LocalTime time, ZoneOffset offset) { |
|
341 |
if (this.time == time && this.offset.equals(offset)) { |
|
342 |
return this; |
|
343 |
} |
|
344 |
return new OffsetTime(time, offset); |
|
345 |
} |
|
346 |
||
347 |
//----------------------------------------------------------------------- |
|
348 |
/** |
|
349 |
* Checks if the specified field is supported. |
|
350 |
* <p> |
|
351 |
* This checks if this time can be queried for the specified field. |
|
19030 | 352 |
* If false, then calling the {@link #range(TemporalField) range}, |
353 |
* {@link #get(TemporalField) get} and {@link #with(TemporalField, long)} |
|
354 |
* methods will throw an exception. |
|
15658 | 355 |
* <p> |
356 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
357 |
* The supported fields are: |
|
358 |
* <ul> |
|
359 |
* <li>{@code NANO_OF_SECOND} |
|
360 |
* <li>{@code NANO_OF_DAY} |
|
361 |
* <li>{@code MICRO_OF_SECOND} |
|
362 |
* <li>{@code MICRO_OF_DAY} |
|
363 |
* <li>{@code MILLI_OF_SECOND} |
|
364 |
* <li>{@code MILLI_OF_DAY} |
|
365 |
* <li>{@code SECOND_OF_MINUTE} |
|
366 |
* <li>{@code SECOND_OF_DAY} |
|
367 |
* <li>{@code MINUTE_OF_HOUR} |
|
368 |
* <li>{@code MINUTE_OF_DAY} |
|
369 |
* <li>{@code HOUR_OF_AMPM} |
|
370 |
* <li>{@code CLOCK_HOUR_OF_AMPM} |
|
371 |
* <li>{@code HOUR_OF_DAY} |
|
372 |
* <li>{@code CLOCK_HOUR_OF_DAY} |
|
373 |
* <li>{@code AMPM_OF_DAY} |
|
374 |
* <li>{@code OFFSET_SECONDS} |
|
375 |
* </ul> |
|
376 |
* All other {@code ChronoField} instances will return false. |
|
377 |
* <p> |
|
378 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
379 |
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} |
|
380 |
* passing {@code this} as the argument. |
|
381 |
* Whether the field is supported is determined by the field. |
|
382 |
* |
|
383 |
* @param field the field to check, null returns false |
|
384 |
* @return true if the field is supported on this time, false if not |
|
385 |
*/ |
|
386 |
@Override |
|
387 |
public boolean isSupported(TemporalField field) { |
|
388 |
if (field instanceof ChronoField) { |
|
16852 | 389 |
return field.isTimeBased() || field == OFFSET_SECONDS; |
15658 | 390 |
} |
391 |
return field != null && field.isSupportedBy(this); |
|
392 |
} |
|
393 |
||
394 |
/** |
|
19030 | 395 |
* Checks if the specified unit is supported. |
396 |
* <p> |
|
397 |
* This checks if the specified unit can be added to, or subtracted from, this date-time. |
|
398 |
* If false, then calling the {@link #plus(long, TemporalUnit)} and |
|
399 |
* {@link #minus(long, TemporalUnit) minus} methods will throw an exception. |
|
400 |
* <p> |
|
401 |
* If the unit is a {@link ChronoUnit} then the query is implemented here. |
|
402 |
* The supported units are: |
|
403 |
* <ul> |
|
404 |
* <li>{@code NANOS} |
|
405 |
* <li>{@code MICROS} |
|
406 |
* <li>{@code MILLIS} |
|
407 |
* <li>{@code SECONDS} |
|
408 |
* <li>{@code MINUTES} |
|
409 |
* <li>{@code HOURS} |
|
410 |
* <li>{@code HALF_DAYS} |
|
411 |
* </ul> |
|
412 |
* All other {@code ChronoUnit} instances will return false. |
|
413 |
* <p> |
|
414 |
* If the unit is not a {@code ChronoUnit}, then the result of this method |
|
415 |
* is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)} |
|
416 |
* passing {@code this} as the argument. |
|
417 |
* Whether the unit is supported is determined by the unit. |
|
418 |
* |
|
419 |
* @param unit the unit to check, null returns false |
|
420 |
* @return true if the unit can be added/subtracted, false if not |
|
421 |
*/ |
|
422 |
@Override // override for Javadoc |
|
423 |
public boolean isSupported(TemporalUnit unit) { |
|
424 |
if (unit instanceof ChronoUnit) { |
|
425 |
return unit.isTimeBased(); |
|
426 |
} |
|
427 |
return unit != null && unit.isSupportedBy(this); |
|
428 |
} |
|
429 |
||
430 |
//----------------------------------------------------------------------- |
|
431 |
/** |
|
15658 | 432 |
* Gets the range of valid values for the specified field. |
433 |
* <p> |
|
434 |
* The range object expresses the minimum and maximum valid values for a field. |
|
435 |
* This time is used to enhance the accuracy of the returned range. |
|
436 |
* If it is not possible to return the range, because the field is not supported |
|
437 |
* or for some other reason, an exception is thrown. |
|
438 |
* <p> |
|
439 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
440 |
* The {@link #isSupported(TemporalField) supported fields} will return |
|
441 |
* appropriate range instances. |
|
16852 | 442 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15658 | 443 |
* <p> |
444 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
445 |
* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} |
|
446 |
* passing {@code this} as the argument. |
|
447 |
* Whether the range can be obtained is determined by the field. |
|
448 |
* |
|
449 |
* @param field the field to query the range for, not null |
|
450 |
* @return the range of valid values for the field, not null |
|
451 |
* @throws DateTimeException if the range for the field cannot be obtained |
|
16852 | 452 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15658 | 453 |
*/ |
454 |
@Override |
|
455 |
public ValueRange range(TemporalField field) { |
|
456 |
if (field instanceof ChronoField) { |
|
457 |
if (field == OFFSET_SECONDS) { |
|
458 |
return field.range(); |
|
459 |
} |
|
460 |
return time.range(field); |
|
461 |
} |
|
462 |
return field.rangeRefinedBy(this); |
|
463 |
} |
|
464 |
||
465 |
/** |
|
466 |
* Gets the value of the specified field from this time as an {@code int}. |
|
467 |
* <p> |
|
468 |
* This queries this time for the value for the specified field. |
|
469 |
* The returned value will always be within the valid range of values for the field. |
|
470 |
* If it is not possible to return the value, because the field is not supported |
|
471 |
* or for some other reason, an exception is thrown. |
|
472 |
* <p> |
|
473 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
474 |
* The {@link #isSupported(TemporalField) supported fields} will return valid |
|
475 |
* values based on this time, except {@code NANO_OF_DAY} and {@code MICRO_OF_DAY} |
|
476 |
* which are too large to fit in an {@code int} and throw a {@code DateTimeException}. |
|
16852 | 477 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15658 | 478 |
* <p> |
479 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
480 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
|
481 |
* passing {@code this} as the argument. Whether the value can be obtained, |
|
482 |
* and what the value represents, is determined by the field. |
|
483 |
* |
|
484 |
* @param field the field to get, not null |
|
485 |
* @return the value for the field |
|
16852 | 486 |
* @throws DateTimeException if a value for the field cannot be obtained or |
487 |
* the value is outside the range of valid values for the field |
|
488 |
* @throws UnsupportedTemporalTypeException if the field is not supported or |
|
489 |
* the range of values exceeds an {@code int} |
|
15658 | 490 |
* @throws ArithmeticException if numeric overflow occurs |
491 |
*/ |
|
492 |
@Override // override for Javadoc |
|
493 |
public int get(TemporalField field) { |
|
494 |
return Temporal.super.get(field); |
|
495 |
} |
|
496 |
||
497 |
/** |
|
498 |
* Gets the value of the specified field from this time as a {@code long}. |
|
499 |
* <p> |
|
500 |
* This queries this time for the value for the specified field. |
|
501 |
* If it is not possible to return the value, because the field is not supported |
|
502 |
* or for some other reason, an exception is thrown. |
|
503 |
* <p> |
|
504 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
505 |
* The {@link #isSupported(TemporalField) supported fields} will return valid |
|
506 |
* values based on this time. |
|
16852 | 507 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15658 | 508 |
* <p> |
509 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
510 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
|
511 |
* passing {@code this} as the argument. Whether the value can be obtained, |
|
512 |
* and what the value represents, is determined by the field. |
|
513 |
* |
|
514 |
* @param field the field to get, not null |
|
515 |
* @return the value for the field |
|
516 |
* @throws DateTimeException if a value for the field cannot be obtained |
|
16852 | 517 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15658 | 518 |
* @throws ArithmeticException if numeric overflow occurs |
519 |
*/ |
|
520 |
@Override |
|
521 |
public long getLong(TemporalField field) { |
|
522 |
if (field instanceof ChronoField) { |
|
523 |
if (field == OFFSET_SECONDS) { |
|
524 |
return offset.getTotalSeconds(); |
|
525 |
} |
|
526 |
return time.getLong(field); |
|
527 |
} |
|
528 |
return field.getFrom(this); |
|
529 |
} |
|
530 |
||
531 |
//----------------------------------------------------------------------- |
|
532 |
/** |
|
533 |
* Gets the zone offset, such as '+01:00'. |
|
534 |
* <p> |
|
535 |
* This is the offset of the local time from UTC/Greenwich. |
|
536 |
* |
|
537 |
* @return the zone offset, not null |
|
538 |
*/ |
|
539 |
public ZoneOffset getOffset() { |
|
540 |
return offset; |
|
541 |
} |
|
542 |
||
543 |
/** |
|
544 |
* Returns a copy of this {@code OffsetTime} with the specified offset ensuring |
|
545 |
* that the result has the same local time. |
|
546 |
* <p> |
|
547 |
* This method returns an object with the same {@code LocalTime} and the specified {@code ZoneOffset}. |
|
548 |
* No calculation is needed or performed. |
|
549 |
* For example, if this time represents {@code 10:30+02:00} and the offset specified is |
|
550 |
* {@code +03:00}, then this method will return {@code 10:30+03:00}. |
|
551 |
* <p> |
|
552 |
* To take into account the difference between the offsets, and adjust the time fields, |
|
553 |
* use {@link #withOffsetSameInstant}. |
|
554 |
* <p> |
|
555 |
* This instance is immutable and unaffected by this method call. |
|
556 |
* |
|
557 |
* @param offset the zone offset to change to, not null |
|
558 |
* @return an {@code OffsetTime} based on this time with the requested offset, not null |
|
559 |
*/ |
|
560 |
public OffsetTime withOffsetSameLocal(ZoneOffset offset) { |
|
561 |
return offset != null && offset.equals(this.offset) ? this : new OffsetTime(time, offset); |
|
562 |
} |
|
563 |
||
564 |
/** |
|
565 |
* Returns a copy of this {@code OffsetTime} with the specified offset ensuring |
|
566 |
* that the result is at the same instant on an implied day. |
|
567 |
* <p> |
|
568 |
* This method returns an object with the specified {@code ZoneOffset} and a {@code LocalTime} |
|
569 |
* adjusted by the difference between the two offsets. |
|
570 |
* This will result in the old and new objects representing the same instant an an implied day. |
|
571 |
* This is useful for finding the local time in a different offset. |
|
572 |
* For example, if this time represents {@code 10:30+02:00} and the offset specified is |
|
573 |
* {@code +03:00}, then this method will return {@code 11:30+03:00}. |
|
574 |
* <p> |
|
575 |
* To change the offset without adjusting the local time use {@link #withOffsetSameLocal}. |
|
576 |
* <p> |
|
577 |
* This instance is immutable and unaffected by this method call. |
|
578 |
* |
|
579 |
* @param offset the zone offset to change to, not null |
|
580 |
* @return an {@code OffsetTime} based on this time with the requested offset, not null |
|
581 |
*/ |
|
582 |
public OffsetTime withOffsetSameInstant(ZoneOffset offset) { |
|
583 |
if (offset.equals(this.offset)) { |
|
584 |
return this; |
|
585 |
} |
|
586 |
int difference = offset.getTotalSeconds() - this.offset.getTotalSeconds(); |
|
587 |
LocalTime adjusted = time.plusSeconds(difference); |
|
588 |
return new OffsetTime(adjusted, offset); |
|
589 |
} |
|
590 |
||
591 |
//----------------------------------------------------------------------- |
|
592 |
/** |
|
593 |
* Gets the {@code LocalTime} part of this date-time. |
|
594 |
* <p> |
|
595 |
* This returns a {@code LocalTime} with the same hour, minute, second and |
|
596 |
* nanosecond as this date-time. |
|
597 |
* |
|
598 |
* @return the time part of this date-time, not null |
|
599 |
*/ |
|
600 |
public LocalTime toLocalTime() { |
|
601 |
return time; |
|
602 |
} |
|
603 |
||
604 |
//----------------------------------------------------------------------- |
|
605 |
/** |
|
606 |
* Gets the hour-of-day field. |
|
607 |
* |
|
608 |
* @return the hour-of-day, from 0 to 23 |
|
609 |
*/ |
|
610 |
public int getHour() { |
|
611 |
return time.getHour(); |
|
612 |
} |
|
613 |
||
614 |
/** |
|
615 |
* Gets the minute-of-hour field. |
|
616 |
* |
|
617 |
* @return the minute-of-hour, from 0 to 59 |
|
618 |
*/ |
|
619 |
public int getMinute() { |
|
620 |
return time.getMinute(); |
|
621 |
} |
|
622 |
||
623 |
/** |
|
624 |
* Gets the second-of-minute field. |
|
625 |
* |
|
626 |
* @return the second-of-minute, from 0 to 59 |
|
627 |
*/ |
|
628 |
public int getSecond() { |
|
629 |
return time.getSecond(); |
|
630 |
} |
|
631 |
||
632 |
/** |
|
633 |
* Gets the nano-of-second field. |
|
634 |
* |
|
635 |
* @return the nano-of-second, from 0 to 999,999,999 |
|
636 |
*/ |
|
637 |
public int getNano() { |
|
638 |
return time.getNano(); |
|
639 |
} |
|
640 |
||
641 |
//----------------------------------------------------------------------- |
|
642 |
/** |
|
643 |
* Returns an adjusted copy of this time. |
|
644 |
* <p> |
|
645 |
* This returns an {@code OffsetTime}, based on this one, with the time adjusted. |
|
646 |
* The adjustment takes place using the specified adjuster strategy object. |
|
647 |
* Read the documentation of the adjuster to understand what adjustment will be made. |
|
648 |
* <p> |
|
649 |
* A simple adjuster might simply set the one of the fields, such as the hour field. |
|
650 |
* A more complex adjuster might set the time to the last hour of the day. |
|
651 |
* <p> |
|
652 |
* The classes {@link LocalTime} and {@link ZoneOffset} implement {@code TemporalAdjuster}, |
|
653 |
* thus this method can be used to change the time or offset: |
|
654 |
* <pre> |
|
655 |
* result = offsetTime.with(time); |
|
656 |
* result = offsetTime.with(offset); |
|
657 |
* </pre> |
|
658 |
* <p> |
|
659 |
* The result of this method is obtained by invoking the |
|
660 |
* {@link TemporalAdjuster#adjustInto(Temporal)} method on the |
|
661 |
* specified adjuster passing {@code this} as the argument. |
|
662 |
* <p> |
|
663 |
* This instance is immutable and unaffected by this method call. |
|
664 |
* |
|
665 |
* @param adjuster the adjuster to use, not null |
|
666 |
* @return an {@code OffsetTime} based on {@code this} with the adjustment made, not null |
|
667 |
* @throws DateTimeException if the adjustment cannot be made |
|
668 |
* @throws ArithmeticException if numeric overflow occurs |
|
669 |
*/ |
|
670 |
@Override |
|
671 |
public OffsetTime with(TemporalAdjuster adjuster) { |
|
672 |
// optimizations |
|
673 |
if (adjuster instanceof LocalTime) { |
|
674 |
return with((LocalTime) adjuster, offset); |
|
675 |
} else if (adjuster instanceof ZoneOffset) { |
|
676 |
return with(time, (ZoneOffset) adjuster); |
|
677 |
} else if (adjuster instanceof OffsetTime) { |
|
678 |
return (OffsetTime) adjuster; |
|
679 |
} |
|
680 |
return (OffsetTime) adjuster.adjustInto(this); |
|
681 |
} |
|
682 |
||
683 |
/** |
|
684 |
* Returns a copy of this time with the specified field set to a new value. |
|
685 |
* <p> |
|
686 |
* This returns an {@code OffsetTime}, based on this one, with the value |
|
687 |
* for the specified field changed. |
|
688 |
* This can be used to change any supported field, such as the hour, minute or second. |
|
689 |
* If it is not possible to set the value, because the field is not supported or for |
|
690 |
* some other reason, an exception is thrown. |
|
691 |
* <p> |
|
692 |
* If the field is a {@link ChronoField} then the adjustment is implemented here. |
|
693 |
* <p> |
|
694 |
* The {@code OFFSET_SECONDS} field will return a time with the specified offset. |
|
695 |
* The local time is unaltered. If the new offset value is outside the valid range |
|
696 |
* then a {@code DateTimeException} will be thrown. |
|
697 |
* <p> |
|
698 |
* The other {@link #isSupported(TemporalField) supported fields} will behave as per |
|
699 |
* the matching method on {@link LocalTime#with(TemporalField, long)} LocalTime}. |
|
700 |
* In this case, the offset is not part of the calculation and will be unchanged. |
|
701 |
* <p> |
|
16852 | 702 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15658 | 703 |
* <p> |
704 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
705 |
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} |
|
706 |
* passing {@code this} as the argument. In this case, the field determines |
|
707 |
* whether and how to adjust the instant. |
|
708 |
* <p> |
|
709 |
* This instance is immutable and unaffected by this method call. |
|
710 |
* |
|
711 |
* @param field the field to set in the result, not null |
|
712 |
* @param newValue the new value of the field in the result |
|
713 |
* @return an {@code OffsetTime} based on {@code this} with the specified field set, not null |
|
714 |
* @throws DateTimeException if the field cannot be set |
|
16852 | 715 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15658 | 716 |
* @throws ArithmeticException if numeric overflow occurs |
717 |
*/ |
|
718 |
@Override |
|
719 |
public OffsetTime with(TemporalField field, long newValue) { |
|
720 |
if (field instanceof ChronoField) { |
|
721 |
if (field == OFFSET_SECONDS) { |
|
722 |
ChronoField f = (ChronoField) field; |
|
723 |
return with(time, ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue))); |
|
724 |
} |
|
725 |
return with(time.with(field, newValue), offset); |
|
726 |
} |
|
727 |
return field.adjustInto(this, newValue); |
|
728 |
} |
|
729 |
||
730 |
//----------------------------------------------------------------------- |
|
731 |
/** |
|
732 |
* Returns a copy of this {@code OffsetTime} with the hour-of-day value altered. |
|
733 |
* <p> |
|
734 |
* The offset does not affect the calculation and will be the same in the result. |
|
735 |
* <p> |
|
736 |
* This instance is immutable and unaffected by this method call. |
|
737 |
* |
|
738 |
* @param hour the hour-of-day to set in the result, from 0 to 23 |
|
739 |
* @return an {@code OffsetTime} based on this time with the requested hour, not null |
|
740 |
* @throws DateTimeException if the hour value is invalid |
|
741 |
*/ |
|
742 |
public OffsetTime withHour(int hour) { |
|
743 |
return with(time.withHour(hour), offset); |
|
744 |
} |
|
745 |
||
746 |
/** |
|
747 |
* Returns a copy of this {@code OffsetTime} with the minute-of-hour value altered. |
|
748 |
* <p> |
|
749 |
* The offset does not affect the calculation and will be the same in the result. |
|
750 |
* <p> |
|
751 |
* This instance is immutable and unaffected by this method call. |
|
752 |
* |
|
753 |
* @param minute the minute-of-hour to set in the result, from 0 to 59 |
|
754 |
* @return an {@code OffsetTime} based on this time with the requested minute, not null |
|
755 |
* @throws DateTimeException if the minute value is invalid |
|
756 |
*/ |
|
757 |
public OffsetTime withMinute(int minute) { |
|
758 |
return with(time.withMinute(minute), offset); |
|
759 |
} |
|
760 |
||
761 |
/** |
|
762 |
* Returns a copy of this {@code OffsetTime} with the second-of-minute value altered. |
|
763 |
* <p> |
|
764 |
* The offset does not affect the calculation and will be the same in the result. |
|
765 |
* <p> |
|
766 |
* This instance is immutable and unaffected by this method call. |
|
767 |
* |
|
768 |
* @param second the second-of-minute to set in the result, from 0 to 59 |
|
769 |
* @return an {@code OffsetTime} based on this time with the requested second, not null |
|
770 |
* @throws DateTimeException if the second value is invalid |
|
771 |
*/ |
|
772 |
public OffsetTime withSecond(int second) { |
|
773 |
return with(time.withSecond(second), offset); |
|
774 |
} |
|
775 |
||
776 |
/** |
|
777 |
* Returns a copy of this {@code OffsetTime} with the nano-of-second value altered. |
|
778 |
* <p> |
|
779 |
* The offset does not affect the calculation and will be the same in the result. |
|
780 |
* <p> |
|
781 |
* This instance is immutable and unaffected by this method call. |
|
782 |
* |
|
783 |
* @param nanoOfSecond the nano-of-second to set in the result, from 0 to 999,999,999 |
|
784 |
* @return an {@code OffsetTime} based on this time with the requested nanosecond, not null |
|
785 |
* @throws DateTimeException if the nanos value is invalid |
|
786 |
*/ |
|
787 |
public OffsetTime withNano(int nanoOfSecond) { |
|
788 |
return with(time.withNano(nanoOfSecond), offset); |
|
789 |
} |
|
790 |
||
791 |
//----------------------------------------------------------------------- |
|
792 |
/** |
|
793 |
* Returns a copy of this {@code OffsetTime} with the time truncated. |
|
794 |
* <p> |
|
795 |
* Truncation returns a copy of the original time with fields |
|
796 |
* smaller than the specified unit set to zero. |
|
797 |
* For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit |
|
798 |
* will set the second-of-minute and nano-of-second field to zero. |
|
799 |
* <p> |
|
800 |
* The unit must have a {@linkplain TemporalUnit#getDuration() duration} |
|
801 |
* that divides into the length of a standard day without remainder. |
|
802 |
* This includes all supplied time units on {@link ChronoUnit} and |
|
803 |
* {@link ChronoUnit#DAYS DAYS}. Other units throw an exception. |
|
804 |
* <p> |
|
805 |
* The offset does not affect the calculation and will be the same in the result. |
|
806 |
* <p> |
|
807 |
* This instance is immutable and unaffected by this method call. |
|
808 |
* |
|
809 |
* @param unit the unit to truncate to, not null |
|
810 |
* @return an {@code OffsetTime} based on this time with the time truncated, not null |
|
811 |
* @throws DateTimeException if unable to truncate |
|
16852 | 812 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15658 | 813 |
*/ |
814 |
public OffsetTime truncatedTo(TemporalUnit unit) { |
|
815 |
return with(time.truncatedTo(unit), offset); |
|
816 |
} |
|
817 |
||
818 |
//----------------------------------------------------------------------- |
|
819 |
/** |
|
820 |
* Returns a copy of this time with the specified amount added. |
|
821 |
* <p> |
|
822 |
* This returns an {@code OffsetTime}, based on this one, with the specified amount added. |
|
823 |
* The amount is typically {@link Duration} but may be any other type implementing |
|
824 |
* the {@link TemporalAmount} interface. |
|
825 |
* <p> |
|
826 |
* The calculation is delegated to the amount object by calling |
|
827 |
* {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free |
|
828 |
* to implement the addition in any way it wishes, however it typically |
|
829 |
* calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation |
|
830 |
* of the amount implementation to determine if it can be successfully added. |
|
831 |
* <p> |
|
832 |
* This instance is immutable and unaffected by this method call. |
|
833 |
* |
|
834 |
* @param amountToAdd the amount to add, not null |
|
835 |
* @return an {@code OffsetTime} based on this time with the addition made, not null |
|
836 |
* @throws DateTimeException if the addition cannot be made |
|
837 |
* @throws ArithmeticException if numeric overflow occurs |
|
838 |
*/ |
|
839 |
@Override |
|
840 |
public OffsetTime plus(TemporalAmount amountToAdd) { |
|
841 |
return (OffsetTime) amountToAdd.addTo(this); |
|
842 |
} |
|
843 |
||
844 |
/** |
|
845 |
* Returns a copy of this time with the specified amount added. |
|
846 |
* <p> |
|
847 |
* This returns an {@code OffsetTime}, based on this one, with the amount |
|
848 |
* in terms of the unit added. If it is not possible to add the amount, because the |
|
849 |
* unit is not supported or for some other reason, an exception is thrown. |
|
850 |
* <p> |
|
851 |
* If the field is a {@link ChronoUnit} then the addition is implemented by |
|
852 |
* {@link LocalTime#plus(long, TemporalUnit)}. |
|
853 |
* The offset is not part of the calculation and will be unchanged in the result. |
|
854 |
* <p> |
|
855 |
* If the field is not a {@code ChronoUnit}, then the result of this method |
|
856 |
* is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)} |
|
857 |
* passing {@code this} as the argument. In this case, the unit determines |
|
858 |
* whether and how to perform the addition. |
|
859 |
* <p> |
|
860 |
* This instance is immutable and unaffected by this method call. |
|
861 |
* |
|
862 |
* @param amountToAdd the amount of the unit to add to the result, may be negative |
|
863 |
* @param unit the unit of the amount to add, not null |
|
864 |
* @return an {@code OffsetTime} based on this time with the specified amount added, not null |
|
865 |
* @throws DateTimeException if the addition cannot be made |
|
16852 | 866 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15658 | 867 |
* @throws ArithmeticException if numeric overflow occurs |
868 |
*/ |
|
869 |
@Override |
|
870 |
public OffsetTime plus(long amountToAdd, TemporalUnit unit) { |
|
871 |
if (unit instanceof ChronoUnit) { |
|
872 |
return with(time.plus(amountToAdd, unit), offset); |
|
873 |
} |
|
874 |
return unit.addTo(this, amountToAdd); |
|
875 |
} |
|
876 |
||
877 |
//----------------------------------------------------------------------- |
|
878 |
/** |
|
879 |
* Returns a copy of this {@code OffsetTime} with the specified period in hours added. |
|
880 |
* <p> |
|
881 |
* This adds the specified number of hours to this time, returning a new time. |
|
882 |
* The calculation wraps around midnight. |
|
883 |
* <p> |
|
884 |
* This instance is immutable and unaffected by this method call. |
|
885 |
* |
|
886 |
* @param hours the hours to add, may be negative |
|
887 |
* @return an {@code OffsetTime} based on this time with the hours added, not null |
|
888 |
*/ |
|
889 |
public OffsetTime plusHours(long hours) { |
|
890 |
return with(time.plusHours(hours), offset); |
|
891 |
} |
|
892 |
||
893 |
/** |
|
894 |
* Returns a copy of this {@code OffsetTime} with the specified period in minutes added. |
|
895 |
* <p> |
|
896 |
* This adds the specified number of minutes to this time, returning a new time. |
|
897 |
* The calculation wraps around midnight. |
|
898 |
* <p> |
|
899 |
* This instance is immutable and unaffected by this method call. |
|
900 |
* |
|
901 |
* @param minutes the minutes to add, may be negative |
|
902 |
* @return an {@code OffsetTime} based on this time with the minutes added, not null |
|
903 |
*/ |
|
904 |
public OffsetTime plusMinutes(long minutes) { |
|
905 |
return with(time.plusMinutes(minutes), offset); |
|
906 |
} |
|
907 |
||
908 |
/** |
|
909 |
* Returns a copy of this {@code OffsetTime} with the specified period in seconds added. |
|
910 |
* <p> |
|
911 |
* This adds the specified number of seconds to this time, returning a new time. |
|
912 |
* The calculation wraps around midnight. |
|
913 |
* <p> |
|
914 |
* This instance is immutable and unaffected by this method call. |
|
915 |
* |
|
916 |
* @param seconds the seconds to add, may be negative |
|
917 |
* @return an {@code OffsetTime} based on this time with the seconds added, not null |
|
918 |
*/ |
|
919 |
public OffsetTime plusSeconds(long seconds) { |
|
920 |
return with(time.plusSeconds(seconds), offset); |
|
921 |
} |
|
922 |
||
923 |
/** |
|
924 |
* Returns a copy of this {@code OffsetTime} with the specified period in nanoseconds added. |
|
925 |
* <p> |
|
926 |
* This adds the specified number of nanoseconds to this time, returning a new time. |
|
927 |
* The calculation wraps around midnight. |
|
928 |
* <p> |
|
929 |
* This instance is immutable and unaffected by this method call. |
|
930 |
* |
|
931 |
* @param nanos the nanos to add, may be negative |
|
932 |
* @return an {@code OffsetTime} based on this time with the nanoseconds added, not null |
|
933 |
*/ |
|
934 |
public OffsetTime plusNanos(long nanos) { |
|
935 |
return with(time.plusNanos(nanos), offset); |
|
936 |
} |
|
937 |
||
938 |
//----------------------------------------------------------------------- |
|
939 |
/** |
|
940 |
* Returns a copy of this time with the specified amount subtracted. |
|
941 |
* <p> |
|
942 |
* This returns an {@code OffsetTime}, based on this one, with the specified amount subtracted. |
|
943 |
* The amount is typically {@link Duration} but may be any other type implementing |
|
944 |
* the {@link TemporalAmount} interface. |
|
945 |
* <p> |
|
946 |
* The calculation is delegated to the amount object by calling |
|
947 |
* {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free |
|
948 |
* to implement the subtraction in any way it wishes, however it typically |
|
949 |
* calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation |
|
950 |
* of the amount implementation to determine if it can be successfully subtracted. |
|
951 |
* <p> |
|
952 |
* This instance is immutable and unaffected by this method call. |
|
953 |
* |
|
954 |
* @param amountToSubtract the amount to subtract, not null |
|
955 |
* @return an {@code OffsetTime} based on this time with the subtraction made, not null |
|
956 |
* @throws DateTimeException if the subtraction cannot be made |
|
957 |
* @throws ArithmeticException if numeric overflow occurs |
|
958 |
*/ |
|
959 |
@Override |
|
960 |
public OffsetTime minus(TemporalAmount amountToSubtract) { |
|
961 |
return (OffsetTime) amountToSubtract.subtractFrom(this); |
|
962 |
} |
|
963 |
||
964 |
/** |
|
965 |
* Returns a copy of this time with the specified amount subtracted. |
|
966 |
* <p> |
|
967 |
* This returns an {@code OffsetTime}, based on this one, with the amount |
|
968 |
* in terms of the unit subtracted. If it is not possible to subtract the amount, |
|
969 |
* because the unit is not supported or for some other reason, an exception is thrown. |
|
970 |
* <p> |
|
971 |
* This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated. |
|
972 |
* See that method for a full description of how addition, and thus subtraction, works. |
|
973 |
* <p> |
|
974 |
* This instance is immutable and unaffected by this method call. |
|
975 |
* |
|
976 |
* @param amountToSubtract the amount of the unit to subtract from the result, may be negative |
|
977 |
* @param unit the unit of the amount to subtract, not null |
|
978 |
* @return an {@code OffsetTime} based on this time with the specified amount subtracted, not null |
|
979 |
* @throws DateTimeException if the subtraction cannot be made |
|
16852 | 980 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15658 | 981 |
* @throws ArithmeticException if numeric overflow occurs |
982 |
*/ |
|
983 |
@Override |
|
984 |
public OffsetTime minus(long amountToSubtract, TemporalUnit unit) { |
|
985 |
return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); |
|
986 |
} |
|
987 |
||
988 |
//----------------------------------------------------------------------- |
|
989 |
/** |
|
990 |
* Returns a copy of this {@code OffsetTime} with the specified period in hours subtracted. |
|
991 |
* <p> |
|
992 |
* This subtracts the specified number of hours from this time, returning a new time. |
|
993 |
* The calculation wraps around midnight. |
|
994 |
* <p> |
|
995 |
* This instance is immutable and unaffected by this method call. |
|
996 |
* |
|
997 |
* @param hours the hours to subtract, may be negative |
|
998 |
* @return an {@code OffsetTime} based on this time with the hours subtracted, not null |
|
999 |
*/ |
|
1000 |
public OffsetTime minusHours(long hours) { |
|
1001 |
return with(time.minusHours(hours), offset); |
|
1002 |
} |
|
1003 |
||
1004 |
/** |
|
1005 |
* Returns a copy of this {@code OffsetTime} with the specified period in minutes subtracted. |
|
1006 |
* <p> |
|
1007 |
* This subtracts the specified number of minutes from this time, returning a new time. |
|
1008 |
* The calculation wraps around midnight. |
|
1009 |
* <p> |
|
1010 |
* This instance is immutable and unaffected by this method call. |
|
1011 |
* |
|
1012 |
* @param minutes the minutes to subtract, may be negative |
|
1013 |
* @return an {@code OffsetTime} based on this time with the minutes subtracted, not null |
|
1014 |
*/ |
|
1015 |
public OffsetTime minusMinutes(long minutes) { |
|
1016 |
return with(time.minusMinutes(minutes), offset); |
|
1017 |
} |
|
1018 |
||
1019 |
/** |
|
1020 |
* Returns a copy of this {@code OffsetTime} with the specified period in seconds subtracted. |
|
1021 |
* <p> |
|
1022 |
* This subtracts the specified number of seconds from this time, returning a new time. |
|
1023 |
* The calculation wraps around midnight. |
|
1024 |
* <p> |
|
1025 |
* This instance is immutable and unaffected by this method call. |
|
1026 |
* |
|
1027 |
* @param seconds the seconds to subtract, may be negative |
|
1028 |
* @return an {@code OffsetTime} based on this time with the seconds subtracted, not null |
|
1029 |
*/ |
|
1030 |
public OffsetTime minusSeconds(long seconds) { |
|
1031 |
return with(time.minusSeconds(seconds), offset); |
|
1032 |
} |
|
1033 |
||
1034 |
/** |
|
1035 |
* Returns a copy of this {@code OffsetTime} with the specified period in nanoseconds subtracted. |
|
1036 |
* <p> |
|
1037 |
* This subtracts the specified number of nanoseconds from this time, returning a new time. |
|
1038 |
* The calculation wraps around midnight. |
|
1039 |
* <p> |
|
1040 |
* This instance is immutable and unaffected by this method call. |
|
1041 |
* |
|
1042 |
* @param nanos the nanos to subtract, may be negative |
|
1043 |
* @return an {@code OffsetTime} based on this time with the nanoseconds subtracted, not null |
|
1044 |
*/ |
|
1045 |
public OffsetTime minusNanos(long nanos) { |
|
1046 |
return with(time.minusNanos(nanos), offset); |
|
1047 |
} |
|
1048 |
||
1049 |
//----------------------------------------------------------------------- |
|
1050 |
/** |
|
1051 |
* Queries this time using the specified query. |
|
1052 |
* <p> |
|
1053 |
* This queries this time using the specified query strategy object. |
|
1054 |
* The {@code TemporalQuery} object defines the logic to be used to |
|
1055 |
* obtain the result. Read the documentation of the query to understand |
|
1056 |
* what the result of this method will be. |
|
1057 |
* <p> |
|
1058 |
* The result of this method is obtained by invoking the |
|
1059 |
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the |
|
1060 |
* specified query passing {@code this} as the argument. |
|
1061 |
* |
|
1062 |
* @param <R> the type of the result |
|
1063 |
* @param query the query to invoke, not null |
|
1064 |
* @return the query result, null may be returned (defined by the query) |
|
1065 |
* @throws DateTimeException if unable to query (defined by the query) |
|
1066 |
* @throws ArithmeticException if numeric overflow occurs (defined by the query) |
|
1067 |
*/ |
|
1068 |
@SuppressWarnings("unchecked") |
|
1069 |
@Override |
|
1070 |
public <R> R query(TemporalQuery<R> query) { |
|
20795 | 1071 |
if (query == TemporalQueries.offset() || query == TemporalQueries.zone()) { |
15658 | 1072 |
return (R) offset; |
20795 | 1073 |
} else if (query == TemporalQueries.zoneId() | query == TemporalQueries.chronology() || query == TemporalQueries.localDate()) { |
15658 | 1074 |
return null; |
20795 | 1075 |
} else if (query == TemporalQueries.localTime()) { |
15658 | 1076 |
return (R) time; |
20795 | 1077 |
} else if (query == TemporalQueries.precision()) { |
15658 | 1078 |
return (R) NANOS; |
1079 |
} |
|
1080 |
// inline TemporalAccessor.super.query(query) as an optimization |
|
1081 |
// non-JDK classes are not permitted to make this optimization |
|
1082 |
return query.queryFrom(this); |
|
1083 |
} |
|
1084 |
||
1085 |
/** |
|
1086 |
* Adjusts the specified temporal object to have the same offset and time |
|
1087 |
* as this object. |
|
1088 |
* <p> |
|
1089 |
* This returns a temporal object of the same observable type as the input |
|
1090 |
* with the offset and time changed to be the same as this. |
|
1091 |
* <p> |
|
1092 |
* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} |
|
16852 | 1093 |
* twice, passing {@link ChronoField#NANO_OF_DAY} and |
1094 |
* {@link ChronoField#OFFSET_SECONDS} as the fields. |
|
15658 | 1095 |
* <p> |
1096 |
* In most cases, it is clearer to reverse the calling pattern by using |
|
1097 |
* {@link Temporal#with(TemporalAdjuster)}: |
|
1098 |
* <pre> |
|
1099 |
* // these two lines are equivalent, but the second approach is recommended |
|
1100 |
* temporal = thisOffsetTime.adjustInto(temporal); |
|
1101 |
* temporal = temporal.with(thisOffsetTime); |
|
1102 |
* </pre> |
|
1103 |
* <p> |
|
1104 |
* This instance is immutable and unaffected by this method call. |
|
1105 |
* |
|
1106 |
* @param temporal the target object to be adjusted, not null |
|
1107 |
* @return the adjusted object, not null |
|
1108 |
* @throws DateTimeException if unable to make the adjustment |
|
1109 |
* @throws ArithmeticException if numeric overflow occurs |
|
1110 |
*/ |
|
1111 |
@Override |
|
1112 |
public Temporal adjustInto(Temporal temporal) { |
|
1113 |
return temporal |
|
16852 | 1114 |
.with(NANO_OF_DAY, time.toNanoOfDay()) |
1115 |
.with(OFFSET_SECONDS, offset.getTotalSeconds()); |
|
15658 | 1116 |
} |
1117 |
||
1118 |
/** |
|
17474 | 1119 |
* Calculates the amount of time until another time in terms of the specified unit. |
15658 | 1120 |
* <p> |
17474 | 1121 |
* This calculates the amount of time between two {@code OffsetTime} |
1122 |
* objects in terms of a single {@code TemporalUnit}. |
|
15658 | 1123 |
* The start and end points are {@code this} and the specified time. |
1124 |
* The result will be negative if the end is before the start. |
|
1125 |
* For example, the period in hours between two times can be calculated |
|
19030 | 1126 |
* using {@code startTime.until(endTime, HOURS)}. |
15658 | 1127 |
* <p> |
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
1128 |
* The {@code Temporal} passed to this method is converted to a |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
1129 |
* {@code OffsetTime} using {@link #from(TemporalAccessor)}. |
15658 | 1130 |
* If the offset differs between the two times, then the specified |
1131 |
* end time is normalized to have the same offset as this time. |
|
1132 |
* <p> |
|
1133 |
* The calculation returns a whole number, representing the number of |
|
1134 |
* complete units between the two times. |
|
1135 |
* For example, the period in hours between 11:30Z and 13:29Z will only |
|
1136 |
* be one hour as it is one minute short of two hours. |
|
1137 |
* <p> |
|
1138 |
* There are two equivalent ways of using this method. |
|
1139 |
* The first is to invoke this method. |
|
1140 |
* The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: |
|
1141 |
* <pre> |
|
1142 |
* // these two lines are equivalent |
|
19030 | 1143 |
* amount = start.until(end, MINUTES); |
15658 | 1144 |
* amount = MINUTES.between(start, end); |
1145 |
* </pre> |
|
1146 |
* The choice should be made based on which makes the code more readable. |
|
1147 |
* <p> |
|
1148 |
* The calculation is implemented in this method for {@link ChronoUnit}. |
|
1149 |
* The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS}, |
|
1150 |
* {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS} are supported. |
|
1151 |
* Other {@code ChronoUnit} values will throw an exception. |
|
1152 |
* <p> |
|
1153 |
* If the unit is not a {@code ChronoUnit}, then the result of this method |
|
1154 |
* is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
1155 |
* passing {@code this} as the first argument and the converted input temporal |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
1156 |
* as the second argument. |
15658 | 1157 |
* <p> |
1158 |
* This instance is immutable and unaffected by this method call. |
|
1159 |
* |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
1160 |
* @param endExclusive the end date, exclusive, which is converted to an {@code OffsetTime}, not null |
17474 | 1161 |
* @param unit the unit to measure the amount in, not null |
1162 |
* @return the amount of time between this time and the end time |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
1163 |
* @throws DateTimeException if the amount cannot be calculated, or the end |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
1164 |
* temporal cannot be converted to an {@code OffsetTime} |
16852 | 1165 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15658 | 1166 |
* @throws ArithmeticException if numeric overflow occurs |
1167 |
*/ |
|
1168 |
@Override |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
1169 |
public long until(Temporal endExclusive, TemporalUnit unit) { |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
1170 |
OffsetTime end = OffsetTime.from(endExclusive); |
15658 | 1171 |
if (unit instanceof ChronoUnit) { |
1172 |
long nanosUntil = end.toEpochNano() - toEpochNano(); // no overflow |
|
1173 |
switch ((ChronoUnit) unit) { |
|
1174 |
case NANOS: return nanosUntil; |
|
1175 |
case MICROS: return nanosUntil / 1000; |
|
1176 |
case MILLIS: return nanosUntil / 1000_000; |
|
1177 |
case SECONDS: return nanosUntil / NANOS_PER_SECOND; |
|
1178 |
case MINUTES: return nanosUntil / NANOS_PER_MINUTE; |
|
1179 |
case HOURS: return nanosUntil / NANOS_PER_HOUR; |
|
1180 |
case HALF_DAYS: return nanosUntil / (12 * NANOS_PER_HOUR); |
|
1181 |
} |
|
19030 | 1182 |
throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); |
15658 | 1183 |
} |
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19841
diff
changeset
|
1184 |
return unit.between(this, end); |
15658 | 1185 |
} |
1186 |
||
16852 | 1187 |
/** |
1188 |
* Formats this time using the specified formatter. |
|
1189 |
* <p> |
|
1190 |
* This time will be passed to the formatter to produce a string. |
|
1191 |
* |
|
1192 |
* @param formatter the formatter to use, not null |
|
1193 |
* @return the formatted time string, not null |
|
1194 |
* @throws DateTimeException if an error occurs during printing |
|
1195 |
*/ |
|
1196 |
public String format(DateTimeFormatter formatter) { |
|
1197 |
Objects.requireNonNull(formatter, "formatter"); |
|
1198 |
return formatter.format(this); |
|
1199 |
} |
|
1200 |
||
15658 | 1201 |
//----------------------------------------------------------------------- |
1202 |
/** |
|
16852 | 1203 |
* Combines this time with a date to create an {@code OffsetDateTime}. |
15658 | 1204 |
* <p> |
1205 |
* This returns an {@code OffsetDateTime} formed from this time and the specified date. |
|
1206 |
* All possible combinations of date and time are valid. |
|
1207 |
* |
|
1208 |
* @param date the date to combine with, not null |
|
1209 |
* @return the offset date-time formed from this time and the specified date, not null |
|
1210 |
*/ |
|
1211 |
public OffsetDateTime atDate(LocalDate date) { |
|
1212 |
return OffsetDateTime.of(date, time, offset); |
|
1213 |
} |
|
1214 |
||
1215 |
//----------------------------------------------------------------------- |
|
1216 |
/** |
|
1217 |
* Converts this time to epoch nanos based on 1970-01-01Z. |
|
1218 |
* |
|
1219 |
* @return the epoch nanos value |
|
1220 |
*/ |
|
1221 |
private long toEpochNano() { |
|
1222 |
long nod = time.toNanoOfDay(); |
|
1223 |
long offsetNanos = offset.getTotalSeconds() * NANOS_PER_SECOND; |
|
1224 |
return nod - offsetNanos; |
|
1225 |
} |
|
1226 |
||
1227 |
//----------------------------------------------------------------------- |
|
1228 |
/** |
|
1229 |
* Compares this {@code OffsetTime} to another time. |
|
1230 |
* <p> |
|
1231 |
* The comparison is based first on the UTC equivalent instant, then on the local time. |
|
1232 |
* It is "consistent with equals", as defined by {@link Comparable}. |
|
1233 |
* <p> |
|
1234 |
* For example, the following is the comparator order: |
|
1235 |
* <ol> |
|
1236 |
* <li>{@code 10:30+01:00}</li> |
|
1237 |
* <li>{@code 11:00+01:00}</li> |
|
1238 |
* <li>{@code 12:00+02:00}</li> |
|
1239 |
* <li>{@code 11:30+01:00}</li> |
|
1240 |
* <li>{@code 12:00+01:00}</li> |
|
1241 |
* <li>{@code 12:30+01:00}</li> |
|
1242 |
* </ol> |
|
1243 |
* Values #2 and #3 represent the same instant on the time-line. |
|
1244 |
* When two values represent the same instant, the local time is compared |
|
1245 |
* to distinguish them. This step is needed to make the ordering |
|
1246 |
* consistent with {@code equals()}. |
|
1247 |
* <p> |
|
1248 |
* To compare the underlying local time of two {@code TemporalAccessor} instances, |
|
1249 |
* use {@link ChronoField#NANO_OF_DAY} as a comparator. |
|
1250 |
* |
|
1251 |
* @param other the other time to compare to, not null |
|
1252 |
* @return the comparator value, negative if less, positive if greater |
|
1253 |
* @throws NullPointerException if {@code other} is null |
|
1254 |
*/ |
|
1255 |
@Override |
|
1256 |
public int compareTo(OffsetTime other) { |
|
1257 |
if (offset.equals(other.offset)) { |
|
1258 |
return time.compareTo(other.time); |
|
1259 |
} |
|
1260 |
int compare = Long.compare(toEpochNano(), other.toEpochNano()); |
|
1261 |
if (compare == 0) { |
|
1262 |
compare = time.compareTo(other.time); |
|
1263 |
} |
|
1264 |
return compare; |
|
1265 |
} |
|
1266 |
||
1267 |
//----------------------------------------------------------------------- |
|
1268 |
/** |
|
1269 |
* Checks if the instant of this {@code OffsetTime} is after that of the |
|
1270 |
* specified time applying both times to a common date. |
|
1271 |
* <p> |
|
1272 |
* This method differs from the comparison in {@link #compareTo} in that it |
|
1273 |
* only compares the instant of the time. This is equivalent to converting both |
|
1274 |
* times to an instant using the same date and comparing the instants. |
|
1275 |
* |
|
1276 |
* @param other the other time to compare to, not null |
|
1277 |
* @return true if this is after the instant of the specified time |
|
1278 |
*/ |
|
1279 |
public boolean isAfter(OffsetTime other) { |
|
1280 |
return toEpochNano() > other.toEpochNano(); |
|
1281 |
} |
|
1282 |
||
1283 |
/** |
|
1284 |
* Checks if the instant of this {@code OffsetTime} is before that of the |
|
1285 |
* specified time applying both times to a common date. |
|
1286 |
* <p> |
|
1287 |
* This method differs from the comparison in {@link #compareTo} in that it |
|
1288 |
* only compares the instant of the time. This is equivalent to converting both |
|
1289 |
* times to an instant using the same date and comparing the instants. |
|
1290 |
* |
|
1291 |
* @param other the other time to compare to, not null |
|
1292 |
* @return true if this is before the instant of the specified time |
|
1293 |
*/ |
|
1294 |
public boolean isBefore(OffsetTime other) { |
|
1295 |
return toEpochNano() < other.toEpochNano(); |
|
1296 |
} |
|
1297 |
||
1298 |
/** |
|
1299 |
* Checks if the instant of this {@code OffsetTime} is equal to that of the |
|
1300 |
* specified time applying both times to a common date. |
|
1301 |
* <p> |
|
1302 |
* This method differs from the comparison in {@link #compareTo} and {@link #equals} |
|
1303 |
* in that it only compares the instant of the time. This is equivalent to converting both |
|
1304 |
* times to an instant using the same date and comparing the instants. |
|
1305 |
* |
|
1306 |
* @param other the other time to compare to, not null |
|
1307 |
* @return true if this is equal to the instant of the specified time |
|
1308 |
*/ |
|
1309 |
public boolean isEqual(OffsetTime other) { |
|
1310 |
return toEpochNano() == other.toEpochNano(); |
|
1311 |
} |
|
1312 |
||
1313 |
//----------------------------------------------------------------------- |
|
1314 |
/** |
|
1315 |
* Checks if this time is equal to another time. |
|
1316 |
* <p> |
|
1317 |
* The comparison is based on the local-time and the offset. |
|
1318 |
* To compare for the same instant on the time-line, use {@link #isEqual(OffsetTime)}. |
|
1319 |
* <p> |
|
1320 |
* Only objects of type {@code OffsetTime} are compared, other types return false. |
|
1321 |
* To compare the underlying local time of two {@code TemporalAccessor} instances, |
|
1322 |
* use {@link ChronoField#NANO_OF_DAY} as a comparator. |
|
1323 |
* |
|
1324 |
* @param obj the object to check, null returns false |
|
1325 |
* @return true if this is equal to the other time |
|
1326 |
*/ |
|
1327 |
@Override |
|
1328 |
public boolean equals(Object obj) { |
|
1329 |
if (this == obj) { |
|
1330 |
return true; |
|
1331 |
} |
|
1332 |
if (obj instanceof OffsetTime) { |
|
1333 |
OffsetTime other = (OffsetTime) obj; |
|
1334 |
return time.equals(other.time) && offset.equals(other.offset); |
|
1335 |
} |
|
1336 |
return false; |
|
1337 |
} |
|
1338 |
||
1339 |
/** |
|
1340 |
* A hash code for this time. |
|
1341 |
* |
|
1342 |
* @return a suitable hash code |
|
1343 |
*/ |
|
1344 |
@Override |
|
1345 |
public int hashCode() { |
|
1346 |
return time.hashCode() ^ offset.hashCode(); |
|
1347 |
} |
|
1348 |
||
1349 |
//----------------------------------------------------------------------- |
|
1350 |
/** |
|
1351 |
* Outputs this time as a {@code String}, such as {@code 10:15:30+01:00}. |
|
1352 |
* <p> |
|
1353 |
* The output will be one of the following ISO-8601 formats: |
|
20873 | 1354 |
* <ul> |
15658 | 1355 |
* <li>{@code HH:mmXXXXX}</li> |
1356 |
* <li>{@code HH:mm:ssXXXXX}</li> |
|
1357 |
* <li>{@code HH:mm:ss.SSSXXXXX}</li> |
|
1358 |
* <li>{@code HH:mm:ss.SSSSSSXXXXX}</li> |
|
1359 |
* <li>{@code HH:mm:ss.SSSSSSSSSXXXXX}</li> |
|
20873 | 1360 |
* </ul> |
15658 | 1361 |
* The format used will be the shortest that outputs the full value of |
1362 |
* the time where the omitted parts are implied to be zero. |
|
1363 |
* |
|
1364 |
* @return a string representation of this time, not null |
|
1365 |
*/ |
|
1366 |
@Override |
|
1367 |
public String toString() { |
|
1368 |
return time.toString() + offset.toString(); |
|
1369 |
} |
|
1370 |
||
16852 | 1371 |
//----------------------------------------------------------------------- |
15658 | 1372 |
/** |
1373 |
* Writes the object using a |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
1374 |
* <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>. |
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
1375 |
* @serialData |
15658 | 1376 |
* <pre> |
21331
8ee181e7e48b
8024686: Cleanup of java.time serialization source
rriggs
parents:
20873
diff
changeset
|
1377 |
* out.writeByte(9); // identifies an OffsetTime |
8ee181e7e48b
8024686: Cleanup of java.time serialization source
rriggs
parents:
20873
diff
changeset
|
1378 |
* // the <a href="../../serialized-form.html#java.time.LocalTime">time</a> excluding the one byte header |
8ee181e7e48b
8024686: Cleanup of java.time serialization source
rriggs
parents:
20873
diff
changeset
|
1379 |
* // the <a href="../../serialized-form.html#java.time.ZoneOffset">offset</a> excluding the one byte header |
15658 | 1380 |
* </pre> |
1381 |
* |
|
1382 |
* @return the instance of {@code Ser}, not null |
|
1383 |
*/ |
|
1384 |
private Object writeReplace() { |
|
1385 |
return new Ser(Ser.OFFSET_TIME_TYPE, this); |
|
1386 |
} |
|
1387 |
||
1388 |
/** |
|
1389 |
* Defend against malicious streams. |
|
1390 |
* @return never |
|
1391 |
* @throws InvalidObjectException always |
|
1392 |
*/ |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
1393 |
private Object readResolve() throws InvalidObjectException { |
15658 | 1394 |
throw new InvalidObjectException("Deserialization via serialization delegate"); |
1395 |
} |
|
1396 |
||
1397 |
void writeExternal(ObjectOutput out) throws IOException { |
|
21331
8ee181e7e48b
8024686: Cleanup of java.time serialization source
rriggs
parents:
20873
diff
changeset
|
1398 |
time.writeExternal(out); |
8ee181e7e48b
8024686: Cleanup of java.time serialization source
rriggs
parents:
20873
diff
changeset
|
1399 |
offset.writeExternal(out); |
15658 | 1400 |
} |
1401 |
||
1402 |
static OffsetTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException { |
|
21331
8ee181e7e48b
8024686: Cleanup of java.time serialization source
rriggs
parents:
20873
diff
changeset
|
1403 |
LocalTime time = LocalTime.readExternal(in); |
8ee181e7e48b
8024686: Cleanup of java.time serialization source
rriggs
parents:
20873
diff
changeset
|
1404 |
ZoneOffset offset = ZoneOffset.readExternal(in); |
15658 | 1405 |
return OffsetTime.of(time, offset); |
1406 |
} |
|
1407 |
||
1408 |
} |