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