15658
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
/*
|
|
27 |
* This file is available under and governed by the GNU General Public
|
|
28 |
* License version 2 only, as published by the Free Software Foundation.
|
|
29 |
* However, the following notice accompanied the original version of this
|
|
30 |
* file:
|
|
31 |
*
|
|
32 |
* Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
|
|
33 |
*
|
|
34 |
* All rights reserved.
|
|
35 |
*
|
|
36 |
* Redistribution and use in source and binary forms, with or without
|
|
37 |
* modification, are permitted provided that the following conditions are met:
|
|
38 |
*
|
|
39 |
* * Redistributions of source code must retain the above copyright notice,
|
|
40 |
* this list of conditions and the following disclaimer.
|
|
41 |
*
|
|
42 |
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
43 |
* this list of conditions and the following disclaimer in the documentation
|
|
44 |
* and/or other materials provided with the distribution.
|
|
45 |
*
|
|
46 |
* * Neither the name of JSR-310 nor the names of its contributors
|
|
47 |
* may be used to endorse or promote products derived from this software
|
|
48 |
* without specific prior written permission.
|
|
49 |
*
|
|
50 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
51 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
52 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
53 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
54 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
55 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
56 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
57 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
58 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
59 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
60 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
61 |
*/
|
|
62 |
package java.time;
|
|
63 |
|
|
64 |
import static java.time.temporal.ChronoField.EPOCH_DAY;
|
|
65 |
import static java.time.temporal.ChronoField.INSTANT_SECONDS;
|
|
66 |
import static java.time.temporal.ChronoField.NANO_OF_DAY;
|
|
67 |
import static java.time.temporal.ChronoField.OFFSET_SECONDS;
|
|
68 |
import static java.time.temporal.ChronoUnit.NANOS;
|
|
69 |
|
|
70 |
import java.io.IOException;
|
|
71 |
import java.io.InvalidObjectException;
|
|
72 |
import java.io.ObjectInput;
|
|
73 |
import java.io.ObjectOutput;
|
|
74 |
import java.io.ObjectStreamException;
|
|
75 |
import java.io.Serializable;
|
|
76 |
import java.time.chrono.IsoChronology;
|
|
77 |
import java.time.format.DateTimeFormatter;
|
|
78 |
import java.time.format.DateTimeParseException;
|
|
79 |
import java.time.temporal.ChronoField;
|
|
80 |
import java.time.temporal.ChronoUnit;
|
|
81 |
import java.time.temporal.Queries;
|
|
82 |
import java.time.temporal.Temporal;
|
|
83 |
import java.time.temporal.TemporalAccessor;
|
|
84 |
import java.time.temporal.TemporalAdjuster;
|
|
85 |
import java.time.temporal.TemporalAmount;
|
|
86 |
import java.time.temporal.TemporalField;
|
|
87 |
import java.time.temporal.TemporalQuery;
|
|
88 |
import java.time.temporal.TemporalUnit;
|
|
89 |
import java.time.temporal.ValueRange;
|
|
90 |
import java.time.zone.ZoneRules;
|
|
91 |
import java.util.Comparator;
|
|
92 |
import java.util.Objects;
|
|
93 |
|
|
94 |
/**
|
|
95 |
* A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system,
|
|
96 |
* such as {@code 2007-12-03T10:15:30+01:00}.
|
|
97 |
* <p>
|
|
98 |
* {@code OffsetDateTime} is an immutable representation of a date-time with an offset.
|
|
99 |
* This class stores all date and time fields, to a precision of nanoseconds,
|
|
100 |
* as well as the offset from UTC/Greenwich. For example, the value
|
|
101 |
* "2nd October 2007 at 13:45.30.123456789 +02:00" can be stored in an {@code OffsetDateTime}.
|
|
102 |
* <p>
|
|
103 |
* {@code OffsetDateTime}, {@link java.time.ZonedDateTime} and {@link java.time.Instant} all store an instant
|
|
104 |
* on the time-line to nanosecond precision.
|
|
105 |
* {@code Instant} is the simplest, simply representing the instant.
|
|
106 |
* {@code OffsetDateTime} adds to the instant the offset from UTC/Greenwich, which allows
|
|
107 |
* the local date-time to be obtained.
|
|
108 |
* {@code ZonedDateTime} adds full time-zone rules.
|
|
109 |
* <p>
|
|
110 |
* It is intended that {@code ZonedDateTime} or {@code Instant} is used to model data
|
|
111 |
* in simpler applications. This class may be used when modeling date-time concepts in
|
|
112 |
* more detail, or when communicating to a database or in a network protocol.
|
|
113 |
*
|
|
114 |
* <h3>Specification for implementors</h3>
|
|
115 |
* This class is immutable and thread-safe.
|
|
116 |
*
|
|
117 |
* @since 1.8
|
|
118 |
*/
|
|
119 |
public final class OffsetDateTime
|
|
120 |
implements Temporal, TemporalAdjuster, Comparable<OffsetDateTime>, Serializable {
|
|
121 |
|
|
122 |
/**
|
|
123 |
* The minimum supported {@code OffsetDateTime}, '-999999999-01-01T00:00:00+18:00'.
|
|
124 |
* This is the local date-time of midnight at the start of the minimum date
|
|
125 |
* in the maximum offset (larger offsets are earlier on the time-line).
|
|
126 |
* This combines {@link LocalDateTime#MIN} and {@link ZoneOffset#MAX}.
|
|
127 |
* This could be used by an application as a "far past" date-time.
|
|
128 |
*/
|
|
129 |
public static final OffsetDateTime MIN = LocalDateTime.MIN.atOffset(ZoneOffset.MAX);
|
|
130 |
/**
|
|
131 |
* The maximum supported {@code OffsetDateTime}, '+999999999-12-31T23:59:59.999999999-18:00'.
|
|
132 |
* This is the local date-time just before midnight at the end of the maximum date
|
|
133 |
* in the minimum offset (larger negative offsets are later on the time-line).
|
|
134 |
* This combines {@link LocalDateTime#MAX} and {@link ZoneOffset#MIN}.
|
|
135 |
* This could be used by an application as a "far future" date-time.
|
|
136 |
*/
|
|
137 |
public static final OffsetDateTime MAX = LocalDateTime.MAX.atOffset(ZoneOffset.MIN);
|
|
138 |
|
|
139 |
/**
|
|
140 |
* Comparator for two {@code OffsetDateTime} instances based solely on the instant.
|
|
141 |
* <p>
|
|
142 |
* This method differs from the comparison in {@link #compareTo} in that it
|
|
143 |
* only compares the underlying instant.
|
|
144 |
*
|
|
145 |
* @see #isAfter
|
|
146 |
* @see #isBefore
|
|
147 |
* @see #isEqual
|
|
148 |
*/
|
|
149 |
public static final Comparator<OffsetDateTime> INSTANT_COMPARATOR = new Comparator<OffsetDateTime>() {
|
|
150 |
@Override
|
|
151 |
public int compare(OffsetDateTime datetime1, OffsetDateTime datetime2) {
|
|
152 |
int cmp = Long.compare(datetime1.toEpochSecond(), datetime2.toEpochSecond());
|
|
153 |
if (cmp == 0) {
|
|
154 |
cmp = Long.compare(datetime1.toLocalTime().toNanoOfDay(), datetime2.toLocalTime().toNanoOfDay());
|
|
155 |
}
|
|
156 |
return cmp;
|
|
157 |
}
|
|
158 |
};
|
|
159 |
|
|
160 |
/**
|
|
161 |
* Serialization version.
|
|
162 |
*/
|
|
163 |
private static final long serialVersionUID = 2287754244819255394L;
|
|
164 |
|
|
165 |
/**
|
|
166 |
* The local date-time.
|
|
167 |
*/
|
|
168 |
private final LocalDateTime dateTime;
|
|
169 |
/**
|
|
170 |
* The offset from UTC/Greenwich.
|
|
171 |
*/
|
|
172 |
private final ZoneOffset offset;
|
|
173 |
|
|
174 |
//-----------------------------------------------------------------------
|
|
175 |
/**
|
|
176 |
* Obtains the current date-time from the system clock in the default time-zone.
|
|
177 |
* <p>
|
|
178 |
* This will query the {@link java.time.Clock#systemDefaultZone() system clock} in the default
|
|
179 |
* time-zone to obtain the current date-time.
|
|
180 |
* The offset will be calculated from the time-zone in the clock.
|
|
181 |
* <p>
|
|
182 |
* Using this method will prevent the ability to use an alternate clock for testing
|
|
183 |
* because the clock is hard-coded.
|
|
184 |
*
|
|
185 |
* @return the current date-time using the system clock, not null
|
|
186 |
*/
|
|
187 |
public static OffsetDateTime now() {
|
|
188 |
return now(Clock.systemDefaultZone());
|
|
189 |
}
|
|
190 |
|
|
191 |
/**
|
|
192 |
* Obtains the current date-time from the system clock in the specified time-zone.
|
|
193 |
* <p>
|
|
194 |
* This will query the {@link Clock#system(java.time.ZoneId) system clock} to obtain the current date-time.
|
|
195 |
* Specifying the time-zone avoids dependence on the default time-zone.
|
|
196 |
* The offset will be calculated from the specified time-zone.
|
|
197 |
* <p>
|
|
198 |
* Using this method will prevent the ability to use an alternate clock for testing
|
|
199 |
* because the clock is hard-coded.
|
|
200 |
*
|
|
201 |
* @param zone the zone ID to use, not null
|
|
202 |
* @return the current date-time using the system clock, not null
|
|
203 |
*/
|
|
204 |
public static OffsetDateTime now(ZoneId zone) {
|
|
205 |
return now(Clock.system(zone));
|
|
206 |
}
|
|
207 |
|
|
208 |
/**
|
|
209 |
* Obtains the current date-time from the specified clock.
|
|
210 |
* <p>
|
|
211 |
* This will query the specified clock to obtain the current date-time.
|
|
212 |
* The offset will be calculated from the time-zone in the clock.
|
|
213 |
* <p>
|
|
214 |
* Using this method allows the use of an alternate clock for testing.
|
|
215 |
* The alternate clock may be introduced using {@link Clock dependency injection}.
|
|
216 |
*
|
|
217 |
* @param clock the clock to use, not null
|
|
218 |
* @return the current date-time, not null
|
|
219 |
*/
|
|
220 |
public static OffsetDateTime now(Clock clock) {
|
|
221 |
Objects.requireNonNull(clock, "clock");
|
|
222 |
final Instant now = clock.instant(); // called once
|
|
223 |
return ofInstant(now, clock.getZone().getRules().getOffset(now));
|
|
224 |
}
|
|
225 |
|
|
226 |
//-----------------------------------------------------------------------
|
|
227 |
/**
|
|
228 |
* Obtains an instance of {@code OffsetDateTime} from a date, time and offset.
|
|
229 |
* <p>
|
|
230 |
* This creates an offset date-time with the specified local date, time and offset.
|
|
231 |
*
|
|
232 |
* @param date the local date, not null
|
|
233 |
* @param time the local time, not null
|
|
234 |
* @param offset the zone offset, not null
|
|
235 |
* @return the offset date-time, not null
|
|
236 |
*/
|
|
237 |
public static OffsetDateTime of(LocalDate date, LocalTime time, ZoneOffset offset) {
|
|
238 |
LocalDateTime dt = LocalDateTime.of(date, time);
|
|
239 |
return new OffsetDateTime(dt, offset);
|
|
240 |
}
|
|
241 |
|
|
242 |
/**
|
|
243 |
* Obtains an instance of {@code OffsetDateTime} from a date-time and offset.
|
|
244 |
* <p>
|
|
245 |
* This creates an offset date-time with the specified local date-time and offset.
|
|
246 |
*
|
|
247 |
* @param dateTime the local date-time, not null
|
|
248 |
* @param offset the zone offset, not null
|
|
249 |
* @return the offset date-time, not null
|
|
250 |
*/
|
|
251 |
public static OffsetDateTime of(LocalDateTime dateTime, ZoneOffset offset) {
|
|
252 |
return new OffsetDateTime(dateTime, offset);
|
|
253 |
}
|
|
254 |
|
|
255 |
/**
|
|
256 |
* Obtains an instance of {@code OffsetDateTime} from a year, month, day,
|
|
257 |
* hour, minute, second, nanosecond and offset.
|
|
258 |
* <p>
|
|
259 |
* This creates an offset date-time with the seven specified fields.
|
|
260 |
* <p>
|
|
261 |
* This method exists primarily for writing test cases.
|
|
262 |
* Non test-code will typically use other methods to create an offset time.
|
|
263 |
* {@code LocalDateTime} has five additional convenience variants of the
|
|
264 |
* equivalent factory method taking fewer arguments.
|
|
265 |
* They are not provided here to reduce the footprint of the API.
|
|
266 |
*
|
|
267 |
* @param year the year to represent, from MIN_YEAR to MAX_YEAR
|
|
268 |
* @param month the month-of-year to represent, from 1 (January) to 12 (December)
|
|
269 |
* @param dayOfMonth the day-of-month to represent, from 1 to 31
|
|
270 |
* @param hour the hour-of-day to represent, from 0 to 23
|
|
271 |
* @param minute the minute-of-hour to represent, from 0 to 59
|
|
272 |
* @param second the second-of-minute to represent, from 0 to 59
|
|
273 |
* @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999
|
|
274 |
* @param offset the zone offset, not null
|
|
275 |
* @return the offset date-time, not null
|
|
276 |
* @throws DateTimeException if the value of any field is out of range, or
|
|
277 |
* if the day-of-month is invalid for the month-year
|
|
278 |
*/
|
|
279 |
public static OffsetDateTime of(
|
|
280 |
int year, int month, int dayOfMonth,
|
|
281 |
int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset) {
|
|
282 |
LocalDateTime dt = LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond);
|
|
283 |
return new OffsetDateTime(dt, offset);
|
|
284 |
}
|
|
285 |
|
|
286 |
//-----------------------------------------------------------------------
|
|
287 |
/**
|
|
288 |
* Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID.
|
|
289 |
* <p>
|
|
290 |
* This creates an offset date-time with the same instant as that specified.
|
|
291 |
* Finding the offset from UTC/Greenwich is simple as there is only one valid
|
|
292 |
* offset for each instant.
|
|
293 |
*
|
|
294 |
* @param instant the instant to create the date-time from, not null
|
|
295 |
* @param zone the time-zone, which may be an offset, not null
|
|
296 |
* @return the offset date-time, not null
|
|
297 |
* @throws DateTimeException if the result exceeds the supported range
|
|
298 |
*/
|
|
299 |
public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) {
|
|
300 |
Objects.requireNonNull(instant, "instant");
|
|
301 |
Objects.requireNonNull(zone, "zone");
|
|
302 |
ZoneRules rules = zone.getRules();
|
|
303 |
ZoneOffset offset = rules.getOffset(instant);
|
|
304 |
LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
|
|
305 |
return new OffsetDateTime(ldt, offset);
|
|
306 |
}
|
|
307 |
|
|
308 |
//-----------------------------------------------------------------------
|
|
309 |
/**
|
|
310 |
* Obtains an instance of {@code OffsetDateTime} from a temporal object.
|
|
311 |
* <p>
|
|
312 |
* This obtains an offset date-time based on the specified temporal.
|
|
313 |
* A {@code TemporalAccessor} represents an arbitrary set of date and time information,
|
|
314 |
* which this factory converts to an instance of {@code OffsetDateTime}.
|
|
315 |
* <p>
|
|
316 |
* The conversion will first obtain a {@code ZoneOffset} from the temporal object.
|
|
317 |
* It will then try to obtain a {@code LocalDateTime}, falling back to an {@code Instant} if necessary.
|
|
318 |
* The result will be the combination of {@code ZoneOffset} with either
|
|
319 |
* with {@code LocalDateTime} or {@code Instant}.
|
|
320 |
* Implementations are permitted to perform optimizations such as accessing
|
|
321 |
* those fields that are equivalent to the relevant objects.
|
|
322 |
* <p>
|
|
323 |
* This method matches the signature of the functional interface {@link TemporalQuery}
|
|
324 |
* allowing it to be used in queries via method reference, {@code OffsetDateTime::from}.
|
|
325 |
*
|
|
326 |
* @param temporal the temporal object to convert, not null
|
|
327 |
* @return the offset date-time, not null
|
|
328 |
* @throws DateTimeException if unable to convert to an {@code OffsetDateTime}
|
|
329 |
*/
|
|
330 |
public static OffsetDateTime from(TemporalAccessor temporal) {
|
|
331 |
if (temporal instanceof OffsetDateTime) {
|
|
332 |
return (OffsetDateTime) temporal;
|
|
333 |
}
|
|
334 |
ZoneOffset offset = ZoneOffset.from(temporal);
|
|
335 |
try {
|
|
336 |
try {
|
|
337 |
LocalDateTime ldt = LocalDateTime.from(temporal);
|
|
338 |
return OffsetDateTime.of(ldt, offset);
|
|
339 |
} catch (DateTimeException ignore) {
|
|
340 |
Instant instant = Instant.from(temporal);
|
|
341 |
return OffsetDateTime.ofInstant(instant, offset);
|
|
342 |
}
|
|
343 |
} catch (DateTimeException ex) {
|
|
344 |
throw new DateTimeException("Unable to obtain OffsetDateTime from TemporalAccessor: " + temporal.getClass(), ex);
|
|
345 |
}
|
|
346 |
}
|
|
347 |
|
|
348 |
//-----------------------------------------------------------------------
|
|
349 |
/**
|
|
350 |
* Obtains an instance of {@code OffsetDateTime} from a text string
|
|
351 |
* such as {@code 2007-12-03T10:15:30+01:00}.
|
|
352 |
* <p>
|
|
353 |
* The string must represent a valid date-time and is parsed using
|
|
354 |
* {@link java.time.format.DateTimeFormatter#ISO_OFFSET_DATE_TIME}.
|
|
355 |
*
|
|
356 |
* @param text the text to parse such as "2007-12-03T10:15:30+01:00", not null
|
|
357 |
* @return the parsed offset date-time, not null
|
|
358 |
* @throws DateTimeParseException if the text cannot be parsed
|
|
359 |
*/
|
|
360 |
public static OffsetDateTime parse(CharSequence text) {
|
|
361 |
return parse(text, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
|
362 |
}
|
|
363 |
|
|
364 |
/**
|
|
365 |
* Obtains an instance of {@code OffsetDateTime} from a text string using a specific formatter.
|
|
366 |
* <p>
|
|
367 |
* The text is parsed using the formatter, returning a date-time.
|
|
368 |
*
|
|
369 |
* @param text the text to parse, not null
|
|
370 |
* @param formatter the formatter to use, not null
|
|
371 |
* @return the parsed offset date-time, not null
|
|
372 |
* @throws DateTimeParseException if the text cannot be parsed
|
|
373 |
*/
|
|
374 |
public static OffsetDateTime parse(CharSequence text, DateTimeFormatter formatter) {
|
|
375 |
Objects.requireNonNull(formatter, "formatter");
|
|
376 |
return formatter.parse(text, OffsetDateTime::from);
|
|
377 |
}
|
|
378 |
|
|
379 |
//-----------------------------------------------------------------------
|
|
380 |
/**
|
|
381 |
* Constructor.
|
|
382 |
*
|
|
383 |
* @param dateTime the local date-time, not null
|
|
384 |
* @param offset the zone offset, not null
|
|
385 |
*/
|
|
386 |
private OffsetDateTime(LocalDateTime dateTime, ZoneOffset offset) {
|
|
387 |
this.dateTime = Objects.requireNonNull(dateTime, "dateTime");
|
|
388 |
this.offset = Objects.requireNonNull(offset, "offset");
|
|
389 |
}
|
|
390 |
|
|
391 |
/**
|
|
392 |
* Returns a new date-time based on this one, returning {@code this} where possible.
|
|
393 |
*
|
|
394 |
* @param dateTime the date-time to create with, not null
|
|
395 |
* @param offset the zone offset to create with, not null
|
|
396 |
*/
|
|
397 |
private OffsetDateTime with(LocalDateTime dateTime, ZoneOffset offset) {
|
|
398 |
if (this.dateTime == dateTime && this.offset.equals(offset)) {
|
|
399 |
return this;
|
|
400 |
}
|
|
401 |
return new OffsetDateTime(dateTime, offset);
|
|
402 |
}
|
|
403 |
|
|
404 |
//-----------------------------------------------------------------------
|
|
405 |
/**
|
|
406 |
* Checks if the specified field is supported.
|
|
407 |
* <p>
|
|
408 |
* This checks if this date-time can be queried for the specified field.
|
|
409 |
* If false, then calling the {@link #range(TemporalField) range} and
|
|
410 |
* {@link #get(TemporalField) get} methods will throw an exception.
|
|
411 |
* <p>
|
|
412 |
* If the field is a {@link ChronoField} then the query is implemented here.
|
|
413 |
* The supported fields are:
|
|
414 |
* <ul>
|
|
415 |
* <li>{@code NANO_OF_SECOND}
|
|
416 |
* <li>{@code NANO_OF_DAY}
|
|
417 |
* <li>{@code MICRO_OF_SECOND}
|
|
418 |
* <li>{@code MICRO_OF_DAY}
|
|
419 |
* <li>{@code MILLI_OF_SECOND}
|
|
420 |
* <li>{@code MILLI_OF_DAY}
|
|
421 |
* <li>{@code SECOND_OF_MINUTE}
|
|
422 |
* <li>{@code SECOND_OF_DAY}
|
|
423 |
* <li>{@code MINUTE_OF_HOUR}
|
|
424 |
* <li>{@code MINUTE_OF_DAY}
|
|
425 |
* <li>{@code HOUR_OF_AMPM}
|
|
426 |
* <li>{@code CLOCK_HOUR_OF_AMPM}
|
|
427 |
* <li>{@code HOUR_OF_DAY}
|
|
428 |
* <li>{@code CLOCK_HOUR_OF_DAY}
|
|
429 |
* <li>{@code AMPM_OF_DAY}
|
|
430 |
* <li>{@code DAY_OF_WEEK}
|
|
431 |
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
|
|
432 |
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
|
|
433 |
* <li>{@code DAY_OF_MONTH}
|
|
434 |
* <li>{@code DAY_OF_YEAR}
|
|
435 |
* <li>{@code EPOCH_DAY}
|
|
436 |
* <li>{@code ALIGNED_WEEK_OF_MONTH}
|
|
437 |
* <li>{@code ALIGNED_WEEK_OF_YEAR}
|
|
438 |
* <li>{@code MONTH_OF_YEAR}
|
|
439 |
* <li>{@code EPOCH_MONTH}
|
|
440 |
* <li>{@code YEAR_OF_ERA}
|
|
441 |
* <li>{@code YEAR}
|
|
442 |
* <li>{@code ERA}
|
|
443 |
* <li>{@code INSTANT_SECONDS}
|
|
444 |
* <li>{@code OFFSET_SECONDS}
|
|
445 |
* </ul>
|
|
446 |
* All other {@code ChronoField} instances will return false.
|
|
447 |
* <p>
|
|
448 |
* If the field is not a {@code ChronoField}, then the result of this method
|
|
449 |
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
|
|
450 |
* passing {@code this} as the argument.
|
|
451 |
* Whether the field is supported is determined by the field.
|
|
452 |
*
|
|
453 |
* @param field the field to check, null returns false
|
|
454 |
* @return true if the field is supported on this date-time, false if not
|
|
455 |
*/
|
|
456 |
@Override
|
|
457 |
public boolean isSupported(TemporalField field) {
|
|
458 |
return field instanceof ChronoField || (field != null && field.isSupportedBy(this));
|
|
459 |
}
|
|
460 |
|
|
461 |
/**
|
|
462 |
* Gets the range of valid values for the specified field.
|
|
463 |
* <p>
|
|
464 |
* The range object expresses the minimum and maximum valid values for a field.
|
|
465 |
* This date-time is used to enhance the accuracy of the returned range.
|
|
466 |
* If it is not possible to return the range, because the field is not supported
|
|
467 |
* or for some other reason, an exception is thrown.
|
|
468 |
* <p>
|
|
469 |
* If the field is a {@link ChronoField} then the query is implemented here.
|
|
470 |
* The {@link #isSupported(TemporalField) supported fields} will return
|
|
471 |
* appropriate range instances.
|
|
472 |
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
|
|
473 |
* <p>
|
|
474 |
* If the field is not a {@code ChronoField}, then the result of this method
|
|
475 |
* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
|
|
476 |
* passing {@code this} as the argument.
|
|
477 |
* Whether the range can be obtained is determined by the field.
|
|
478 |
*
|
|
479 |
* @param field the field to query the range for, not null
|
|
480 |
* @return the range of valid values for the field, not null
|
|
481 |
* @throws DateTimeException if the range for the field cannot be obtained
|
|
482 |
*/
|
|
483 |
@Override
|
|
484 |
public ValueRange range(TemporalField field) {
|
|
485 |
if (field instanceof ChronoField) {
|
|
486 |
if (field == INSTANT_SECONDS || field == OFFSET_SECONDS) {
|
|
487 |
return field.range();
|
|
488 |
}
|
|
489 |
return dateTime.range(field);
|
|
490 |
}
|
|
491 |
return field.rangeRefinedBy(this);
|
|
492 |
}
|
|
493 |
|
|
494 |
/**
|
|
495 |
* Gets the value of the specified field from this date-time as an {@code int}.
|
|
496 |
* <p>
|
|
497 |
* This queries this date-time for the value for the specified field.
|
|
498 |
* The returned value will always be within the valid range of values for the field.
|
|
499 |
* If it is not possible to return the value, because the field is not supported
|
|
500 |
* or for some other reason, an exception is thrown.
|
|
501 |
* <p>
|
|
502 |
* If the field is a {@link ChronoField} then the query is implemented here.
|
|
503 |
* The {@link #isSupported(TemporalField) supported fields} will return valid
|
|
504 |
* values based on this date-time, except {@code NANO_OF_DAY}, {@code MICRO_OF_DAY},
|
|
505 |
* {@code EPOCH_DAY}, {@code EPOCH_MONTH} and {@code INSTANT_SECONDS} which are too
|
|
506 |
* large to fit in an {@code int} and throw a {@code DateTimeException}.
|
|
507 |
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
|
|
508 |
* <p>
|
|
509 |
* If the field is not a {@code ChronoField}, then the result of this method
|
|
510 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
|
|
511 |
* passing {@code this} as the argument. Whether the value can be obtained,
|
|
512 |
* and what the value represents, is determined by the field.
|
|
513 |
*
|
|
514 |
* @param field the field to get, not null
|
|
515 |
* @return the value for the field
|
|
516 |
* @throws DateTimeException if a value for the field cannot be obtained
|
|
517 |
* @throws ArithmeticException if numeric overflow occurs
|
|
518 |
*/
|
|
519 |
@Override
|
|
520 |
public int get(TemporalField field) {
|
|
521 |
if (field instanceof ChronoField) {
|
|
522 |
switch ((ChronoField) field) {
|
|
523 |
case INSTANT_SECONDS: throw new DateTimeException("Field too large for an int: " + field);
|
|
524 |
case OFFSET_SECONDS: return getOffset().getTotalSeconds();
|
|
525 |
}
|
|
526 |
return dateTime.get(field);
|
|
527 |
}
|
|
528 |
return Temporal.super.get(field);
|
|
529 |
}
|
|
530 |
|
|
531 |
/**
|
|
532 |
* Gets the value of the specified field from this date-time as a {@code long}.
|
|
533 |
* <p>
|
|
534 |
* This queries this date-time for the value for the specified field.
|
|
535 |
* If it is not possible to return the value, because the field is not supported
|
|
536 |
* or for some other reason, an exception is thrown.
|
|
537 |
* <p>
|
|
538 |
* If the field is a {@link ChronoField} then the query is implemented here.
|
|
539 |
* The {@link #isSupported(TemporalField) supported fields} will return valid
|
|
540 |
* values based on this date-time.
|
|
541 |
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
|
|
542 |
* <p>
|
|
543 |
* If the field is not a {@code ChronoField}, then the result of this method
|
|
544 |
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
|
|
545 |
* passing {@code this} as the argument. Whether the value can be obtained,
|
|
546 |
* and what the value represents, is determined by the field.
|
|
547 |
*
|
|
548 |
* @param field the field to get, not null
|
|
549 |
* @return the value for the field
|
|
550 |
* @throws DateTimeException if a value for the field cannot be obtained
|
|
551 |
* @throws ArithmeticException if numeric overflow occurs
|
|
552 |
*/
|
|
553 |
@Override
|
|
554 |
public long getLong(TemporalField field) {
|
|
555 |
if (field instanceof ChronoField) {
|
|
556 |
switch ((ChronoField) field) {
|
|
557 |
case INSTANT_SECONDS: return toEpochSecond();
|
|
558 |
case OFFSET_SECONDS: return getOffset().getTotalSeconds();
|
|
559 |
}
|
|
560 |
return dateTime.getLong(field);
|
|
561 |
}
|
|
562 |
return field.getFrom(this);
|
|
563 |
}
|
|
564 |
|
|
565 |
//-----------------------------------------------------------------------
|
|
566 |
/**
|
|
567 |
* Gets the zone offset, such as '+01:00'.
|
|
568 |
* <p>
|
|
569 |
* This is the offset of the local date-time from UTC/Greenwich.
|
|
570 |
*
|
|
571 |
* @return the zone offset, not null
|
|
572 |
*/
|
|
573 |
public ZoneOffset getOffset() {
|
|
574 |
return offset;
|
|
575 |
}
|
|
576 |
|
|
577 |
/**
|
|
578 |
* Returns a copy of this {@code OffsetDateTime} with the specified offset ensuring
|
|
579 |
* that the result has the same local date-time.
|
|
580 |
* <p>
|
|
581 |
* This method returns an object with the same {@code LocalDateTime} and the specified {@code ZoneOffset}.
|
|
582 |
* No calculation is needed or performed.
|
|
583 |
* For example, if this time represents {@code 2007-12-03T10:30+02:00} and the offset specified is
|
|
584 |
* {@code +03:00}, then this method will return {@code 2007-12-03T10:30+03:00}.
|
|
585 |
* <p>
|
|
586 |
* To take into account the difference between the offsets, and adjust the time fields,
|
|
587 |
* use {@link #withOffsetSameInstant}.
|
|
588 |
* <p>
|
|
589 |
* This instance is immutable and unaffected by this method call.
|
|
590 |
*
|
|
591 |
* @param offset the zone offset to change to, not null
|
|
592 |
* @return an {@code OffsetDateTime} based on this date-time with the requested offset, not null
|
|
593 |
*/
|
|
594 |
public OffsetDateTime withOffsetSameLocal(ZoneOffset offset) {
|
|
595 |
return with(dateTime, offset);
|
|
596 |
}
|
|
597 |
|
|
598 |
/**
|
|
599 |
* Returns a copy of this {@code OffsetDateTime} with the specified offset ensuring
|
|
600 |
* that the result is at the same instant.
|
|
601 |
* <p>
|
|
602 |
* This method returns an object with the specified {@code ZoneOffset} and a {@code LocalDateTime}
|
|
603 |
* adjusted by the difference between the two offsets.
|
|
604 |
* This will result in the old and new objects representing the same instant.
|
|
605 |
* This is useful for finding the local time in a different offset.
|
|
606 |
* For example, if this time represents {@code 2007-12-03T10:30+02:00} and the offset specified is
|
|
607 |
* {@code +03:00}, then this method will return {@code 2007-12-03T11:30+03:00}.
|
|
608 |
* <p>
|
|
609 |
* To change the offset without adjusting the local time use {@link #withOffsetSameLocal}.
|
|
610 |
* <p>
|
|
611 |
* This instance is immutable and unaffected by this method call.
|
|
612 |
*
|
|
613 |
* @param offset the zone offset to change to, not null
|
|
614 |
* @return an {@code OffsetDateTime} based on this date-time with the requested offset, not null
|
|
615 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
616 |
*/
|
|
617 |
public OffsetDateTime withOffsetSameInstant(ZoneOffset offset) {
|
|
618 |
if (offset.equals(this.offset)) {
|
|
619 |
return this;
|
|
620 |
}
|
|
621 |
int difference = offset.getTotalSeconds() - this.offset.getTotalSeconds();
|
|
622 |
LocalDateTime adjusted = dateTime.plusSeconds(difference);
|
|
623 |
return new OffsetDateTime(adjusted, offset);
|
|
624 |
}
|
|
625 |
|
|
626 |
//-----------------------------------------------------------------------
|
|
627 |
/**
|
|
628 |
* Gets the {@code LocalDateTime} part of this offset date-time.
|
|
629 |
* <p>
|
|
630 |
* This returns a {@code LocalDateTime} with the same year, month, day and time
|
|
631 |
* as this date-time.
|
|
632 |
*
|
|
633 |
* @return the local date-time part of this date-time, not null
|
|
634 |
*/
|
|
635 |
public LocalDateTime toLocalDateTime() {
|
|
636 |
return dateTime;
|
|
637 |
}
|
|
638 |
|
|
639 |
//-----------------------------------------------------------------------
|
|
640 |
/**
|
|
641 |
* Gets the {@code LocalDate} part of this date-time.
|
|
642 |
* <p>
|
|
643 |
* This returns a {@code LocalDate} with the same year, month and day
|
|
644 |
* as this date-time.
|
|
645 |
*
|
|
646 |
* @return the date part of this date-time, not null
|
|
647 |
*/
|
|
648 |
public LocalDate toLocalDate() {
|
|
649 |
return dateTime.toLocalDate();
|
|
650 |
}
|
|
651 |
|
|
652 |
/**
|
|
653 |
* Gets the year field.
|
|
654 |
* <p>
|
|
655 |
* This method returns the primitive {@code int} value for the year.
|
|
656 |
* <p>
|
|
657 |
* The year returned by this method is proleptic as per {@code get(YEAR)}.
|
|
658 |
* To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}.
|
|
659 |
*
|
|
660 |
* @return the year, from MIN_YEAR to MAX_YEAR
|
|
661 |
*/
|
|
662 |
public int getYear() {
|
|
663 |
return dateTime.getYear();
|
|
664 |
}
|
|
665 |
|
|
666 |
/**
|
|
667 |
* Gets the month-of-year field from 1 to 12.
|
|
668 |
* <p>
|
|
669 |
* This method returns the month as an {@code int} from 1 to 12.
|
|
670 |
* Application code is frequently clearer if the enum {@link Month}
|
|
671 |
* is used by calling {@link #getMonth()}.
|
|
672 |
*
|
|
673 |
* @return the month-of-year, from 1 to 12
|
|
674 |
* @see #getMonth()
|
|
675 |
*/
|
|
676 |
public int getMonthValue() {
|
|
677 |
return dateTime.getMonthValue();
|
|
678 |
}
|
|
679 |
|
|
680 |
/**
|
|
681 |
* Gets the month-of-year field using the {@code Month} enum.
|
|
682 |
* <p>
|
|
683 |
* This method returns the enum {@link Month} for the month.
|
|
684 |
* This avoids confusion as to what {@code int} values mean.
|
|
685 |
* If you need access to the primitive {@code int} value then the enum
|
|
686 |
* provides the {@link Month#getValue() int value}.
|
|
687 |
*
|
|
688 |
* @return the month-of-year, not null
|
|
689 |
* @see #getMonthValue()
|
|
690 |
*/
|
|
691 |
public Month getMonth() {
|
|
692 |
return dateTime.getMonth();
|
|
693 |
}
|
|
694 |
|
|
695 |
/**
|
|
696 |
* Gets the day-of-month field.
|
|
697 |
* <p>
|
|
698 |
* This method returns the primitive {@code int} value for the day-of-month.
|
|
699 |
*
|
|
700 |
* @return the day-of-month, from 1 to 31
|
|
701 |
*/
|
|
702 |
public int getDayOfMonth() {
|
|
703 |
return dateTime.getDayOfMonth();
|
|
704 |
}
|
|
705 |
|
|
706 |
/**
|
|
707 |
* Gets the day-of-year field.
|
|
708 |
* <p>
|
|
709 |
* This method returns the primitive {@code int} value for the day-of-year.
|
|
710 |
*
|
|
711 |
* @return the day-of-year, from 1 to 365, or 366 in a leap year
|
|
712 |
*/
|
|
713 |
public int getDayOfYear() {
|
|
714 |
return dateTime.getDayOfYear();
|
|
715 |
}
|
|
716 |
|
|
717 |
/**
|
|
718 |
* Gets the day-of-week field, which is an enum {@code DayOfWeek}.
|
|
719 |
* <p>
|
|
720 |
* This method returns the enum {@link java.time.DayOfWeek} for the day-of-week.
|
|
721 |
* This avoids confusion as to what {@code int} values mean.
|
|
722 |
* If you need access to the primitive {@code int} value then the enum
|
|
723 |
* provides the {@link java.time.DayOfWeek#getValue() int value}.
|
|
724 |
* <p>
|
|
725 |
* Additional information can be obtained from the {@code DayOfWeek}.
|
|
726 |
* This includes textual names of the values.
|
|
727 |
*
|
|
728 |
* @return the day-of-week, not null
|
|
729 |
*/
|
|
730 |
public DayOfWeek getDayOfWeek() {
|
|
731 |
return dateTime.getDayOfWeek();
|
|
732 |
}
|
|
733 |
|
|
734 |
//-----------------------------------------------------------------------
|
|
735 |
/**
|
|
736 |
* Gets the {@code LocalTime} part of this date-time.
|
|
737 |
* <p>
|
|
738 |
* This returns a {@code LocalTime} with the same hour, minute, second and
|
|
739 |
* nanosecond as this date-time.
|
|
740 |
*
|
|
741 |
* @return the time part of this date-time, not null
|
|
742 |
*/
|
|
743 |
public LocalTime toLocalTime() {
|
|
744 |
return dateTime.toLocalTime();
|
|
745 |
}
|
|
746 |
|
|
747 |
/**
|
|
748 |
* Gets the hour-of-day field.
|
|
749 |
*
|
|
750 |
* @return the hour-of-day, from 0 to 23
|
|
751 |
*/
|
|
752 |
public int getHour() {
|
|
753 |
return dateTime.getHour();
|
|
754 |
}
|
|
755 |
|
|
756 |
/**
|
|
757 |
* Gets the minute-of-hour field.
|
|
758 |
*
|
|
759 |
* @return the minute-of-hour, from 0 to 59
|
|
760 |
*/
|
|
761 |
public int getMinute() {
|
|
762 |
return dateTime.getMinute();
|
|
763 |
}
|
|
764 |
|
|
765 |
/**
|
|
766 |
* Gets the second-of-minute field.
|
|
767 |
*
|
|
768 |
* @return the second-of-minute, from 0 to 59
|
|
769 |
*/
|
|
770 |
public int getSecond() {
|
|
771 |
return dateTime.getSecond();
|
|
772 |
}
|
|
773 |
|
|
774 |
/**
|
|
775 |
* Gets the nano-of-second field.
|
|
776 |
*
|
|
777 |
* @return the nano-of-second, from 0 to 999,999,999
|
|
778 |
*/
|
|
779 |
public int getNano() {
|
|
780 |
return dateTime.getNano();
|
|
781 |
}
|
|
782 |
|
|
783 |
//-----------------------------------------------------------------------
|
|
784 |
/**
|
|
785 |
* Returns an adjusted copy of this date-time.
|
|
786 |
* <p>
|
|
787 |
* This returns an {@code OffsetDateTime}, based on this one, with the date-time adjusted.
|
|
788 |
* The adjustment takes place using the specified adjuster strategy object.
|
|
789 |
* Read the documentation of the adjuster to understand what adjustment will be made.
|
|
790 |
* <p>
|
|
791 |
* A simple adjuster might simply set the one of the fields, such as the year field.
|
|
792 |
* A more complex adjuster might set the date to the last day of the month.
|
|
793 |
* A selection of common adjustments is provided in {@link java.time.temporal.Adjusters}.
|
|
794 |
* These include finding the "last day of the month" and "next Wednesday".
|
|
795 |
* Key date-time classes also implement the {@code TemporalAdjuster} interface,
|
|
796 |
* such as {@link Month} and {@link java.time.MonthDay MonthDay}.
|
|
797 |
* The adjuster is responsible for handling special cases, such as the varying
|
|
798 |
* lengths of month and leap years.
|
|
799 |
* <p>
|
|
800 |
* For example this code returns a date on the last day of July:
|
|
801 |
* <pre>
|
|
802 |
* import static java.time.Month.*;
|
|
803 |
* import static java.time.temporal.Adjusters.*;
|
|
804 |
*
|
|
805 |
* result = offsetDateTime.with(JULY).with(lastDayOfMonth());
|
|
806 |
* </pre>
|
|
807 |
* <p>
|
|
808 |
* The classes {@link LocalDate}, {@link LocalTime} and {@link ZoneOffset} implement
|
|
809 |
* {@code TemporalAdjuster}, thus this method can be used to change the date, time or offset:
|
|
810 |
* <pre>
|
|
811 |
* result = offsetDateTime.with(date);
|
|
812 |
* result = offsetDateTime.with(time);
|
|
813 |
* result = offsetDateTime.with(offset);
|
|
814 |
* </pre>
|
|
815 |
* <p>
|
|
816 |
* The result of this method is obtained by invoking the
|
|
817 |
* {@link TemporalAdjuster#adjustInto(Temporal)} method on the
|
|
818 |
* specified adjuster passing {@code this} as the argument.
|
|
819 |
* <p>
|
|
820 |
* This instance is immutable and unaffected by this method call.
|
|
821 |
*
|
|
822 |
* @param adjuster the adjuster to use, not null
|
|
823 |
* @return an {@code OffsetDateTime} based on {@code this} with the adjustment made, not null
|
|
824 |
* @throws DateTimeException if the adjustment cannot be made
|
|
825 |
* @throws ArithmeticException if numeric overflow occurs
|
|
826 |
*/
|
|
827 |
@Override
|
|
828 |
public OffsetDateTime with(TemporalAdjuster adjuster) {
|
|
829 |
// optimizations
|
|
830 |
if (adjuster instanceof LocalDate || adjuster instanceof LocalTime || adjuster instanceof LocalDateTime) {
|
|
831 |
return with(dateTime.with(adjuster), offset);
|
|
832 |
} else if (adjuster instanceof Instant) {
|
|
833 |
return ofInstant((Instant) adjuster, offset);
|
|
834 |
} else if (adjuster instanceof ZoneOffset) {
|
|
835 |
return with(dateTime, (ZoneOffset) adjuster);
|
|
836 |
} else if (adjuster instanceof OffsetDateTime) {
|
|
837 |
return (OffsetDateTime) adjuster;
|
|
838 |
}
|
|
839 |
return (OffsetDateTime) adjuster.adjustInto(this);
|
|
840 |
}
|
|
841 |
|
|
842 |
/**
|
|
843 |
* Returns a copy of this date-time with the specified field set to a new value.
|
|
844 |
* <p>
|
|
845 |
* TThis returns an {@code OffsetDateTime}, based on this one, with the value
|
|
846 |
* for the specified field changed.
|
|
847 |
* This can be used to change any supported field, such as the year, month or day-of-month.
|
|
848 |
* If it is not possible to set the value, because the field is not supported or for
|
|
849 |
* some other reason, an exception is thrown.
|
|
850 |
* <p>
|
|
851 |
* In some cases, changing the specified field can cause the resulting date-time to become invalid,
|
|
852 |
* such as changing the month from 31st January to February would make the day-of-month invalid.
|
|
853 |
* In cases like this, the field is responsible for resolving the date. Typically it will choose
|
|
854 |
* the previous valid date, which would be the last valid day of February in this example.
|
|
855 |
* <p>
|
|
856 |
* If the field is a {@link ChronoField} then the adjustment is implemented here.
|
|
857 |
* <p>
|
|
858 |
* The {@code INSTANT_SECONDS} field will return a date-time with the specified instant.
|
|
859 |
* The offset and nano-of-second are unchanged.
|
|
860 |
* If the new instant value is outside the valid range then a {@code DateTimeException} will be thrown.
|
|
861 |
* <p>
|
|
862 |
* The {@code OFFSET_SECONDS} field will return a date-time with the specified offset.
|
|
863 |
* The local date-time is unaltered. If the new offset value is outside the valid range
|
|
864 |
* then a {@code DateTimeException} will be thrown.
|
|
865 |
* <p>
|
|
866 |
* The other {@link #isSupported(TemporalField) supported fields} will behave as per
|
|
867 |
* the matching method on {@link LocalDateTime#with(TemporalField, long) LocalDateTime}.
|
|
868 |
* In this case, the offset is not part of the calculation and will be unchanged.
|
|
869 |
* <p>
|
|
870 |
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
|
|
871 |
* <p>
|
|
872 |
* If the field is not a {@code ChronoField}, then the result of this method
|
|
873 |
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
|
|
874 |
* passing {@code this} as the argument. In this case, the field determines
|
|
875 |
* whether and how to adjust the instant.
|
|
876 |
* <p>
|
|
877 |
* This instance is immutable and unaffected by this method call.
|
|
878 |
*
|
|
879 |
* @param field the field to set in the result, not null
|
|
880 |
* @param newValue the new value of the field in the result
|
|
881 |
* @return an {@code OffsetDateTime} based on {@code this} with the specified field set, not null
|
|
882 |
* @throws DateTimeException if the field cannot be set
|
|
883 |
* @throws ArithmeticException if numeric overflow occurs
|
|
884 |
*/
|
|
885 |
@Override
|
|
886 |
public OffsetDateTime with(TemporalField field, long newValue) {
|
|
887 |
if (field instanceof ChronoField) {
|
|
888 |
ChronoField f = (ChronoField) field;
|
|
889 |
switch (f) {
|
|
890 |
case INSTANT_SECONDS: return ofInstant(Instant.ofEpochSecond(newValue, getNano()), offset);
|
|
891 |
case OFFSET_SECONDS: {
|
|
892 |
return with(dateTime, ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue)));
|
|
893 |
}
|
|
894 |
}
|
|
895 |
return with(dateTime.with(field, newValue), offset);
|
|
896 |
}
|
|
897 |
return field.adjustInto(this, newValue);
|
|
898 |
}
|
|
899 |
|
|
900 |
//-----------------------------------------------------------------------
|
|
901 |
/**
|
|
902 |
* Returns a copy of this {@code OffsetDateTime} with the year altered.
|
|
903 |
* The offset does not affect the calculation and will be the same in the result.
|
|
904 |
* If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
|
|
905 |
* <p>
|
|
906 |
* This instance is immutable and unaffected by this method call.
|
|
907 |
*
|
|
908 |
* @param year the year to set in the result, from MIN_YEAR to MAX_YEAR
|
|
909 |
* @return an {@code OffsetDateTime} based on this date-time with the requested year, not null
|
|
910 |
* @throws DateTimeException if the year value is invalid
|
|
911 |
*/
|
|
912 |
public OffsetDateTime withYear(int year) {
|
|
913 |
return with(dateTime.withYear(year), offset);
|
|
914 |
}
|
|
915 |
|
|
916 |
/**
|
|
917 |
* Returns a copy of this {@code OffsetDateTime} with the month-of-year altered.
|
|
918 |
* The offset does not affect the calculation and will be the same in the result.
|
|
919 |
* If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
|
|
920 |
* <p>
|
|
921 |
* This instance is immutable and unaffected by this method call.
|
|
922 |
*
|
|
923 |
* @param month the month-of-year to set in the result, from 1 (January) to 12 (December)
|
|
924 |
* @return an {@code OffsetDateTime} based on this date-time with the requested month, not null
|
|
925 |
* @throws DateTimeException if the month-of-year value is invalid
|
|
926 |
*/
|
|
927 |
public OffsetDateTime withMonth(int month) {
|
|
928 |
return with(dateTime.withMonth(month), offset);
|
|
929 |
}
|
|
930 |
|
|
931 |
/**
|
|
932 |
* Returns a copy of this {@code OffsetDateTime} with the day-of-month altered.
|
|
933 |
* If the resulting {@code OffsetDateTime} is invalid, an exception is thrown.
|
|
934 |
* The offset does not affect the calculation and will be the same in the result.
|
|
935 |
* <p>
|
|
936 |
* This instance is immutable and unaffected by this method call.
|
|
937 |
*
|
|
938 |
* @param dayOfMonth the day-of-month to set in the result, from 1 to 28-31
|
|
939 |
* @return an {@code OffsetDateTime} based on this date-time with the requested day, not null
|
|
940 |
* @throws DateTimeException if the day-of-month value is invalid,
|
|
941 |
* or if the day-of-month is invalid for the month-year
|
|
942 |
*/
|
|
943 |
public OffsetDateTime withDayOfMonth(int dayOfMonth) {
|
|
944 |
return with(dateTime.withDayOfMonth(dayOfMonth), offset);
|
|
945 |
}
|
|
946 |
|
|
947 |
/**
|
|
948 |
* Returns a copy of this {@code OffsetDateTime} with the day-of-year altered.
|
|
949 |
* If the resulting {@code OffsetDateTime} is invalid, an exception is thrown.
|
|
950 |
* <p>
|
|
951 |
* This instance is immutable and unaffected by this method call.
|
|
952 |
*
|
|
953 |
* @param dayOfYear the day-of-year to set in the result, from 1 to 365-366
|
|
954 |
* @return an {@code OffsetDateTime} based on this date with the requested day, not null
|
|
955 |
* @throws DateTimeException if the day-of-year value is invalid,
|
|
956 |
* or if the day-of-year is invalid for the year
|
|
957 |
*/
|
|
958 |
public OffsetDateTime withDayOfYear(int dayOfYear) {
|
|
959 |
return with(dateTime.withDayOfYear(dayOfYear), offset);
|
|
960 |
}
|
|
961 |
|
|
962 |
//-----------------------------------------------------------------------
|
|
963 |
/**
|
|
964 |
* Returns a copy of this {@code OffsetDateTime} with the hour-of-day value altered.
|
|
965 |
* <p>
|
|
966 |
* The offset does not affect the calculation and will be the same in the result.
|
|
967 |
* <p>
|
|
968 |
* This instance is immutable and unaffected by this method call.
|
|
969 |
*
|
|
970 |
* @param hour the hour-of-day to set in the result, from 0 to 23
|
|
971 |
* @return an {@code OffsetDateTime} based on this date-time with the requested hour, not null
|
|
972 |
* @throws DateTimeException if the hour value is invalid
|
|
973 |
*/
|
|
974 |
public OffsetDateTime withHour(int hour) {
|
|
975 |
return with(dateTime.withHour(hour), offset);
|
|
976 |
}
|
|
977 |
|
|
978 |
/**
|
|
979 |
* Returns a copy of this {@code OffsetDateTime} with the minute-of-hour value altered.
|
|
980 |
* <p>
|
|
981 |
* The offset does not affect the calculation and will be the same in the result.
|
|
982 |
* <p>
|
|
983 |
* This instance is immutable and unaffected by this method call.
|
|
984 |
*
|
|
985 |
* @param minute the minute-of-hour to set in the result, from 0 to 59
|
|
986 |
* @return an {@code OffsetDateTime} based on this date-time with the requested minute, not null
|
|
987 |
* @throws DateTimeException if the minute value is invalid
|
|
988 |
*/
|
|
989 |
public OffsetDateTime withMinute(int minute) {
|
|
990 |
return with(dateTime.withMinute(minute), offset);
|
|
991 |
}
|
|
992 |
|
|
993 |
/**
|
|
994 |
* Returns a copy of this {@code OffsetDateTime} with the second-of-minute value altered.
|
|
995 |
* <p>
|
|
996 |
* The offset does not affect the calculation and will be the same in the result.
|
|
997 |
* <p>
|
|
998 |
* This instance is immutable and unaffected by this method call.
|
|
999 |
*
|
|
1000 |
* @param second the second-of-minute to set in the result, from 0 to 59
|
|
1001 |
* @return an {@code OffsetDateTime} based on this date-time with the requested second, not null
|
|
1002 |
* @throws DateTimeException if the second value is invalid
|
|
1003 |
*/
|
|
1004 |
public OffsetDateTime withSecond(int second) {
|
|
1005 |
return with(dateTime.withSecond(second), offset);
|
|
1006 |
}
|
|
1007 |
|
|
1008 |
/**
|
|
1009 |
* Returns a copy of this {@code OffsetDateTime} with the nano-of-second value altered.
|
|
1010 |
* <p>
|
|
1011 |
* The offset does not affect the calculation and will be the same in the result.
|
|
1012 |
* <p>
|
|
1013 |
* This instance is immutable and unaffected by this method call.
|
|
1014 |
*
|
|
1015 |
* @param nanoOfSecond the nano-of-second to set in the result, from 0 to 999,999,999
|
|
1016 |
* @return an {@code OffsetDateTime} based on this date-time with the requested nanosecond, not null
|
|
1017 |
* @throws DateTimeException if the nanos value is invalid
|
|
1018 |
*/
|
|
1019 |
public OffsetDateTime withNano(int nanoOfSecond) {
|
|
1020 |
return with(dateTime.withNano(nanoOfSecond), offset);
|
|
1021 |
}
|
|
1022 |
|
|
1023 |
//-----------------------------------------------------------------------
|
|
1024 |
/**
|
|
1025 |
* Returns a copy of this {@code OffsetDateTime} with the time truncated.
|
|
1026 |
* <p>
|
|
1027 |
* Truncation returns a copy of the original date-time with fields
|
|
1028 |
* smaller than the specified unit set to zero.
|
|
1029 |
* For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
|
|
1030 |
* will set the second-of-minute and nano-of-second field to zero.
|
|
1031 |
* <p>
|
|
1032 |
* The unit must have a {@linkplain TemporalUnit#getDuration() duration}
|
|
1033 |
* that divides into the length of a standard day without remainder.
|
|
1034 |
* This includes all supplied time units on {@link ChronoUnit} and
|
|
1035 |
* {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
|
|
1036 |
* <p>
|
|
1037 |
* The offset does not affect the calculation and will be the same in the result.
|
|
1038 |
* <p>
|
|
1039 |
* This instance is immutable and unaffected by this method call.
|
|
1040 |
*
|
|
1041 |
* @param unit the unit to truncate to, not null
|
|
1042 |
* @return an {@code OffsetDateTime} based on this date-time with the time truncated, not null
|
|
1043 |
* @throws DateTimeException if unable to truncate
|
|
1044 |
*/
|
|
1045 |
public OffsetDateTime truncatedTo(TemporalUnit unit) {
|
|
1046 |
return with(dateTime.truncatedTo(unit), offset);
|
|
1047 |
}
|
|
1048 |
|
|
1049 |
//-----------------------------------------------------------------------
|
|
1050 |
/**
|
|
1051 |
* Returns a copy of this date-time with the specified amount added.
|
|
1052 |
* <p>
|
|
1053 |
* This returns an {@code OffsetDateTime}, based on this one, with the specified amount added.
|
|
1054 |
* The amount is typically {@link Period} or {@link Duration} but may be
|
|
1055 |
* any other type implementing the {@link TemporalAmount} interface.
|
|
1056 |
* <p>
|
|
1057 |
* The calculation is delegated to the amount object by calling
|
|
1058 |
* {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
|
|
1059 |
* to implement the addition in any way it wishes, however it typically
|
|
1060 |
* calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
|
|
1061 |
* of the amount implementation to determine if it can be successfully added.
|
|
1062 |
* <p>
|
|
1063 |
* This instance is immutable and unaffected by this method call.
|
|
1064 |
*
|
|
1065 |
* @param amountToAdd the amount to add, not null
|
|
1066 |
* @return an {@code OffsetDateTime} based on this date-time with the addition made, not null
|
|
1067 |
* @throws DateTimeException if the addition cannot be made
|
|
1068 |
* @throws ArithmeticException if numeric overflow occurs
|
|
1069 |
*/
|
|
1070 |
@Override
|
|
1071 |
public OffsetDateTime plus(TemporalAmount amountToAdd) {
|
|
1072 |
return (OffsetDateTime) amountToAdd.addTo(this);
|
|
1073 |
}
|
|
1074 |
|
|
1075 |
/**
|
|
1076 |
* Returns a copy of this date-time with the specified amount added.
|
|
1077 |
* <p>
|
|
1078 |
* This returns an {@code OffsetDateTime}, based on this one, with the amount
|
|
1079 |
* in terms of the unit added. If it is not possible to add the amount, because the
|
|
1080 |
* unit is not supported or for some other reason, an exception is thrown.
|
|
1081 |
* <p>
|
|
1082 |
* If the field is a {@link ChronoUnit} then the addition is implemented by
|
|
1083 |
* {@link LocalDateTime#plus(long, TemporalUnit)}.
|
|
1084 |
* The offset is not part of the calculation and will be unchanged in the result.
|
|
1085 |
* <p>
|
|
1086 |
* If the field is not a {@code ChronoUnit}, then the result of this method
|
|
1087 |
* is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
|
|
1088 |
* passing {@code this} as the argument. In this case, the unit determines
|
|
1089 |
* whether and how to perform the addition.
|
|
1090 |
* <p>
|
|
1091 |
* This instance is immutable and unaffected by this method call.
|
|
1092 |
*
|
|
1093 |
* @param amountToAdd the amount of the unit to add to the result, may be negative
|
|
1094 |
* @param unit the unit of the amount to add, not null
|
|
1095 |
* @return an {@code OffsetDateTime} based on this date-time with the specified amount added, not null
|
|
1096 |
* @throws DateTimeException if the addition cannot be made
|
|
1097 |
* @throws ArithmeticException if numeric overflow occurs
|
|
1098 |
*/
|
|
1099 |
@Override
|
|
1100 |
public OffsetDateTime plus(long amountToAdd, TemporalUnit unit) {
|
|
1101 |
if (unit instanceof ChronoUnit) {
|
|
1102 |
return with(dateTime.plus(amountToAdd, unit), offset);
|
|
1103 |
}
|
|
1104 |
return unit.addTo(this, amountToAdd);
|
|
1105 |
}
|
|
1106 |
|
|
1107 |
//-----------------------------------------------------------------------
|
|
1108 |
/**
|
|
1109 |
* Returns a copy of this {@code OffsetDateTime} with the specified period in years added.
|
|
1110 |
* <p>
|
|
1111 |
* This method adds the specified amount to the years field in three steps:
|
|
1112 |
* <ol>
|
|
1113 |
* <li>Add the input years to the year field</li>
|
|
1114 |
* <li>Check if the resulting date would be invalid</li>
|
|
1115 |
* <li>Adjust the day-of-month to the last valid day if necessary</li>
|
|
1116 |
* </ol>
|
|
1117 |
* <p>
|
|
1118 |
* For example, 2008-02-29 (leap year) plus one year would result in the
|
|
1119 |
* invalid date 2009-02-29 (standard year). Instead of returning an invalid
|
|
1120 |
* result, the last valid day of the month, 2009-02-28, is selected instead.
|
|
1121 |
* <p>
|
|
1122 |
* This instance is immutable and unaffected by this method call.
|
|
1123 |
*
|
|
1124 |
* @param years the years to add, may be negative
|
|
1125 |
* @return an {@code OffsetDateTime} based on this date-time with the years added, not null
|
|
1126 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1127 |
*/
|
|
1128 |
public OffsetDateTime plusYears(long years) {
|
|
1129 |
return with(dateTime.plusYears(years), offset);
|
|
1130 |
}
|
|
1131 |
|
|
1132 |
/**
|
|
1133 |
* Returns a copy of this {@code OffsetDateTime} with the specified period in months added.
|
|
1134 |
* <p>
|
|
1135 |
* This method adds the specified amount to the months field in three steps:
|
|
1136 |
* <ol>
|
|
1137 |
* <li>Add the input months to the month-of-year field</li>
|
|
1138 |
* <li>Check if the resulting date would be invalid</li>
|
|
1139 |
* <li>Adjust the day-of-month to the last valid day if necessary</li>
|
|
1140 |
* </ol>
|
|
1141 |
* <p>
|
|
1142 |
* For example, 2007-03-31 plus one month would result in the invalid date
|
|
1143 |
* 2007-04-31. Instead of returning an invalid result, the last valid day
|
|
1144 |
* of the month, 2007-04-30, is selected instead.
|
|
1145 |
* <p>
|
|
1146 |
* This instance is immutable and unaffected by this method call.
|
|
1147 |
*
|
|
1148 |
* @param months the months to add, may be negative
|
|
1149 |
* @return an {@code OffsetDateTime} based on this date-time with the months added, not null
|
|
1150 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1151 |
*/
|
|
1152 |
public OffsetDateTime plusMonths(long months) {
|
|
1153 |
return with(dateTime.plusMonths(months), offset);
|
|
1154 |
}
|
|
1155 |
|
|
1156 |
/**
|
|
1157 |
* Returns a copy of this OffsetDateTime with the specified period in weeks added.
|
|
1158 |
* <p>
|
|
1159 |
* This method adds the specified amount in weeks to the days field incrementing
|
|
1160 |
* the month and year fields as necessary to ensure the result remains valid.
|
|
1161 |
* The result is only invalid if the maximum/minimum year is exceeded.
|
|
1162 |
* <p>
|
|
1163 |
* For example, 2008-12-31 plus one week would result in the 2009-01-07.
|
|
1164 |
* <p>
|
|
1165 |
* This instance is immutable and unaffected by this method call.
|
|
1166 |
*
|
|
1167 |
* @param weeks the weeks to add, may be negative
|
|
1168 |
* @return an {@code OffsetDateTime} based on this date-time with the weeks added, not null
|
|
1169 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1170 |
*/
|
|
1171 |
public OffsetDateTime plusWeeks(long weeks) {
|
|
1172 |
return with(dateTime.plusWeeks(weeks), offset);
|
|
1173 |
}
|
|
1174 |
|
|
1175 |
/**
|
|
1176 |
* Returns a copy of this OffsetDateTime with the specified period in days added.
|
|
1177 |
* <p>
|
|
1178 |
* This method adds the specified amount to the days field incrementing the
|
|
1179 |
* month and year fields as necessary to ensure the result remains valid.
|
|
1180 |
* The result is only invalid if the maximum/minimum year is exceeded.
|
|
1181 |
* <p>
|
|
1182 |
* For example, 2008-12-31 plus one day would result in the 2009-01-01.
|
|
1183 |
* <p>
|
|
1184 |
* This instance is immutable and unaffected by this method call.
|
|
1185 |
*
|
|
1186 |
* @param days the days to add, may be negative
|
|
1187 |
* @return an {@code OffsetDateTime} based on this date-time with the days added, not null
|
|
1188 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1189 |
*/
|
|
1190 |
public OffsetDateTime plusDays(long days) {
|
|
1191 |
return with(dateTime.plusDays(days), offset);
|
|
1192 |
}
|
|
1193 |
|
|
1194 |
/**
|
|
1195 |
* Returns a copy of this {@code OffsetDateTime} with the specified period in hours added.
|
|
1196 |
* <p>
|
|
1197 |
* This instance is immutable and unaffected by this method call.
|
|
1198 |
*
|
|
1199 |
* @param hours the hours to add, may be negative
|
|
1200 |
* @return an {@code OffsetDateTime} based on this date-time with the hours added, not null
|
|
1201 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1202 |
*/
|
|
1203 |
public OffsetDateTime plusHours(long hours) {
|
|
1204 |
return with(dateTime.plusHours(hours), offset);
|
|
1205 |
}
|
|
1206 |
|
|
1207 |
/**
|
|
1208 |
* Returns a copy of this {@code OffsetDateTime} with the specified period in minutes added.
|
|
1209 |
* <p>
|
|
1210 |
* This instance is immutable and unaffected by this method call.
|
|
1211 |
*
|
|
1212 |
* @param minutes the minutes to add, may be negative
|
|
1213 |
* @return an {@code OffsetDateTime} based on this date-time with the minutes added, not null
|
|
1214 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1215 |
*/
|
|
1216 |
public OffsetDateTime plusMinutes(long minutes) {
|
|
1217 |
return with(dateTime.plusMinutes(minutes), offset);
|
|
1218 |
}
|
|
1219 |
|
|
1220 |
/**
|
|
1221 |
* Returns a copy of this {@code OffsetDateTime} with the specified period in seconds added.
|
|
1222 |
* <p>
|
|
1223 |
* This instance is immutable and unaffected by this method call.
|
|
1224 |
*
|
|
1225 |
* @param seconds the seconds to add, may be negative
|
|
1226 |
* @return an {@code OffsetDateTime} based on this date-time with the seconds added, not null
|
|
1227 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1228 |
*/
|
|
1229 |
public OffsetDateTime plusSeconds(long seconds) {
|
|
1230 |
return with(dateTime.plusSeconds(seconds), offset);
|
|
1231 |
}
|
|
1232 |
|
|
1233 |
/**
|
|
1234 |
* Returns a copy of this {@code OffsetDateTime} with the specified period in nanoseconds added.
|
|
1235 |
* <p>
|
|
1236 |
* This instance is immutable and unaffected by this method call.
|
|
1237 |
*
|
|
1238 |
* @param nanos the nanos to add, may be negative
|
|
1239 |
* @return an {@code OffsetDateTime} based on this date-time with the nanoseconds added, not null
|
|
1240 |
* @throws DateTimeException if the unit cannot be added to this type
|
|
1241 |
*/
|
|
1242 |
public OffsetDateTime plusNanos(long nanos) {
|
|
1243 |
return with(dateTime.plusNanos(nanos), offset);
|
|
1244 |
}
|
|
1245 |
|
|
1246 |
//-----------------------------------------------------------------------
|
|
1247 |
/**
|
|
1248 |
* Returns a copy of this date-time with the specified amount subtracted.
|
|
1249 |
* <p>
|
|
1250 |
* This returns an {@code OffsetDateTime}, based on this one, with the specified amount subtracted.
|
|
1251 |
* The amount is typically {@link Period} or {@link Duration} but may be
|
|
1252 |
* any other type implementing the {@link TemporalAmount} interface.
|
|
1253 |
* <p>
|
|
1254 |
* The calculation is delegated to the amount object by calling
|
|
1255 |
* {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
|
|
1256 |
* to implement the subtraction in any way it wishes, however it typically
|
|
1257 |
* calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
|
|
1258 |
* of the amount implementation to determine if it can be successfully subtracted.
|
|
1259 |
* <p>
|
|
1260 |
* This instance is immutable and unaffected by this method call.
|
|
1261 |
*
|
|
1262 |
* @param amountToSubtract the amount to subtract, not null
|
|
1263 |
* @return an {@code OffsetDateTime} based on this date-time with the subtraction made, not null
|
|
1264 |
* @throws DateTimeException if the subtraction cannot be made
|
|
1265 |
* @throws ArithmeticException if numeric overflow occurs
|
|
1266 |
*/
|
|
1267 |
@Override
|
|
1268 |
public OffsetDateTime minus(TemporalAmount amountToSubtract) {
|
|
1269 |
return (OffsetDateTime) amountToSubtract.subtractFrom(this);
|
|
1270 |
}
|
|
1271 |
|
|
1272 |
/**
|
|
1273 |
* Returns a copy of this date-time with the specified amount subtracted.
|
|
1274 |
* <p>
|
|
1275 |
* This returns an {@code OffsetDateTime}, based on this one, with the amount
|
|
1276 |
* in terms of the unit subtracted. If it is not possible to subtract the amount,
|
|
1277 |
* because the unit is not supported or for some other reason, an exception is thrown.
|
|
1278 |
* <p>
|
|
1279 |
* This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
|
|
1280 |
* See that method for a full description of how addition, and thus subtraction, works.
|
|
1281 |
* <p>
|
|
1282 |
* This instance is immutable and unaffected by this method call.
|
|
1283 |
*
|
|
1284 |
* @param amountToSubtract the amount of the unit to subtract from the result, may be negative
|
|
1285 |
* @param unit the unit of the amount to subtract, not null
|
|
1286 |
* @return an {@code OffsetDateTime} based on this date-time with the specified amount subtracted, not null
|
|
1287 |
* @throws DateTimeException if the subtraction cannot be made
|
|
1288 |
* @throws ArithmeticException if numeric overflow occurs
|
|
1289 |
*/
|
|
1290 |
@Override
|
|
1291 |
public OffsetDateTime minus(long amountToSubtract, TemporalUnit unit) {
|
|
1292 |
return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
|
|
1293 |
}
|
|
1294 |
|
|
1295 |
//-----------------------------------------------------------------------
|
|
1296 |
/**
|
|
1297 |
* Returns a copy of this {@code OffsetDateTime} with the specified period in years subtracted.
|
|
1298 |
* <p>
|
|
1299 |
* This method subtracts the specified amount from the years field in three steps:
|
|
1300 |
* <ol>
|
|
1301 |
* <li>Subtract the input years to the year field</li>
|
|
1302 |
* <li>Check if the resulting date would be invalid</li>
|
|
1303 |
* <li>Adjust the day-of-month to the last valid day if necessary</li>
|
|
1304 |
* </ol>
|
|
1305 |
* <p>
|
|
1306 |
* For example, 2008-02-29 (leap year) minus one year would result in the
|
|
1307 |
* invalid date 2009-02-29 (standard year). Instead of returning an invalid
|
|
1308 |
* result, the last valid day of the month, 2009-02-28, is selected instead.
|
|
1309 |
* <p>
|
|
1310 |
* This instance is immutable and unaffected by this method call.
|
|
1311 |
*
|
|
1312 |
* @param years the years to subtract, may be negative
|
|
1313 |
* @return an {@code OffsetDateTime} based on this date-time with the years subtracted, not null
|
|
1314 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1315 |
*/
|
|
1316 |
public OffsetDateTime minusYears(long years) {
|
|
1317 |
return (years == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-years));
|
|
1318 |
}
|
|
1319 |
|
|
1320 |
/**
|
|
1321 |
* Returns a copy of this {@code OffsetDateTime} with the specified period in months subtracted.
|
|
1322 |
* <p>
|
|
1323 |
* This method subtracts the specified amount from the months field in three steps:
|
|
1324 |
* <ol>
|
|
1325 |
* <li>Subtract the input months to the month-of-year field</li>
|
|
1326 |
* <li>Check if the resulting date would be invalid</li>
|
|
1327 |
* <li>Adjust the day-of-month to the last valid day if necessary</li>
|
|
1328 |
* </ol>
|
|
1329 |
* <p>
|
|
1330 |
* For example, 2007-03-31 minus one month would result in the invalid date
|
|
1331 |
* 2007-04-31. Instead of returning an invalid result, the last valid day
|
|
1332 |
* of the month, 2007-04-30, is selected instead.
|
|
1333 |
* <p>
|
|
1334 |
* This instance is immutable and unaffected by this method call.
|
|
1335 |
*
|
|
1336 |
* @param months the months to subtract, may be negative
|
|
1337 |
* @return an {@code OffsetDateTime} based on this date-time with the months subtracted, not null
|
|
1338 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1339 |
*/
|
|
1340 |
public OffsetDateTime minusMonths(long months) {
|
|
1341 |
return (months == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-months));
|
|
1342 |
}
|
|
1343 |
|
|
1344 |
/**
|
|
1345 |
* Returns a copy of this {@code OffsetDateTime} with the specified period in weeks subtracted.
|
|
1346 |
* <p>
|
|
1347 |
* This method subtracts the specified amount in weeks from the days field decrementing
|
|
1348 |
* the month and year fields as necessary to ensure the result remains valid.
|
|
1349 |
* The result is only invalid if the maximum/minimum year is exceeded.
|
|
1350 |
* <p>
|
|
1351 |
* For example, 2008-12-31 minus one week would result in the 2009-01-07.
|
|
1352 |
* <p>
|
|
1353 |
* This instance is immutable and unaffected by this method call.
|
|
1354 |
*
|
|
1355 |
* @param weeks the weeks to subtract, may be negative
|
|
1356 |
* @return an {@code OffsetDateTime} based on this date-time with the weeks subtracted, not null
|
|
1357 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1358 |
*/
|
|
1359 |
public OffsetDateTime minusWeeks(long weeks) {
|
|
1360 |
return (weeks == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeks));
|
|
1361 |
}
|
|
1362 |
|
|
1363 |
/**
|
|
1364 |
* Returns a copy of this {@code OffsetDateTime} with the specified period in days subtracted.
|
|
1365 |
* <p>
|
|
1366 |
* This method subtracts the specified amount from the days field incrementing the
|
|
1367 |
* month and year fields as necessary to ensure the result remains valid.
|
|
1368 |
* The result is only invalid if the maximum/minimum year is exceeded.
|
|
1369 |
* <p>
|
|
1370 |
* For example, 2008-12-31 minus one day would result in the 2009-01-01.
|
|
1371 |
* <p>
|
|
1372 |
* This instance is immutable and unaffected by this method call.
|
|
1373 |
*
|
|
1374 |
* @param days the days to subtract, may be negative
|
|
1375 |
* @return an {@code OffsetDateTime} based on this date-time with the days subtracted, not null
|
|
1376 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1377 |
*/
|
|
1378 |
public OffsetDateTime minusDays(long days) {
|
|
1379 |
return (days == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-days));
|
|
1380 |
}
|
|
1381 |
|
|
1382 |
/**
|
|
1383 |
* Returns a copy of this {@code OffsetDateTime} with the specified period in hours subtracted.
|
|
1384 |
* <p>
|
|
1385 |
* This instance is immutable and unaffected by this method call.
|
|
1386 |
*
|
|
1387 |
* @param hours the hours to subtract, may be negative
|
|
1388 |
* @return an {@code OffsetDateTime} based on this date-time with the hours subtracted, not null
|
|
1389 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1390 |
*/
|
|
1391 |
public OffsetDateTime minusHours(long hours) {
|
|
1392 |
return (hours == Long.MIN_VALUE ? plusHours(Long.MAX_VALUE).plusHours(1) : plusHours(-hours));
|
|
1393 |
}
|
|
1394 |
|
|
1395 |
/**
|
|
1396 |
* Returns a copy of this {@code OffsetDateTime} with the specified period in minutes subtracted.
|
|
1397 |
* <p>
|
|
1398 |
* This instance is immutable and unaffected by this method call.
|
|
1399 |
*
|
|
1400 |
* @param minutes the minutes to subtract, may be negative
|
|
1401 |
* @return an {@code OffsetDateTime} based on this date-time with the minutes subtracted, not null
|
|
1402 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1403 |
*/
|
|
1404 |
public OffsetDateTime minusMinutes(long minutes) {
|
|
1405 |
return (minutes == Long.MIN_VALUE ? plusMinutes(Long.MAX_VALUE).plusMinutes(1) : plusMinutes(-minutes));
|
|
1406 |
}
|
|
1407 |
|
|
1408 |
/**
|
|
1409 |
* Returns a copy of this {@code OffsetDateTime} with the specified period in seconds subtracted.
|
|
1410 |
* <p>
|
|
1411 |
* This instance is immutable and unaffected by this method call.
|
|
1412 |
*
|
|
1413 |
* @param seconds the seconds to subtract, may be negative
|
|
1414 |
* @return an {@code OffsetDateTime} based on this date-time with the seconds subtracted, not null
|
|
1415 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1416 |
*/
|
|
1417 |
public OffsetDateTime minusSeconds(long seconds) {
|
|
1418 |
return (seconds == Long.MIN_VALUE ? plusSeconds(Long.MAX_VALUE).plusSeconds(1) : plusSeconds(-seconds));
|
|
1419 |
}
|
|
1420 |
|
|
1421 |
/**
|
|
1422 |
* Returns a copy of this {@code OffsetDateTime} with the specified period in nanoseconds subtracted.
|
|
1423 |
* <p>
|
|
1424 |
* This instance is immutable and unaffected by this method call.
|
|
1425 |
*
|
|
1426 |
* @param nanos the nanos to subtract, may be negative
|
|
1427 |
* @return an {@code OffsetDateTime} based on this date-time with the nanoseconds subtracted, not null
|
|
1428 |
* @throws DateTimeException if the result exceeds the supported date range
|
|
1429 |
*/
|
|
1430 |
public OffsetDateTime minusNanos(long nanos) {
|
|
1431 |
return (nanos == Long.MIN_VALUE ? plusNanos(Long.MAX_VALUE).plusNanos(1) : plusNanos(-nanos));
|
|
1432 |
}
|
|
1433 |
|
|
1434 |
//-----------------------------------------------------------------------
|
|
1435 |
/**
|
|
1436 |
* Queries this date-time using the specified query.
|
|
1437 |
* <p>
|
|
1438 |
* This queries this date-time using the specified query strategy object.
|
|
1439 |
* The {@code TemporalQuery} object defines the logic to be used to
|
|
1440 |
* obtain the result. Read the documentation of the query to understand
|
|
1441 |
* what the result of this method will be.
|
|
1442 |
* <p>
|
|
1443 |
* The result of this method is obtained by invoking the
|
|
1444 |
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
|
|
1445 |
* specified query passing {@code this} as the argument.
|
|
1446 |
*
|
|
1447 |
* @param <R> the type of the result
|
|
1448 |
* @param query the query to invoke, not null
|
|
1449 |
* @return the query result, null may be returned (defined by the query)
|
|
1450 |
* @throws DateTimeException if unable to query (defined by the query)
|
|
1451 |
* @throws ArithmeticException if numeric overflow occurs (defined by the query)
|
|
1452 |
*/
|
|
1453 |
@SuppressWarnings("unchecked")
|
|
1454 |
@Override
|
|
1455 |
public <R> R query(TemporalQuery<R> query) {
|
|
1456 |
if (query == Queries.offset() || query == Queries.zone()) {
|
|
1457 |
return (R) getOffset();
|
|
1458 |
} else if (query == Queries.zoneId()) {
|
|
1459 |
return null;
|
|
1460 |
} else if (query == Queries.localDate()) {
|
|
1461 |
return (R) toLocalDate();
|
|
1462 |
} else if (query == Queries.localTime()) {
|
|
1463 |
return (R) toLocalTime();
|
|
1464 |
} else if (query == Queries.chronology()) {
|
|
1465 |
return (R) IsoChronology.INSTANCE;
|
|
1466 |
} else if (query == Queries.precision()) {
|
|
1467 |
return (R) NANOS;
|
|
1468 |
}
|
|
1469 |
// inline TemporalAccessor.super.query(query) as an optimization
|
|
1470 |
// non-JDK classes are not permitted to make this optimization
|
|
1471 |
return query.queryFrom(this);
|
|
1472 |
}
|
|
1473 |
|
|
1474 |
/**
|
|
1475 |
* Adjusts the specified temporal object to have the same offset, date
|
|
1476 |
* and time as this object.
|
|
1477 |
* <p>
|
|
1478 |
* This returns a temporal object of the same observable type as the input
|
|
1479 |
* with the offset, date and time changed to be the same as this.
|
|
1480 |
* <p>
|
|
1481 |
* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
|
|
1482 |
* three times, passing {@link ChronoField#OFFSET_SECONDS},
|
|
1483 |
* {@link ChronoField#EPOCH_DAY} and {@link ChronoField#NANO_OF_DAY} as the fields.
|
|
1484 |
* <p>
|
|
1485 |
* In most cases, it is clearer to reverse the calling pattern by using
|
|
1486 |
* {@link Temporal#with(TemporalAdjuster)}:
|
|
1487 |
* <pre>
|
|
1488 |
* // these two lines are equivalent, but the second approach is recommended
|
|
1489 |
* temporal = thisOffsetDateTime.adjustInto(temporal);
|
|
1490 |
* temporal = temporal.with(thisOffsetDateTime);
|
|
1491 |
* </pre>
|
|
1492 |
* <p>
|
|
1493 |
* This instance is immutable and unaffected by this method call.
|
|
1494 |
*
|
|
1495 |
* @param temporal the target object to be adjusted, not null
|
|
1496 |
* @return the adjusted object, not null
|
|
1497 |
* @throws DateTimeException if unable to make the adjustment
|
|
1498 |
* @throws ArithmeticException if numeric overflow occurs
|
|
1499 |
*/
|
|
1500 |
@Override
|
|
1501 |
public Temporal adjustInto(Temporal temporal) {
|
|
1502 |
return temporal
|
|
1503 |
.with(OFFSET_SECONDS, getOffset().getTotalSeconds())
|
|
1504 |
.with(EPOCH_DAY, toLocalDate().toEpochDay())
|
|
1505 |
.with(NANO_OF_DAY, toLocalTime().toNanoOfDay());
|
|
1506 |
}
|
|
1507 |
|
|
1508 |
/**
|
|
1509 |
* Calculates the period between this date-time and another date-time in
|
|
1510 |
* terms of the specified unit.
|
|
1511 |
* <p>
|
|
1512 |
* This calculates the period between two date-times in terms of a single unit.
|
|
1513 |
* The start and end points are {@code this} and the specified date-time.
|
|
1514 |
* The result will be negative if the end is before the start.
|
|
1515 |
* For example, the period in days between two date-times can be calculated
|
|
1516 |
* using {@code startDateTime.periodUntil(endDateTime, DAYS)}.
|
|
1517 |
* <p>
|
|
1518 |
* The {@code Temporal} passed to this method must be an {@code OffsetDateTime}.
|
|
1519 |
* If the offset differs between the two date-times, the specified
|
|
1520 |
* end date-time is normalized to have the same offset as this date-time.
|
|
1521 |
* <p>
|
|
1522 |
* The calculation returns a whole number, representing the number of
|
|
1523 |
* complete units between the two date-times.
|
|
1524 |
* For example, the period in months between 2012-06-15T00:00Z and 2012-08-14T23:59Z
|
|
1525 |
* will only be one month as it is one minute short of two months.
|
|
1526 |
* <p>
|
|
1527 |
* There are two equivalent ways of using this method.
|
|
1528 |
* The first is to invoke this method.
|
|
1529 |
* The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
|
|
1530 |
* <pre>
|
|
1531 |
* // these two lines are equivalent
|
|
1532 |
* amount = start.periodUntil(end, MONTHS);
|
|
1533 |
* amount = MONTHS.between(start, end);
|
|
1534 |
* </pre>
|
|
1535 |
* The choice should be made based on which makes the code more readable.
|
|
1536 |
* <p>
|
|
1537 |
* The calculation is implemented in this method for {@link ChronoUnit}.
|
|
1538 |
* The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
|
|
1539 |
* {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS},
|
|
1540 |
* {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES},
|
|
1541 |
* {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported.
|
|
1542 |
* Other {@code ChronoUnit} values will throw an exception.
|
|
1543 |
* <p>
|
|
1544 |
* If the unit is not a {@code ChronoUnit}, then the result of this method
|
|
1545 |
* is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
|
|
1546 |
* passing {@code this} as the first argument and the input temporal as
|
|
1547 |
* the second argument.
|
|
1548 |
* <p>
|
|
1549 |
* This instance is immutable and unaffected by this method call.
|
|
1550 |
*
|
|
1551 |
* @param endDateTime the end date-time, which must be an {@code OffsetDateTime}, not null
|
|
1552 |
* @param unit the unit to measure the period in, not null
|
|
1553 |
* @return the amount of the period between this date-time and the end date-time
|
|
1554 |
* @throws DateTimeException if the period cannot be calculated
|
|
1555 |
* @throws ArithmeticException if numeric overflow occurs
|
|
1556 |
*/
|
|
1557 |
@Override
|
|
1558 |
public long periodUntil(Temporal endDateTime, TemporalUnit unit) {
|
|
1559 |
if (endDateTime instanceof OffsetDateTime == false) {
|
|
1560 |
Objects.requireNonNull(endDateTime, "endDateTime");
|
|
1561 |
throw new DateTimeException("Unable to calculate period between objects of two different types");
|
|
1562 |
}
|
|
1563 |
if (unit instanceof ChronoUnit) {
|
|
1564 |
OffsetDateTime end = (OffsetDateTime) endDateTime;
|
|
1565 |
end = end.withOffsetSameInstant(offset);
|
|
1566 |
return dateTime.periodUntil(end.dateTime, unit);
|
|
1567 |
}
|
|
1568 |
return unit.between(this, endDateTime);
|
|
1569 |
}
|
|
1570 |
|
|
1571 |
//-----------------------------------------------------------------------
|
|
1572 |
/**
|
|
1573 |
* Combines this date-time with a time-zone to create a {@code ZonedDateTime}
|
|
1574 |
* ensuring that the result has the same instant.
|
|
1575 |
* <p>
|
|
1576 |
* This returns a {@code ZonedDateTime} formed from this date-time and the specified time-zone.
|
|
1577 |
* This conversion will ignore the visible local date-time and use the underlying instant instead.
|
|
1578 |
* This avoids any problems with local time-line gaps or overlaps.
|
|
1579 |
* The result might have different values for fields such as hour, minute an even day.
|
|
1580 |
* <p>
|
|
1581 |
* To attempt to retain the values of the fields, use {@link #atZoneSimilarLocal(ZoneId)}.
|
|
1582 |
* To use the offset as the zone ID, use {@link #toZonedDateTime()}.
|
|
1583 |
*
|
|
1584 |
* @param zone the time-zone to use, not null
|
|
1585 |
* @return the zoned date-time formed from this date-time, not null
|
|
1586 |
*/
|
|
1587 |
public ZonedDateTime atZoneSameInstant(ZoneId zone) {
|
|
1588 |
return ZonedDateTime.ofInstant(dateTime, offset, zone);
|
|
1589 |
}
|
|
1590 |
|
|
1591 |
/**
|
|
1592 |
* Combines this date-time with a time-zone to create a {@code ZonedDateTime}
|
|
1593 |
* trying to keep the same local date and time.
|
|
1594 |
* <p>
|
|
1595 |
* This returns a {@code ZonedDateTime} formed from this date-time and the specified time-zone.
|
|
1596 |
* Where possible, the result will have the same local date-time as this object.
|
|
1597 |
* <p>
|
|
1598 |
* Time-zone rules, such as daylight savings, mean that not every time on the
|
|
1599 |
* local time-line exists. If the local date-time is in a gap or overlap according to
|
|
1600 |
* the rules then a resolver is used to determine the resultant local time and offset.
|
|
1601 |
* This method uses {@link ZonedDateTime#ofLocal(LocalDateTime, ZoneId, ZoneOffset)}
|
|
1602 |
* to retain the offset from this instance if possible.
|
|
1603 |
* <p>
|
|
1604 |
* Finer control over gaps and overlaps is available in two ways.
|
|
1605 |
* If you simply want to use the later offset at overlaps then call
|
|
1606 |
* {@link ZonedDateTime#withLaterOffsetAtOverlap()} immediately after this method.
|
|
1607 |
* <p>
|
|
1608 |
* To create a zoned date-time at the same instant irrespective of the local time-line,
|
|
1609 |
* use {@link #atZoneSameInstant(ZoneId)}.
|
|
1610 |
* To use the offset as the zone ID, use {@link #toZonedDateTime()}.
|
|
1611 |
*
|
|
1612 |
* @param zone the time-zone to use, not null
|
|
1613 |
* @return the zoned date-time formed from this date and the earliest valid time for the zone, not null
|
|
1614 |
*/
|
|
1615 |
public ZonedDateTime atZoneSimilarLocal(ZoneId zone) {
|
|
1616 |
return ZonedDateTime.ofLocal(dateTime, zone, offset);
|
|
1617 |
}
|
|
1618 |
|
|
1619 |
//-----------------------------------------------------------------------
|
|
1620 |
/**
|
|
1621 |
* Converts this date-time to an {@code OffsetTime}.
|
|
1622 |
* <p>
|
|
1623 |
* This returns an offset time with the same local time and offset.
|
|
1624 |
*
|
|
1625 |
* @return an OffsetTime representing the time and offset, not null
|
|
1626 |
*/
|
|
1627 |
public OffsetTime toOffsetTime() {
|
|
1628 |
return OffsetTime.of(dateTime.toLocalTime(), offset);
|
|
1629 |
}
|
|
1630 |
|
|
1631 |
/**
|
|
1632 |
* Converts this date-time to a {@code ZonedDateTime} using the offset as the zone ID.
|
|
1633 |
* <p>
|
|
1634 |
* This creates the simplest possible {@code ZonedDateTime} using the offset
|
|
1635 |
* as the zone ID.
|
|
1636 |
* <p>
|
|
1637 |
* To control the time-zone used, see {@link #atZoneSameInstant(ZoneId)} and
|
|
1638 |
* {@link #atZoneSimilarLocal(ZoneId)}.
|
|
1639 |
*
|
|
1640 |
* @return a zoned date-time representing the same local date-time and offset, not null
|
|
1641 |
*/
|
|
1642 |
public ZonedDateTime toZonedDateTime() {
|
|
1643 |
return ZonedDateTime.of(dateTime, offset);
|
|
1644 |
}
|
|
1645 |
|
|
1646 |
/**
|
|
1647 |
* Converts this date-time to an {@code Instant}.
|
|
1648 |
* <p>
|
|
1649 |
* This returns an {@code Instant} representing the same point on the
|
|
1650 |
* time-line as this date-time.
|
|
1651 |
*
|
|
1652 |
* @return an {@code Instant} representing the same instant, not null
|
|
1653 |
*/
|
|
1654 |
public Instant toInstant() {
|
|
1655 |
return dateTime.toInstant(offset);
|
|
1656 |
}
|
|
1657 |
|
|
1658 |
/**
|
|
1659 |
* Converts this date-time to the number of seconds from the epoch of 1970-01-01T00:00:00Z.
|
|
1660 |
* <p>
|
|
1661 |
* This allows this date-time to be converted to a value of the
|
|
1662 |
* {@link ChronoField#INSTANT_SECONDS epoch-seconds} field. This is primarily
|
|
1663 |
* intended for low-level conversions rather than general application usage.
|
|
1664 |
*
|
|
1665 |
* @return the number of seconds from the epoch of 1970-01-01T00:00:00Z
|
|
1666 |
*/
|
|
1667 |
public long toEpochSecond() {
|
|
1668 |
return dateTime.toEpochSecond(offset);
|
|
1669 |
}
|
|
1670 |
|
|
1671 |
//-----------------------------------------------------------------------
|
|
1672 |
/**
|
|
1673 |
* Compares this {@code OffsetDateTime} to another date-time.
|
|
1674 |
* <p>
|
|
1675 |
* The comparison is based on the instant then on the local date-time.
|
|
1676 |
* It is "consistent with equals", as defined by {@link Comparable}.
|
|
1677 |
* <p>
|
|
1678 |
* For example, the following is the comparator order:
|
|
1679 |
* <ol>
|
|
1680 |
* <li>{@code 2008-12-03T10:30+01:00}</li>
|
|
1681 |
* <li>{@code 2008-12-03T11:00+01:00}</li>
|
|
1682 |
* <li>{@code 2008-12-03T12:00+02:00}</li>
|
|
1683 |
* <li>{@code 2008-12-03T11:30+01:00}</li>
|
|
1684 |
* <li>{@code 2008-12-03T12:00+01:00}</li>
|
|
1685 |
* <li>{@code 2008-12-03T12:30+01:00}</li>
|
|
1686 |
* </ol>
|
|
1687 |
* Values #2 and #3 represent the same instant on the time-line.
|
|
1688 |
* When two values represent the same instant, the local date-time is compared
|
|
1689 |
* to distinguish them. This step is needed to make the ordering
|
|
1690 |
* consistent with {@code equals()}.
|
|
1691 |
*
|
|
1692 |
* @param other the other date-time to compare to, not null
|
|
1693 |
* @return the comparator value, negative if less, positive if greater
|
|
1694 |
*/
|
|
1695 |
@Override
|
|
1696 |
public int compareTo(OffsetDateTime other) {
|
|
1697 |
if (getOffset().equals(other.getOffset())) {
|
|
1698 |
return toLocalDateTime().compareTo(other.toLocalDateTime());
|
|
1699 |
}
|
|
1700 |
int cmp = Long.compare(toEpochSecond(), other.toEpochSecond());
|
|
1701 |
if (cmp == 0) {
|
|
1702 |
cmp = toLocalTime().getNano() - other.toLocalTime().getNano();
|
|
1703 |
if (cmp == 0) {
|
|
1704 |
cmp = toLocalDateTime().compareTo(other.toLocalDateTime());
|
|
1705 |
}
|
|
1706 |
}
|
|
1707 |
return cmp;
|
|
1708 |
}
|
|
1709 |
|
|
1710 |
//-----------------------------------------------------------------------
|
|
1711 |
/**
|
|
1712 |
* Checks if the instant of this date-time is after that of the specified date-time.
|
|
1713 |
* <p>
|
|
1714 |
* This method differs from the comparison in {@link #compareTo} and {@link #equals} in that it
|
|
1715 |
* only compares the instant of the date-time. This is equivalent to using
|
|
1716 |
* {@code dateTime1.toInstant().isAfter(dateTime2.toInstant());}.
|
|
1717 |
*
|
|
1718 |
* @param other the other date-time to compare to, not null
|
|
1719 |
* @return true if this is after the instant of the specified date-time
|
|
1720 |
*/
|
|
1721 |
public boolean isAfter(OffsetDateTime other) {
|
|
1722 |
long thisEpochSec = toEpochSecond();
|
|
1723 |
long otherEpochSec = other.toEpochSecond();
|
|
1724 |
return thisEpochSec > otherEpochSec ||
|
|
1725 |
(thisEpochSec == otherEpochSec && toLocalTime().getNano() > other.toLocalTime().getNano());
|
|
1726 |
}
|
|
1727 |
|
|
1728 |
/**
|
|
1729 |
* Checks if the instant of this date-time is before that of the specified date-time.
|
|
1730 |
* <p>
|
|
1731 |
* This method differs from the comparison in {@link #compareTo} in that it
|
|
1732 |
* only compares the instant of the date-time. This is equivalent to using
|
|
1733 |
* {@code dateTime1.toInstant().isBefore(dateTime2.toInstant());}.
|
|
1734 |
*
|
|
1735 |
* @param other the other date-time to compare to, not null
|
|
1736 |
* @return true if this is before the instant of the specified date-time
|
|
1737 |
*/
|
|
1738 |
public boolean isBefore(OffsetDateTime other) {
|
|
1739 |
long thisEpochSec = toEpochSecond();
|
|
1740 |
long otherEpochSec = other.toEpochSecond();
|
|
1741 |
return thisEpochSec < otherEpochSec ||
|
|
1742 |
(thisEpochSec == otherEpochSec && toLocalTime().getNano() < other.toLocalTime().getNano());
|
|
1743 |
}
|
|
1744 |
|
|
1745 |
/**
|
|
1746 |
* Checks if the instant of this date-time is equal to that of the specified date-time.
|
|
1747 |
* <p>
|
|
1748 |
* This method differs from the comparison in {@link #compareTo} and {@link #equals}
|
|
1749 |
* in that it only compares the instant of the date-time. This is equivalent to using
|
|
1750 |
* {@code dateTime1.toInstant().equals(dateTime2.toInstant());}.
|
|
1751 |
*
|
|
1752 |
* @param other the other date-time to compare to, not null
|
|
1753 |
* @return true if the instant equals the instant of the specified date-time
|
|
1754 |
*/
|
|
1755 |
public boolean isEqual(OffsetDateTime other) {
|
|
1756 |
return toEpochSecond() == other.toEpochSecond() &&
|
|
1757 |
toLocalTime().getNano() == other.toLocalTime().getNano();
|
|
1758 |
}
|
|
1759 |
|
|
1760 |
//-----------------------------------------------------------------------
|
|
1761 |
/**
|
|
1762 |
* Checks if this date-time is equal to another date-time.
|
|
1763 |
* <p>
|
|
1764 |
* The comparison is based on the local date-time and the offset.
|
|
1765 |
* To compare for the same instant on the time-line, use {@link #isEqual}.
|
|
1766 |
* Only objects of type {@code OffsetDateTime} are compared, other types return false.
|
|
1767 |
*
|
|
1768 |
* @param obj the object to check, null returns false
|
|
1769 |
* @return true if this is equal to the other date-time
|
|
1770 |
*/
|
|
1771 |
@Override
|
|
1772 |
public boolean equals(Object obj) {
|
|
1773 |
if (this == obj) {
|
|
1774 |
return true;
|
|
1775 |
}
|
|
1776 |
if (obj instanceof OffsetDateTime) {
|
|
1777 |
OffsetDateTime other = (OffsetDateTime) obj;
|
|
1778 |
return dateTime.equals(other.dateTime) && offset.equals(other.offset);
|
|
1779 |
}
|
|
1780 |
return false;
|
|
1781 |
}
|
|
1782 |
|
|
1783 |
/**
|
|
1784 |
* A hash code for this date-time.
|
|
1785 |
*
|
|
1786 |
* @return a suitable hash code
|
|
1787 |
*/
|
|
1788 |
@Override
|
|
1789 |
public int hashCode() {
|
|
1790 |
return dateTime.hashCode() ^ offset.hashCode();
|
|
1791 |
}
|
|
1792 |
|
|
1793 |
//-----------------------------------------------------------------------
|
|
1794 |
/**
|
|
1795 |
* Outputs this date-time as a {@code String}, such as {@code 2007-12-03T10:15:30+01:00}.
|
|
1796 |
* <p>
|
|
1797 |
* The output will be one of the following ISO-8601 formats:
|
|
1798 |
* <p><ul>
|
|
1799 |
* <li>{@code yyyy-MM-dd'T'HH:mmXXXXX}</li>
|
|
1800 |
* <li>{@code yyyy-MM-dd'T'HH:mm:ssXXXXX}</li>
|
|
1801 |
* <li>{@code yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX}</li>
|
|
1802 |
* <li>{@code yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXXXX}</li>
|
|
1803 |
* <li>{@code yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSXXXXX}</li>
|
|
1804 |
* </ul><p>
|
|
1805 |
* The format used will be the shortest that outputs the full value of
|
|
1806 |
* the time where the omitted parts are implied to be zero.
|
|
1807 |
*
|
|
1808 |
* @return a string representation of this date-time, not null
|
|
1809 |
*/
|
|
1810 |
@Override
|
|
1811 |
public String toString() {
|
|
1812 |
return dateTime.toString() + offset.toString();
|
|
1813 |
}
|
|
1814 |
|
|
1815 |
/**
|
|
1816 |
* Outputs this date-time as a {@code String} using the formatter.
|
|
1817 |
* <p>
|
|
1818 |
* This date-time will be passed to the formatter
|
|
1819 |
* {@link DateTimeFormatter#format(TemporalAccessor) format method}.
|
|
1820 |
*
|
|
1821 |
* @param formatter the formatter to use, not null
|
|
1822 |
* @return the formatted date-time string, not null
|
|
1823 |
* @throws DateTimeException if an error occurs during printing
|
|
1824 |
*/
|
|
1825 |
public String toString(DateTimeFormatter formatter) {
|
|
1826 |
Objects.requireNonNull(formatter, "formatter");
|
|
1827 |
return formatter.format(this);
|
|
1828 |
}
|
|
1829 |
|
|
1830 |
//-----------------------------------------------------------------------
|
|
1831 |
/**
|
|
1832 |
* Writes the object using a
|
|
1833 |
* <a href="../../../serialized-form.html#java.time.temporal.Ser">dedicated serialized form</a>.
|
|
1834 |
* <pre>
|
|
1835 |
* out.writeByte(10); // identifies this as a OffsetDateTime
|
|
1836 |
* out.writeObject(dateTime);
|
|
1837 |
* out.writeObject(offset);
|
|
1838 |
* </pre>
|
|
1839 |
*
|
|
1840 |
* @return the instance of {@code Ser}, not null
|
|
1841 |
*/
|
|
1842 |
private Object writeReplace() {
|
|
1843 |
return new Ser(Ser.OFFSET_DATE_TIME_TYPE, this);
|
|
1844 |
}
|
|
1845 |
|
|
1846 |
/**
|
|
1847 |
* Defend against malicious streams.
|
|
1848 |
* @return never
|
|
1849 |
* @throws InvalidObjectException always
|
|
1850 |
*/
|
|
1851 |
private Object readResolve() throws ObjectStreamException {
|
|
1852 |
throw new InvalidObjectException("Deserialization via serialization delegate");
|
|
1853 |
}
|
|
1854 |
|
|
1855 |
void writeExternal(ObjectOutput out) throws IOException {
|
|
1856 |
out.writeObject(dateTime);
|
|
1857 |
out.writeObject(offset);
|
|
1858 |
}
|
|
1859 |
|
|
1860 |
static OffsetDateTime readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
|
|
1861 |
LocalDateTime dateTime = (LocalDateTime) in.readObject();
|
|
1862 |
ZoneOffset offset = (ZoneOffset) in.readObject();
|
|
1863 |
return OffsetDateTime.of(dateTime, offset);
|
|
1864 |
}
|
|
1865 |
|
|
1866 |
}
|