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.ObjectStreamException;
|
|
80 |
import java.io.Serializable;
|
15658
|
81 |
import java.time.chrono.ChronoLocalDateTime;
|
15289
|
82 |
import java.time.format.DateTimeFormatter;
|
|
83 |
import java.time.format.DateTimeParseException;
|
|
84 |
import java.time.temporal.ChronoField;
|
|
85 |
import java.time.temporal.ChronoUnit;
|
|
86 |
import java.time.temporal.Temporal;
|
|
87 |
import java.time.temporal.TemporalAccessor;
|
|
88 |
import java.time.temporal.TemporalAdjuster;
|
15658
|
89 |
import java.time.temporal.TemporalAmount;
|
15289
|
90 |
import java.time.temporal.TemporalField;
|
|
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) {
|
|
452 |
throw new DateTimeException("Unable to obtain LocalDateTime from TemporalAccessor: " + temporal.getClass(), ex);
|
|
453 |
}
|
|
454 |
}
|
|
455 |
|
|
456 |
//-----------------------------------------------------------------------
|
|
457 |
/**
|
|
458 |
* Obtains an instance of {@code LocalDateTime} from a text string such as {@code 2007-12-03T10:15:30}.
|
|
459 |
* <p>
|
|
460 |
* The string must represent a valid date-time and is parsed using
|
15658
|
461 |
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME}.
|
15289
|
462 |
*
|
|
463 |
* @param text the text to parse such as "2007-12-03T10:15:30", not null
|
|
464 |
* @return the parsed local date-time, not null
|
|
465 |
* @throws DateTimeParseException if the text cannot be parsed
|
|
466 |
*/
|
|
467 |
public static LocalDateTime parse(CharSequence text) {
|
15658
|
468 |
return parse(text, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
15289
|
469 |
}
|
|
470 |
|
|
471 |
/**
|
|
472 |
* Obtains an instance of {@code LocalDateTime} from a text string using a specific formatter.
|
|
473 |
* <p>
|
|
474 |
* The text is parsed using the formatter, returning a date-time.
|
|
475 |
*
|
|
476 |
* @param text the text to parse, not null
|
|
477 |
* @param formatter the formatter to use, not null
|
|
478 |
* @return the parsed local date-time, not null
|
|
479 |
* @throws DateTimeParseException if the text cannot be parsed
|
|
480 |
*/
|
|
481 |
public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) {
|
|
482 |
Objects.requireNonNull(formatter, "formatter");
|
|
483 |
return formatter.parse(text, LocalDateTime::from);
|
|
484 |
}
|
|
485 |
|
|
486 |
//-----------------------------------------------------------------------
|
|
487 |
/**
|
|
488 |
* Constructor.
|
|
489 |
*
|
|
490 |
* @param date the date part of the date-time, validated not null
|
|
491 |
* @param time the time part of the date-time, validated not null
|
|
492 |
*/
|
|
493 |
private LocalDateTime(LocalDate date, LocalTime time) {
|
|
494 |
this.date = date;
|
|
495 |
this.time = time;
|
|
496 |
}
|
|
497 |
|
|
498 |
/**
|
|
499 |
* Returns a copy of this date-time with the new date and time, checking
|
|
500 |
* to see if a new object is in fact required.
|
|
501 |
*
|
|
502 |
* @param newDate the date of the new date-time, not null
|
|
503 |
* @param newTime the time of the new date-time, not null
|
|
504 |
* @return the date-time, not null
|
|
505 |
*/
|
|
506 |
private LocalDateTime with(LocalDate newDate, LocalTime newTime) {
|
|
507 |
if (date == newDate && time == newTime) {
|
|
508 |
return this;
|
|
509 |
}
|
|
510 |
return new LocalDateTime(newDate, newTime);
|
|
511 |
}
|
|
512 |
|
|
513 |
//-----------------------------------------------------------------------
|
|
514 |
/**
|
|
515 |
* Checks if the specified field is supported.
|
|
516 |
* <p>
|
|
517 |
* This checks if this date-time can be queried for the specified field.
|
|
518 |
* If false, then calling the {@link #range(TemporalField) range} and
|
|
519 |
* {@link #get(TemporalField) get} methods will throw an exception.
|
|
520 |
* <p>
|
|
521 |
* If the field is a {@link ChronoField} then the query is implemented here.
|
|
522 |
* The supported fields are:
|
|
523 |
* <ul>
|
|
524 |
* <li>{@code NANO_OF_SECOND}
|
|
525 |
* <li>{@code NANO_OF_DAY}
|
|
526 |
* <li>{@code MICRO_OF_SECOND}
|
|
527 |
* <li>{@code MICRO_OF_DAY}
|
|
528 |
* <li>{@code MILLI_OF_SECOND}
|
|
529 |
* <li>{@code MILLI_OF_DAY}
|
|
530 |
* <li>{@code SECOND_OF_MINUTE}
|
|
531 |
* <li>{@code SECOND_OF_DAY}
|
|
532 |
* <li>{@code MINUTE_OF_HOUR}
|
|
533 |
* <li>{@code MINUTE_OF_DAY}
|
|
534 |
* <li>{@code HOUR_OF_AMPM}
|
|
535 |
* <li>{@code CLOCK_HOUR_OF_AMPM}
|
|
536 |
* <li>{@code HOUR_OF_DAY}
|
|
537 |
* <li>{@code CLOCK_HOUR_OF_DAY}
|
|
538 |
* <li>{@code AMPM_OF_DAY}
|
|
539 |
* <li>{@code DAY_OF_WEEK}
|
|
540 |
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
|
|
541 |
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
|
|
542 |
* <li>{@code DAY_OF_MONTH}
|
|
543 |
* <li>{@code DAY_OF_YEAR}
|
|
544 |
* <li>{@code EPOCH_DAY}
|
|
545 |
* <li>{@code ALIGNED_WEEK_OF_MONTH}
|
|
546 |
* <li>{@code ALIGNED_WEEK_OF_YEAR}
|
|
547 |
* <li>{@code MONTH_OF_YEAR}
|
16852
|
548 |
* <li>{@code PROLEPTIC_MONTH}
|
15289
|
549 |
* <li>{@code YEAR_OF_ERA}
|
|
550 |
* <li>{@code YEAR}
|
|
551 |
* <li>{@code ERA}
|
|
552 |
* </ul>
|
|
553 |
* All other {@code ChronoField} instances will return false.
|
|
554 |
* <p>
|
|
555 |
* If the field is not a {@code ChronoField}, then the result of this method
|
15658
|
556 |
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
|
15289
|
557 |
* passing {@code this} as the argument.
|
|
558 |
* Whether the field is supported is determined by the field.
|
|
559 |
*
|
|
560 |
* @param field the field to check, null returns false
|
|
561 |
* @return true if the field is supported on this date-time, false if not
|
|
562 |
*/
|
|
563 |
@Override
|
|
564 |
public boolean isSupported(TemporalField field) {
|
|
565 |
if (field instanceof ChronoField) {
|
|
566 |
ChronoField f = (ChronoField) field;
|
16852
|
567 |
return f.isDateBased() || f.isTimeBased();
|
15289
|
568 |
}
|
15658
|
569 |
return field != null && field.isSupportedBy(this);
|
15289
|
570 |
}
|
|
571 |
|
|
572 |
/**
|
|
573 |
* Gets the range of valid values for the specified field.
|
|
574 |
* <p>
|
|
575 |
* The range object expresses the minimum and maximum valid values for a field.
|
|
576 |
* This date-time is used to enhance the accuracy of the returned range.
|
|
577 |
* If it is not possible to return the range, because the field is not supported
|
|
578 |
* or for some other reason, an exception is thrown.
|
|
579 |
* <p>
|
|
580 |
* If the field is a {@link ChronoField} then the query is implemented here.
|
|
581 |
* The {@link #isSupported(TemporalField) supported fields} will return
|
|
582 |
* appropriate range instances.
|
16852
|
583 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
|
15289
|
584 |
* <p>
|
|
585 |
* If the field is not a {@code ChronoField}, then the result of this method
|
15658
|
586 |
* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
|
15289
|
587 |
* passing {@code this} as the argument.
|
|
588 |
* Whether the range can be obtained is determined by the field.
|
|
589 |
*
|
|
590 |
* @param field the field to query the range for, not null
|
|
591 |
* @return the range of valid values for the field, not null
|
|
592 |
* @throws DateTimeException if the range for the field cannot be obtained
|
16852
|
593 |
* @throws UnsupportedTemporalTypeException if the field is not supported
|
15289
|
594 |
*/
|
|
595 |
@Override
|
|
596 |
public ValueRange range(TemporalField field) {
|
|
597 |
if (field instanceof ChronoField) {
|
|
598 |
ChronoField f = (ChronoField) field;
|
16852
|
599 |
return (f.isTimeBased() ? time.range(field) : date.range(field));
|
15289
|
600 |
}
|
15658
|
601 |
return field.rangeRefinedBy(this);
|
15289
|
602 |
}
|
|
603 |
|
|
604 |
/**
|
|
605 |
* Gets the value of the specified field from this date-time as an {@code int}.
|
|
606 |
* <p>
|
|
607 |
* This queries this date-time for the value for the specified field.
|
|
608 |
* The returned value will always be within the valid range of values for the field.
|
|
609 |
* If it is not possible to return the value, because the field is not supported
|
|
610 |
* or for some other reason, an exception is thrown.
|
|
611 |
* <p>
|
|
612 |
* If the field is a {@link ChronoField} then the query is implemented here.
|
|
613 |
* The {@link #isSupported(TemporalField) supported fields} will return valid
|
|
614 |
* values based on this date-time, except {@code NANO_OF_DAY}, {@code MICRO_OF_DAY},
|
16852
|
615 |
* {@code EPOCH_DAY} and {@code PROLEPTIC_MONTH} which are too large to fit in
|
15289
|
616 |
* an {@code int} and throw a {@code DateTimeException}.
|
16852
|
617 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
|
15289
|
618 |
* <p>
|
|
619 |
* If the field is not a {@code ChronoField}, then the result of this method
|
15658
|
620 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
|
15289
|
621 |
* passing {@code this} as the argument. Whether the value can be obtained,
|
|
622 |
* and what the value represents, is determined by the field.
|
|
623 |
*
|
|
624 |
* @param field the field to get, not null
|
|
625 |
* @return the value for the field
|
16852
|
626 |
* @throws DateTimeException if a value for the field cannot be obtained or
|
|
627 |
* the value is outside the range of valid values for the field
|
|
628 |
* @throws UnsupportedTemporalTypeException if the field is not supported or
|
|
629 |
* the range of values exceeds an {@code int}
|
15289
|
630 |
* @throws ArithmeticException if numeric overflow occurs
|
|
631 |
*/
|
|
632 |
@Override
|
|
633 |
public int get(TemporalField field) {
|
|
634 |
if (field instanceof ChronoField) {
|
|
635 |
ChronoField f = (ChronoField) field;
|
16852
|
636 |
return (f.isTimeBased() ? time.get(field) : date.get(field));
|
15289
|
637 |
}
|
|
638 |
return ChronoLocalDateTime.super.get(field);
|
|
639 |
}
|
|
640 |
|
|
641 |
/**
|
|
642 |
* Gets the value of the specified field from this date-time as a {@code long}.
|
|
643 |
* <p>
|
|
644 |
* This queries this date-time for the value for the specified field.
|
|
645 |
* If it is not possible to return the value, because the field is not supported
|
|
646 |
* or for some other reason, an exception is thrown.
|
|
647 |
* <p>
|
|
648 |
* If the field is a {@link ChronoField} then the query is implemented here.
|
|
649 |
* The {@link #isSupported(TemporalField) supported fields} will return valid
|
|
650 |
* values based on this date-time.
|
16852
|
651 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
|
15289
|
652 |
* <p>
|
|
653 |
* If the field is not a {@code ChronoField}, then the result of this method
|
15658
|
654 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
|
15289
|
655 |
* passing {@code this} as the argument. Whether the value can be obtained,
|
|
656 |
* and what the value represents, is determined by the field.
|
|
657 |
*
|
|
658 |
* @param field the field to get, not null
|
|
659 |
* @return the value for the field
|
|
660 |
* @throws DateTimeException if a value for the field cannot be obtained
|
16852
|
661 |
* @throws UnsupportedTemporalTypeException if the field is not supported
|
15289
|
662 |
* @throws ArithmeticException if numeric overflow occurs
|
|
663 |
*/
|
|
664 |
@Override
|
|
665 |
public long getLong(TemporalField field) {
|
|
666 |
if (field instanceof ChronoField) {
|
|
667 |
ChronoField f = (ChronoField) field;
|
16852
|
668 |
return (f.isTimeBased() ? time.getLong(field) : date.getLong(field));
|
15289
|
669 |
}
|
15658
|
670 |
return field.getFrom(this);
|
15289
|
671 |
}
|
|
672 |
|
|
673 |
//-----------------------------------------------------------------------
|
|
674 |
/**
|
|
675 |
* Gets the {@code LocalDate} part of this date-time.
|
|
676 |
* <p>
|
|
677 |
* This returns a {@code LocalDate} with the same year, month and day
|
|
678 |
* as this date-time.
|
|
679 |
*
|
|
680 |
* @return the date part of this date-time, not null
|
|
681 |
*/
|
|
682 |
@Override
|
15658
|
683 |
public LocalDate toLocalDate() {
|
15289
|
684 |
return date;
|
|
685 |
}
|
|
686 |
|
|
687 |
/**
|
|
688 |
* Gets the year field.
|
|
689 |
* <p>
|
|
690 |
* This method returns the primitive {@code int} value for the year.
|
|
691 |
* <p>
|
|
692 |
* The year returned by this method is proleptic as per {@code get(YEAR)}.
|
15658
|
693 |
* To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}.
|
15289
|
694 |
*
|
|
695 |
* @return the year, from MIN_YEAR to MAX_YEAR
|
|
696 |
*/
|
|
697 |
public int getYear() {
|
|
698 |
return date.getYear();
|
|
699 |
}
|
|
700 |
|
|
701 |
/**
|
|
702 |
* Gets the month-of-year field from 1 to 12.
|
|
703 |
* <p>
|
|
704 |
* This method returns the month as an {@code int} from 1 to 12.
|
|
705 |
* Application code is frequently clearer if the enum {@link Month}
|
|
706 |
* is used by calling {@link #getMonth()}.
|
|
707 |
*
|
|
708 |
* @return the month-of-year, from 1 to 12
|
|
709 |
* @see #getMonth()
|
|
710 |
*/
|
|
711 |
public int getMonthValue() {
|
|
712 |
return date.getMonthValue();
|
|
713 |
}
|
|
714 |
|
|
715 |
/**
|
|
716 |
* Gets the month-of-year field using the {@code Month} enum.
|
|
717 |
* <p>
|
|
718 |
* This method returns the enum {@link Month} for the month.
|
|
719 |
* This avoids confusion as to what {@code int} values mean.
|
|
720 |
* If you need access to the primitive {@code int} value then the enum
|
|
721 |
* provides the {@link Month#getValue() int value}.
|
|
722 |
*
|
|
723 |
* @return the month-of-year, not null
|
|
724 |
* @see #getMonthValue()
|
|
725 |
*/
|
|
726 |
public Month getMonth() {
|
|
727 |
return date.getMonth();
|
|
728 |
}
|
|
729 |
|
|
730 |
/**
|
|
731 |
* Gets the day-of-month field.
|
|
732 |
* <p>
|
|
733 |
* This method returns the primitive {@code int} value for the day-of-month.
|
|
734 |
*
|
|
735 |
* @return the day-of-month, from 1 to 31
|
|
736 |
*/
|
|
737 |
public int getDayOfMonth() {
|
|
738 |
return date.getDayOfMonth();
|
|
739 |
}
|
|
740 |
|
|
741 |
/**
|
|
742 |
* Gets the day-of-year field.
|
|
743 |
* <p>
|
|
744 |
* This method returns the primitive {@code int} value for the day-of-year.
|
|
745 |
*
|
|
746 |
* @return the day-of-year, from 1 to 365, or 366 in a leap year
|
|
747 |
*/
|
|
748 |
public int getDayOfYear() {
|
|
749 |
return date.getDayOfYear();
|
|
750 |
}
|
|
751 |
|
|
752 |
/**
|
|
753 |
* Gets the day-of-week field, which is an enum {@code DayOfWeek}.
|
|
754 |
* <p>
|
|
755 |
* This method returns the enum {@link DayOfWeek} for the day-of-week.
|
|
756 |
* This avoids confusion as to what {@code int} values mean.
|
|
757 |
* If you need access to the primitive {@code int} value then the enum
|
|
758 |
* provides the {@link DayOfWeek#getValue() int value}.
|
|
759 |
* <p>
|
|
760 |
* Additional information can be obtained from the {@code DayOfWeek}.
|
|
761 |
* This includes textual names of the values.
|
|
762 |
*
|
|
763 |
* @return the day-of-week, not null
|
|
764 |
*/
|
|
765 |
public DayOfWeek getDayOfWeek() {
|
|
766 |
return date.getDayOfWeek();
|
|
767 |
}
|
|
768 |
|
|
769 |
//-----------------------------------------------------------------------
|
|
770 |
/**
|
|
771 |
* Gets the {@code LocalTime} part of this date-time.
|
|
772 |
* <p>
|
|
773 |
* This returns a {@code LocalTime} with the same hour, minute, second and
|
|
774 |
* nanosecond as this date-time.
|
|
775 |
*
|
|
776 |
* @return the time part of this date-time, not null
|
|
777 |
*/
|
|
778 |
@Override
|
15658
|
779 |
public LocalTime toLocalTime() {
|
15289
|
780 |
return time;
|
|
781 |
}
|
|
782 |
|
|
783 |
/**
|
|
784 |
* Gets the hour-of-day field.
|
|
785 |
*
|
|
786 |
* @return the hour-of-day, from 0 to 23
|
|
787 |
*/
|
|
788 |
public int getHour() {
|
|
789 |
return time.getHour();
|
|
790 |
}
|
|
791 |
|
|
792 |
/**
|
|
793 |
* Gets the minute-of-hour field.
|
|
794 |
*
|
|
795 |
* @return the minute-of-hour, from 0 to 59
|
|
796 |
*/
|
|
797 |
public int getMinute() {
|
|
798 |
return time.getMinute();
|
|
799 |
}
|
|
800 |
|
|
801 |
/**
|
|
802 |
* Gets the second-of-minute field.
|
|
803 |
*
|
|
804 |
* @return the second-of-minute, from 0 to 59
|
|
805 |
*/
|
|
806 |
public int getSecond() {
|
|
807 |
return time.getSecond();
|
|
808 |
}
|
|
809 |
|
|
810 |
/**
|
|
811 |
* Gets the nano-of-second field.
|
|
812 |
*
|
|
813 |
* @return the nano-of-second, from 0 to 999,999,999
|
|
814 |
*/
|
|
815 |
public int getNano() {
|
|
816 |
return time.getNano();
|
|
817 |
}
|
|
818 |
|
|
819 |
//-----------------------------------------------------------------------
|
|
820 |
/**
|
|
821 |
* Returns an adjusted copy of this date-time.
|
|
822 |
* <p>
|
15658
|
823 |
* This returns a {@code LocalDateTime}, based on this one, with the date-time adjusted.
|
15289
|
824 |
* The adjustment takes place using the specified adjuster strategy object.
|
|
825 |
* Read the documentation of the adjuster to understand what adjustment will be made.
|
|
826 |
* <p>
|
|
827 |
* A simple adjuster might simply set the one of the fields, such as the year field.
|
|
828 |
* A more complex adjuster might set the date to the last day of the month.
|
16852
|
829 |
* A selection of common adjustments is provided in {@link TemporalAdjuster}.
|
15289
|
830 |
* These include finding the "last day of the month" and "next Wednesday".
|
|
831 |
* Key date-time classes also implement the {@code TemporalAdjuster} interface,
|
15658
|
832 |
* such as {@link Month} and {@link java.time.MonthDay MonthDay}.
|
15289
|
833 |
* The adjuster is responsible for handling special cases, such as the varying
|
|
834 |
* lengths of month and leap years.
|
|
835 |
* <p>
|
|
836 |
* For example this code returns a date on the last day of July:
|
|
837 |
* <pre>
|
|
838 |
* import static java.time.Month.*;
|
|
839 |
* import static java.time.temporal.Adjusters.*;
|
|
840 |
*
|
|
841 |
* result = localDateTime.with(JULY).with(lastDayOfMonth());
|
|
842 |
* </pre>
|
|
843 |
* <p>
|
|
844 |
* The classes {@link LocalDate} and {@link LocalTime} implement {@code TemporalAdjuster},
|
|
845 |
* thus this method can be used to change the date, time or offset:
|
|
846 |
* <pre>
|
|
847 |
* result = localDateTime.with(date);
|
|
848 |
* result = localDateTime.with(time);
|
|
849 |
* </pre>
|
|
850 |
* <p>
|
|
851 |
* The result of this method is obtained by invoking the
|
|
852 |
* {@link TemporalAdjuster#adjustInto(Temporal)} method on the
|
|
853 |
* specified adjuster passing {@code this} as the argument.
|
|
854 |
* <p>
|
|
855 |
* This instance is immutable and unaffected by this method call.
|
|
856 |
*
|
|
857 |
* @param adjuster the adjuster to use, not null
|
|
858 |
* @return a {@code LocalDateTime} based on {@code this} with the adjustment made, not null
|
|
859 |
* @throws DateTimeException if the adjustment cannot be made
|
|
860 |
* @throws ArithmeticException if numeric overflow occurs
|
|
861 |
*/
|
|
862 |
@Override
|
|
863 |
public LocalDateTime with(TemporalAdjuster adjuster) {
|
|
864 |
// optimizations
|
|
865 |
if (adjuster instanceof LocalDate) {
|
|
866 |
return with((LocalDate) adjuster, time);
|
|
867 |
} else if (adjuster instanceof LocalTime) {
|
|
868 |
return with(date, (LocalTime) adjuster);
|
|
869 |
} else if (adjuster instanceof LocalDateTime) {
|
|
870 |
return (LocalDateTime) adjuster;
|
|
871 |
}
|
|
872 |
return (LocalDateTime) adjuster.adjustInto(this);
|
|
873 |
}
|
|
874 |
|
|
875 |
/**
|
|
876 |
* Returns a copy of this date-time with the specified field set to a new value.
|
|
877 |
* <p>
|
15658
|
878 |
* This returns a {@code LocalDateTime}, based on this one, with the value
|
15289
|
879 |
* for the specified field changed.
|
|
880 |
* This can be used to change any supported field, such as the year, month or day-of-month.
|
|
881 |
* If it is not possible to set the value, because the field is not supported or for
|
|
882 |
* some other reason, an exception is thrown.
|
|
883 |
* <p>
|
|
884 |
* In some cases, changing the specified field can cause the resulting date-time to become invalid,
|
|
885 |
* such as changing the month from 31st January to February would make the day-of-month invalid.
|
|
886 |
* In cases like this, the field is responsible for resolving the date. Typically it will choose
|
|
887 |
* the previous valid date, which would be the last valid day of February in this example.
|
|
888 |
* <p>
|
|
889 |
* If the field is a {@link ChronoField} then the adjustment is implemented here.
|
|
890 |
* The {@link #isSupported(TemporalField) supported fields} will behave as per
|
|
891 |
* the matching method on {@link LocalDate#with(TemporalField, long) LocalDate}
|
|
892 |
* or {@link LocalTime#with(TemporalField, long) LocalTime}.
|
16852
|
893 |
* All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
|
15289
|
894 |
* <p>
|
|
895 |
* If the field is not a {@code ChronoField}, then the result of this method
|
15658
|
896 |
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
|
15289
|
897 |
* passing {@code this} as the argument. In this case, the field determines
|
|
898 |
* whether and how to adjust the instant.
|
|
899 |
* <p>
|
|
900 |
* This instance is immutable and unaffected by this method call.
|
|
901 |
*
|
|
902 |
* @param field the field to set in the result, not null
|
|
903 |
* @param newValue the new value of the field in the result
|
|
904 |
* @return a {@code LocalDateTime} based on {@code this} with the specified field set, not null
|
|
905 |
* @throws DateTimeException if the field cannot be set
|
16852
|
906 |
* @throws UnsupportedTemporalTypeException if the field is not supported
|
15289
|
907 |
* @throws ArithmeticException if numeric overflow occurs
|
|
908 |
*/
|
|
909 |
@Override
|
|
910 |
public LocalDateTime with(TemporalField field, long newValue) {
|
|
911 |
if (field instanceof ChronoField) {
|
|
912 |
ChronoField f = (ChronoField) field;
|
16852
|
913 |
if (f.isTimeBased()) {
|
15289
|
914 |
return with(date, time.with(field, newValue));
|
|
915 |
} else {
|
|
916 |
return with(date.with(field, newValue), time);
|
|
917 |
}
|
|
918 |
}
|
15658
|
919 |
return field.adjustInto(this, newValue);
|
15289
|
920 |
}
|
|
921 |
|
|
922 |
//-----------------------------------------------------------------------
|
|
923 |
/**
|
|
924 |
* Returns a copy of this {@code LocalDateTime} with the year altered.
|
|
925 |
* The time does not affect the calculation and will be the same in the result.
|
|
926 |
* If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
|
|
927 |
* <p>
|
|
928 |
* This instance is immutable and unaffected by this method call.
|
|
929 |
*
|
|
930 |
* @param year the year to set in the result, from MIN_YEAR to MAX_YEAR
|
|
931 |
* @return a {@code LocalDateTime} based on this date-time with the requested year, not null
|
|
932 |
* @throws DateTimeException if the year value is invalid
|
|
933 |
*/
|
|
934 |
public LocalDateTime withYear(int year) {
|
|
935 |
return with(date.withYear(year), time);
|
|
936 |
}
|
|
937 |
|
|
938 |
/**
|
|
939 |
* Returns a copy of this {@code LocalDateTime} with the month-of-year altered.
|
|
940 |
* The time does not affect the calculation and will be the same in the result.
|
|
941 |
* If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
|
|
942 |
* <p>
|
|
943 |
* This instance is immutable and unaffected by this method call.
|
|
944 |
*
|
|
945 |
* @param month the month-of-year to set in the result, from 1 (January) to 12 (December)
|
|
946 |
* @return a {@code LocalDateTime} based on this date-time with the requested month, not null
|
|
947 |
* @throws DateTimeException if the month-of-year value is invalid
|
|
948 |
*/
|
|
949 |
public LocalDateTime withMonth(int month) {
|
|
950 |
return with(date.withMonth(month), time);
|
|
951 |
}
|
|
952 |
|
|
953 |
/**
|
|
954 |
* Returns a copy of this {@code LocalDateTime} with the day-of-month altered.
|
|
955 |
* If the resulting {@code LocalDateTime} is invalid, an exception is thrown.
|
|
956 |
* The time does not affect the calculation and will be the same in the result.
|
|
957 |
* <p>
|
|
958 |
* This instance is immutable and unaffected by this method call.
|
|
959 |
*
|
|
960 |
* @param dayOfMonth the day-of-month to set in the result, from 1 to 28-31
|
|
961 |
* @return a {@code LocalDateTime} based on this date-time with the requested day, not null
|
15658
|
962 |
* @throws DateTimeException if the day-of-month value is invalid,
|
|
963 |
* or if the day-of-month is invalid for the month-year
|
15289
|
964 |
*/
|
|
965 |
public LocalDateTime withDayOfMonth(int dayOfMonth) {
|
|
966 |
return with(date.withDayOfMonth(dayOfMonth), time);
|
|
967 |
}
|
|
968 |
|
|
969 |
/**
|
|
970 |
* Returns a copy of this {@code LocalDateTime} with the day-of-year altered.
|
|
971 |
* If the resulting {@code LocalDateTime} is invalid, an exception is thrown.
|
|
972 |
* <p>
|
|
973 |
* This instance is immutable and unaffected by this method call.
|
|
974 |
*
|
|
975 |
* @param dayOfYear the day-of-year to set in the result, from 1 to 365-366
|
|
976 |
* @return a {@code LocalDateTime} based on this date with the requested day, not null
|
15658
|
977 |
* @throws DateTimeException if the day-of-year value is invalid,
|
|
978 |
* or if the day-of-year is invalid for the year
|
15289
|
979 |
*/
|
|
980 |
public LocalDateTime withDayOfYear(int dayOfYear) {
|
|
981 |
return with(date.withDayOfYear(dayOfYear), time);
|
|
982 |
}
|
|
983 |
|
|
984 |
//-----------------------------------------------------------------------
|
|
985 |
/**
|
|
986 |
* Returns a copy of this {@code LocalDateTime} with the hour-of-day value altered.
|
|
987 |
* <p>
|
|
988 |
* This instance is immutable and unaffected by this method call.
|
|
989 |
*
|
|
990 |
* @param hour the hour-of-day to set in the result, from 0 to 23
|
|
991 |
* @return a {@code LocalDateTime} based on this date-time with the requested hour, not null
|
|
992 |
* @throws DateTimeException if the hour value is invalid
|
|
993 |
*/
|
|
994 |
public LocalDateTime withHour(int hour) {
|
|
995 |
LocalTime newTime = time.withHour(hour);
|
|
996 |
return with(date, newTime);
|
|
997 |
}
|
|
998 |
|
|
999 |
/**
|
|
1000 |
* Returns a copy of this {@code LocalDateTime} with the minute-of-hour value altered.
|
|
1001 |
* <p>
|
|
1002 |
* This instance is immutable and unaffected by this method call.
|
|
1003 |
*
|
|
1004 |
* @param minute the minute-of-hour to set in the result, from 0 to 59
|
|
1005 |
* @return a {@code LocalDateTime} based on this date-time with the requested minute, not null
|
|
1006 |
* @throws DateTimeException if the minute value is invalid
|
|
1007 |
*/
|
|
1008 |
public LocalDateTime withMinute(int minute) {
|
|
1009 |
LocalTime newTime = time.withMinute(minute);
|
|
1010 |
return with(date, newTime);
|
|
1011 |
}
|
|
1012 |
|
|
1013 |
/**
|
|
1014 |
* Returns a copy of this {@code LocalDateTime} with the second-of-minute value altered.
|
|
1015 |
* <p>
|
|
1016 |
* This instance is immutable and unaffected by this method call.
|
|
1017 |
*
|
|
1018 |
* @param second the second-of-minute to set in the result, from 0 to 59
|
|
1019 |
* @return a {@code LocalDateTime} based on this date-time with the requested second, not null
|
|
1020 |
* @throws DateTimeException if the second value is invalid
|
|
1021 |
*/
|
|
1022 |
public LocalDateTime withSecond(int second) {
|
|
1023 |
LocalTime newTime = time.withSecond(second);
|
|
1024 |
return with(date, newTime);
|
|
1025 |
}
|
|
1026 |
|
|
1027 |
/**
|
|
1028 |
* Returns a copy of this {@code LocalDateTime} with the nano-of-second value altered.
|
|
1029 |
* <p>
|
|
1030 |
* This instance is immutable and unaffected by this method call.
|
|
1031 |
*
|
|
1032 |
* @param nanoOfSecond the nano-of-second to set in the result, from 0 to 999,999,999
|
|
1033 |
* @return a {@code LocalDateTime} based on this date-time with the requested nanosecond, not null
|
|
1034 |
* @throws DateTimeException if the nano value is invalid
|
|
1035 |
*/
|
|
1036 |
public LocalDateTime withNano(int nanoOfSecond) {
|
|
1037 |
LocalTime newTime = time.withNano(nanoOfSecond);
|
|
1038 |
return with(date, newTime);
|
|
1039 |
}
|
|
1040 |
|
|
1041 |
//-----------------------------------------------------------------------
|
|
1042 |
/**
|
|
1043 |
* Returns a copy of this {@code LocalDateTime} with the time truncated.
|
|
1044 |
* <p>
|
|
1045 |
* Truncation returns a copy of the original date-time with fields
|
|
1046 |
* smaller than the specified unit set to zero.
|
|
1047 |
* For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
|
|
1048 |
* will set the second-of-minute and nano-of-second field to zero.
|
|
1049 |
* <p>
|
15658
|
1050 |
* The unit must have a {@linkplain TemporalUnit#getDuration() duration}
|
|
1051 |
* that divides into the length of a standard day without remainder.
|
|
1052 |
* This includes all supplied time units on {@link ChronoUnit} and
|
|
1053 |
* {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
|
15289
|
1054 |
* <p>
|
|
1055 |
* This instance is immutable and unaffected by this method call.
|
|
1056 |
*
|
|
1057 |
* @param unit the unit to truncate to, not null
|
|
1058 |
* @return a {@code LocalDateTime} based on this date-time with the time truncated, not null
|
|
1059 |
* @throws DateTimeException if unable to truncate
|
16852
|
1060 |
* @throws UnsupportedTemporalTypeException if the field is not supported
|
15289
|
1061 |
*/
|
|
1062 |
public LocalDateTime truncatedTo(TemporalUnit unit) {
|
|
1063 |
return with(date, time.truncatedTo(unit));
|
|
1064 |
}
|
|
1065 |
|
|
1066 |
//-----------------------------------------------------------------------
|
|
1067 |
/**
|
15658
|
1068 |
* Returns a copy of this date-time with the specified amount added.
|
|
1069 |
* <p>
|
|
1070 |
* This returns a {@code LocalDateTime}, based on this one, with the specified amount added.
|
|
1071 |
* The amount is typically {@link Period} or {@link Duration} but may be
|
|
1072 |
* any other type implementing the {@link TemporalAmount} interface.
|
15289
|
1073 |
* <p>
|
15658
|
1074 |
* The calculation is delegated to the amount object by calling
|
|
1075 |
* {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
|
|
1076 |
* to implement the addition in any way it wishes, however it typically
|
|
1077 |
* calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
|
|
1078 |
* of the amount implementation to determine if it can be successfully added.
|
15289
|
1079 |
* <p>
|
|
1080 |
* This instance is immutable and unaffected by this method call.
|
|
1081 |
*
|
15658
|
1082 |
* @param amountToAdd the amount to add, not null
|
15289
|
1083 |
* @return a {@code LocalDateTime} based on this date-time with the addition made, not null
|
|
1084 |
* @throws DateTimeException if the addition cannot be made
|
|
1085 |
* @throws ArithmeticException if numeric overflow occurs
|
|
1086 |
*/
|
|
1087 |
@Override
|
15658
|
1088 |
public LocalDateTime plus(TemporalAmount amountToAdd) {
|
|
1089 |
return (LocalDateTime) amountToAdd.addTo(this);
|
15289
|
1090 |
}
|
|
1091 |
|
|
1092 |
/**
|
15658
|
1093 |
* Returns a copy of this date-time with the specified amount added.
|
|
1094 |
* <p>
|
|
1095 |
* This returns a {@code LocalDateTime}, based on this one, with the amount
|
|
1096 |
* in terms of the unit added. If it is not possible to add the amount, because the
|
|
1097 |
* unit is not supported or for some other reason, an exception is thrown.
|
15289
|
1098 |
* <p>
|
15658
|
1099 |
* If the field is a {@link ChronoUnit} then the addition is implemented here.
|
|
1100 |
* Date units are added as per {@link LocalDate#plus(long, TemporalUnit)}.
|
|
1101 |
* Time units are added as per {@link LocalTime#plus(long, TemporalUnit)} with
|
|
1102 |
* any overflow in days added equivalent to using {@link #plusDays(long)}.
|
|
1103 |
* <p>
|
|
1104 |
* If the field is not a {@code ChronoUnit}, then the result of this method
|
|
1105 |
* is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
|
|
1106 |
* passing {@code this} as the argument. In this case, the unit determines
|
|
1107 |
* whether and how to perform the addition.
|
15289
|
1108 |
* <p>
|
|
1109 |
* This instance is immutable and unaffected by this method call.
|
|
1110 |
*
|
|
1111 |
* @param amountToAdd the amount of the unit to add to the result, may be negative
|
15658
|
1112 |
* @param unit the unit of the amount to add, not null
|
|
1113 |
* @return a {@code LocalDateTime} based on this date-time with the specified amount added, not null
|
|
1114 |
* @throws DateTimeException if the addition cannot be made
|
16852
|
1115 |
* @throws UnsupportedTemporalTypeException if the unit is not supported
|
15658
|
1116 |
* @throws ArithmeticException if numeric overflow occurs
|
15289
|
1117 |
*/
|
|
1118 |
@Override
|
|
1119 |
public LocalDateTime plus(long amountToAdd, TemporalUnit unit) {
|
|
1120 |
if (unit instanceof ChronoUnit) {
|
|
1121 |
ChronoUnit f = (ChronoUnit) unit;
|
|
1122 |
switch (f) {
|
|
1123 |
case NANOS: return plusNanos(amountToAdd);
|
|
1124 |
case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
|
|
1125 |
case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
|
|
1126 |
case SECONDS: return plusSeconds(amountToAdd);
|
|
1127 |
case MINUTES: return plusMinutes(amountToAdd);
|
|
1128 |
case HOURS: return plusHours(amountToAdd);
|
|
1129 |
case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is multiple of 2)
|
|
1130 |
}
|
|
1131 |
return with(date.plus(amountToAdd, unit), time);
|
|
1132 |
}
|
15658
|
1133 |
return unit.addTo(this, amountToAdd);
|
15289
|
1134 |
}
|
|
1135 |
|
|
1136 |
//-----------------------------------------------------------------------
|
|
1137 |
/**
|
|
1138 |
* Returns a copy of this {@code LocalDateTime} with the specified period in years added.
|
|
1139 |
* <p>
|
|
1140 |
* This method adds the specified amount to the years field in three steps:
|
|
1141 |
* <ol>
|
|
1142 |
* <li>Add the input years to the year field</li>
|
|
1143 |
* <li>Check if the resulting date would be invalid</li>
|
|
1144 |
* <li>Adjust the day-of-month to the last valid day if necessary</li>
|
|
1145 |
* </ol>
|
|
1146 |
* <p>
|
|
1147 |
* For example, 2008-02-29 (leap year) plus one year would result in the
|
|
1148 |
* invalid date 2009-02-29 (standard year). Instead of returning an invalid
|
|
1149 |
* result, the last valid day of the month, 2009-02-28, is selected instead.
|
|
1150 |
* <p>
|
|
1151 |
* This instance is immutable and unaffected by this method call.
|
|
1152 |
*
|
|
1153 |
* @param years the years to add, may be negative
|
|
1154 |
* @return a {@code LocalDateTime} based on this date-time with the years added, not null
|
|
1155 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1156 |
*/
|
|
1157 |
public LocalDateTime plusYears(long years) {
|
|
1158 |
LocalDate newDate = date.plusYears(years);
|
|
1159 |
return with(newDate, time);
|
|
1160 |
}
|
|
1161 |
|
|
1162 |
/**
|
|
1163 |
* Returns a copy of this {@code LocalDateTime} with the specified period in months added.
|
|
1164 |
* <p>
|
|
1165 |
* This method adds the specified amount to the months field in three steps:
|
|
1166 |
* <ol>
|
|
1167 |
* <li>Add the input months to the month-of-year field</li>
|
|
1168 |
* <li>Check if the resulting date would be invalid</li>
|
|
1169 |
* <li>Adjust the day-of-month to the last valid day if necessary</li>
|
|
1170 |
* </ol>
|
|
1171 |
* <p>
|
|
1172 |
* For example, 2007-03-31 plus one month would result in the invalid date
|
|
1173 |
* 2007-04-31. Instead of returning an invalid result, the last valid day
|
|
1174 |
* of the month, 2007-04-30, is selected instead.
|
|
1175 |
* <p>
|
|
1176 |
* This instance is immutable and unaffected by this method call.
|
|
1177 |
*
|
|
1178 |
* @param months the months to add, may be negative
|
|
1179 |
* @return a {@code LocalDateTime} based on this date-time with the months added, not null
|
|
1180 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1181 |
*/
|
|
1182 |
public LocalDateTime plusMonths(long months) {
|
|
1183 |
LocalDate newDate = date.plusMonths(months);
|
|
1184 |
return with(newDate, time);
|
|
1185 |
}
|
|
1186 |
|
|
1187 |
/**
|
|
1188 |
* Returns a copy of this {@code LocalDateTime} with the specified period in weeks added.
|
|
1189 |
* <p>
|
|
1190 |
* This method adds the specified amount in weeks to the days field incrementing
|
|
1191 |
* the month and year fields as necessary to ensure the result remains valid.
|
|
1192 |
* The result is only invalid if the maximum/minimum year is exceeded.
|
|
1193 |
* <p>
|
|
1194 |
* For example, 2008-12-31 plus one week would result in 2009-01-07.
|
|
1195 |
* <p>
|
|
1196 |
* This instance is immutable and unaffected by this method call.
|
|
1197 |
*
|
|
1198 |
* @param weeks the weeks to add, may be negative
|
|
1199 |
* @return a {@code LocalDateTime} based on this date-time with the weeks added, not null
|
|
1200 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1201 |
*/
|
|
1202 |
public LocalDateTime plusWeeks(long weeks) {
|
|
1203 |
LocalDate newDate = date.plusWeeks(weeks);
|
|
1204 |
return with(newDate, time);
|
|
1205 |
}
|
|
1206 |
|
|
1207 |
/**
|
|
1208 |
* Returns a copy of this {@code LocalDateTime} with the specified period in days added.
|
|
1209 |
* <p>
|
|
1210 |
* This method adds the specified amount to the days field incrementing the
|
|
1211 |
* month and year fields as necessary to ensure the result remains valid.
|
|
1212 |
* The result is only invalid if the maximum/minimum year is exceeded.
|
|
1213 |
* <p>
|
|
1214 |
* For example, 2008-12-31 plus one day would result in 2009-01-01.
|
|
1215 |
* <p>
|
|
1216 |
* This instance is immutable and unaffected by this method call.
|
|
1217 |
*
|
|
1218 |
* @param days the days to add, may be negative
|
|
1219 |
* @return a {@code LocalDateTime} based on this date-time with the days added, not null
|
|
1220 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1221 |
*/
|
|
1222 |
public LocalDateTime plusDays(long days) {
|
|
1223 |
LocalDate newDate = date.plusDays(days);
|
|
1224 |
return with(newDate, time);
|
|
1225 |
}
|
|
1226 |
|
|
1227 |
//-----------------------------------------------------------------------
|
|
1228 |
/**
|
|
1229 |
* Returns a copy of this {@code LocalDateTime} with the specified period in hours added.
|
|
1230 |
* <p>
|
|
1231 |
* This instance is immutable and unaffected by this method call.
|
|
1232 |
*
|
|
1233 |
* @param hours the hours to add, may be negative
|
|
1234 |
* @return a {@code LocalDateTime} based on this date-time with the hours added, not null
|
|
1235 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1236 |
*/
|
|
1237 |
public LocalDateTime plusHours(long hours) {
|
|
1238 |
return plusWithOverflow(date, hours, 0, 0, 0, 1);
|
|
1239 |
}
|
|
1240 |
|
|
1241 |
/**
|
|
1242 |
* Returns a copy of this {@code LocalDateTime} with the specified period in minutes added.
|
|
1243 |
* <p>
|
|
1244 |
* This instance is immutable and unaffected by this method call.
|
|
1245 |
*
|
|
1246 |
* @param minutes the minutes to add, may be negative
|
|
1247 |
* @return a {@code LocalDateTime} based on this date-time with the minutes added, not null
|
|
1248 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1249 |
*/
|
|
1250 |
public LocalDateTime plusMinutes(long minutes) {
|
|
1251 |
return plusWithOverflow(date, 0, minutes, 0, 0, 1);
|
|
1252 |
}
|
|
1253 |
|
|
1254 |
/**
|
|
1255 |
* Returns a copy of this {@code LocalDateTime} with the specified period in seconds added.
|
|
1256 |
* <p>
|
|
1257 |
* This instance is immutable and unaffected by this method call.
|
|
1258 |
*
|
|
1259 |
* @param seconds the seconds to add, may be negative
|
|
1260 |
* @return a {@code LocalDateTime} based on this date-time with the seconds added, not null
|
|
1261 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1262 |
*/
|
|
1263 |
public LocalDateTime plusSeconds(long seconds) {
|
|
1264 |
return plusWithOverflow(date, 0, 0, seconds, 0, 1);
|
|
1265 |
}
|
|
1266 |
|
|
1267 |
/**
|
|
1268 |
* Returns a copy of this {@code LocalDateTime} with the specified period in nanoseconds added.
|
|
1269 |
* <p>
|
|
1270 |
* This instance is immutable and unaffected by this method call.
|
|
1271 |
*
|
|
1272 |
* @param nanos the nanos to add, may be negative
|
|
1273 |
* @return a {@code LocalDateTime} based on this date-time with the nanoseconds added, not null
|
|
1274 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1275 |
*/
|
|
1276 |
public LocalDateTime plusNanos(long nanos) {
|
|
1277 |
return plusWithOverflow(date, 0, 0, 0, nanos, 1);
|
|
1278 |
}
|
|
1279 |
|
|
1280 |
//-----------------------------------------------------------------------
|
|
1281 |
/**
|
15658
|
1282 |
* Returns a copy of this date-time with the specified amount subtracted.
|
|
1283 |
* <p>
|
|
1284 |
* This returns a {@code LocalDateTime}, based on this one, with the specified amount subtracted.
|
|
1285 |
* The amount is typically {@link Period} or {@link Duration} but may be
|
|
1286 |
* any other type implementing the {@link TemporalAmount} interface.
|
15289
|
1287 |
* <p>
|
15658
|
1288 |
* The calculation is delegated to the amount object by calling
|
|
1289 |
* {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
|
|
1290 |
* to implement the subtraction in any way it wishes, however it typically
|
|
1291 |
* calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
|
|
1292 |
* of the amount implementation to determine if it can be successfully subtracted.
|
15289
|
1293 |
* <p>
|
|
1294 |
* This instance is immutable and unaffected by this method call.
|
|
1295 |
*
|
15658
|
1296 |
* @param amountToSubtract the amount to subtract, not null
|
15289
|
1297 |
* @return a {@code LocalDateTime} based on this date-time with the subtraction made, not null
|
|
1298 |
* @throws DateTimeException if the subtraction cannot be made
|
|
1299 |
* @throws ArithmeticException if numeric overflow occurs
|
|
1300 |
*/
|
|
1301 |
@Override
|
15658
|
1302 |
public LocalDateTime minus(TemporalAmount amountToSubtract) {
|
|
1303 |
return (LocalDateTime) amountToSubtract.subtractFrom(this);
|
15289
|
1304 |
}
|
|
1305 |
|
|
1306 |
/**
|
15658
|
1307 |
* Returns a copy of this date-time with the specified amount subtracted.
|
15289
|
1308 |
* <p>
|
15658
|
1309 |
* This returns a {@code LocalDateTime}, based on this one, with the amount
|
|
1310 |
* in terms of the unit subtracted. If it is not possible to subtract the amount,
|
|
1311 |
* because the unit is not supported or for some other reason, an exception is thrown.
|
|
1312 |
* <p>
|
|
1313 |
* This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
|
|
1314 |
* See that method for a full description of how addition, and thus subtraction, works.
|
15289
|
1315 |
* <p>
|
|
1316 |
* This instance is immutable and unaffected by this method call.
|
|
1317 |
*
|
|
1318 |
* @param amountToSubtract the amount of the unit to subtract from the result, may be negative
|
15658
|
1319 |
* @param unit the unit of the amount to subtract, not null
|
|
1320 |
* @return a {@code LocalDateTime} based on this date-time with the specified amount subtracted, not null
|
|
1321 |
* @throws DateTimeException if the subtraction cannot be made
|
16852
|
1322 |
* @throws UnsupportedTemporalTypeException if the unit is not supported
|
15658
|
1323 |
* @throws ArithmeticException if numeric overflow occurs
|
15289
|
1324 |
*/
|
|
1325 |
@Override
|
|
1326 |
public LocalDateTime minus(long amountToSubtract, TemporalUnit unit) {
|
|
1327 |
return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
|
|
1328 |
}
|
|
1329 |
|
|
1330 |
//-----------------------------------------------------------------------
|
|
1331 |
/**
|
|
1332 |
* Returns a copy of this {@code LocalDateTime} with the specified period in years subtracted.
|
|
1333 |
* <p>
|
|
1334 |
* This method subtracts the specified amount from the years field in three steps:
|
|
1335 |
* <ol>
|
|
1336 |
* <li>Subtract the input years from the year field</li>
|
|
1337 |
* <li>Check if the resulting date would be invalid</li>
|
|
1338 |
* <li>Adjust the day-of-month to the last valid day if necessary</li>
|
|
1339 |
* </ol>
|
|
1340 |
* <p>
|
|
1341 |
* For example, 2008-02-29 (leap year) minus one year would result in the
|
|
1342 |
* invalid date 2009-02-29 (standard year). Instead of returning an invalid
|
|
1343 |
* result, the last valid day of the month, 2009-02-28, is selected instead.
|
|
1344 |
* <p>
|
|
1345 |
* This instance is immutable and unaffected by this method call.
|
|
1346 |
*
|
|
1347 |
* @param years the years to subtract, may be negative
|
|
1348 |
* @return a {@code LocalDateTime} based on this date-time with the years subtracted, not null
|
|
1349 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1350 |
*/
|
|
1351 |
public LocalDateTime minusYears(long years) {
|
|
1352 |
return (years == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-years));
|
|
1353 |
}
|
|
1354 |
|
|
1355 |
/**
|
|
1356 |
* Returns a copy of this {@code LocalDateTime} with the specified period in months subtracted.
|
|
1357 |
* <p>
|
|
1358 |
* This method subtracts the specified amount from the months field in three steps:
|
|
1359 |
* <ol>
|
|
1360 |
* <li>Subtract the input months from the month-of-year field</li>
|
|
1361 |
* <li>Check if the resulting date would be invalid</li>
|
|
1362 |
* <li>Adjust the day-of-month to the last valid day if necessary</li>
|
|
1363 |
* </ol>
|
|
1364 |
* <p>
|
|
1365 |
* For example, 2007-03-31 minus one month would result in the invalid date
|
|
1366 |
* 2007-04-31. Instead of returning an invalid result, the last valid day
|
|
1367 |
* of the month, 2007-04-30, is selected instead.
|
|
1368 |
* <p>
|
|
1369 |
* This instance is immutable and unaffected by this method call.
|
|
1370 |
*
|
|
1371 |
* @param months the months to subtract, may be negative
|
|
1372 |
* @return a {@code LocalDateTime} based on this date-time with the months subtracted, not null
|
|
1373 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1374 |
*/
|
|
1375 |
public LocalDateTime minusMonths(long months) {
|
|
1376 |
return (months == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-months));
|
|
1377 |
}
|
|
1378 |
|
|
1379 |
/**
|
|
1380 |
* Returns a copy of this {@code LocalDateTime} with the specified period in weeks subtracted.
|
|
1381 |
* <p>
|
|
1382 |
* This method subtracts the specified amount in weeks from the days field decrementing
|
|
1383 |
* the month and year fields as necessary to ensure the result remains valid.
|
|
1384 |
* The result is only invalid if the maximum/minimum year is exceeded.
|
|
1385 |
* <p>
|
|
1386 |
* For example, 2009-01-07 minus one week would result in 2008-12-31.
|
|
1387 |
* <p>
|
|
1388 |
* This instance is immutable and unaffected by this method call.
|
|
1389 |
*
|
|
1390 |
* @param weeks the weeks to subtract, may be negative
|
|
1391 |
* @return a {@code LocalDateTime} based on this date-time with the weeks subtracted, not null
|
|
1392 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1393 |
*/
|
|
1394 |
public LocalDateTime minusWeeks(long weeks) {
|
|
1395 |
return (weeks == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeks));
|
|
1396 |
}
|
|
1397 |
|
|
1398 |
/**
|
|
1399 |
* Returns a copy of this {@code LocalDateTime} with the specified period in days subtracted.
|
|
1400 |
* <p>
|
|
1401 |
* This method subtracts the specified amount from the days field incrementing the
|
|
1402 |
* month and year fields as necessary to ensure the result remains valid.
|
|
1403 |
* The result is only invalid if the maximum/minimum year is exceeded.
|
|
1404 |
* <p>
|
|
1405 |
* For example, 2009-01-01 minus one day would result in 2008-12-31.
|
|
1406 |
* <p>
|
|
1407 |
* This instance is immutable and unaffected by this method call.
|
|
1408 |
*
|
|
1409 |
* @param days the days to subtract, may be negative
|
|
1410 |
* @return a {@code LocalDateTime} based on this date-time with the days subtracted, not null
|
|
1411 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1412 |
*/
|
|
1413 |
public LocalDateTime minusDays(long days) {
|
|
1414 |
return (days == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-days));
|
|
1415 |
}
|
|
1416 |
|
|
1417 |
//-----------------------------------------------------------------------
|
|
1418 |
/**
|
|
1419 |
* Returns a copy of this {@code LocalDateTime} with the specified period in hours subtracted.
|
|
1420 |
* <p>
|
|
1421 |
* This instance is immutable and unaffected by this method call.
|
|
1422 |
*
|
|
1423 |
* @param hours the hours to subtract, may be negative
|
|
1424 |
* @return a {@code LocalDateTime} based on this date-time with the hours subtracted, not null
|
|
1425 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1426 |
*/
|
|
1427 |
public LocalDateTime minusHours(long hours) {
|
|
1428 |
return plusWithOverflow(date, hours, 0, 0, 0, -1);
|
|
1429 |
}
|
|
1430 |
|
|
1431 |
/**
|
|
1432 |
* Returns a copy of this {@code LocalDateTime} with the specified period in minutes subtracted.
|
|
1433 |
* <p>
|
|
1434 |
* This instance is immutable and unaffected by this method call.
|
|
1435 |
*
|
|
1436 |
* @param minutes the minutes to subtract, may be negative
|
|
1437 |
* @return a {@code LocalDateTime} based on this date-time with the minutes subtracted, not null
|
|
1438 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1439 |
*/
|
|
1440 |
public LocalDateTime minusMinutes(long minutes) {
|
|
1441 |
return plusWithOverflow(date, 0, minutes, 0, 0, -1);
|
|
1442 |
}
|
|
1443 |
|
|
1444 |
/**
|
|
1445 |
* Returns a copy of this {@code LocalDateTime} with the specified period in seconds subtracted.
|
|
1446 |
* <p>
|
|
1447 |
* This instance is immutable and unaffected by this method call.
|
|
1448 |
*
|
|
1449 |
* @param seconds the seconds to subtract, may be negative
|
|
1450 |
* @return a {@code LocalDateTime} based on this date-time with the seconds subtracted, not null
|
|
1451 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1452 |
*/
|
|
1453 |
public LocalDateTime minusSeconds(long seconds) {
|
|
1454 |
return plusWithOverflow(date, 0, 0, seconds, 0, -1);
|
|
1455 |
}
|
|
1456 |
|
|
1457 |
/**
|
|
1458 |
* Returns a copy of this {@code LocalDateTime} with the specified period in nanoseconds subtracted.
|
|
1459 |
* <p>
|
|
1460 |
* This instance is immutable and unaffected by this method call.
|
|
1461 |
*
|
|
1462 |
* @param nanos the nanos to subtract, may be negative
|
|
1463 |
* @return a {@code LocalDateTime} based on this date-time with the nanoseconds subtracted, not null
|
|
1464 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1465 |
*/
|
|
1466 |
public LocalDateTime minusNanos(long nanos) {
|
|
1467 |
return plusWithOverflow(date, 0, 0, 0, nanos, -1);
|
|
1468 |
}
|
|
1469 |
|
|
1470 |
//-----------------------------------------------------------------------
|
|
1471 |
/**
|
|
1472 |
* Returns a copy of this {@code LocalDateTime} with the specified period added.
|
|
1473 |
* <p>
|
|
1474 |
* This instance is immutable and unaffected by this method call.
|
|
1475 |
*
|
|
1476 |
* @param newDate the new date to base the calculation on, not null
|
|
1477 |
* @param hours the hours to add, may be negative
|
|
1478 |
* @param minutes the minutes to add, may be negative
|
|
1479 |
* @param seconds the seconds to add, may be negative
|
|
1480 |
* @param nanos the nanos to add, may be negative
|
|
1481 |
* @param sign the sign to determine add or subtract
|
|
1482 |
* @return the combined result, not null
|
|
1483 |
*/
|
|
1484 |
private LocalDateTime plusWithOverflow(LocalDate newDate, long hours, long minutes, long seconds, long nanos, int sign) {
|
|
1485 |
// 9223372036854775808 long, 2147483648 int
|
|
1486 |
if ((hours | minutes | seconds | nanos) == 0) {
|
|
1487 |
return with(newDate, time);
|
|
1488 |
}
|
|
1489 |
long totDays = nanos / NANOS_PER_DAY + // max/24*60*60*1B
|
|
1490 |
seconds / SECONDS_PER_DAY + // max/24*60*60
|
|
1491 |
minutes / MINUTES_PER_DAY + // max/24*60
|
|
1492 |
hours / HOURS_PER_DAY; // max/24
|
|
1493 |
totDays *= sign; // total max*0.4237...
|
|
1494 |
long totNanos = nanos % NANOS_PER_DAY + // max 86400000000000
|
|
1495 |
(seconds % SECONDS_PER_DAY) * NANOS_PER_SECOND + // max 86400000000000
|
|
1496 |
(minutes % MINUTES_PER_DAY) * NANOS_PER_MINUTE + // max 86400000000000
|
|
1497 |
(hours % HOURS_PER_DAY) * NANOS_PER_HOUR; // max 86400000000000
|
|
1498 |
long curNoD = time.toNanoOfDay(); // max 86400000000000
|
|
1499 |
totNanos = totNanos * sign + curNoD; // total 432000000000000
|
|
1500 |
totDays += Math.floorDiv(totNanos, NANOS_PER_DAY);
|
|
1501 |
long newNoD = Math.floorMod(totNanos, NANOS_PER_DAY);
|
|
1502 |
LocalTime newTime = (newNoD == curNoD ? time : LocalTime.ofNanoOfDay(newNoD));
|
|
1503 |
return with(newDate.plusDays(totDays), newTime);
|
|
1504 |
}
|
|
1505 |
|
|
1506 |
//-----------------------------------------------------------------------
|
|
1507 |
/**
|
|
1508 |
* Queries this date-time using the specified query.
|
|
1509 |
* <p>
|
|
1510 |
* This queries this date-time using the specified query strategy object.
|
|
1511 |
* The {@code TemporalQuery} object defines the logic to be used to
|
|
1512 |
* obtain the result. Read the documentation of the query to understand
|
|
1513 |
* what the result of this method will be.
|
|
1514 |
* <p>
|
|
1515 |
* The result of this method is obtained by invoking the
|
|
1516 |
* {@link java.time.temporal.TemporalQuery#queryFrom(TemporalAccessor)} method on the
|
|
1517 |
* specified query passing {@code this} as the argument.
|
|
1518 |
*
|
|
1519 |
* @param <R> the type of the result
|
|
1520 |
* @param query the query to invoke, not null
|
|
1521 |
* @return the query result, null may be returned (defined by the query)
|
|
1522 |
* @throws DateTimeException if unable to query (defined by the query)
|
|
1523 |
* @throws ArithmeticException if numeric overflow occurs (defined by the query)
|
|
1524 |
*/
|
15658
|
1525 |
@SuppressWarnings("unchecked")
|
15289
|
1526 |
@Override // override for Javadoc
|
|
1527 |
public <R> R query(TemporalQuery<R> query) {
|
16852
|
1528 |
if (query == TemporalQuery.localDate()) {
|
15658
|
1529 |
return (R) date;
|
|
1530 |
}
|
15289
|
1531 |
return ChronoLocalDateTime.super.query(query);
|
|
1532 |
}
|
|
1533 |
|
|
1534 |
/**
|
|
1535 |
* Adjusts the specified temporal object to have the same date and time as this object.
|
|
1536 |
* <p>
|
|
1537 |
* This returns a temporal object of the same observable type as the input
|
|
1538 |
* with the date and time changed to be the same as this.
|
|
1539 |
* <p>
|
|
1540 |
* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
|
|
1541 |
* twice, passing {@link ChronoField#EPOCH_DAY} and
|
|
1542 |
* {@link ChronoField#NANO_OF_DAY} as the fields.
|
|
1543 |
* <p>
|
|
1544 |
* In most cases, it is clearer to reverse the calling pattern by using
|
|
1545 |
* {@link Temporal#with(TemporalAdjuster)}:
|
|
1546 |
* <pre>
|
|
1547 |
* // these two lines are equivalent, but the second approach is recommended
|
|
1548 |
* temporal = thisLocalDateTime.adjustInto(temporal);
|
|
1549 |
* temporal = temporal.with(thisLocalDateTime);
|
|
1550 |
* </pre>
|
|
1551 |
* <p>
|
|
1552 |
* This instance is immutable and unaffected by this method call.
|
|
1553 |
*
|
|
1554 |
* @param temporal the target object to be adjusted, not null
|
|
1555 |
* @return the adjusted object, not null
|
|
1556 |
* @throws DateTimeException if unable to make the adjustment
|
|
1557 |
* @throws ArithmeticException if numeric overflow occurs
|
|
1558 |
*/
|
|
1559 |
@Override // override for Javadoc
|
|
1560 |
public Temporal adjustInto(Temporal temporal) {
|
|
1561 |
return ChronoLocalDateTime.super.adjustInto(temporal);
|
|
1562 |
}
|
|
1563 |
|
|
1564 |
/**
|
17474
|
1565 |
* Calculates the amount of time until another date-time in terms of the specified unit.
|
15289
|
1566 |
* <p>
|
17474
|
1567 |
* This calculates the amount of time between two {@code LocalDateTime}
|
|
1568 |
* objects in terms of a single {@code TemporalUnit}.
|
15289
|
1569 |
* The start and end points are {@code this} and the specified date-time.
|
|
1570 |
* The result will be negative if the end is before the start.
|
|
1571 |
* The {@code Temporal} passed to this method must be a {@code LocalDateTime}.
|
17474
|
1572 |
* For example, the amount in days between two date-times can be calculated
|
15289
|
1573 |
* using {@code startDateTime.periodUntil(endDateTime, DAYS)}.
|
|
1574 |
* <p>
|
|
1575 |
* The calculation returns a whole number, representing the number of
|
|
1576 |
* complete units between the two date-times.
|
17474
|
1577 |
* For example, the amount in months between 2012-06-15T00:00 and 2012-08-14T23:59
|
15289
|
1578 |
* will only be one month as it is one minute short of two months.
|
|
1579 |
* <p>
|
15658
|
1580 |
* There are two equivalent ways of using this method.
|
|
1581 |
* The first is to invoke this method.
|
|
1582 |
* The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
|
15289
|
1583 |
* <pre>
|
15658
|
1584 |
* // these two lines are equivalent
|
|
1585 |
* amount = start.periodUntil(end, MONTHS);
|
|
1586 |
* amount = MONTHS.between(start, end);
|
15289
|
1587 |
* </pre>
|
15658
|
1588 |
* The choice should be made based on which makes the code more readable.
|
15289
|
1589 |
* <p>
|
|
1590 |
* The calculation is implemented in this method for {@link ChronoUnit}.
|
|
1591 |
* The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
|
|
1592 |
* {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS},
|
|
1593 |
* {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES},
|
|
1594 |
* {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported.
|
|
1595 |
* Other {@code ChronoUnit} values will throw an exception.
|
|
1596 |
* <p>
|
|
1597 |
* If the unit is not a {@code ChronoUnit}, then the result of this method
|
|
1598 |
* is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
|
|
1599 |
* passing {@code this} as the first argument and the input temporal as
|
|
1600 |
* the second argument.
|
|
1601 |
* <p>
|
|
1602 |
* This instance is immutable and unaffected by this method call.
|
|
1603 |
*
|
|
1604 |
* @param endDateTime the end date-time, which must be a {@code LocalDateTime}, not null
|
17474
|
1605 |
* @param unit the unit to measure the amount in, not null
|
|
1606 |
* @return the amount of time between this date-time and the end date-time
|
|
1607 |
* @throws DateTimeException if the amount cannot be calculated
|
16852
|
1608 |
* @throws UnsupportedTemporalTypeException if the unit is not supported
|
15289
|
1609 |
* @throws ArithmeticException if numeric overflow occurs
|
|
1610 |
*/
|
|
1611 |
@Override
|
|
1612 |
public long periodUntil(Temporal endDateTime, TemporalUnit unit) {
|
|
1613 |
if (endDateTime instanceof LocalDateTime == false) {
|
|
1614 |
Objects.requireNonNull(endDateTime, "endDateTime");
|
17474
|
1615 |
throw new DateTimeException("Unable to calculate amount as objects are of two different types");
|
15289
|
1616 |
}
|
|
1617 |
LocalDateTime end = (LocalDateTime) endDateTime;
|
|
1618 |
if (unit instanceof ChronoUnit) {
|
|
1619 |
ChronoUnit f = (ChronoUnit) unit;
|
|
1620 |
if (f.isTimeUnit()) {
|
|
1621 |
long amount = date.daysUntil(end.date);
|
16852
|
1622 |
if (amount == 0) {
|
|
1623 |
return time.periodUntil(end.time, unit);
|
|
1624 |
}
|
|
1625 |
long timePart = end.time.toNanoOfDay() - time.toNanoOfDay();
|
|
1626 |
if (amount > 0) {
|
|
1627 |
amount--; // safe
|
|
1628 |
timePart += NANOS_PER_DAY; // safe
|
|
1629 |
} else {
|
|
1630 |
amount++; // safe
|
|
1631 |
timePart -= NANOS_PER_DAY; // safe
|
|
1632 |
}
|
15289
|
1633 |
switch (f) {
|
16852
|
1634 |
case NANOS:
|
|
1635 |
amount = Math.multiplyExact(amount, NANOS_PER_DAY);
|
|
1636 |
break;
|
|
1637 |
case MICROS:
|
|
1638 |
amount = Math.multiplyExact(amount, MICROS_PER_DAY);
|
|
1639 |
timePart = timePart / 1000;
|
|
1640 |
break;
|
|
1641 |
case MILLIS:
|
|
1642 |
amount = Math.multiplyExact(amount, MILLIS_PER_DAY);
|
|
1643 |
timePart = timePart / 1_000_000;
|
|
1644 |
break;
|
|
1645 |
case SECONDS:
|
|
1646 |
amount = Math.multiplyExact(amount, SECONDS_PER_DAY);
|
|
1647 |
timePart = timePart / NANOS_PER_SECOND;
|
|
1648 |
break;
|
|
1649 |
case MINUTES:
|
|
1650 |
amount = Math.multiplyExact(amount, MINUTES_PER_DAY);
|
|
1651 |
timePart = timePart / NANOS_PER_MINUTE;
|
|
1652 |
break;
|
|
1653 |
case HOURS:
|
|
1654 |
amount = Math.multiplyExact(amount, HOURS_PER_DAY);
|
|
1655 |
timePart = timePart / NANOS_PER_HOUR;
|
|
1656 |
break;
|
|
1657 |
case HALF_DAYS:
|
|
1658 |
amount = Math.multiplyExact(amount, 2);
|
|
1659 |
timePart = timePart / (NANOS_PER_HOUR * 12);
|
|
1660 |
break;
|
15289
|
1661 |
}
|
16852
|
1662 |
return Math.addExact(amount, timePart);
|
15289
|
1663 |
}
|
|
1664 |
LocalDate endDate = end.date;
|
16852
|
1665 |
if (endDate.isAfter(date) && end.time.isBefore(time)) {
|
15289
|
1666 |
endDate = endDate.minusDays(1);
|
16852
|
1667 |
} else if (endDate.isBefore(date) && end.time.isAfter(time)) {
|
|
1668 |
endDate = endDate.plusDays(1);
|
15289
|
1669 |
}
|
|
1670 |
return date.periodUntil(endDate, unit);
|
|
1671 |
}
|
15658
|
1672 |
return unit.between(this, endDateTime);
|
15289
|
1673 |
}
|
|
1674 |
|
16852
|
1675 |
/**
|
|
1676 |
* Formats this date-time using the specified formatter.
|
|
1677 |
* <p>
|
|
1678 |
* This date-time will be passed to the formatter to produce a string.
|
|
1679 |
*
|
|
1680 |
* @param formatter the formatter to use, not null
|
|
1681 |
* @return the formatted date-time string, not null
|
|
1682 |
* @throws DateTimeException if an error occurs during printing
|
|
1683 |
*/
|
|
1684 |
@Override // override for Javadoc and performance
|
|
1685 |
public String format(DateTimeFormatter formatter) {
|
|
1686 |
Objects.requireNonNull(formatter, "formatter");
|
|
1687 |
return formatter.format(this);
|
|
1688 |
}
|
|
1689 |
|
15289
|
1690 |
//-----------------------------------------------------------------------
|
|
1691 |
/**
|
16852
|
1692 |
* Combines this date-time with an offset to create an {@code OffsetDateTime}.
|
15289
|
1693 |
* <p>
|
16852
|
1694 |
* This returns an {@code OffsetDateTime} formed from this date-time at the specified offset.
|
15289
|
1695 |
* All possible combinations of date-time and offset are valid.
|
|
1696 |
*
|
|
1697 |
* @param offset the offset to combine with, not null
|
|
1698 |
* @return the offset date-time formed from this date-time and the specified offset, not null
|
|
1699 |
*/
|
|
1700 |
public OffsetDateTime atOffset(ZoneOffset offset) {
|
|
1701 |
return OffsetDateTime.of(this, offset);
|
|
1702 |
}
|
|
1703 |
|
|
1704 |
/**
|
16852
|
1705 |
* Combines this date-time with a time-zone to create a {@code ZonedDateTime}.
|
15289
|
1706 |
* <p>
|
15658
|
1707 |
* This returns a {@code ZonedDateTime} formed from this date-time at the
|
|
1708 |
* specified time-zone. The result will match this date-time as closely as possible.
|
15289
|
1709 |
* Time-zone rules, such as daylight savings, mean that not every local date-time
|
|
1710 |
* is valid for the specified zone, thus the local date-time may be adjusted.
|
|
1711 |
* <p>
|
|
1712 |
* The local date-time is resolved to a single instant on the time-line.
|
|
1713 |
* This is achieved by finding a valid offset from UTC/Greenwich for the local
|
|
1714 |
* date-time as defined by the {@link ZoneRules rules} of the zone ID.
|
|
1715 |
*<p>
|
|
1716 |
* In most cases, there is only one valid offset for a local date-time.
|
|
1717 |
* In the case of an overlap, where clocks are set back, there are two valid offsets.
|
|
1718 |
* This method uses the earlier offset typically corresponding to "summer".
|
|
1719 |
* <p>
|
|
1720 |
* In the case of a gap, where clocks jump forward, there is no valid offset.
|
|
1721 |
* Instead, the local date-time is adjusted to be later by the length of the gap.
|
|
1722 |
* For a typical one hour daylight savings change, the local date-time will be
|
|
1723 |
* moved one hour later into the offset typically corresponding to "summer".
|
|
1724 |
* <p>
|
|
1725 |
* To obtain the later offset during an overlap, call
|
|
1726 |
* {@link ZonedDateTime#withLaterOffsetAtOverlap()} on the result of this method.
|
|
1727 |
* To throw an exception when there is a gap or overlap, use
|
|
1728 |
* {@link ZonedDateTime#ofStrict(LocalDateTime, ZoneOffset, ZoneId)}.
|
|
1729 |
*
|
|
1730 |
* @param zone the time-zone to use, not null
|
|
1731 |
* @return the zoned date-time formed from this date-time, not null
|
|
1732 |
*/
|
|
1733 |
@Override
|
|
1734 |
public ZonedDateTime atZone(ZoneId zone) {
|
|
1735 |
return ZonedDateTime.of(this, zone);
|
|
1736 |
}
|
|
1737 |
|
|
1738 |
//-----------------------------------------------------------------------
|
|
1739 |
/**
|
|
1740 |
* Compares this date-time to another date-time.
|
|
1741 |
* <p>
|
|
1742 |
* The comparison is primarily based on the date-time, from earliest to latest.
|
|
1743 |
* It is "consistent with equals", as defined by {@link Comparable}.
|
|
1744 |
* <p>
|
|
1745 |
* If all the date-times being compared are instances of {@code LocalDateTime},
|
|
1746 |
* then the comparison will be entirely based on the date-time.
|
|
1747 |
* If some dates being compared are in different chronologies, then the
|
|
1748 |
* chronology is also considered, see {@link ChronoLocalDateTime#compareTo}.
|
|
1749 |
*
|
|
1750 |
* @param other the other date-time to compare to, not null
|
|
1751 |
* @return the comparator value, negative if less, positive if greater
|
|
1752 |
*/
|
|
1753 |
@Override // override for Javadoc and performance
|
|
1754 |
public int compareTo(ChronoLocalDateTime<?> other) {
|
|
1755 |
if (other instanceof LocalDateTime) {
|
|
1756 |
return compareTo0((LocalDateTime) other);
|
|
1757 |
}
|
|
1758 |
return ChronoLocalDateTime.super.compareTo(other);
|
|
1759 |
}
|
|
1760 |
|
|
1761 |
private int compareTo0(LocalDateTime other) {
|
15658
|
1762 |
int cmp = date.compareTo0(other.toLocalDate());
|
15289
|
1763 |
if (cmp == 0) {
|
15658
|
1764 |
cmp = time.compareTo(other.toLocalTime());
|
15289
|
1765 |
}
|
|
1766 |
return cmp;
|
|
1767 |
}
|
|
1768 |
|
|
1769 |
/**
|
|
1770 |
* Checks if this date-time is after the specified date-time.
|
|
1771 |
* <p>
|
|
1772 |
* This checks to see if this date-time represents a point on the
|
|
1773 |
* local time-line after the other date-time.
|
|
1774 |
* <pre>
|
|
1775 |
* LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
|
|
1776 |
* LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
|
|
1777 |
* a.isAfter(b) == false
|
|
1778 |
* a.isAfter(a) == false
|
|
1779 |
* b.isAfter(a) == true
|
|
1780 |
* </pre>
|
|
1781 |
* <p>
|
|
1782 |
* This method only considers the position of the two date-times on the local time-line.
|
|
1783 |
* It does not take into account the chronology, or calendar system.
|
|
1784 |
* This is different from the comparison in {@link #compareTo(ChronoLocalDateTime)},
|
16852
|
1785 |
* but is the same approach as {@link ChronoLocalDateTime#timeLineOrder()}.
|
15289
|
1786 |
*
|
|
1787 |
* @param other the other date-time to compare to, not null
|
|
1788 |
* @return true if this date-time is after the specified date-time
|
|
1789 |
*/
|
|
1790 |
@Override // override for Javadoc and performance
|
|
1791 |
public boolean isAfter(ChronoLocalDateTime<?> other) {
|
|
1792 |
if (other instanceof LocalDateTime) {
|
|
1793 |
return compareTo0((LocalDateTime) other) > 0;
|
|
1794 |
}
|
|
1795 |
return ChronoLocalDateTime.super.isAfter(other);
|
|
1796 |
}
|
|
1797 |
|
|
1798 |
/**
|
|
1799 |
* Checks if this date-time is before the specified date-time.
|
|
1800 |
* <p>
|
|
1801 |
* This checks to see if this date-time represents a point on the
|
|
1802 |
* local time-line before the other date-time.
|
|
1803 |
* <pre>
|
|
1804 |
* LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
|
|
1805 |
* LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
|
|
1806 |
* a.isBefore(b) == true
|
|
1807 |
* a.isBefore(a) == false
|
|
1808 |
* b.isBefore(a) == false
|
|
1809 |
* </pre>
|
|
1810 |
* <p>
|
|
1811 |
* This method only considers the position of the two date-times on the local time-line.
|
|
1812 |
* It does not take into account the chronology, or calendar system.
|
|
1813 |
* This is different from the comparison in {@link #compareTo(ChronoLocalDateTime)},
|
16852
|
1814 |
* but is the same approach as {@link ChronoLocalDateTime#timeLineOrder()}.
|
15289
|
1815 |
*
|
|
1816 |
* @param other the other date-time to compare to, not null
|
|
1817 |
* @return true if this date-time is before the specified date-time
|
|
1818 |
*/
|
|
1819 |
@Override // override for Javadoc and performance
|
|
1820 |
public boolean isBefore(ChronoLocalDateTime<?> other) {
|
|
1821 |
if (other instanceof LocalDateTime) {
|
|
1822 |
return compareTo0((LocalDateTime) other) < 0;
|
|
1823 |
}
|
|
1824 |
return ChronoLocalDateTime.super.isBefore(other);
|
|
1825 |
}
|
|
1826 |
|
|
1827 |
/**
|
|
1828 |
* Checks if this date-time is equal to the specified date-time.
|
|
1829 |
* <p>
|
|
1830 |
* This checks to see if this date-time represents the same point on the
|
|
1831 |
* local time-line as the other date-time.
|
|
1832 |
* <pre>
|
|
1833 |
* LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
|
|
1834 |
* LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
|
|
1835 |
* a.isEqual(b) == false
|
|
1836 |
* a.isEqual(a) == true
|
|
1837 |
* b.isEqual(a) == false
|
|
1838 |
* </pre>
|
|
1839 |
* <p>
|
|
1840 |
* This method only considers the position of the two date-times on the local time-line.
|
|
1841 |
* It does not take into account the chronology, or calendar system.
|
|
1842 |
* This is different from the comparison in {@link #compareTo(ChronoLocalDateTime)},
|
16852
|
1843 |
* but is the same approach as {@link ChronoLocalDateTime#timeLineOrder()}.
|
15289
|
1844 |
*
|
|
1845 |
* @param other the other date-time to compare to, not null
|
|
1846 |
* @return true if this date-time is equal to the specified date-time
|
|
1847 |
*/
|
|
1848 |
@Override // override for Javadoc and performance
|
|
1849 |
public boolean isEqual(ChronoLocalDateTime<?> other) {
|
|
1850 |
if (other instanceof LocalDateTime) {
|
|
1851 |
return compareTo0((LocalDateTime) other) == 0;
|
|
1852 |
}
|
|
1853 |
return ChronoLocalDateTime.super.isEqual(other);
|
|
1854 |
}
|
|
1855 |
|
|
1856 |
//-----------------------------------------------------------------------
|
|
1857 |
/**
|
|
1858 |
* Checks if this date-time is equal to another date-time.
|
|
1859 |
* <p>
|
|
1860 |
* Compares this {@code LocalDateTime} with another ensuring that the date-time is the same.
|
|
1861 |
* Only objects of type {@code LocalDateTime} are compared, other types return false.
|
|
1862 |
*
|
|
1863 |
* @param obj the object to check, null returns false
|
|
1864 |
* @return true if this is equal to the other date-time
|
|
1865 |
*/
|
|
1866 |
@Override
|
|
1867 |
public boolean equals(Object obj) {
|
|
1868 |
if (this == obj) {
|
|
1869 |
return true;
|
|
1870 |
}
|
|
1871 |
if (obj instanceof LocalDateTime) {
|
|
1872 |
LocalDateTime other = (LocalDateTime) obj;
|
|
1873 |
return date.equals(other.date) && time.equals(other.time);
|
|
1874 |
}
|
|
1875 |
return false;
|
|
1876 |
}
|
|
1877 |
|
|
1878 |
/**
|
|
1879 |
* A hash code for this date-time.
|
|
1880 |
*
|
|
1881 |
* @return a suitable hash code
|
|
1882 |
*/
|
|
1883 |
@Override
|
|
1884 |
public int hashCode() {
|
|
1885 |
return date.hashCode() ^ time.hashCode();
|
|
1886 |
}
|
|
1887 |
|
|
1888 |
//-----------------------------------------------------------------------
|
|
1889 |
/**
|
|
1890 |
* Outputs this date-time as a {@code String}, such as {@code 2007-12-03T10:15:30}.
|
|
1891 |
* <p>
|
|
1892 |
* The output will be one of the following ISO-8601 formats:
|
|
1893 |
* <p><ul>
|
16852
|
1894 |
* <li>{@code uuuu-MM-dd'T'HH:mm}</li>
|
|
1895 |
* <li>{@code uuuu-MM-dd'T'HH:mm:ss}</li>
|
|
1896 |
* <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSS}</li>
|
|
1897 |
* <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSSSS}</li>
|
|
1898 |
* <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS}</li>
|
15289
|
1899 |
* </ul><p>
|
|
1900 |
* The format used will be the shortest that outputs the full value of
|
|
1901 |
* the time where the omitted parts are implied to be zero.
|
|
1902 |
*
|
|
1903 |
* @return a string representation of this date-time, not null
|
|
1904 |
*/
|
|
1905 |
@Override
|
|
1906 |
public String toString() {
|
|
1907 |
return date.toString() + 'T' + time.toString();
|
|
1908 |
}
|
|
1909 |
|
|
1910 |
//-----------------------------------------------------------------------
|
|
1911 |
/**
|
|
1912 |
* Writes the object using a
|
|
1913 |
* <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
|
|
1914 |
* <pre>
|
|
1915 |
* out.writeByte(5); // identifies this as a LocalDateTime
|
|
1916 |
* // the <a href="../../serialized-form.html#java.time.LocalDate">date</a> excluding the one byte header
|
|
1917 |
* // the <a href="../../serialized-form.html#java.time.LocalTime">time</a> excluding the one byte header
|
|
1918 |
* </pre>
|
|
1919 |
*
|
|
1920 |
* @return the instance of {@code Ser}, not null
|
|
1921 |
*/
|
|
1922 |
private Object writeReplace() {
|
|
1923 |
return new Ser(Ser.LOCAL_DATE_TIME_TYPE, this);
|
|
1924 |
}
|
|
1925 |
|
|
1926 |
/**
|
|
1927 |
* Defend against malicious streams.
|
|
1928 |
* @return never
|
|
1929 |
* @throws InvalidObjectException always
|
|
1930 |
*/
|
|
1931 |
private Object readResolve() throws ObjectStreamException {
|
|
1932 |
throw new InvalidObjectException("Deserialization via serialization delegate");
|
|
1933 |
}
|
|
1934 |
|
|
1935 |
void writeExternal(DataOutput out) throws IOException {
|
|
1936 |
date.writeExternal(out);
|
|
1937 |
time.writeExternal(out);
|
|
1938 |
}
|
|
1939 |
|
|
1940 |
static LocalDateTime readExternal(DataInput in) throws IOException {
|
|
1941 |
LocalDate date = LocalDate.readExternal(in);
|
|
1942 |
LocalTime time = LocalTime.readExternal(in);
|
|
1943 |
return LocalDateTime.of(date, time);
|
|
1944 |
}
|
|
1945 |
|
|
1946 |
}
|