author | rriggs |
Tue, 15 Oct 2013 13:14:03 -0400 | |
changeset 20795 | 8ec9e5b79828 |
parent 20794 | ec823009c5f7 |
child 24256 | da9a41004459 |
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.chrono; |
|
63 |
||
64 |
import static java.time.temporal.ChronoField.EPOCH_DAY; |
|
65 |
import static java.time.temporal.ChronoField.NANO_OF_DAY; |
|
19030 | 66 |
import static java.time.temporal.ChronoUnit.FOREVER; |
15658 | 67 |
import static java.time.temporal.ChronoUnit.NANOS; |
68 |
||
69 |
import java.time.DateTimeException; |
|
70 |
import java.time.Instant; |
|
71 |
import java.time.LocalDateTime; |
|
72 |
import java.time.LocalTime; |
|
73 |
import java.time.ZoneId; |
|
74 |
import java.time.ZoneOffset; |
|
75 |
import java.time.format.DateTimeFormatter; |
|
76 |
import java.time.temporal.ChronoField; |
|
19030 | 77 |
import java.time.temporal.ChronoUnit; |
15658 | 78 |
import java.time.temporal.Temporal; |
79 |
import java.time.temporal.TemporalAccessor; |
|
80 |
import java.time.temporal.TemporalAdjuster; |
|
81 |
import java.time.temporal.TemporalAmount; |
|
82 |
import java.time.temporal.TemporalField; |
|
20795 | 83 |
import java.time.temporal.TemporalQueries; |
15658 | 84 |
import java.time.temporal.TemporalQuery; |
85 |
import java.time.temporal.TemporalUnit; |
|
86 |
import java.time.zone.ZoneRules; |
|
87 |
import java.util.Comparator; |
|
88 |
import java.util.Objects; |
|
89 |
||
90 |
/** |
|
91 |
* A date-time without a time-zone in an arbitrary chronology, intended |
|
92 |
* for advanced globalization use cases. |
|
93 |
* <p> |
|
94 |
* <b>Most applications should declare method signatures, fields and variables |
|
95 |
* as {@link LocalDateTime}, not this interface.</b> |
|
96 |
* <p> |
|
97 |
* A {@code ChronoLocalDateTime} is the abstract representation of a local date-time |
|
98 |
* where the {@code Chronology chronology}, or calendar system, is pluggable. |
|
99 |
* The date-time is defined in terms of fields expressed by {@link TemporalField}, |
|
100 |
* where most common implementations are defined in {@link ChronoField}. |
|
101 |
* The chronology defines how the calendar system operates and the meaning of |
|
102 |
* the standard fields. |
|
103 |
* |
|
104 |
* <h3>When to use this interface</h3> |
|
105 |
* The design of the API encourages the use of {@code LocalDateTime} rather than this |
|
106 |
* interface, even in the case where the application needs to deal with multiple |
|
107 |
* calendar systems. The rationale for this is explored in detail in {@link ChronoLocalDate}. |
|
108 |
* <p> |
|
109 |
* Ensure that the discussion in {@code ChronoLocalDate} has been read and understood |
|
110 |
* before using this interface. |
|
111 |
* |
|
17474 | 112 |
* @implSpec |
15658 | 113 |
* This interface must be implemented with care to ensure other classes operate correctly. |
114 |
* All implementations that can be instantiated must be final, immutable and thread-safe. |
|
115 |
* Subclasses should be Serializable wherever possible. |
|
116 |
* |
|
117 |
* @param <D> the concrete type for the date of this date-time |
|
118 |
* @since 1.8 |
|
119 |
*/ |
|
19030 | 120 |
public interface ChronoLocalDateTime<D extends ChronoLocalDate> |
15658 | 121 |
extends Temporal, TemporalAdjuster, Comparable<ChronoLocalDateTime<?>> { |
122 |
||
123 |
/** |
|
16852 | 124 |
* Gets a comparator that compares {@code ChronoLocalDateTime} in |
125 |
* time-line order ignoring the chronology. |
|
15658 | 126 |
* <p> |
16852 | 127 |
* This comparator differs from the comparison in {@link #compareTo} in that it |
128 |
* only compares the underlying date-time and not the chronology. |
|
15658 | 129 |
* This allows dates in different calendar systems to be compared based |
16852 | 130 |
* on the position of the date-time on the local time-line. |
131 |
* The underlying comparison is equivalent to comparing the epoch-day and nano-of-day. |
|
15658 | 132 |
* |
17474 | 133 |
* @return a comparator that compares in time-line order ignoring the chronology |
134 |
* |
|
15658 | 135 |
* @see #isAfter |
136 |
* @see #isBefore |
|
137 |
* @see #isEqual |
|
138 |
*/ |
|
16852 | 139 |
static Comparator<ChronoLocalDateTime<?>> timeLineOrder() { |
20794 | 140 |
return AbstractChronology.DATE_TIME_ORDER; |
16852 | 141 |
} |
142 |
||
143 |
//----------------------------------------------------------------------- |
|
144 |
/** |
|
145 |
* Obtains an instance of {@code ChronoLocalDateTime} from a temporal object. |
|
146 |
* <p> |
|
147 |
* This obtains a local date-time based on the specified temporal. |
|
148 |
* A {@code TemporalAccessor} represents an arbitrary set of date and time information, |
|
149 |
* which this factory converts to an instance of {@code ChronoLocalDateTime}. |
|
150 |
* <p> |
|
151 |
* The conversion extracts and combines the chronology and the date-time |
|
152 |
* from the temporal object. The behavior is equivalent to using |
|
153 |
* {@link Chronology#localDateTime(TemporalAccessor)} with the extracted chronology. |
|
154 |
* Implementations are permitted to perform optimizations such as accessing |
|
155 |
* those fields that are equivalent to the relevant objects. |
|
156 |
* <p> |
|
157 |
* This method matches the signature of the functional interface {@link TemporalQuery} |
|
158 |
* allowing it to be used as a query via method reference, {@code ChronoLocalDateTime::from}. |
|
159 |
* |
|
160 |
* @param temporal the temporal object to convert, not null |
|
161 |
* @return the date-time, not null |
|
162 |
* @throws DateTimeException if unable to convert to a {@code ChronoLocalDateTime} |
|
163 |
* @see Chronology#localDateTime(TemporalAccessor) |
|
164 |
*/ |
|
165 |
static ChronoLocalDateTime<?> from(TemporalAccessor temporal) { |
|
166 |
if (temporal instanceof ChronoLocalDateTime) { |
|
167 |
return (ChronoLocalDateTime<?>) temporal; |
|
15658 | 168 |
} |
20520
0952771e3e25
8024835: Change until() to accept any compatible temporal
rriggs
parents:
19030
diff
changeset
|
169 |
Objects.requireNonNull(temporal, "temporal"); |
20795 | 170 |
Chronology chrono = temporal.query(TemporalQueries.chronology()); |
16852 | 171 |
if (chrono == null) { |
172 |
throw new DateTimeException("Unable to obtain ChronoLocalDateTime from TemporalAccessor: " + temporal.getClass()); |
|
173 |
} |
|
174 |
return chrono.localDateTime(temporal); |
|
175 |
} |
|
15658 | 176 |
|
16852 | 177 |
//----------------------------------------------------------------------- |
15658 | 178 |
/** |
20522 | 179 |
* Gets the chronology of this date-time. |
180 |
* <p> |
|
181 |
* The {@code Chronology} represents the calendar system in use. |
|
182 |
* The era and other fields in {@link ChronoField} are defined by the chronology. |
|
183 |
* |
|
184 |
* @return the chronology, not null |
|
185 |
*/ |
|
186 |
default Chronology getChronology() { |
|
187 |
return toLocalDate().getChronology(); |
|
188 |
} |
|
189 |
||
190 |
/** |
|
15658 | 191 |
* Gets the local date part of this date-time. |
192 |
* <p> |
|
193 |
* This returns a local date with the same year, month and day |
|
194 |
* as this date-time. |
|
195 |
* |
|
196 |
* @return the date part of this date-time, not null |
|
197 |
*/ |
|
198 |
D toLocalDate() ; |
|
199 |
||
200 |
/** |
|
201 |
* Gets the local time part of this date-time. |
|
202 |
* <p> |
|
203 |
* This returns a local time with the same hour, minute, second and |
|
204 |
* nanosecond as this date-time. |
|
205 |
* |
|
206 |
* @return the time part of this date-time, not null |
|
207 |
*/ |
|
208 |
LocalTime toLocalTime(); |
|
209 |
||
19030 | 210 |
/** |
211 |
* Checks if the specified field is supported. |
|
212 |
* <p> |
|
213 |
* This checks if the specified field can be queried on this date-time. |
|
214 |
* If false, then calling the {@link #range(TemporalField) range}, |
|
215 |
* {@link #get(TemporalField) get} and {@link #with(TemporalField, long)} |
|
216 |
* methods will throw an exception. |
|
217 |
* <p> |
|
218 |
* The set of supported fields is defined by the chronology and normally includes |
|
219 |
* all {@code ChronoField} date and time fields. |
|
220 |
* <p> |
|
221 |
* If the field is not a {@code ChronoField}, then the result of this method |
|
222 |
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} |
|
223 |
* passing {@code this} as the argument. |
|
224 |
* Whether the field is supported is determined by the field. |
|
225 |
* |
|
226 |
* @param field the field to check, null returns false |
|
227 |
* @return true if the field can be queried, false if not |
|
228 |
*/ |
|
229 |
@Override |
|
16852 | 230 |
boolean isSupported(TemporalField field); |
15658 | 231 |
|
19030 | 232 |
/** |
233 |
* Checks if the specified unit is supported. |
|
234 |
* <p> |
|
235 |
* This checks if the specified unit can be added to or subtracted from this date-time. |
|
236 |
* If false, then calling the {@link #plus(long, TemporalUnit)} and |
|
237 |
* {@link #minus(long, TemporalUnit) minus} methods will throw an exception. |
|
238 |
* <p> |
|
239 |
* The set of supported units is defined by the chronology and normally includes |
|
240 |
* all {@code ChronoUnit} units except {@code FOREVER}. |
|
241 |
* <p> |
|
242 |
* If the unit is not a {@code ChronoUnit}, then the result of this method |
|
243 |
* is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)} |
|
244 |
* passing {@code this} as the argument. |
|
245 |
* Whether the unit is supported is determined by the unit. |
|
246 |
* |
|
247 |
* @param unit the unit to check, null returns false |
|
248 |
* @return true if the unit can be added/subtracted, false if not |
|
249 |
*/ |
|
250 |
@Override |
|
251 |
default boolean isSupported(TemporalUnit unit) { |
|
252 |
if (unit instanceof ChronoUnit) { |
|
253 |
return unit != FOREVER; |
|
254 |
} |
|
255 |
return unit != null && unit.isSupportedBy(this); |
|
256 |
} |
|
257 |
||
15658 | 258 |
//----------------------------------------------------------------------- |
259 |
// override for covariant return type |
|
260 |
/** |
|
261 |
* {@inheritDoc} |
|
262 |
* @throws DateTimeException {@inheritDoc} |
|
263 |
* @throws ArithmeticException {@inheritDoc} |
|
264 |
*/ |
|
265 |
@Override |
|
16852 | 266 |
default ChronoLocalDateTime<D> with(TemporalAdjuster adjuster) { |
20522 | 267 |
return ChronoLocalDateTimeImpl.ensureValid(getChronology(), Temporal.super.with(adjuster)); |
15658 | 268 |
} |
269 |
||
270 |
/** |
|
271 |
* {@inheritDoc} |
|
272 |
* @throws DateTimeException {@inheritDoc} |
|
273 |
* @throws ArithmeticException {@inheritDoc} |
|
274 |
*/ |
|
275 |
@Override |
|
276 |
ChronoLocalDateTime<D> with(TemporalField field, long newValue); |
|
277 |
||
278 |
/** |
|
279 |
* {@inheritDoc} |
|
280 |
* @throws DateTimeException {@inheritDoc} |
|
281 |
* @throws ArithmeticException {@inheritDoc} |
|
282 |
*/ |
|
283 |
@Override |
|
16852 | 284 |
default ChronoLocalDateTime<D> plus(TemporalAmount amount) { |
20522 | 285 |
return ChronoLocalDateTimeImpl.ensureValid(getChronology(), Temporal.super.plus(amount)); |
15658 | 286 |
} |
287 |
||
288 |
/** |
|
289 |
* {@inheritDoc} |
|
290 |
* @throws DateTimeException {@inheritDoc} |
|
291 |
* @throws ArithmeticException {@inheritDoc} |
|
292 |
*/ |
|
293 |
@Override |
|
294 |
ChronoLocalDateTime<D> plus(long amountToAdd, TemporalUnit unit); |
|
295 |
||
296 |
/** |
|
297 |
* {@inheritDoc} |
|
298 |
* @throws DateTimeException {@inheritDoc} |
|
299 |
* @throws ArithmeticException {@inheritDoc} |
|
300 |
*/ |
|
301 |
@Override |
|
16852 | 302 |
default ChronoLocalDateTime<D> minus(TemporalAmount amount) { |
20522 | 303 |
return ChronoLocalDateTimeImpl.ensureValid(getChronology(), Temporal.super.minus(amount)); |
15658 | 304 |
} |
305 |
||
306 |
/** |
|
307 |
* {@inheritDoc} |
|
308 |
* @throws DateTimeException {@inheritDoc} |
|
309 |
* @throws ArithmeticException {@inheritDoc} |
|
310 |
*/ |
|
311 |
@Override |
|
16852 | 312 |
default ChronoLocalDateTime<D> minus(long amountToSubtract, TemporalUnit unit) { |
20522 | 313 |
return ChronoLocalDateTimeImpl.ensureValid(getChronology(), Temporal.super.minus(amountToSubtract, unit)); |
15658 | 314 |
} |
315 |
||
316 |
//----------------------------------------------------------------------- |
|
317 |
/** |
|
318 |
* Queries this date-time using the specified query. |
|
319 |
* <p> |
|
320 |
* This queries this date-time using the specified query strategy object. |
|
321 |
* The {@code TemporalQuery} object defines the logic to be used to |
|
322 |
* obtain the result. Read the documentation of the query to understand |
|
323 |
* what the result of this method will be. |
|
324 |
* <p> |
|
325 |
* The result of this method is obtained by invoking the |
|
326 |
* {@link java.time.temporal.TemporalQuery#queryFrom(TemporalAccessor)} method on the |
|
327 |
* specified query passing {@code this} as the argument. |
|
328 |
* |
|
329 |
* @param <R> the type of the result |
|
330 |
* @param query the query to invoke, not null |
|
331 |
* @return the query result, null may be returned (defined by the query) |
|
332 |
* @throws DateTimeException if unable to query (defined by the query) |
|
333 |
* @throws ArithmeticException if numeric overflow occurs (defined by the query) |
|
334 |
*/ |
|
335 |
@SuppressWarnings("unchecked") |
|
336 |
@Override |
|
16852 | 337 |
default <R> R query(TemporalQuery<R> query) { |
20795 | 338 |
if (query == TemporalQueries.zoneId() || query == TemporalQueries.zone() || query == TemporalQueries.offset()) { |
15658 | 339 |
return null; |
20795 | 340 |
} else if (query == TemporalQueries.localTime()) { |
15658 | 341 |
return (R) toLocalTime(); |
20795 | 342 |
} else if (query == TemporalQueries.chronology()) { |
20522 | 343 |
return (R) getChronology(); |
20795 | 344 |
} else if (query == TemporalQueries.precision()) { |
15658 | 345 |
return (R) NANOS; |
346 |
} |
|
347 |
// inline TemporalAccessor.super.query(query) as an optimization |
|
348 |
// non-JDK classes are not permitted to make this optimization |
|
349 |
return query.queryFrom(this); |
|
350 |
} |
|
351 |
||
352 |
/** |
|
353 |
* Adjusts the specified temporal object to have the same date and time as this object. |
|
354 |
* <p> |
|
355 |
* This returns a temporal object of the same observable type as the input |
|
356 |
* with the date and time changed to be the same as this. |
|
357 |
* <p> |
|
358 |
* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)} |
|
359 |
* twice, passing {@link ChronoField#EPOCH_DAY} and |
|
360 |
* {@link ChronoField#NANO_OF_DAY} as the fields. |
|
361 |
* <p> |
|
362 |
* In most cases, it is clearer to reverse the calling pattern by using |
|
363 |
* {@link Temporal#with(TemporalAdjuster)}: |
|
364 |
* <pre> |
|
365 |
* // these two lines are equivalent, but the second approach is recommended |
|
366 |
* temporal = thisLocalDateTime.adjustInto(temporal); |
|
367 |
* temporal = temporal.with(thisLocalDateTime); |
|
368 |
* </pre> |
|
369 |
* <p> |
|
370 |
* This instance is immutable and unaffected by this method call. |
|
371 |
* |
|
372 |
* @param temporal the target object to be adjusted, not null |
|
373 |
* @return the adjusted object, not null |
|
374 |
* @throws DateTimeException if unable to make the adjustment |
|
375 |
* @throws ArithmeticException if numeric overflow occurs |
|
376 |
*/ |
|
377 |
@Override |
|
16852 | 378 |
default Temporal adjustInto(Temporal temporal) { |
15658 | 379 |
return temporal |
380 |
.with(EPOCH_DAY, toLocalDate().toEpochDay()) |
|
381 |
.with(NANO_OF_DAY, toLocalTime().toNanoOfDay()); |
|
382 |
} |
|
383 |
||
16852 | 384 |
/** |
385 |
* Formats this date-time using the specified formatter. |
|
386 |
* <p> |
|
387 |
* This date-time will be passed to the formatter to produce a string. |
|
388 |
* <p> |
|
389 |
* The default implementation must behave as follows: |
|
390 |
* <pre> |
|
391 |
* return formatter.format(this); |
|
392 |
* </pre> |
|
393 |
* |
|
394 |
* @param formatter the formatter to use, not null |
|
395 |
* @return the formatted date-time string, not null |
|
396 |
* @throws DateTimeException if an error occurs during printing |
|
397 |
*/ |
|
398 |
default String format(DateTimeFormatter formatter) { |
|
399 |
Objects.requireNonNull(formatter, "formatter"); |
|
400 |
return formatter.format(this); |
|
401 |
} |
|
402 |
||
15658 | 403 |
//----------------------------------------------------------------------- |
404 |
/** |
|
405 |
* Combines this time with a time-zone to create a {@code ChronoZonedDateTime}. |
|
406 |
* <p> |
|
407 |
* This returns a {@code ChronoZonedDateTime} formed from this date-time at the |
|
408 |
* specified time-zone. The result will match this date-time as closely as possible. |
|
409 |
* Time-zone rules, such as daylight savings, mean that not every local date-time |
|
410 |
* is valid for the specified zone, thus the local date-time may be adjusted. |
|
411 |
* <p> |
|
412 |
* The local date-time is resolved to a single instant on the time-line. |
|
413 |
* This is achieved by finding a valid offset from UTC/Greenwich for the local |
|
414 |
* date-time as defined by the {@link ZoneRules rules} of the zone ID. |
|
415 |
*<p> |
|
416 |
* In most cases, there is only one valid offset for a local date-time. |
|
417 |
* In the case of an overlap, where clocks are set back, there are two valid offsets. |
|
418 |
* This method uses the earlier offset typically corresponding to "summer". |
|
419 |
* <p> |
|
420 |
* In the case of a gap, where clocks jump forward, there is no valid offset. |
|
421 |
* Instead, the local date-time is adjusted to be later by the length of the gap. |
|
422 |
* For a typical one hour daylight savings change, the local date-time will be |
|
423 |
* moved one hour later into the offset typically corresponding to "summer". |
|
424 |
* <p> |
|
425 |
* To obtain the later offset during an overlap, call |
|
426 |
* {@link ChronoZonedDateTime#withLaterOffsetAtOverlap()} on the result of this method. |
|
427 |
* |
|
428 |
* @param zone the time-zone to use, not null |
|
429 |
* @return the zoned date-time formed from this date-time, not null |
|
430 |
*/ |
|
431 |
ChronoZonedDateTime<D> atZone(ZoneId zone); |
|
432 |
||
433 |
//----------------------------------------------------------------------- |
|
434 |
/** |
|
435 |
* Converts this date-time to an {@code Instant}. |
|
436 |
* <p> |
|
437 |
* This combines this local date-time and the specified offset to form |
|
438 |
* an {@code Instant}. |
|
439 |
* <p> |
|
440 |
* This default implementation calculates from the epoch-day of the date and the |
|
441 |
* second-of-day of the time. |
|
442 |
* |
|
443 |
* @param offset the offset to use for the conversion, not null |
|
444 |
* @return an {@code Instant} representing the same instant, not null |
|
445 |
*/ |
|
16852 | 446 |
default Instant toInstant(ZoneOffset offset) { |
15658 | 447 |
return Instant.ofEpochSecond(toEpochSecond(offset), toLocalTime().getNano()); |
448 |
} |
|
449 |
||
450 |
/** |
|
451 |
* Converts this date-time to the number of seconds from the epoch |
|
452 |
* of 1970-01-01T00:00:00Z. |
|
453 |
* <p> |
|
454 |
* This combines this local date-time and the specified offset to calculate the |
|
455 |
* epoch-second value, which is the number of elapsed seconds from 1970-01-01T00:00:00Z. |
|
456 |
* Instants on the time-line after the epoch are positive, earlier are negative. |
|
457 |
* <p> |
|
458 |
* This default implementation calculates from the epoch-day of the date and the |
|
459 |
* second-of-day of the time. |
|
460 |
* |
|
461 |
* @param offset the offset to use for the conversion, not null |
|
462 |
* @return the number of seconds from the epoch of 1970-01-01T00:00:00Z |
|
463 |
*/ |
|
16852 | 464 |
default long toEpochSecond(ZoneOffset offset) { |
15658 | 465 |
Objects.requireNonNull(offset, "offset"); |
466 |
long epochDay = toLocalDate().toEpochDay(); |
|
467 |
long secs = epochDay * 86400 + toLocalTime().toSecondOfDay(); |
|
468 |
secs -= offset.getTotalSeconds(); |
|
469 |
return secs; |
|
470 |
} |
|
471 |
||
472 |
//----------------------------------------------------------------------- |
|
473 |
/** |
|
474 |
* Compares this date-time to another date-time, including the chronology. |
|
475 |
* <p> |
|
476 |
* The comparison is based first on the underlying time-line date-time, then |
|
477 |
* on the chronology. |
|
478 |
* It is "consistent with equals", as defined by {@link Comparable}. |
|
479 |
* <p> |
|
480 |
* For example, the following is the comparator order: |
|
481 |
* <ol> |
|
482 |
* <li>{@code 2012-12-03T12:00 (ISO)}</li> |
|
483 |
* <li>{@code 2012-12-04T12:00 (ISO)}</li> |
|
484 |
* <li>{@code 2555-12-04T12:00 (ThaiBuddhist)}</li> |
|
485 |
* <li>{@code 2012-12-05T12:00 (ISO)}</li> |
|
486 |
* </ol> |
|
487 |
* Values #2 and #3 represent the same date-time on the time-line. |
|
488 |
* When two values represent the same date-time, the chronology ID is compared to distinguish them. |
|
489 |
* This step is needed to make the ordering "consistent with equals". |
|
490 |
* <p> |
|
491 |
* If all the date-time objects being compared are in the same chronology, then the |
|
492 |
* additional chronology stage is not required and only the local date-time is used. |
|
493 |
* <p> |
|
494 |
* This default implementation performs the comparison defined above. |
|
495 |
* |
|
496 |
* @param other the other date-time to compare to, not null |
|
497 |
* @return the comparator value, negative if less, positive if greater |
|
498 |
*/ |
|
499 |
@Override |
|
16852 | 500 |
default int compareTo(ChronoLocalDateTime<?> other) { |
15658 | 501 |
int cmp = toLocalDate().compareTo(other.toLocalDate()); |
502 |
if (cmp == 0) { |
|
503 |
cmp = toLocalTime().compareTo(other.toLocalTime()); |
|
504 |
if (cmp == 0) { |
|
20522 | 505 |
cmp = getChronology().compareTo(other.getChronology()); |
15658 | 506 |
} |
507 |
} |
|
508 |
return cmp; |
|
509 |
} |
|
510 |
||
511 |
/** |
|
512 |
* Checks if this date-time is after the specified date-time ignoring the chronology. |
|
513 |
* <p> |
|
514 |
* This method differs from the comparison in {@link #compareTo} in that it |
|
515 |
* only compares the underlying date-time and not the chronology. |
|
516 |
* This allows dates in different calendar systems to be compared based |
|
517 |
* on the time-line position. |
|
518 |
* <p> |
|
519 |
* This default implementation performs the comparison based on the epoch-day |
|
520 |
* and nano-of-day. |
|
521 |
* |
|
522 |
* @param other the other date-time to compare to, not null |
|
523 |
* @return true if this is after the specified date-time |
|
524 |
*/ |
|
16852 | 525 |
default boolean isAfter(ChronoLocalDateTime<?> other) { |
15658 | 526 |
long thisEpDay = this.toLocalDate().toEpochDay(); |
527 |
long otherEpDay = other.toLocalDate().toEpochDay(); |
|
528 |
return thisEpDay > otherEpDay || |
|
529 |
(thisEpDay == otherEpDay && this.toLocalTime().toNanoOfDay() > other.toLocalTime().toNanoOfDay()); |
|
530 |
} |
|
531 |
||
532 |
/** |
|
533 |
* Checks if this date-time is before the specified date-time ignoring the chronology. |
|
534 |
* <p> |
|
535 |
* This method differs from the comparison in {@link #compareTo} in that it |
|
536 |
* only compares the underlying date-time and not the chronology. |
|
537 |
* This allows dates in different calendar systems to be compared based |
|
538 |
* on the time-line position. |
|
539 |
* <p> |
|
540 |
* This default implementation performs the comparison based on the epoch-day |
|
541 |
* and nano-of-day. |
|
542 |
* |
|
543 |
* @param other the other date-time to compare to, not null |
|
544 |
* @return true if this is before the specified date-time |
|
545 |
*/ |
|
16852 | 546 |
default boolean isBefore(ChronoLocalDateTime<?> other) { |
15658 | 547 |
long thisEpDay = this.toLocalDate().toEpochDay(); |
548 |
long otherEpDay = other.toLocalDate().toEpochDay(); |
|
549 |
return thisEpDay < otherEpDay || |
|
550 |
(thisEpDay == otherEpDay && this.toLocalTime().toNanoOfDay() < other.toLocalTime().toNanoOfDay()); |
|
551 |
} |
|
552 |
||
553 |
/** |
|
554 |
* Checks if this date-time is equal to the specified date-time ignoring the chronology. |
|
555 |
* <p> |
|
556 |
* This method differs from the comparison in {@link #compareTo} in that it |
|
557 |
* only compares the underlying date and time and not the chronology. |
|
558 |
* This allows date-times in different calendar systems to be compared based |
|
559 |
* on the time-line position. |
|
560 |
* <p> |
|
561 |
* This default implementation performs the comparison based on the epoch-day |
|
562 |
* and nano-of-day. |
|
563 |
* |
|
564 |
* @param other the other date-time to compare to, not null |
|
565 |
* @return true if the underlying date-time is equal to the specified date-time on the timeline |
|
566 |
*/ |
|
16852 | 567 |
default boolean isEqual(ChronoLocalDateTime<?> other) { |
15658 | 568 |
// Do the time check first, it is cheaper than computing EPOCH day. |
569 |
return this.toLocalTime().toNanoOfDay() == other.toLocalTime().toNanoOfDay() && |
|
570 |
this.toLocalDate().toEpochDay() == other.toLocalDate().toEpochDay(); |
|
571 |
} |
|
572 |
||
573 |
/** |
|
574 |
* Checks if this date-time is equal to another date-time, including the chronology. |
|
575 |
* <p> |
|
576 |
* Compares this date-time with another ensuring that the date-time and chronology are the same. |
|
577 |
* |
|
578 |
* @param obj the object to check, null returns false |
|
579 |
* @return true if this is equal to the other date |
|
580 |
*/ |
|
581 |
@Override |
|
582 |
boolean equals(Object obj); |
|
583 |
||
584 |
/** |
|
585 |
* A hash code for this date-time. |
|
586 |
* |
|
587 |
* @return a suitable hash code |
|
588 |
*/ |
|
589 |
@Override |
|
590 |
int hashCode(); |
|
591 |
||
592 |
//----------------------------------------------------------------------- |
|
593 |
/** |
|
594 |
* Outputs this date-time as a {@code String}. |
|
595 |
* <p> |
|
16852 | 596 |
* The output will include the full local date-time. |
15658 | 597 |
* |
598 |
* @return a string representation of this date-time, not null |
|
599 |
*/ |
|
600 |
@Override |
|
601 |
String toString(); |
|
602 |
||
603 |
} |