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