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.LocalTime.HOURS_PER_DAY; |
|
65 |
import static java.time.LocalTime.MICROS_PER_DAY; |
|
66 |
import static java.time.LocalTime.MILLIS_PER_DAY; |
|
67 |
import static java.time.LocalTime.MINUTES_PER_DAY; |
|
68 |
import static java.time.LocalTime.NANOS_PER_DAY; |
|
69 |
import static java.time.LocalTime.NANOS_PER_HOUR; |
|
70 |
import static java.time.LocalTime.NANOS_PER_MINUTE; |
|
71 |
import static java.time.LocalTime.NANOS_PER_SECOND; |
|
72 |
import static java.time.LocalTime.SECONDS_PER_DAY; |
|
15658 | 73 |
import static java.time.temporal.ChronoField.NANO_OF_SECOND; |
15289 | 74 |
|
75 |
import java.io.DataInput; |
|
76 |
import java.io.DataOutput; |
|
77 |
import java.io.IOException; |
|
78 |
import java.io.InvalidObjectException; |
|
79 |
import java.io.Serializable; |
|
15658 | 80 |
import java.time.chrono.ChronoLocalDateTime; |
15289 | 81 |
import java.time.format.DateTimeFormatter; |
82 |
import java.time.format.DateTimeParseException; |
|
83 |
import java.time.temporal.ChronoField; |
|
84 |
import java.time.temporal.ChronoUnit; |
|
85 |
import java.time.temporal.Temporal; |
|
86 |
import java.time.temporal.TemporalAccessor; |
|
87 |
import java.time.temporal.TemporalAdjuster; |
|
15658 | 88 |
import java.time.temporal.TemporalAmount; |
15289 | 89 |
import java.time.temporal.TemporalField; |
20795 | 90 |
import java.time.temporal.TemporalQueries; |
15289 | 91 |
import java.time.temporal.TemporalQuery; |
92 |
import java.time.temporal.TemporalUnit; |
|
16852 | 93 |
import java.time.temporal.UnsupportedTemporalTypeException; |
15289 | 94 |
import java.time.temporal.ValueRange; |
95 |
import java.time.zone.ZoneRules; |
|
96 |
import java.util.Objects; |
|
97 |
||
98 |
/** |
|
99 |
* A date-time without a time-zone in the ISO-8601 calendar system, |
|
100 |
* such as {@code 2007-12-03T10:15:30}. |
|
101 |
* <p> |
|
102 |
* {@code LocalDateTime} is an immutable date-time object that represents a date-time, |
|
103 |
* often viewed as year-month-day-hour-minute-second. Other date and time fields, |
|
104 |
* such as day-of-year, day-of-week and week-of-year, can also be accessed. |
|
105 |
* Time is represented to nanosecond precision. |
|
106 |
* For example, the value "2nd October 2007 at 13:45.30.123456789" can be |
|
107 |
* stored in a {@code LocalDateTime}. |
|
108 |
* <p> |
|
109 |
* This class does not store or represent a time-zone. |
|
110 |
* Instead, it is a description of the date, as used for birthdays, combined with |
|
111 |
* the local time as seen on a wall clock. |
|
112 |
* It cannot represent an instant on the time-line without additional information |
|
113 |
* such as an offset or time-zone. |
|
114 |
* <p> |
|
115 |
* The ISO-8601 calendar system is the modern civil calendar system used today |
|
116 |
* in most of the world. It is equivalent to the proleptic Gregorian calendar |
|
117 |
* system, in which today's rules for leap years are applied for all time. |
|
118 |
* For most applications written today, the ISO-8601 rules are entirely suitable. |
|
119 |
* However, any application that makes use of historical dates, and requires them |
|
120 |
* to be accurate will find the ISO-8601 approach unsuitable. |
|
121 |
* |
|
17474 | 122 |
* @implSpec |
15289 | 123 |
* This class is immutable and thread-safe. |
124 |
* |
|
125 |
* @since 1.8 |
|
126 |
*/ |
|
127 |
public final class LocalDateTime |
|
15658 | 128 |
implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable { |
15289 | 129 |
|
130 |
/** |
|
131 |
* The minimum supported {@code LocalDateTime}, '-999999999-01-01T00:00:00'. |
|
132 |
* This is the local date-time of midnight at the start of the minimum date. |
|
133 |
* This combines {@link LocalDate#MIN} and {@link LocalTime#MIN}. |
|
134 |
* This could be used by an application as a "far past" date-time. |
|
135 |
*/ |
|
136 |
public static final LocalDateTime MIN = LocalDateTime.of(LocalDate.MIN, LocalTime.MIN); |
|
137 |
/** |
|
138 |
* The maximum supported {@code LocalDateTime}, '+999999999-12-31T23:59:59.999999999'. |
|
139 |
* This is the local date-time just before midnight at the end of the maximum date. |
|
140 |
* This combines {@link LocalDate#MAX} and {@link LocalTime#MAX}. |
|
141 |
* This could be used by an application as a "far future" date-time. |
|
142 |
*/ |
|
143 |
public static final LocalDateTime MAX = LocalDateTime.of(LocalDate.MAX, LocalTime.MAX); |
|
144 |
||
145 |
/** |
|
146 |
* Serialization version. |
|
147 |
*/ |
|
148 |
private static final long serialVersionUID = 6207766400415563566L; |
|
149 |
||
150 |
/** |
|
151 |
* The date part. |
|
152 |
*/ |
|
153 |
private final LocalDate date; |
|
154 |
/** |
|
155 |
* The time part. |
|
156 |
*/ |
|
157 |
private final LocalTime time; |
|
158 |
||
159 |
//----------------------------------------------------------------------- |
|
160 |
/** |
|
161 |
* Obtains the current date-time from the system clock in the default time-zone. |
|
162 |
* <p> |
|
163 |
* This will query the {@link Clock#systemDefaultZone() system clock} in the default |
|
164 |
* time-zone to obtain the current date-time. |
|
165 |
* <p> |
|
166 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
167 |
* because the clock is hard-coded. |
|
168 |
* |
|
169 |
* @return the current date-time using the system clock and default time-zone, not null |
|
170 |
*/ |
|
171 |
public static LocalDateTime now() { |
|
172 |
return now(Clock.systemDefaultZone()); |
|
173 |
} |
|
174 |
||
175 |
/** |
|
176 |
* Obtains the current date-time from the system clock in the specified time-zone. |
|
177 |
* <p> |
|
178 |
* This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date-time. |
|
179 |
* Specifying the time-zone avoids dependence on the default time-zone. |
|
180 |
* <p> |
|
181 |
* Using this method will prevent the ability to use an alternate clock for testing |
|
182 |
* because the clock is hard-coded. |
|
183 |
* |
|
184 |
* @param zone the zone ID to use, not null |
|
185 |
* @return the current date-time using the system clock, not null |
|
186 |
*/ |
|
187 |
public static LocalDateTime now(ZoneId zone) { |
|
188 |
return now(Clock.system(zone)); |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* Obtains the current date-time from the specified clock. |
|
193 |
* <p> |
|
194 |
* This will query the specified clock to obtain the current date-time. |
|
195 |
* Using this method allows the use of an alternate clock for testing. |
|
196 |
* The alternate clock may be introduced using {@link Clock dependency injection}. |
|
197 |
* |
|
198 |
* @param clock the clock to use, not null |
|
199 |
* @return the current date-time, not null |
|
200 |
*/ |
|
201 |
public static LocalDateTime now(Clock clock) { |
|
202 |
Objects.requireNonNull(clock, "clock"); |
|
203 |
final Instant now = clock.instant(); // called once |
|
204 |
ZoneOffset offset = clock.getZone().getRules().getOffset(now); |
|
205 |
return ofEpochSecond(now.getEpochSecond(), now.getNano(), offset); |
|
206 |
} |
|
207 |
||
208 |
//----------------------------------------------------------------------- |
|
209 |
/** |
|
210 |
* Obtains an instance of {@code LocalDateTime} from year, month, |
|
211 |
* day, hour and minute, setting the second and nanosecond to zero. |
|
212 |
* <p> |
|
15658 | 213 |
* This returns a {@code LocalDateTime} with the specified year, month, |
214 |
* day-of-month, hour and minute. |
|
15289 | 215 |
* The day must be valid for the year and month, otherwise an exception will be thrown. |
216 |
* The second and nanosecond fields will be set to zero. |
|
217 |
* |
|
218 |
* @param year the year to represent, from MIN_YEAR to MAX_YEAR |
|
219 |
* @param month the month-of-year to represent, not null |
|
220 |
* @param dayOfMonth the day-of-month to represent, from 1 to 31 |
|
221 |
* @param hour the hour-of-day to represent, from 0 to 23 |
|
222 |
* @param minute the minute-of-hour to represent, from 0 to 59 |
|
223 |
* @return the local date-time, not null |
|
15658 | 224 |
* @throws DateTimeException if the value of any field is out of range, |
225 |
* or if the day-of-month is invalid for the month-year |
|
15289 | 226 |
*/ |
227 |
public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute) { |
|
228 |
LocalDate date = LocalDate.of(year, month, dayOfMonth); |
|
229 |
LocalTime time = LocalTime.of(hour, minute); |
|
230 |
return new LocalDateTime(date, time); |
|
231 |
} |
|
232 |
||
233 |
/** |
|
234 |
* Obtains an instance of {@code LocalDateTime} from year, month, |
|
235 |
* day, hour, minute and second, setting the nanosecond to zero. |
|
236 |
* <p> |
|
15658 | 237 |
* This returns a {@code LocalDateTime} with the specified year, month, |
238 |
* day-of-month, hour, minute and second. |
|
15289 | 239 |
* The day must be valid for the year and month, otherwise an exception will be thrown. |
240 |
* The nanosecond field will be set to zero. |
|
241 |
* |
|
242 |
* @param year the year to represent, from MIN_YEAR to MAX_YEAR |
|
243 |
* @param month the month-of-year to represent, not null |
|
244 |
* @param dayOfMonth the day-of-month to represent, from 1 to 31 |
|
245 |
* @param hour the hour-of-day to represent, from 0 to 23 |
|
246 |
* @param minute the minute-of-hour to represent, from 0 to 59 |
|
247 |
* @param second the second-of-minute to represent, from 0 to 59 |
|
248 |
* @return the local date-time, not null |
|
15658 | 249 |
* @throws DateTimeException if the value of any field is out of range, |
250 |
* or if the day-of-month is invalid for the month-year |
|
15289 | 251 |
*/ |
252 |
public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) { |
|
253 |
LocalDate date = LocalDate.of(year, month, dayOfMonth); |
|
254 |
LocalTime time = LocalTime.of(hour, minute, second); |
|
255 |
return new LocalDateTime(date, time); |
|
256 |
} |
|
257 |
||
258 |
/** |
|
259 |
* Obtains an instance of {@code LocalDateTime} from year, month, |
|
260 |
* day, hour, minute, second and nanosecond. |
|
261 |
* <p> |
|
15658 | 262 |
* This returns a {@code LocalDateTime} with the specified year, month, |
263 |
* day-of-month, hour, minute, second and nanosecond. |
|
15289 | 264 |
* The day must be valid for the year and month, otherwise an exception will be thrown. |
265 |
* |
|
266 |
* @param year the year to represent, from MIN_YEAR to MAX_YEAR |
|
267 |
* @param month the month-of-year to represent, not null |
|
268 |
* @param dayOfMonth the day-of-month to represent, from 1 to 31 |
|
269 |
* @param hour the hour-of-day to represent, from 0 to 23 |
|
270 |
* @param minute the minute-of-hour to represent, from 0 to 59 |
|
271 |
* @param second the second-of-minute to represent, from 0 to 59 |
|
272 |
* @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 |
|
273 |
* @return the local date-time, not null |
|
15658 | 274 |
* @throws DateTimeException if the value of any field is out of range, |
275 |
* or if the day-of-month is invalid for the month-year |
|
15289 | 276 |
*/ |
277 |
public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) { |
|
278 |
LocalDate date = LocalDate.of(year, month, dayOfMonth); |
|
279 |
LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond); |
|
280 |
return new LocalDateTime(date, time); |
|
281 |
} |
|
282 |
||
283 |
//----------------------------------------------------------------------- |
|
284 |
/** |
|
285 |
* Obtains an instance of {@code LocalDateTime} from year, month, |
|
286 |
* day, hour and minute, setting the second and nanosecond to zero. |
|
287 |
* <p> |
|
15658 | 288 |
* This returns a {@code LocalDateTime} with the specified year, month, |
289 |
* day-of-month, hour and minute. |
|
15289 | 290 |
* The day must be valid for the year and month, otherwise an exception will be thrown. |
291 |
* The second and nanosecond fields will be set to zero. |
|
292 |
* |
|
293 |
* @param year the year to represent, from MIN_YEAR to MAX_YEAR |
|
294 |
* @param month the month-of-year to represent, from 1 (January) to 12 (December) |
|
295 |
* @param dayOfMonth the day-of-month to represent, from 1 to 31 |
|
296 |
* @param hour the hour-of-day to represent, from 0 to 23 |
|
297 |
* @param minute the minute-of-hour to represent, from 0 to 59 |
|
298 |
* @return the local date-time, not null |
|
15658 | 299 |
* @throws DateTimeException if the value of any field is out of range, |
300 |
* or if the day-of-month is invalid for the month-year |
|
15289 | 301 |
*/ |
302 |
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) { |
|
303 |
LocalDate date = LocalDate.of(year, month, dayOfMonth); |
|
304 |
LocalTime time = LocalTime.of(hour, minute); |
|
305 |
return new LocalDateTime(date, time); |
|
306 |
} |
|
307 |
||
308 |
/** |
|
309 |
* Obtains an instance of {@code LocalDateTime} from year, month, |
|
310 |
* day, hour, minute and second, setting the nanosecond to zero. |
|
311 |
* <p> |
|
15658 | 312 |
* This returns a {@code LocalDateTime} with the specified year, month, |
313 |
* day-of-month, hour, minute and second. |
|
15289 | 314 |
* The day must be valid for the year and month, otherwise an exception will be thrown. |
315 |
* The nanosecond field will be set to zero. |
|
316 |
* |
|
317 |
* @param year the year to represent, from MIN_YEAR to MAX_YEAR |
|
318 |
* @param month the month-of-year to represent, from 1 (January) to 12 (December) |
|
319 |
* @param dayOfMonth the day-of-month to represent, from 1 to 31 |
|
320 |
* @param hour the hour-of-day to represent, from 0 to 23 |
|
321 |
* @param minute the minute-of-hour to represent, from 0 to 59 |
|
322 |
* @param second the second-of-minute to represent, from 0 to 59 |
|
323 |
* @return the local date-time, not null |
|
15658 | 324 |
* @throws DateTimeException if the value of any field is out of range, |
325 |
* or if the day-of-month is invalid for the month-year |
|
15289 | 326 |
*/ |
327 |
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) { |
|
328 |
LocalDate date = LocalDate.of(year, month, dayOfMonth); |
|
329 |
LocalTime time = LocalTime.of(hour, minute, second); |
|
330 |
return new LocalDateTime(date, time); |
|
331 |
} |
|
332 |
||
333 |
/** |
|
334 |
* Obtains an instance of {@code LocalDateTime} from year, month, |
|
335 |
* day, hour, minute, second and nanosecond. |
|
336 |
* <p> |
|
15658 | 337 |
* This returns a {@code LocalDateTime} with the specified year, month, |
338 |
* day-of-month, hour, minute, second and nanosecond. |
|
15289 | 339 |
* The day must be valid for the year and month, otherwise an exception will be thrown. |
340 |
* |
|
341 |
* @param year the year to represent, from MIN_YEAR to MAX_YEAR |
|
342 |
* @param month the month-of-year to represent, from 1 (January) to 12 (December) |
|
343 |
* @param dayOfMonth the day-of-month to represent, from 1 to 31 |
|
344 |
* @param hour the hour-of-day to represent, from 0 to 23 |
|
345 |
* @param minute the minute-of-hour to represent, from 0 to 59 |
|
346 |
* @param second the second-of-minute to represent, from 0 to 59 |
|
347 |
* @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 |
|
348 |
* @return the local date-time, not null |
|
15658 | 349 |
* @throws DateTimeException if the value of any field is out of range, |
350 |
* or if the day-of-month is invalid for the month-year |
|
15289 | 351 |
*/ |
352 |
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) { |
|
353 |
LocalDate date = LocalDate.of(year, month, dayOfMonth); |
|
354 |
LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond); |
|
355 |
return new LocalDateTime(date, time); |
|
356 |
} |
|
357 |
||
358 |
/** |
|
359 |
* Obtains an instance of {@code LocalDateTime} from a date and time. |
|
360 |
* |
|
361 |
* @param date the local date, not null |
|
362 |
* @param time the local time, not null |
|
363 |
* @return the local date-time, not null |
|
364 |
*/ |
|
365 |
public static LocalDateTime of(LocalDate date, LocalTime time) { |
|
366 |
Objects.requireNonNull(date, "date"); |
|
367 |
Objects.requireNonNull(time, "time"); |
|
368 |
return new LocalDateTime(date, time); |
|
369 |
} |
|
370 |
||
371 |
//------------------------------------------------------------------------- |
|
372 |
/** |
|
373 |
* Obtains an instance of {@code LocalDateTime} from an {@code Instant} and zone ID. |
|
374 |
* <p> |
|
375 |
* This creates a local date-time based on the specified instant. |
|
376 |
* First, the offset from UTC/Greenwich is obtained using the zone ID and instant, |
|
377 |
* which is simple as there is only one valid offset for each instant. |
|
378 |
* Then, the instant and offset are used to calculate the local date-time. |
|
379 |
* |
|
380 |
* @param instant the instant to create the date-time from, not null |
|
381 |
* @param zone the time-zone, which may be an offset, not null |
|
382 |
* @return the local date-time, not null |
|
383 |
* @throws DateTimeException if the result exceeds the supported range |
|
384 |
*/ |
|
385 |
public static LocalDateTime ofInstant(Instant instant, ZoneId zone) { |
|
386 |
Objects.requireNonNull(instant, "instant"); |
|
387 |
Objects.requireNonNull(zone, "zone"); |
|
388 |
ZoneRules rules = zone.getRules(); |
|
389 |
ZoneOffset offset = rules.getOffset(instant); |
|
390 |
return ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset); |
|
391 |
} |
|
392 |
||
393 |
/** |
|
394 |
* Obtains an instance of {@code LocalDateTime} using seconds from the |
|
395 |
* epoch of 1970-01-01T00:00:00Z. |
|
396 |
* <p> |
|
397 |
* This allows the {@link ChronoField#INSTANT_SECONDS epoch-second} field |
|
398 |
* to be converted to a local date-time. This is primarily intended for |
|
399 |
* low-level conversions rather than general application usage. |
|
400 |
* |
|
401 |
* @param epochSecond the number of seconds from the epoch of 1970-01-01T00:00:00Z |
|
402 |
* @param nanoOfSecond the nanosecond within the second, from 0 to 999,999,999 |
|
403 |
* @param offset the zone offset, not null |
|
404 |
* @return the local date-time, not null |
|
15658 | 405 |
* @throws DateTimeException if the result exceeds the supported range, |
406 |
* or if the nano-of-second is invalid |
|
15289 | 407 |
*/ |
408 |
public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) { |
|
409 |
Objects.requireNonNull(offset, "offset"); |
|
15658 | 410 |
NANO_OF_SECOND.checkValidValue(nanoOfSecond); |
15289 | 411 |
long localSecond = epochSecond + offset.getTotalSeconds(); // overflow caught later |
412 |
long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY); |
|
413 |
int secsOfDay = (int)Math.floorMod(localSecond, SECONDS_PER_DAY); |
|
414 |
LocalDate date = LocalDate.ofEpochDay(localEpochDay); |
|
15658 | 415 |
LocalTime time = LocalTime.ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + nanoOfSecond); |
15289 | 416 |
return new LocalDateTime(date, time); |
417 |
} |
|
418 |
||
419 |
//----------------------------------------------------------------------- |
|
420 |
/** |
|
421 |
* Obtains an instance of {@code LocalDateTime} from a temporal object. |
|
422 |
* <p> |
|
15658 | 423 |
* This obtains an offset time based on the specified temporal. |
424 |
* A {@code TemporalAccessor} represents an arbitrary set of date and time information, |
|
425 |
* which this factory converts to an instance of {@code LocalDateTime}. |
|
15289 | 426 |
* <p> |
15658 | 427 |
* The conversion extracts and combines the {@code LocalDate} and the |
428 |
* {@code LocalTime} from the temporal object. |
|
429 |
* Implementations are permitted to perform optimizations such as accessing |
|
430 |
* those fields that are equivalent to the relevant objects. |
|
15289 | 431 |
* <p> |
432 |
* This method matches the signature of the functional interface {@link TemporalQuery} |
|
433 |
* allowing it to be used as a query via method reference, {@code LocalDateTime::from}. |
|
434 |
* |
|
435 |
* @param temporal the temporal object to convert, not null |
|
436 |
* @return the local date-time, not null |
|
437 |
* @throws DateTimeException if unable to convert to a {@code LocalDateTime} |
|
438 |
*/ |
|
439 |
public static LocalDateTime from(TemporalAccessor temporal) { |
|
440 |
if (temporal instanceof LocalDateTime) { |
|
441 |
return (LocalDateTime) temporal; |
|
442 |
} else if (temporal instanceof ZonedDateTime) { |
|
15658 | 443 |
return ((ZonedDateTime) temporal).toLocalDateTime(); |
444 |
} else if (temporal instanceof OffsetDateTime) { |
|
445 |
return ((OffsetDateTime) temporal).toLocalDateTime(); |
|
15289 | 446 |
} |
447 |
try { |
|
448 |
LocalDate date = LocalDate.from(temporal); |
|
449 |
LocalTime time = LocalTime.from(temporal); |
|
450 |
return new LocalDateTime(date, time); |
|
451 |
} catch (DateTimeException ex) { |
|
20747 | 452 |
throw new DateTimeException("Unable to obtain LocalDateTime from TemporalAccessor: " + |
453 |
temporal + " of type " + temporal.getClass().getName(), ex); |
|
15289 | 454 |
} |
455 |
} |
|
456 |
||
457 |
//----------------------------------------------------------------------- |
|
458 |
/** |
|
459 |
* Obtains an instance of {@code LocalDateTime} from a text string such as {@code 2007-12-03T10:15:30}. |
|
460 |
* <p> |
|
461 |
* The string must represent a valid date-time and is parsed using |
|
15658 | 462 |
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME}. |
15289 | 463 |
* |
464 |
* @param text the text to parse such as "2007-12-03T10:15:30", not null |
|
465 |
* @return the parsed local date-time, not null |
|
466 |
* @throws DateTimeParseException if the text cannot be parsed |
|
467 |
*/ |
|
468 |
public static LocalDateTime parse(CharSequence text) { |
|
15658 | 469 |
return parse(text, DateTimeFormatter.ISO_LOCAL_DATE_TIME); |
15289 | 470 |
} |
471 |
||
472 |
/** |
|
473 |
* Obtains an instance of {@code LocalDateTime} from a text string using a specific formatter. |
|
474 |
* <p> |
|
475 |
* The text is parsed using the formatter, returning a date-time. |
|
476 |
* |
|
477 |
* @param text the text to parse, not null |
|
478 |
* @param formatter the formatter to use, not null |
|
479 |
* @return the parsed local date-time, not null |
|
480 |
* @throws DateTimeParseException if the text cannot be parsed |
|
481 |
*/ |
|
482 |
public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) { |
|
483 |
Objects.requireNonNull(formatter, "formatter"); |
|
484 |
return formatter.parse(text, LocalDateTime::from); |
|
485 |
} |
|
486 |
||
487 |
//----------------------------------------------------------------------- |
|
488 |
/** |
|
489 |
* Constructor. |
|
490 |
* |
|
491 |
* @param date the date part of the date-time, validated not null |
|
492 |
* @param time the time part of the date-time, validated not null |
|
493 |
*/ |
|
494 |
private LocalDateTime(LocalDate date, LocalTime time) { |
|
495 |
this.date = date; |
|
496 |
this.time = time; |
|
497 |
} |
|
498 |
||
499 |
/** |
|
500 |
* Returns a copy of this date-time with the new date and time, checking |
|
501 |
* to see if a new object is in fact required. |
|
502 |
* |
|
503 |
* @param newDate the date of the new date-time, not null |
|
504 |
* @param newTime the time of the new date-time, not null |
|
505 |
* @return the date-time, not null |
|
506 |
*/ |
|
507 |
private LocalDateTime with(LocalDate newDate, LocalTime newTime) { |
|
508 |
if (date == newDate && time == newTime) { |
|
509 |
return this; |
|
510 |
} |
|
511 |
return new LocalDateTime(newDate, newTime); |
|
512 |
} |
|
513 |
||
514 |
//----------------------------------------------------------------------- |
|
515 |
/** |
|
516 |
* Checks if the specified field is supported. |
|
517 |
* <p> |
|
518 |
* This checks if this date-time can be queried for the specified field. |
|
19030 | 519 |
* If false, then calling the {@link #range(TemporalField) range}, |
520 |
* {@link #get(TemporalField) get} and {@link #with(TemporalField, long)} |
|
521 |
* methods will throw an exception. |
|
15289 | 522 |
* <p> |
523 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
524 |
* The supported fields are: |
|
525 |
* <ul> |
|
526 |
* <li>{@code NANO_OF_SECOND} |
|
527 |
* <li>{@code NANO_OF_DAY} |
|
528 |
* <li>{@code MICRO_OF_SECOND} |
|
529 |
* <li>{@code MICRO_OF_DAY} |
|
530 |
* <li>{@code MILLI_OF_SECOND} |
|
531 |
* <li>{@code MILLI_OF_DAY} |
|
532 |
* <li>{@code SECOND_OF_MINUTE} |
|
533 |
* <li>{@code SECOND_OF_DAY} |
|
534 |
* <li>{@code MINUTE_OF_HOUR} |
|
535 |
* <li>{@code MINUTE_OF_DAY} |
|
536 |
* <li>{@code HOUR_OF_AMPM} |
|
537 |
* <li>{@code CLOCK_HOUR_OF_AMPM} |
|
538 |
* <li>{@code HOUR_OF_DAY} |
|
539 |
* <li>{@code CLOCK_HOUR_OF_DAY} |
|
540 |
* <li>{@code AMPM_OF_DAY} |
|
541 |
* <li>{@code DAY_OF_WEEK} |
|
542 |
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} |
|
543 |
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR} |
|
544 |
* <li>{@code DAY_OF_MONTH} |
|
545 |
* <li>{@code DAY_OF_YEAR} |
|
546 |
* <li>{@code EPOCH_DAY} |
|
547 |
* <li>{@code ALIGNED_WEEK_OF_MONTH} |
|
548 |
* <li>{@code ALIGNED_WEEK_OF_YEAR} |
|
549 |
* <li>{@code MONTH_OF_YEAR} |
|
16852 | 550 |
* <li>{@code PROLEPTIC_MONTH} |
15289 | 551 |
* <li>{@code YEAR_OF_ERA} |
552 |
* <li>{@code YEAR} |
|
553 |
* <li>{@code ERA} |
|
554 |
* </ul> |
|
555 |
* All other {@code ChronoField} instances will return false. |
|
556 |
* <p> |
|
557 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
15658 | 558 |
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} |
15289 | 559 |
* passing {@code this} as the argument. |
560 |
* Whether the field is supported is determined by the field. |
|
561 |
* |
|
562 |
* @param field the field to check, null returns false |
|
563 |
* @return true if the field is supported on this date-time, false if not |
|
564 |
*/ |
|
565 |
@Override |
|
566 |
public boolean isSupported(TemporalField field) { |
|
567 |
if (field instanceof ChronoField) { |
|
568 |
ChronoField f = (ChronoField) field; |
|
16852 | 569 |
return f.isDateBased() || f.isTimeBased(); |
15289 | 570 |
} |
15658 | 571 |
return field != null && field.isSupportedBy(this); |
15289 | 572 |
} |
573 |
||
574 |
/** |
|
19030 | 575 |
* Checks if the specified unit is supported. |
576 |
* <p> |
|
577 |
* This checks if the specified unit can be added to, or subtracted from, this date-time. |
|
578 |
* If false, then calling the {@link #plus(long, TemporalUnit)} and |
|
579 |
* {@link #minus(long, TemporalUnit) minus} methods will throw an exception. |
|
580 |
* <p> |
|
581 |
* If the unit is a {@link ChronoUnit} then the query is implemented here. |
|
582 |
* The supported units are: |
|
583 |
* <ul> |
|
584 |
* <li>{@code NANOS} |
|
585 |
* <li>{@code MICROS} |
|
586 |
* <li>{@code MILLIS} |
|
587 |
* <li>{@code SECONDS} |
|
588 |
* <li>{@code MINUTES} |
|
589 |
* <li>{@code HOURS} |
|
590 |
* <li>{@code HALF_DAYS} |
|
591 |
* <li>{@code DAYS} |
|
592 |
* <li>{@code WEEKS} |
|
593 |
* <li>{@code MONTHS} |
|
594 |
* <li>{@code YEARS} |
|
595 |
* <li>{@code DECADES} |
|
596 |
* <li>{@code CENTURIES} |
|
597 |
* <li>{@code MILLENNIA} |
|
598 |
* <li>{@code ERAS} |
|
599 |
* </ul> |
|
600 |
* All other {@code ChronoUnit} instances will return false. |
|
601 |
* <p> |
|
602 |
* If the unit is not a {@code ChronoUnit}, then the result of this method |
|
603 |
* is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)} |
|
604 |
* passing {@code this} as the argument. |
|
605 |
* Whether the unit is supported is determined by the unit. |
|
606 |
* |
|
607 |
* @param unit the unit to check, null returns false |
|
608 |
* @return true if the unit can be added/subtracted, false if not |
|
609 |
*/ |
|
610 |
@Override // override for Javadoc |
|
611 |
public boolean isSupported(TemporalUnit unit) { |
|
612 |
return ChronoLocalDateTime.super.isSupported(unit); |
|
613 |
} |
|
614 |
||
615 |
//----------------------------------------------------------------------- |
|
616 |
/** |
|
15289 | 617 |
* Gets the range of valid values for the specified field. |
618 |
* <p> |
|
619 |
* The range object expresses the minimum and maximum valid values for a field. |
|
620 |
* This date-time is used to enhance the accuracy of the returned range. |
|
621 |
* If it is not possible to return the range, because the field is not supported |
|
622 |
* or for some other reason, an exception is thrown. |
|
623 |
* <p> |
|
624 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
625 |
* The {@link #isSupported(TemporalField) supported fields} will return |
|
626 |
* appropriate range instances. |
|
16852 | 627 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15289 | 628 |
* <p> |
629 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
15658 | 630 |
* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} |
15289 | 631 |
* passing {@code this} as the argument. |
632 |
* Whether the range can be obtained is determined by the field. |
|
633 |
* |
|
634 |
* @param field the field to query the range for, not null |
|
635 |
* @return the range of valid values for the field, not null |
|
636 |
* @throws DateTimeException if the range for the field cannot be obtained |
|
16852 | 637 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15289 | 638 |
*/ |
639 |
@Override |
|
640 |
public ValueRange range(TemporalField field) { |
|
641 |
if (field instanceof ChronoField) { |
|
642 |
ChronoField f = (ChronoField) field; |
|
16852 | 643 |
return (f.isTimeBased() ? time.range(field) : date.range(field)); |
15289 | 644 |
} |
15658 | 645 |
return field.rangeRefinedBy(this); |
15289 | 646 |
} |
647 |
||
648 |
/** |
|
649 |
* Gets the value of the specified field from this date-time as an {@code int}. |
|
650 |
* <p> |
|
651 |
* This queries this date-time for the value for the specified field. |
|
652 |
* The returned value will always be within the valid range of values for the field. |
|
653 |
* If it is not possible to return the value, because the field is not supported |
|
654 |
* or for some other reason, an exception is thrown. |
|
655 |
* <p> |
|
656 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
657 |
* The {@link #isSupported(TemporalField) supported fields} will return valid |
|
658 |
* values based on this date-time, except {@code NANO_OF_DAY}, {@code MICRO_OF_DAY}, |
|
16852 | 659 |
* {@code EPOCH_DAY} and {@code PROLEPTIC_MONTH} which are too large to fit in |
15289 | 660 |
* an {@code int} and throw a {@code DateTimeException}. |
16852 | 661 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15289 | 662 |
* <p> |
663 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
15658 | 664 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
15289 | 665 |
* passing {@code this} as the argument. Whether the value can be obtained, |
666 |
* and what the value represents, is determined by the field. |
|
667 |
* |
|
668 |
* @param field the field to get, not null |
|
669 |
* @return the value for the field |
|
16852 | 670 |
* @throws DateTimeException if a value for the field cannot be obtained or |
671 |
* the value is outside the range of valid values for the field |
|
672 |
* @throws UnsupportedTemporalTypeException if the field is not supported or |
|
673 |
* the range of values exceeds an {@code int} |
|
15289 | 674 |
* @throws ArithmeticException if numeric overflow occurs |
675 |
*/ |
|
676 |
@Override |
|
677 |
public int get(TemporalField field) { |
|
678 |
if (field instanceof ChronoField) { |
|
679 |
ChronoField f = (ChronoField) field; |
|
16852 | 680 |
return (f.isTimeBased() ? time.get(field) : date.get(field)); |
15289 | 681 |
} |
682 |
return ChronoLocalDateTime.super.get(field); |
|
683 |
} |
|
684 |
||
685 |
/** |
|
686 |
* Gets the value of the specified field from this date-time as a {@code long}. |
|
687 |
* <p> |
|
688 |
* This queries this date-time for the value for the specified field. |
|
689 |
* If it is not possible to return the value, because the field is not supported |
|
690 |
* or for some other reason, an exception is thrown. |
|
691 |
* <p> |
|
692 |
* If the field is a {@link ChronoField} then the query is implemented here. |
|
693 |
* The {@link #isSupported(TemporalField) supported fields} will return valid |
|
694 |
* values based on this date-time. |
|
16852 | 695 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15289 | 696 |
* <p> |
697 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
15658 | 698 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
15289 | 699 |
* passing {@code this} as the argument. Whether the value can be obtained, |
700 |
* and what the value represents, is determined by the field. |
|
701 |
* |
|
702 |
* @param field the field to get, not null |
|
703 |
* @return the value for the field |
|
704 |
* @throws DateTimeException if a value for the field cannot be obtained |
|
16852 | 705 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15289 | 706 |
* @throws ArithmeticException if numeric overflow occurs |
707 |
*/ |
|
708 |
@Override |
|
709 |
public long getLong(TemporalField field) { |
|
710 |
if (field instanceof ChronoField) { |
|
711 |
ChronoField f = (ChronoField) field; |
|
16852 | 712 |
return (f.isTimeBased() ? time.getLong(field) : date.getLong(field)); |
15289 | 713 |
} |
15658 | 714 |
return field.getFrom(this); |
15289 | 715 |
} |
716 |
||
717 |
//----------------------------------------------------------------------- |
|
718 |
/** |
|
719 |
* Gets the {@code LocalDate} part of this date-time. |
|
720 |
* <p> |
|
721 |
* This returns a {@code LocalDate} with the same year, month and day |
|
722 |
* as this date-time. |
|
723 |
* |
|
724 |
* @return the date part of this date-time, not null |
|
725 |
*/ |
|
726 |
@Override |
|
15658 | 727 |
public LocalDate toLocalDate() { |
15289 | 728 |
return date; |
729 |
} |
|
730 |
||
731 |
/** |
|
732 |
* Gets the year field. |
|
733 |
* <p> |
|
734 |
* This method returns the primitive {@code int} value for the year. |
|
735 |
* <p> |
|
736 |
* The year returned by this method is proleptic as per {@code get(YEAR)}. |
|
15658 | 737 |
* To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}. |
15289 | 738 |
* |
739 |
* @return the year, from MIN_YEAR to MAX_YEAR |
|
740 |
*/ |
|
741 |
public int getYear() { |
|
742 |
return date.getYear(); |
|
743 |
} |
|
744 |
||
745 |
/** |
|
746 |
* Gets the month-of-year field from 1 to 12. |
|
747 |
* <p> |
|
748 |
* This method returns the month as an {@code int} from 1 to 12. |
|
749 |
* Application code is frequently clearer if the enum {@link Month} |
|
750 |
* is used by calling {@link #getMonth()}. |
|
751 |
* |
|
752 |
* @return the month-of-year, from 1 to 12 |
|
753 |
* @see #getMonth() |
|
754 |
*/ |
|
755 |
public int getMonthValue() { |
|
756 |
return date.getMonthValue(); |
|
757 |
} |
|
758 |
||
759 |
/** |
|
760 |
* Gets the month-of-year field using the {@code Month} enum. |
|
761 |
* <p> |
|
762 |
* This method returns the enum {@link Month} for the month. |
|
763 |
* This avoids confusion as to what {@code int} values mean. |
|
764 |
* If you need access to the primitive {@code int} value then the enum |
|
765 |
* provides the {@link Month#getValue() int value}. |
|
766 |
* |
|
767 |
* @return the month-of-year, not null |
|
768 |
* @see #getMonthValue() |
|
769 |
*/ |
|
770 |
public Month getMonth() { |
|
771 |
return date.getMonth(); |
|
772 |
} |
|
773 |
||
774 |
/** |
|
775 |
* Gets the day-of-month field. |
|
776 |
* <p> |
|
777 |
* This method returns the primitive {@code int} value for the day-of-month. |
|
778 |
* |
|
779 |
* @return the day-of-month, from 1 to 31 |
|
780 |
*/ |
|
781 |
public int getDayOfMonth() { |
|
782 |
return date.getDayOfMonth(); |
|
783 |
} |
|
784 |
||
785 |
/** |
|
786 |
* Gets the day-of-year field. |
|
787 |
* <p> |
|
788 |
* This method returns the primitive {@code int} value for the day-of-year. |
|
789 |
* |
|
790 |
* @return the day-of-year, from 1 to 365, or 366 in a leap year |
|
791 |
*/ |
|
792 |
public int getDayOfYear() { |
|
793 |
return date.getDayOfYear(); |
|
794 |
} |
|
795 |
||
796 |
/** |
|
797 |
* Gets the day-of-week field, which is an enum {@code DayOfWeek}. |
|
798 |
* <p> |
|
799 |
* This method returns the enum {@link DayOfWeek} for the day-of-week. |
|
800 |
* This avoids confusion as to what {@code int} values mean. |
|
801 |
* If you need access to the primitive {@code int} value then the enum |
|
802 |
* provides the {@link DayOfWeek#getValue() int value}. |
|
803 |
* <p> |
|
804 |
* Additional information can be obtained from the {@code DayOfWeek}. |
|
805 |
* This includes textual names of the values. |
|
806 |
* |
|
807 |
* @return the day-of-week, not null |
|
808 |
*/ |
|
809 |
public DayOfWeek getDayOfWeek() { |
|
810 |
return date.getDayOfWeek(); |
|
811 |
} |
|
812 |
||
813 |
//----------------------------------------------------------------------- |
|
814 |
/** |
|
815 |
* Gets the {@code LocalTime} part of this date-time. |
|
816 |
* <p> |
|
817 |
* This returns a {@code LocalTime} with the same hour, minute, second and |
|
818 |
* nanosecond as this date-time. |
|
819 |
* |
|
820 |
* @return the time part of this date-time, not null |
|
821 |
*/ |
|
822 |
@Override |
|
15658 | 823 |
public LocalTime toLocalTime() { |
15289 | 824 |
return time; |
825 |
} |
|
826 |
||
827 |
/** |
|
828 |
* Gets the hour-of-day field. |
|
829 |
* |
|
830 |
* @return the hour-of-day, from 0 to 23 |
|
831 |
*/ |
|
832 |
public int getHour() { |
|
833 |
return time.getHour(); |
|
834 |
} |
|
835 |
||
836 |
/** |
|
837 |
* Gets the minute-of-hour field. |
|
838 |
* |
|
839 |
* @return the minute-of-hour, from 0 to 59 |
|
840 |
*/ |
|
841 |
public int getMinute() { |
|
842 |
return time.getMinute(); |
|
843 |
} |
|
844 |
||
845 |
/** |
|
846 |
* Gets the second-of-minute field. |
|
847 |
* |
|
848 |
* @return the second-of-minute, from 0 to 59 |
|
849 |
*/ |
|
850 |
public int getSecond() { |
|
851 |
return time.getSecond(); |
|
852 |
} |
|
853 |
||
854 |
/** |
|
855 |
* Gets the nano-of-second field. |
|
856 |
* |
|
857 |
* @return the nano-of-second, from 0 to 999,999,999 |
|
858 |
*/ |
|
859 |
public int getNano() { |
|
860 |
return time.getNano(); |
|
861 |
} |
|
862 |
||
863 |
//----------------------------------------------------------------------- |
|
864 |
/** |
|
865 |
* Returns an adjusted copy of this date-time. |
|
866 |
* <p> |
|
15658 | 867 |
* This returns a {@code LocalDateTime}, based on this one, with the date-time adjusted. |
15289 | 868 |
* The adjustment takes place using the specified adjuster strategy object. |
869 |
* Read the documentation of the adjuster to understand what adjustment will be made. |
|
870 |
* <p> |
|
871 |
* A simple adjuster might simply set the one of the fields, such as the year field. |
|
872 |
* A more complex adjuster might set the date to the last day of the month. |
|
16852 | 873 |
* A selection of common adjustments is provided in {@link TemporalAdjuster}. |
15289 | 874 |
* These include finding the "last day of the month" and "next Wednesday". |
875 |
* Key date-time classes also implement the {@code TemporalAdjuster} interface, |
|
15658 | 876 |
* such as {@link Month} and {@link java.time.MonthDay MonthDay}. |
15289 | 877 |
* The adjuster is responsible for handling special cases, such as the varying |
878 |
* lengths of month and leap years. |
|
879 |
* <p> |
|
880 |
* For example this code returns a date on the last day of July: |
|
881 |
* <pre> |
|
882 |
* import static java.time.Month.*; |
|
883 |
* import static java.time.temporal.Adjusters.*; |
|
884 |
* |
|
885 |
* result = localDateTime.with(JULY).with(lastDayOfMonth()); |
|
886 |
* </pre> |
|
887 |
* <p> |
|
888 |
* The classes {@link LocalDate} and {@link LocalTime} implement {@code TemporalAdjuster}, |
|
889 |
* thus this method can be used to change the date, time or offset: |
|
890 |
* <pre> |
|
891 |
* result = localDateTime.with(date); |
|
892 |
* result = localDateTime.with(time); |
|
893 |
* </pre> |
|
894 |
* <p> |
|
895 |
* The result of this method is obtained by invoking the |
|
896 |
* {@link TemporalAdjuster#adjustInto(Temporal)} method on the |
|
897 |
* specified adjuster passing {@code this} as the argument. |
|
898 |
* <p> |
|
899 |
* This instance is immutable and unaffected by this method call. |
|
900 |
* |
|
901 |
* @param adjuster the adjuster to use, not null |
|
902 |
* @return a {@code LocalDateTime} based on {@code this} with the adjustment made, not null |
|
903 |
* @throws DateTimeException if the adjustment cannot be made |
|
904 |
* @throws ArithmeticException if numeric overflow occurs |
|
905 |
*/ |
|
906 |
@Override |
|
907 |
public LocalDateTime with(TemporalAdjuster adjuster) { |
|
908 |
// optimizations |
|
909 |
if (adjuster instanceof LocalDate) { |
|
910 |
return with((LocalDate) adjuster, time); |
|
911 |
} else if (adjuster instanceof LocalTime) { |
|
912 |
return with(date, (LocalTime) adjuster); |
|
913 |
} else if (adjuster instanceof LocalDateTime) { |
|
914 |
return (LocalDateTime) adjuster; |
|
915 |
} |
|
916 |
return (LocalDateTime) adjuster.adjustInto(this); |
|
917 |
} |
|
918 |
||
919 |
/** |
|
920 |
* Returns a copy of this date-time with the specified field set to a new value. |
|
921 |
* <p> |
|
15658 | 922 |
* This returns a {@code LocalDateTime}, based on this one, with the value |
15289 | 923 |
* for the specified field changed. |
924 |
* This can be used to change any supported field, such as the year, month or day-of-month. |
|
925 |
* If it is not possible to set the value, because the field is not supported or for |
|
926 |
* some other reason, an exception is thrown. |
|
927 |
* <p> |
|
928 |
* In some cases, changing the specified field can cause the resulting date-time to become invalid, |
|
929 |
* such as changing the month from 31st January to February would make the day-of-month invalid. |
|
930 |
* In cases like this, the field is responsible for resolving the date. Typically it will choose |
|
931 |
* the previous valid date, which would be the last valid day of February in this example. |
|
932 |
* <p> |
|
933 |
* If the field is a {@link ChronoField} then the adjustment is implemented here. |
|
934 |
* The {@link #isSupported(TemporalField) supported fields} will behave as per |
|
935 |
* the matching method on {@link LocalDate#with(TemporalField, long) LocalDate} |
|
936 |
* or {@link LocalTime#with(TemporalField, long) LocalTime}. |
|
16852 | 937 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}. |
15289 | 938 |
* <p> |
939 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
15658 | 940 |
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} |
15289 | 941 |
* passing {@code this} as the argument. In this case, the field determines |
942 |
* whether and how to adjust the instant. |
|
943 |
* <p> |
|
944 |
* This instance is immutable and unaffected by this method call. |
|
945 |
* |
|
946 |
* @param field the field to set in the result, not null |
|
947 |
* @param newValue the new value of the field in the result |
|
948 |
* @return a {@code LocalDateTime} based on {@code this} with the specified field set, not null |
|
949 |
* @throws DateTimeException if the field cannot be set |
|
16852 | 950 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15289 | 951 |
* @throws ArithmeticException if numeric overflow occurs |
952 |
*/ |
|
953 |
@Override |
|
954 |
public LocalDateTime with(TemporalField field, long newValue) { |
|
955 |
if (field instanceof ChronoField) { |
|
956 |
ChronoField f = (ChronoField) field; |
|
16852 | 957 |
if (f.isTimeBased()) { |
15289 | 958 |
return with(date, time.with(field, newValue)); |
959 |
} else { |
|
960 |
return with(date.with(field, newValue), time); |
|
961 |
} |
|
962 |
} |
|
15658 | 963 |
return field.adjustInto(this, newValue); |
15289 | 964 |
} |
965 |
||
966 |
//----------------------------------------------------------------------- |
|
967 |
/** |
|
968 |
* Returns a copy of this {@code LocalDateTime} with the year altered. |
|
969 |
* The time does not affect the calculation and will be the same in the result. |
|
970 |
* If the day-of-month is invalid for the year, it will be changed to the last valid day of the month. |
|
971 |
* <p> |
|
972 |
* This instance is immutable and unaffected by this method call. |
|
973 |
* |
|
974 |
* @param year the year to set in the result, from MIN_YEAR to MAX_YEAR |
|
975 |
* @return a {@code LocalDateTime} based on this date-time with the requested year, not null |
|
976 |
* @throws DateTimeException if the year value is invalid |
|
977 |
*/ |
|
978 |
public LocalDateTime withYear(int year) { |
|
979 |
return with(date.withYear(year), time); |
|
980 |
} |
|
981 |
||
982 |
/** |
|
983 |
* Returns a copy of this {@code LocalDateTime} with the month-of-year altered. |
|
984 |
* The time does not affect the calculation and will be the same in the result. |
|
985 |
* If the day-of-month is invalid for the year, it will be changed to the last valid day of the month. |
|
986 |
* <p> |
|
987 |
* This instance is immutable and unaffected by this method call. |
|
988 |
* |
|
989 |
* @param month the month-of-year to set in the result, from 1 (January) to 12 (December) |
|
990 |
* @return a {@code LocalDateTime} based on this date-time with the requested month, not null |
|
991 |
* @throws DateTimeException if the month-of-year value is invalid |
|
992 |
*/ |
|
993 |
public LocalDateTime withMonth(int month) { |
|
994 |
return with(date.withMonth(month), time); |
|
995 |
} |
|
996 |
||
997 |
/** |
|
998 |
* Returns a copy of this {@code LocalDateTime} with the day-of-month altered. |
|
999 |
* If the resulting {@code LocalDateTime} is invalid, an exception is thrown. |
|
1000 |
* The time does not affect the calculation and will be the same in the result. |
|
1001 |
* <p> |
|
1002 |
* This instance is immutable and unaffected by this method call. |
|
1003 |
* |
|
1004 |
* @param dayOfMonth the day-of-month to set in the result, from 1 to 28-31 |
|
1005 |
* @return a {@code LocalDateTime} based on this date-time with the requested day, not null |
|
15658 | 1006 |
* @throws DateTimeException if the day-of-month value is invalid, |
1007 |
* or if the day-of-month is invalid for the month-year |
|
15289 | 1008 |
*/ |
1009 |
public LocalDateTime withDayOfMonth(int dayOfMonth) { |
|
1010 |
return with(date.withDayOfMonth(dayOfMonth), time); |
|
1011 |
} |
|
1012 |
||
1013 |
/** |
|
1014 |
* Returns a copy of this {@code LocalDateTime} with the day-of-year altered. |
|
1015 |
* If the resulting {@code LocalDateTime} is invalid, an exception is thrown. |
|
1016 |
* <p> |
|
1017 |
* This instance is immutable and unaffected by this method call. |
|
1018 |
* |
|
1019 |
* @param dayOfYear the day-of-year to set in the result, from 1 to 365-366 |
|
1020 |
* @return a {@code LocalDateTime} based on this date with the requested day, not null |
|
15658 | 1021 |
* @throws DateTimeException if the day-of-year value is invalid, |
1022 |
* or if the day-of-year is invalid for the year |
|
15289 | 1023 |
*/ |
1024 |
public LocalDateTime withDayOfYear(int dayOfYear) { |
|
1025 |
return with(date.withDayOfYear(dayOfYear), time); |
|
1026 |
} |
|
1027 |
||
1028 |
//----------------------------------------------------------------------- |
|
1029 |
/** |
|
1030 |
* Returns a copy of this {@code LocalDateTime} with the hour-of-day value altered. |
|
1031 |
* <p> |
|
1032 |
* This instance is immutable and unaffected by this method call. |
|
1033 |
* |
|
1034 |
* @param hour the hour-of-day to set in the result, from 0 to 23 |
|
1035 |
* @return a {@code LocalDateTime} based on this date-time with the requested hour, not null |
|
1036 |
* @throws DateTimeException if the hour value is invalid |
|
1037 |
*/ |
|
1038 |
public LocalDateTime withHour(int hour) { |
|
1039 |
LocalTime newTime = time.withHour(hour); |
|
1040 |
return with(date, newTime); |
|
1041 |
} |
|
1042 |
||
1043 |
/** |
|
1044 |
* Returns a copy of this {@code LocalDateTime} with the minute-of-hour value altered. |
|
1045 |
* <p> |
|
1046 |
* This instance is immutable and unaffected by this method call. |
|
1047 |
* |
|
1048 |
* @param minute the minute-of-hour to set in the result, from 0 to 59 |
|
1049 |
* @return a {@code LocalDateTime} based on this date-time with the requested minute, not null |
|
1050 |
* @throws DateTimeException if the minute value is invalid |
|
1051 |
*/ |
|
1052 |
public LocalDateTime withMinute(int minute) { |
|
1053 |
LocalTime newTime = time.withMinute(minute); |
|
1054 |
return with(date, newTime); |
|
1055 |
} |
|
1056 |
||
1057 |
/** |
|
1058 |
* Returns a copy of this {@code LocalDateTime} with the second-of-minute value altered. |
|
1059 |
* <p> |
|
1060 |
* This instance is immutable and unaffected by this method call. |
|
1061 |
* |
|
1062 |
* @param second the second-of-minute to set in the result, from 0 to 59 |
|
1063 |
* @return a {@code LocalDateTime} based on this date-time with the requested second, not null |
|
1064 |
* @throws DateTimeException if the second value is invalid |
|
1065 |
*/ |
|
1066 |
public LocalDateTime withSecond(int second) { |
|
1067 |
LocalTime newTime = time.withSecond(second); |
|
1068 |
return with(date, newTime); |
|
1069 |
} |
|
1070 |
||
1071 |
/** |
|
1072 |
* Returns a copy of this {@code LocalDateTime} with the nano-of-second value altered. |
|
1073 |
* <p> |
|
1074 |
* This instance is immutable and unaffected by this method call. |
|
1075 |
* |
|
1076 |
* @param nanoOfSecond the nano-of-second to set in the result, from 0 to 999,999,999 |
|
1077 |
* @return a {@code LocalDateTime} based on this date-time with the requested nanosecond, not null |
|
1078 |
* @throws DateTimeException if the nano value is invalid |
|
1079 |
*/ |
|
1080 |
public LocalDateTime withNano(int nanoOfSecond) { |
|
1081 |
LocalTime newTime = time.withNano(nanoOfSecond); |
|
1082 |
return with(date, newTime); |
|
1083 |
} |
|
1084 |
||
1085 |
//----------------------------------------------------------------------- |
|
1086 |
/** |
|
1087 |
* Returns a copy of this {@code LocalDateTime} with the time truncated. |
|
1088 |
* <p> |
|
1089 |
* Truncation returns a copy of the original date-time with fields |
|
1090 |
* smaller than the specified unit set to zero. |
|
1091 |
* For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit |
|
1092 |
* will set the second-of-minute and nano-of-second field to zero. |
|
1093 |
* <p> |
|
15658 | 1094 |
* The unit must have a {@linkplain TemporalUnit#getDuration() duration} |
1095 |
* that divides into the length of a standard day without remainder. |
|
1096 |
* This includes all supplied time units on {@link ChronoUnit} and |
|
1097 |
* {@link ChronoUnit#DAYS DAYS}. Other units throw an exception. |
|
15289 | 1098 |
* <p> |
1099 |
* This instance is immutable and unaffected by this method call. |
|
1100 |
* |
|
1101 |
* @param unit the unit to truncate to, not null |
|
1102 |
* @return a {@code LocalDateTime} based on this date-time with the time truncated, not null |
|
1103 |
* @throws DateTimeException if unable to truncate |
|
16852 | 1104 |
* @throws UnsupportedTemporalTypeException if the field is not supported |
15289 | 1105 |
*/ |
1106 |
public LocalDateTime truncatedTo(TemporalUnit unit) { |
|
1107 |
return with(date, time.truncatedTo(unit)); |
|
1108 |
} |
|
1109 |
||
1110 |
//----------------------------------------------------------------------- |
|
1111 |
/** |
|
15658 | 1112 |
* Returns a copy of this date-time with the specified amount added. |
1113 |
* <p> |
|
1114 |
* This returns a {@code LocalDateTime}, based on this one, with the specified amount added. |
|
1115 |
* The amount is typically {@link Period} or {@link Duration} but may be |
|
1116 |
* any other type implementing the {@link TemporalAmount} interface. |
|
15289 | 1117 |
* <p> |
15658 | 1118 |
* The calculation is delegated to the amount object by calling |
1119 |
* {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free |
|
1120 |
* to implement the addition in any way it wishes, however it typically |
|
1121 |
* calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation |
|
1122 |
* of the amount implementation to determine if it can be successfully added. |
|
15289 | 1123 |
* <p> |
1124 |
* This instance is immutable and unaffected by this method call. |
|
1125 |
* |
|
15658 | 1126 |
* @param amountToAdd the amount to add, not null |
15289 | 1127 |
* @return a {@code LocalDateTime} based on this date-time with the addition made, not null |
1128 |
* @throws DateTimeException if the addition cannot be made |
|
1129 |
* @throws ArithmeticException if numeric overflow occurs |
|
1130 |
*/ |
|
1131 |
@Override |
|
15658 | 1132 |
public LocalDateTime plus(TemporalAmount amountToAdd) { |
20517 | 1133 |
if (amountToAdd instanceof Period) { |
1134 |
Period periodToAdd = (Period) amountToAdd; |
|
1135 |
return with(date.plus(periodToAdd), time); |
|
1136 |
} |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20517
diff
changeset
|
1137 |
Objects.requireNonNull(amountToAdd, "amountToAdd"); |
15658 | 1138 |
return (LocalDateTime) amountToAdd.addTo(this); |
15289 | 1139 |
} |
1140 |
||
1141 |
/** |
|
15658 | 1142 |
* Returns a copy of this date-time with the specified amount added. |
1143 |
* <p> |
|
1144 |
* This returns a {@code LocalDateTime}, based on this one, with the amount |
|
1145 |
* in terms of the unit added. If it is not possible to add the amount, because the |
|
1146 |
* unit is not supported or for some other reason, an exception is thrown. |
|
15289 | 1147 |
* <p> |
15658 | 1148 |
* If the field is a {@link ChronoUnit} then the addition is implemented here. |
1149 |
* Date units are added as per {@link LocalDate#plus(long, TemporalUnit)}. |
|
1150 |
* Time units are added as per {@link LocalTime#plus(long, TemporalUnit)} with |
|
1151 |
* any overflow in days added equivalent to using {@link #plusDays(long)}. |
|
1152 |
* <p> |
|
1153 |
* If the field is not a {@code ChronoUnit}, then the result of this method |
|
1154 |
* is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)} |
|
1155 |
* passing {@code this} as the argument. In this case, the unit determines |
|
1156 |
* whether and how to perform the addition. |
|
15289 | 1157 |
* <p> |
1158 |
* This instance is immutable and unaffected by this method call. |
|
1159 |
* |
|
1160 |
* @param amountToAdd the amount of the unit to add to the result, may be negative |
|
15658 | 1161 |
* @param unit the unit of the amount to add, not null |
1162 |
* @return a {@code LocalDateTime} based on this date-time with the specified amount added, not null |
|
1163 |
* @throws DateTimeException if the addition cannot be made |
|
16852 | 1164 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15658 | 1165 |
* @throws ArithmeticException if numeric overflow occurs |
15289 | 1166 |
*/ |
1167 |
@Override |
|
1168 |
public LocalDateTime plus(long amountToAdd, TemporalUnit unit) { |
|
1169 |
if (unit instanceof ChronoUnit) { |
|
1170 |
ChronoUnit f = (ChronoUnit) unit; |
|
1171 |
switch (f) { |
|
1172 |
case NANOS: return plusNanos(amountToAdd); |
|
1173 |
case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000); |
|
1174 |
case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000); |
|
1175 |
case SECONDS: return plusSeconds(amountToAdd); |
|
1176 |
case MINUTES: return plusMinutes(amountToAdd); |
|
1177 |
case HOURS: return plusHours(amountToAdd); |
|
1178 |
case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is multiple of 2) |
|
1179 |
} |
|
1180 |
return with(date.plus(amountToAdd, unit), time); |
|
1181 |
} |
|
15658 | 1182 |
return unit.addTo(this, amountToAdd); |
15289 | 1183 |
} |
1184 |
||
1185 |
//----------------------------------------------------------------------- |
|
1186 |
/** |
|
1187 |
* Returns a copy of this {@code LocalDateTime} with the specified period in years added. |
|
1188 |
* <p> |
|
1189 |
* This method adds the specified amount to the years field in three steps: |
|
1190 |
* <ol> |
|
1191 |
* <li>Add the input years to the year field</li> |
|
1192 |
* <li>Check if the resulting date would be invalid</li> |
|
1193 |
* <li>Adjust the day-of-month to the last valid day if necessary</li> |
|
1194 |
* </ol> |
|
1195 |
* <p> |
|
1196 |
* For example, 2008-02-29 (leap year) plus one year would result in the |
|
1197 |
* invalid date 2009-02-29 (standard year). Instead of returning an invalid |
|
1198 |
* result, the last valid day of the month, 2009-02-28, is selected instead. |
|
1199 |
* <p> |
|
1200 |
* This instance is immutable and unaffected by this method call. |
|
1201 |
* |
|
1202 |
* @param years the years to add, may be negative |
|
1203 |
* @return a {@code LocalDateTime} based on this date-time with the years added, not null |
|
1204 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1205 |
*/ |
|
1206 |
public LocalDateTime plusYears(long years) { |
|
1207 |
LocalDate newDate = date.plusYears(years); |
|
1208 |
return with(newDate, time); |
|
1209 |
} |
|
1210 |
||
1211 |
/** |
|
1212 |
* Returns a copy of this {@code LocalDateTime} with the specified period in months added. |
|
1213 |
* <p> |
|
1214 |
* This method adds the specified amount to the months field in three steps: |
|
1215 |
* <ol> |
|
1216 |
* <li>Add the input months to the month-of-year field</li> |
|
1217 |
* <li>Check if the resulting date would be invalid</li> |
|
1218 |
* <li>Adjust the day-of-month to the last valid day if necessary</li> |
|
1219 |
* </ol> |
|
1220 |
* <p> |
|
1221 |
* For example, 2007-03-31 plus one month would result in the invalid date |
|
1222 |
* 2007-04-31. Instead of returning an invalid result, the last valid day |
|
1223 |
* of the month, 2007-04-30, is selected instead. |
|
1224 |
* <p> |
|
1225 |
* This instance is immutable and unaffected by this method call. |
|
1226 |
* |
|
1227 |
* @param months the months to add, may be negative |
|
1228 |
* @return a {@code LocalDateTime} based on this date-time with the months added, not null |
|
1229 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1230 |
*/ |
|
1231 |
public LocalDateTime plusMonths(long months) { |
|
1232 |
LocalDate newDate = date.plusMonths(months); |
|
1233 |
return with(newDate, time); |
|
1234 |
} |
|
1235 |
||
1236 |
/** |
|
1237 |
* Returns a copy of this {@code LocalDateTime} with the specified period in weeks added. |
|
1238 |
* <p> |
|
1239 |
* This method adds the specified amount in weeks to the days field incrementing |
|
1240 |
* the month and year fields as necessary to ensure the result remains valid. |
|
1241 |
* The result is only invalid if the maximum/minimum year is exceeded. |
|
1242 |
* <p> |
|
1243 |
* For example, 2008-12-31 plus one week would result in 2009-01-07. |
|
1244 |
* <p> |
|
1245 |
* This instance is immutable and unaffected by this method call. |
|
1246 |
* |
|
1247 |
* @param weeks the weeks to add, may be negative |
|
1248 |
* @return a {@code LocalDateTime} based on this date-time with the weeks added, not null |
|
1249 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1250 |
*/ |
|
1251 |
public LocalDateTime plusWeeks(long weeks) { |
|
1252 |
LocalDate newDate = date.plusWeeks(weeks); |
|
1253 |
return with(newDate, time); |
|
1254 |
} |
|
1255 |
||
1256 |
/** |
|
1257 |
* Returns a copy of this {@code LocalDateTime} with the specified period in days added. |
|
1258 |
* <p> |
|
1259 |
* This method adds the specified amount to the days field incrementing the |
|
1260 |
* month and year fields as necessary to ensure the result remains valid. |
|
1261 |
* The result is only invalid if the maximum/minimum year is exceeded. |
|
1262 |
* <p> |
|
1263 |
* For example, 2008-12-31 plus one day would result in 2009-01-01. |
|
1264 |
* <p> |
|
1265 |
* This instance is immutable and unaffected by this method call. |
|
1266 |
* |
|
1267 |
* @param days the days to add, may be negative |
|
1268 |
* @return a {@code LocalDateTime} based on this date-time with the days added, not null |
|
1269 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1270 |
*/ |
|
1271 |
public LocalDateTime plusDays(long days) { |
|
1272 |
LocalDate newDate = date.plusDays(days); |
|
1273 |
return with(newDate, time); |
|
1274 |
} |
|
1275 |
||
1276 |
//----------------------------------------------------------------------- |
|
1277 |
/** |
|
1278 |
* Returns a copy of this {@code LocalDateTime} with the specified period in hours added. |
|
1279 |
* <p> |
|
1280 |
* This instance is immutable and unaffected by this method call. |
|
1281 |
* |
|
1282 |
* @param hours the hours to add, may be negative |
|
1283 |
* @return a {@code LocalDateTime} based on this date-time with the hours added, not null |
|
1284 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1285 |
*/ |
|
1286 |
public LocalDateTime plusHours(long hours) { |
|
1287 |
return plusWithOverflow(date, hours, 0, 0, 0, 1); |
|
1288 |
} |
|
1289 |
||
1290 |
/** |
|
1291 |
* Returns a copy of this {@code LocalDateTime} with the specified period in minutes added. |
|
1292 |
* <p> |
|
1293 |
* This instance is immutable and unaffected by this method call. |
|
1294 |
* |
|
1295 |
* @param minutes the minutes to add, may be negative |
|
1296 |
* @return a {@code LocalDateTime} based on this date-time with the minutes added, not null |
|
1297 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1298 |
*/ |
|
1299 |
public LocalDateTime plusMinutes(long minutes) { |
|
1300 |
return plusWithOverflow(date, 0, minutes, 0, 0, 1); |
|
1301 |
} |
|
1302 |
||
1303 |
/** |
|
1304 |
* Returns a copy of this {@code LocalDateTime} with the specified period in seconds added. |
|
1305 |
* <p> |
|
1306 |
* This instance is immutable and unaffected by this method call. |
|
1307 |
* |
|
1308 |
* @param seconds the seconds to add, may be negative |
|
1309 |
* @return a {@code LocalDateTime} based on this date-time with the seconds added, not null |
|
1310 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1311 |
*/ |
|
1312 |
public LocalDateTime plusSeconds(long seconds) { |
|
1313 |
return plusWithOverflow(date, 0, 0, seconds, 0, 1); |
|
1314 |
} |
|
1315 |
||
1316 |
/** |
|
1317 |
* Returns a copy of this {@code LocalDateTime} with the specified period in nanoseconds added. |
|
1318 |
* <p> |
|
1319 |
* This instance is immutable and unaffected by this method call. |
|
1320 |
* |
|
1321 |
* @param nanos the nanos to add, may be negative |
|
1322 |
* @return a {@code LocalDateTime} based on this date-time with the nanoseconds added, not null |
|
1323 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1324 |
*/ |
|
1325 |
public LocalDateTime plusNanos(long nanos) { |
|
1326 |
return plusWithOverflow(date, 0, 0, 0, nanos, 1); |
|
1327 |
} |
|
1328 |
||
1329 |
//----------------------------------------------------------------------- |
|
1330 |
/** |
|
15658 | 1331 |
* Returns a copy of this date-time with the specified amount subtracted. |
1332 |
* <p> |
|
1333 |
* This returns a {@code LocalDateTime}, based on this one, with the specified amount subtracted. |
|
1334 |
* The amount is typically {@link Period} or {@link Duration} but may be |
|
1335 |
* any other type implementing the {@link TemporalAmount} interface. |
|
15289 | 1336 |
* <p> |
15658 | 1337 |
* The calculation is delegated to the amount object by calling |
1338 |
* {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free |
|
1339 |
* to implement the subtraction in any way it wishes, however it typically |
|
1340 |
* calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation |
|
1341 |
* of the amount implementation to determine if it can be successfully subtracted. |
|
15289 | 1342 |
* <p> |
1343 |
* This instance is immutable and unaffected by this method call. |
|
1344 |
* |
|
15658 | 1345 |
* @param amountToSubtract the amount to subtract, not null |
15289 | 1346 |
* @return a {@code LocalDateTime} based on this date-time with the subtraction made, not null |
1347 |
* @throws DateTimeException if the subtraction cannot be made |
|
1348 |
* @throws ArithmeticException if numeric overflow occurs |
|
1349 |
*/ |
|
1350 |
@Override |
|
15658 | 1351 |
public LocalDateTime minus(TemporalAmount amountToSubtract) { |
20517 | 1352 |
if (amountToSubtract instanceof Period) { |
1353 |
Period periodToSubtract = (Period) amountToSubtract; |
|
1354 |
return with(date.minus(periodToSubtract), time); |
|
1355 |
} |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20517
diff
changeset
|
1356 |
Objects.requireNonNull(amountToSubtract, "amountToSubtract"); |
15658 | 1357 |
return (LocalDateTime) amountToSubtract.subtractFrom(this); |
15289 | 1358 |
} |
1359 |
||
1360 |
/** |
|
15658 | 1361 |
* Returns a copy of this date-time with the specified amount subtracted. |
15289 | 1362 |
* <p> |
15658 | 1363 |
* This returns a {@code LocalDateTime}, based on this one, with the amount |
1364 |
* in terms of the unit subtracted. If it is not possible to subtract the amount, |
|
1365 |
* because the unit is not supported or for some other reason, an exception is thrown. |
|
1366 |
* <p> |
|
1367 |
* This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated. |
|
1368 |
* See that method for a full description of how addition, and thus subtraction, works. |
|
15289 | 1369 |
* <p> |
1370 |
* This instance is immutable and unaffected by this method call. |
|
1371 |
* |
|
1372 |
* @param amountToSubtract the amount of the unit to subtract from the result, may be negative |
|
15658 | 1373 |
* @param unit the unit of the amount to subtract, not null |
1374 |
* @return a {@code LocalDateTime} based on this date-time with the specified amount subtracted, not null |
|
1375 |
* @throws DateTimeException if the subtraction cannot be made |
|
16852 | 1376 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15658 | 1377 |
* @throws ArithmeticException if numeric overflow occurs |
15289 | 1378 |
*/ |
1379 |
@Override |
|
1380 |
public LocalDateTime minus(long amountToSubtract, TemporalUnit unit) { |
|
1381 |
return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); |
|
1382 |
} |
|
1383 |
||
1384 |
//----------------------------------------------------------------------- |
|
1385 |
/** |
|
1386 |
* Returns a copy of this {@code LocalDateTime} with the specified period in years subtracted. |
|
1387 |
* <p> |
|
1388 |
* This method subtracts the specified amount from the years field in three steps: |
|
1389 |
* <ol> |
|
1390 |
* <li>Subtract the input years from the year field</li> |
|
1391 |
* <li>Check if the resulting date would be invalid</li> |
|
1392 |
* <li>Adjust the day-of-month to the last valid day if necessary</li> |
|
1393 |
* </ol> |
|
1394 |
* <p> |
|
1395 |
* For example, 2008-02-29 (leap year) minus one year would result in the |
|
1396 |
* invalid date 2009-02-29 (standard year). Instead of returning an invalid |
|
1397 |
* result, the last valid day of the month, 2009-02-28, is selected instead. |
|
1398 |
* <p> |
|
1399 |
* This instance is immutable and unaffected by this method call. |
|
1400 |
* |
|
1401 |
* @param years the years to subtract, may be negative |
|
1402 |
* @return a {@code LocalDateTime} based on this date-time with the years subtracted, not null |
|
1403 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1404 |
*/ |
|
1405 |
public LocalDateTime minusYears(long years) { |
|
1406 |
return (years == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-years)); |
|
1407 |
} |
|
1408 |
||
1409 |
/** |
|
1410 |
* Returns a copy of this {@code LocalDateTime} with the specified period in months subtracted. |
|
1411 |
* <p> |
|
1412 |
* This method subtracts the specified amount from the months field in three steps: |
|
1413 |
* <ol> |
|
1414 |
* <li>Subtract the input months from the month-of-year field</li> |
|
1415 |
* <li>Check if the resulting date would be invalid</li> |
|
1416 |
* <li>Adjust the day-of-month to the last valid day if necessary</li> |
|
1417 |
* </ol> |
|
1418 |
* <p> |
|
1419 |
* For example, 2007-03-31 minus one month would result in the invalid date |
|
1420 |
* 2007-04-31. Instead of returning an invalid result, the last valid day |
|
1421 |
* of the month, 2007-04-30, is selected instead. |
|
1422 |
* <p> |
|
1423 |
* This instance is immutable and unaffected by this method call. |
|
1424 |
* |
|
1425 |
* @param months the months to subtract, may be negative |
|
1426 |
* @return a {@code LocalDateTime} based on this date-time with the months subtracted, not null |
|
1427 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1428 |
*/ |
|
1429 |
public LocalDateTime minusMonths(long months) { |
|
1430 |
return (months == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-months)); |
|
1431 |
} |
|
1432 |
||
1433 |
/** |
|
1434 |
* Returns a copy of this {@code LocalDateTime} with the specified period in weeks subtracted. |
|
1435 |
* <p> |
|
1436 |
* This method subtracts the specified amount in weeks from the days field decrementing |
|
1437 |
* the month and year fields as necessary to ensure the result remains valid. |
|
1438 |
* The result is only invalid if the maximum/minimum year is exceeded. |
|
1439 |
* <p> |
|
1440 |
* For example, 2009-01-07 minus one week would result in 2008-12-31. |
|
1441 |
* <p> |
|
1442 |
* This instance is immutable and unaffected by this method call. |
|
1443 |
* |
|
1444 |
* @param weeks the weeks to subtract, may be negative |
|
1445 |
* @return a {@code LocalDateTime} based on this date-time with the weeks subtracted, not null |
|
1446 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1447 |
*/ |
|
1448 |
public LocalDateTime minusWeeks(long weeks) { |
|
1449 |
return (weeks == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeks)); |
|
1450 |
} |
|
1451 |
||
1452 |
/** |
|
1453 |
* Returns a copy of this {@code LocalDateTime} with the specified period in days subtracted. |
|
1454 |
* <p> |
|
1455 |
* This method subtracts the specified amount from the days field incrementing the |
|
1456 |
* month and year fields as necessary to ensure the result remains valid. |
|
1457 |
* The result is only invalid if the maximum/minimum year is exceeded. |
|
1458 |
* <p> |
|
1459 |
* For example, 2009-01-01 minus one day would result in 2008-12-31. |
|
1460 |
* <p> |
|
1461 |
* This instance is immutable and unaffected by this method call. |
|
1462 |
* |
|
1463 |
* @param days the days to subtract, may be negative |
|
1464 |
* @return a {@code LocalDateTime} based on this date-time with the days subtracted, not null |
|
1465 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1466 |
*/ |
|
1467 |
public LocalDateTime minusDays(long days) { |
|
1468 |
return (days == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-days)); |
|
1469 |
} |
|
1470 |
||
1471 |
//----------------------------------------------------------------------- |
|
1472 |
/** |
|
1473 |
* Returns a copy of this {@code LocalDateTime} with the specified period in hours subtracted. |
|
1474 |
* <p> |
|
1475 |
* This instance is immutable and unaffected by this method call. |
|
1476 |
* |
|
1477 |
* @param hours the hours to subtract, may be negative |
|
1478 |
* @return a {@code LocalDateTime} based on this date-time with the hours subtracted, not null |
|
1479 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1480 |
*/ |
|
1481 |
public LocalDateTime minusHours(long hours) { |
|
1482 |
return plusWithOverflow(date, hours, 0, 0, 0, -1); |
|
1483 |
} |
|
1484 |
||
1485 |
/** |
|
1486 |
* Returns a copy of this {@code LocalDateTime} with the specified period in minutes subtracted. |
|
1487 |
* <p> |
|
1488 |
* This instance is immutable and unaffected by this method call. |
|
1489 |
* |
|
1490 |
* @param minutes the minutes to subtract, may be negative |
|
1491 |
* @return a {@code LocalDateTime} based on this date-time with the minutes subtracted, not null |
|
1492 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1493 |
*/ |
|
1494 |
public LocalDateTime minusMinutes(long minutes) { |
|
1495 |
return plusWithOverflow(date, 0, minutes, 0, 0, -1); |
|
1496 |
} |
|
1497 |
||
1498 |
/** |
|
1499 |
* Returns a copy of this {@code LocalDateTime} with the specified period in seconds subtracted. |
|
1500 |
* <p> |
|
1501 |
* This instance is immutable and unaffected by this method call. |
|
1502 |
* |
|
1503 |
* @param seconds the seconds to subtract, may be negative |
|
1504 |
* @return a {@code LocalDateTime} based on this date-time with the seconds subtracted, not null |
|
1505 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1506 |
*/ |
|
1507 |
public LocalDateTime minusSeconds(long seconds) { |
|
1508 |
return plusWithOverflow(date, 0, 0, seconds, 0, -1); |
|
1509 |
} |
|
1510 |
||
1511 |
/** |
|
1512 |
* Returns a copy of this {@code LocalDateTime} with the specified period in nanoseconds subtracted. |
|
1513 |
* <p> |
|
1514 |
* This instance is immutable and unaffected by this method call. |
|
1515 |
* |
|
1516 |
* @param nanos the nanos to subtract, may be negative |
|
1517 |
* @return a {@code LocalDateTime} based on this date-time with the nanoseconds subtracted, not null |
|
1518 |
* @throws DateTimeException if the result exceeds the supported date range |
|
1519 |
*/ |
|
1520 |
public LocalDateTime minusNanos(long nanos) { |
|
1521 |
return plusWithOverflow(date, 0, 0, 0, nanos, -1); |
|
1522 |
} |
|
1523 |
||
1524 |
//----------------------------------------------------------------------- |
|
1525 |
/** |
|
1526 |
* Returns a copy of this {@code LocalDateTime} with the specified period added. |
|
1527 |
* <p> |
|
1528 |
* This instance is immutable and unaffected by this method call. |
|
1529 |
* |
|
1530 |
* @param newDate the new date to base the calculation on, not null |
|
1531 |
* @param hours the hours to add, may be negative |
|
1532 |
* @param minutes the minutes to add, may be negative |
|
1533 |
* @param seconds the seconds to add, may be negative |
|
1534 |
* @param nanos the nanos to add, may be negative |
|
1535 |
* @param sign the sign to determine add or subtract |
|
1536 |
* @return the combined result, not null |
|
1537 |
*/ |
|
1538 |
private LocalDateTime plusWithOverflow(LocalDate newDate, long hours, long minutes, long seconds, long nanos, int sign) { |
|
1539 |
// 9223372036854775808 long, 2147483648 int |
|
1540 |
if ((hours | minutes | seconds | nanos) == 0) { |
|
1541 |
return with(newDate, time); |
|
1542 |
} |
|
1543 |
long totDays = nanos / NANOS_PER_DAY + // max/24*60*60*1B |
|
1544 |
seconds / SECONDS_PER_DAY + // max/24*60*60 |
|
1545 |
minutes / MINUTES_PER_DAY + // max/24*60 |
|
1546 |
hours / HOURS_PER_DAY; // max/24 |
|
1547 |
totDays *= sign; // total max*0.4237... |
|
1548 |
long totNanos = nanos % NANOS_PER_DAY + // max 86400000000000 |
|
1549 |
(seconds % SECONDS_PER_DAY) * NANOS_PER_SECOND + // max 86400000000000 |
|
1550 |
(minutes % MINUTES_PER_DAY) * NANOS_PER_MINUTE + // max 86400000000000 |
|
1551 |
(hours % HOURS_PER_DAY) * NANOS_PER_HOUR; // max 86400000000000 |
|
1552 |
long curNoD = time.toNanoOfDay(); // max 86400000000000 |
|
1553 |
totNanos = totNanos * sign + curNoD; // total 432000000000000 |
|
1554 |
totDays += Math.floorDiv(totNanos, NANOS_PER_DAY); |
|
1555 |
long newNoD = Math.floorMod(totNanos, NANOS_PER_DAY); |
|
1556 |
LocalTime newTime = (newNoD == curNoD ? time : LocalTime.ofNanoOfDay(newNoD)); |
|
1557 |
return with(newDate.plusDays(totDays), newTime); |
|
1558 |
} |
|
1559 |
||
1560 |
//----------------------------------------------------------------------- |
|
1561 |
/** |
|
1562 |
* Queries this date-time using the specified query. |
|
1563 |
* <p> |
|
1564 |
* This queries this date-time using the specified query strategy object. |
|
1565 |
* The {@code TemporalQuery} object defines the logic to be used to |
|
1566 |
* obtain the result. Read the documentation of the query to understand |
|
1567 |
* what the result of this method will be. |
|
1568 |
* <p> |
|
1569 |
* The result of this method is obtained by invoking the |
|
1570 |
* {@link java.time.temporal.TemporalQuery#queryFrom(TemporalAccessor)} method on the |
|
1571 |
* specified query passing {@code this} as the argument. |
|
1572 |
* |
|
1573 |
* @param <R> the type of the result |
|
1574 |
* @param query the query to invoke, not null |
|
1575 |
* @return the query result, null may be returned (defined by the query) |
|
1576 |
* @throws DateTimeException if unable to query (defined by the query) |
|
1577 |
* @throws ArithmeticException if numeric overflow occurs (defined by the query) |
|
1578 |
*/ |
|
15658 | 1579 |
@SuppressWarnings("unchecked") |
15289 | 1580 |
@Override // override for Javadoc |
1581 |
public <R> R query(TemporalQuery<R> query) { |
|
20795 | 1582 |
if (query == TemporalQueries.localDate()) { |
15658 | 1583 |
return (R) date; |
1584 |
} |
|
15289 | 1585 |
return ChronoLocalDateTime.super.query(query); |
1586 |
} |
|
1587 |
||
1588 |
/** |
|
1589 |
* Adjusts the specified temporal object to have the same date and time as this object. |
|
1590 |
* <p> |
|
1591 |
* This returns a temporal object of the same observable type as the input |
|
1592 |
* with the date and time changed to be the same as this. |
|
1593 |
* <p> |
|
1594 |
* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} |
|
1595 |
* twice, passing {@link ChronoField#EPOCH_DAY} and |
|
1596 |
* {@link ChronoField#NANO_OF_DAY} as the fields. |
|
1597 |
* <p> |
|
1598 |
* In most cases, it is clearer to reverse the calling pattern by using |
|
1599 |
* {@link Temporal#with(TemporalAdjuster)}: |
|
1600 |
* <pre> |
|
1601 |
* // these two lines are equivalent, but the second approach is recommended |
|
1602 |
* temporal = thisLocalDateTime.adjustInto(temporal); |
|
1603 |
* temporal = temporal.with(thisLocalDateTime); |
|
1604 |
* </pre> |
|
1605 |
* <p> |
|
1606 |
* This instance is immutable and unaffected by this method call. |
|
1607 |
* |
|
1608 |
* @param temporal the target object to be adjusted, not null |
|
1609 |
* @return the adjusted object, not null |
|
1610 |
* @throws DateTimeException if unable to make the adjustment |
|
1611 |
* @throws ArithmeticException if numeric overflow occurs |
|
1612 |
*/ |
|
1613 |
@Override // override for Javadoc |
|
1614 |
public Temporal adjustInto(Temporal temporal) { |
|
1615 |
return ChronoLocalDateTime.super.adjustInto(temporal); |
|
1616 |
} |
|
1617 |
||
1618 |
/** |
|
17474 | 1619 |
* Calculates the amount of time until another date-time in terms of the specified unit. |
15289 | 1620 |
* <p> |
17474 | 1621 |
* This calculates the amount of time between two {@code LocalDateTime} |
1622 |
* objects in terms of a single {@code TemporalUnit}. |
|
15289 | 1623 |
* The start and end points are {@code this} and the specified date-time. |
1624 |
* The result will be negative if the end is before the start. |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20517
diff
changeset
|
1625 |
* The {@code Temporal} passed to this method is converted to a |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20517
diff
changeset
|
1626 |
* {@code LocalDateTime} using {@link #from(TemporalAccessor)}. |
17474 | 1627 |
* For example, the amount in days between two date-times can be calculated |
19030 | 1628 |
* using {@code startDateTime.until(endDateTime, DAYS)}. |
15289 | 1629 |
* <p> |
1630 |
* The calculation returns a whole number, representing the number of |
|
1631 |
* complete units between the two date-times. |
|
17474 | 1632 |
* For example, the amount in months between 2012-06-15T00:00 and 2012-08-14T23:59 |
15289 | 1633 |
* will only be one month as it is one minute short of two months. |
1634 |
* <p> |
|
15658 | 1635 |
* There are two equivalent ways of using this method. |
1636 |
* The first is to invoke this method. |
|
1637 |
* The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: |
|
15289 | 1638 |
* <pre> |
15658 | 1639 |
* // these two lines are equivalent |
19030 | 1640 |
* amount = start.until(end, MONTHS); |
15658 | 1641 |
* amount = MONTHS.between(start, end); |
15289 | 1642 |
* </pre> |
15658 | 1643 |
* The choice should be made based on which makes the code more readable. |
15289 | 1644 |
* <p> |
1645 |
* The calculation is implemented in this method for {@link ChronoUnit}. |
|
1646 |
* The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS}, |
|
1647 |
* {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS}, |
|
1648 |
* {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES}, |
|
1649 |
* {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported. |
|
1650 |
* Other {@code ChronoUnit} values will throw an exception. |
|
1651 |
* <p> |
|
1652 |
* If the unit is not a {@code ChronoUnit}, then the result of this method |
|
1653 |
* is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)} |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20517
diff
changeset
|
1654 |
* passing {@code this} as the first argument and the converted input temporal |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20517
diff
changeset
|
1655 |
* as the second argument. |
15289 | 1656 |
* <p> |
1657 |
* This instance is immutable and unaffected by this method call. |
|
1658 |
* |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20517
diff
changeset
|
1659 |
* @param endExclusive the end date, exclusive, which is converted to a {@code LocalDateTime}, not null |
17474 | 1660 |
* @param unit the unit to measure the amount in, not null |
1661 |
* @return the amount of time between this date-time and the end date-time |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20517
diff
changeset
|
1662 |
* @throws DateTimeException if the amount cannot be calculated, or the end |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20517
diff
changeset
|
1663 |
* temporal cannot be converted to a {@code LocalDateTime} |
16852 | 1664 |
* @throws UnsupportedTemporalTypeException if the unit is not supported |
15289 | 1665 |
* @throws ArithmeticException if numeric overflow occurs |
1666 |
*/ |
|
1667 |
@Override |
|
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20517
diff
changeset
|
1668 |
public long until(Temporal endExclusive, TemporalUnit unit) { |
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20517
diff
changeset
|
1669 |
LocalDateTime end = LocalDateTime.from(endExclusive); |
15289 | 1670 |
if (unit instanceof ChronoUnit) { |
19030 | 1671 |
if (unit.isTimeBased()) { |
15289 | 1672 |
long amount = date.daysUntil(end.date); |
16852 | 1673 |
if (amount == 0) { |
19030 | 1674 |
return time.until(end.time, unit); |
16852 | 1675 |
} |
1676 |
long timePart = end.time.toNanoOfDay() - time.toNanoOfDay(); |
|
1677 |
if (amount > 0) { |
|
1678 |
amount--; // safe |
|
1679 |
timePart += NANOS_PER_DAY; // safe |
|
1680 |
} else { |
|
1681 |
amount++; // safe |
|
1682 |
timePart -= NANOS_PER_DAY; // safe |
|
1683 |
} |
|
19030 | 1684 |
switch ((ChronoUnit) unit) { |
16852 | 1685 |
case NANOS: |
1686 |
amount = Math.multiplyExact(amount, NANOS_PER_DAY); |
|
1687 |
break; |
|
1688 |
case MICROS: |
|
1689 |
amount = Math.multiplyExact(amount, MICROS_PER_DAY); |
|
1690 |
timePart = timePart / 1000; |
|
1691 |
break; |
|
1692 |
case MILLIS: |
|
1693 |
amount = Math.multiplyExact(amount, MILLIS_PER_DAY); |
|
1694 |
timePart = timePart / 1_000_000; |
|
1695 |
break; |
|
1696 |
case SECONDS: |
|
1697 |
amount = Math.multiplyExact(amount, SECONDS_PER_DAY); |
|
1698 |
timePart = timePart / NANOS_PER_SECOND; |
|
1699 |
break; |
|
1700 |
case MINUTES: |
|
1701 |
amount = Math.multiplyExact(amount, MINUTES_PER_DAY); |
|
1702 |
timePart = timePart / NANOS_PER_MINUTE; |
|
1703 |
break; |
|
1704 |
case HOURS: |
|
1705 |
amount = Math.multiplyExact(amount, HOURS_PER_DAY); |
|
1706 |
timePart = timePart / NANOS_PER_HOUR; |
|
1707 |
break; |
|
1708 |
case HALF_DAYS: |
|
1709 |
amount = Math.multiplyExact(amount, 2); |
|
1710 |
timePart = timePart / (NANOS_PER_HOUR * 12); |
|
1711 |
break; |
|
15289 | 1712 |
} |
16852 | 1713 |
return Math.addExact(amount, timePart); |
15289 | 1714 |
} |
1715 |
LocalDate endDate = end.date; |
|
16852 | 1716 |
if (endDate.isAfter(date) && end.time.isBefore(time)) { |
15289 | 1717 |
endDate = endDate.minusDays(1); |
16852 | 1718 |
} else if (endDate.isBefore(date) && end.time.isAfter(time)) { |
1719 |
endDate = endDate.plusDays(1); |
|
15289 | 1720 |
} |
19030 | 1721 |
return date.until(endDate, unit); |
15289 | 1722 |
} |
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
20517
diff
changeset
|
1723 |
return unit.between(this, end); |
15289 | 1724 |
} |
1725 |
||
16852 | 1726 |
/** |
1727 |
* Formats this date-time using the specified formatter. |
|
1728 |
* <p> |
|
1729 |
* This date-time will be passed to the formatter to produce a string. |
|
1730 |
* |
|
1731 |
* @param formatter the formatter to use, not null |
|
1732 |
* @return the formatted date-time string, not null |
|
1733 |
* @throws DateTimeException if an error occurs during printing |
|
1734 |
*/ |
|
1735 |
@Override // override for Javadoc and performance |
|
1736 |
public String format(DateTimeFormatter formatter) { |
|
1737 |
Objects.requireNonNull(formatter, "formatter"); |
|
1738 |
return formatter.format(this); |
|
1739 |
} |
|
1740 |
||
15289 | 1741 |
//----------------------------------------------------------------------- |
1742 |
/** |
|
16852 | 1743 |
* Combines this date-time with an offset to create an {@code OffsetDateTime}. |
15289 | 1744 |
* <p> |
16852 | 1745 |
* This returns an {@code OffsetDateTime} formed from this date-time at the specified offset. |
15289 | 1746 |
* All possible combinations of date-time and offset are valid. |
1747 |
* |
|
1748 |
* @param offset the offset to combine with, not null |
|
1749 |
* @return the offset date-time formed from this date-time and the specified offset, not null |
|
1750 |
*/ |
|
1751 |
public OffsetDateTime atOffset(ZoneOffset offset) { |
|
1752 |
return OffsetDateTime.of(this, offset); |
|
1753 |
} |
|
1754 |
||
1755 |
/** |
|
16852 | 1756 |
* Combines this date-time with a time-zone to create a {@code ZonedDateTime}. |
15289 | 1757 |
* <p> |
15658 | 1758 |
* This returns a {@code ZonedDateTime} formed from this date-time at the |
1759 |
* specified time-zone. The result will match this date-time as closely as possible. |
|
15289 | 1760 |
* Time-zone rules, such as daylight savings, mean that not every local date-time |
1761 |
* is valid for the specified zone, thus the local date-time may be adjusted. |
|
1762 |
* <p> |
|
1763 |
* The local date-time is resolved to a single instant on the time-line. |
|
1764 |
* This is achieved by finding a valid offset from UTC/Greenwich for the local |
|
1765 |
* date-time as defined by the {@link ZoneRules rules} of the zone ID. |
|
1766 |
*<p> |
|
1767 |
* In most cases, there is only one valid offset for a local date-time. |
|
1768 |
* In the case of an overlap, where clocks are set back, there are two valid offsets. |
|
1769 |
* This method uses the earlier offset typically corresponding to "summer". |
|
1770 |
* <p> |
|
1771 |
* In the case of a gap, where clocks jump forward, there is no valid offset. |
|
1772 |
* Instead, the local date-time is adjusted to be later by the length of the gap. |
|
1773 |
* For a typical one hour daylight savings change, the local date-time will be |
|
1774 |
* moved one hour later into the offset typically corresponding to "summer". |
|
1775 |
* <p> |
|
1776 |
* To obtain the later offset during an overlap, call |
|
1777 |
* {@link ZonedDateTime#withLaterOffsetAtOverlap()} on the result of this method. |
|
1778 |
* To throw an exception when there is a gap or overlap, use |
|
1779 |
* {@link ZonedDateTime#ofStrict(LocalDateTime, ZoneOffset, ZoneId)}. |
|
1780 |
* |
|
1781 |
* @param zone the time-zone to use, not null |
|
1782 |
* @return the zoned date-time formed from this date-time, not null |
|
1783 |
*/ |
|
1784 |
@Override |
|
1785 |
public ZonedDateTime atZone(ZoneId zone) { |
|
1786 |
return ZonedDateTime.of(this, zone); |
|
1787 |
} |
|
1788 |
||
1789 |
//----------------------------------------------------------------------- |
|
1790 |
/** |
|
1791 |
* Compares this date-time to another date-time. |
|
1792 |
* <p> |
|
1793 |
* The comparison is primarily based on the date-time, from earliest to latest. |
|
1794 |
* It is "consistent with equals", as defined by {@link Comparable}. |
|
1795 |
* <p> |
|
1796 |
* If all the date-times being compared are instances of {@code LocalDateTime}, |
|
1797 |
* then the comparison will be entirely based on the date-time. |
|
1798 |
* If some dates being compared are in different chronologies, then the |
|
1799 |
* chronology is also considered, see {@link ChronoLocalDateTime#compareTo}. |
|
1800 |
* |
|
1801 |
* @param other the other date-time to compare to, not null |
|
1802 |
* @return the comparator value, negative if less, positive if greater |
|
1803 |
*/ |
|
1804 |
@Override // override for Javadoc and performance |
|
1805 |
public int compareTo(ChronoLocalDateTime<?> other) { |
|
1806 |
if (other instanceof LocalDateTime) { |
|
1807 |
return compareTo0((LocalDateTime) other); |
|
1808 |
} |
|
1809 |
return ChronoLocalDateTime.super.compareTo(other); |
|
1810 |
} |
|
1811 |
||
1812 |
private int compareTo0(LocalDateTime other) { |
|
15658 | 1813 |
int cmp = date.compareTo0(other.toLocalDate()); |
15289 | 1814 |
if (cmp == 0) { |
15658 | 1815 |
cmp = time.compareTo(other.toLocalTime()); |
15289 | 1816 |
} |
1817 |
return cmp; |
|
1818 |
} |
|
1819 |
||
1820 |
/** |
|
1821 |
* Checks if this date-time is after the specified date-time. |
|
1822 |
* <p> |
|
1823 |
* This checks to see if this date-time represents a point on the |
|
1824 |
* local time-line after the other date-time. |
|
1825 |
* <pre> |
|
1826 |
* LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00); |
|
1827 |
* LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00); |
|
1828 |
* a.isAfter(b) == false |
|
1829 |
* a.isAfter(a) == false |
|
1830 |
* b.isAfter(a) == true |
|
1831 |
* </pre> |
|
1832 |
* <p> |
|
1833 |
* This method only considers the position of the two date-times on the local time-line. |
|
1834 |
* It does not take into account the chronology, or calendar system. |
|
1835 |
* This is different from the comparison in {@link #compareTo(ChronoLocalDateTime)}, |
|
16852 | 1836 |
* but is the same approach as {@link ChronoLocalDateTime#timeLineOrder()}. |
15289 | 1837 |
* |
1838 |
* @param other the other date-time to compare to, not null |
|
1839 |
* @return true if this date-time is after the specified date-time |
|
1840 |
*/ |
|
1841 |
@Override // override for Javadoc and performance |
|
1842 |
public boolean isAfter(ChronoLocalDateTime<?> other) { |
|
1843 |
if (other instanceof LocalDateTime) { |
|
1844 |
return compareTo0((LocalDateTime) other) > 0; |
|
1845 |
} |
|
1846 |
return ChronoLocalDateTime.super.isAfter(other); |
|
1847 |
} |
|
1848 |
||
1849 |
/** |
|
1850 |
* Checks if this date-time is before the specified date-time. |
|
1851 |
* <p> |
|
1852 |
* This checks to see if this date-time represents a point on the |
|
1853 |
* local time-line before the other date-time. |
|
1854 |
* <pre> |
|
1855 |
* LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00); |
|
1856 |
* LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00); |
|
1857 |
* a.isBefore(b) == true |
|
1858 |
* a.isBefore(a) == false |
|
1859 |
* b.isBefore(a) == false |
|
1860 |
* </pre> |
|
1861 |
* <p> |
|
1862 |
* This method only considers the position of the two date-times on the local time-line. |
|
1863 |
* It does not take into account the chronology, or calendar system. |
|
1864 |
* This is different from the comparison in {@link #compareTo(ChronoLocalDateTime)}, |
|
16852 | 1865 |
* but is the same approach as {@link ChronoLocalDateTime#timeLineOrder()}. |
15289 | 1866 |
* |
1867 |
* @param other the other date-time to compare to, not null |
|
1868 |
* @return true if this date-time is before the specified date-time |
|
1869 |
*/ |
|
1870 |
@Override // override for Javadoc and performance |
|
1871 |
public boolean isBefore(ChronoLocalDateTime<?> other) { |
|
1872 |
if (other instanceof LocalDateTime) { |
|
1873 |
return compareTo0((LocalDateTime) other) < 0; |
|
1874 |
} |
|
1875 |
return ChronoLocalDateTime.super.isBefore(other); |
|
1876 |
} |
|
1877 |
||
1878 |
/** |
|
1879 |
* Checks if this date-time is equal to the specified date-time. |
|
1880 |
* <p> |
|
1881 |
* This checks to see if this date-time represents the same point on the |
|
1882 |
* local time-line as the other date-time. |
|
1883 |
* <pre> |
|
1884 |
* LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00); |
|
1885 |
* LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00); |
|
1886 |
* a.isEqual(b) == false |
|
1887 |
* a.isEqual(a) == true |
|
1888 |
* b.isEqual(a) == false |
|
1889 |
* </pre> |
|
1890 |
* <p> |
|
1891 |
* This method only considers the position of the two date-times on the local time-line. |
|
1892 |
* It does not take into account the chronology, or calendar system. |
|
1893 |
* This is different from the comparison in {@link #compareTo(ChronoLocalDateTime)}, |
|
16852 | 1894 |
* but is the same approach as {@link ChronoLocalDateTime#timeLineOrder()}. |
15289 | 1895 |
* |
1896 |
* @param other the other date-time to compare to, not null |
|
1897 |
* @return true if this date-time is equal to the specified date-time |
|
1898 |
*/ |
|
1899 |
@Override // override for Javadoc and performance |
|
1900 |
public boolean isEqual(ChronoLocalDateTime<?> other) { |
|
1901 |
if (other instanceof LocalDateTime) { |
|
1902 |
return compareTo0((LocalDateTime) other) == 0; |
|
1903 |
} |
|
1904 |
return ChronoLocalDateTime.super.isEqual(other); |
|
1905 |
} |
|
1906 |
||
1907 |
//----------------------------------------------------------------------- |
|
1908 |
/** |
|
1909 |
* Checks if this date-time is equal to another date-time. |
|
1910 |
* <p> |
|
1911 |
* Compares this {@code LocalDateTime} with another ensuring that the date-time is the same. |
|
1912 |
* Only objects of type {@code LocalDateTime} are compared, other types return false. |
|
1913 |
* |
|
1914 |
* @param obj the object to check, null returns false |
|
1915 |
* @return true if this is equal to the other date-time |
|
1916 |
*/ |
|
1917 |
@Override |
|
1918 |
public boolean equals(Object obj) { |
|
1919 |
if (this == obj) { |
|
1920 |
return true; |
|
1921 |
} |
|
1922 |
if (obj instanceof LocalDateTime) { |
|
1923 |
LocalDateTime other = (LocalDateTime) obj; |
|
1924 |
return date.equals(other.date) && time.equals(other.time); |
|
1925 |
} |
|
1926 |
return false; |
|
1927 |
} |
|
1928 |
||
1929 |
/** |
|
1930 |
* A hash code for this date-time. |
|
1931 |
* |
|
1932 |
* @return a suitable hash code |
|
1933 |
*/ |
|
1934 |
@Override |
|
1935 |
public int hashCode() { |
|
1936 |
return date.hashCode() ^ time.hashCode(); |
|
1937 |
} |
|
1938 |
||
1939 |
//----------------------------------------------------------------------- |
|
1940 |
/** |
|
1941 |
* Outputs this date-time as a {@code String}, such as {@code 2007-12-03T10:15:30}. |
|
1942 |
* <p> |
|
1943 |
* The output will be one of the following ISO-8601 formats: |
|
20873 | 1944 |
* <ul> |
16852 | 1945 |
* <li>{@code uuuu-MM-dd'T'HH:mm}</li> |
1946 |
* <li>{@code uuuu-MM-dd'T'HH:mm:ss}</li> |
|
1947 |
* <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSS}</li> |
|
1948 |
* <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSSSS}</li> |
|
1949 |
* <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS}</li> |
|
20873 | 1950 |
* </ul> |
15289 | 1951 |
* The format used will be the shortest that outputs the full value of |
1952 |
* the time where the omitted parts are implied to be zero. |
|
1953 |
* |
|
1954 |
* @return a string representation of this date-time, not null |
|
1955 |
*/ |
|
1956 |
@Override |
|
1957 |
public String toString() { |
|
1958 |
return date.toString() + 'T' + time.toString(); |
|
1959 |
} |
|
1960 |
||
1961 |
//----------------------------------------------------------------------- |
|
1962 |
/** |
|
1963 |
* Writes the object using a |
|
1964 |
* <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
|
1965 |
* @serialData |
15289 | 1966 |
* <pre> |
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
1967 |
* out.writeByte(5); // identifies a LocalDateTime |
15289 | 1968 |
* // the <a href="../../serialized-form.html#java.time.LocalDate">date</a> excluding the one byte header |
1969 |
* // the <a href="../../serialized-form.html#java.time.LocalTime">time</a> excluding the one byte header |
|
1970 |
* </pre> |
|
1971 |
* |
|
1972 |
* @return the instance of {@code Ser}, not null |
|
1973 |
*/ |
|
1974 |
private Object writeReplace() { |
|
1975 |
return new Ser(Ser.LOCAL_DATE_TIME_TYPE, this); |
|
1976 |
} |
|
1977 |
||
1978 |
/** |
|
1979 |
* Defend against malicious streams. |
|
1980 |
* @return never |
|
1981 |
* @throws InvalidObjectException always |
|
1982 |
*/ |
|
19841
15c8e97d6a14
8024164: JSR310 serialization should be described in details
rriggs
parents:
19030
diff
changeset
|
1983 |
private Object readResolve() throws InvalidObjectException { |
15289 | 1984 |
throw new InvalidObjectException("Deserialization via serialization delegate"); |
1985 |
} |
|
1986 |
||
1987 |
void writeExternal(DataOutput out) throws IOException { |
|
1988 |
date.writeExternal(out); |
|
1989 |
time.writeExternal(out); |
|
1990 |
} |
|
1991 |
||
1992 |
static LocalDateTime readExternal(DataInput in) throws IOException { |
|
1993 |
LocalDate date = LocalDate.readExternal(in); |
|
1994 |
LocalTime time = LocalTime.readExternal(in); |
|
1995 |
return LocalDateTime.of(date, time); |
|
1996 |
} |
|
1997 |
||
1998 |
} |