68 import static java.time.LocalTime.NANOS_PER_DAY; |
68 import static java.time.LocalTime.NANOS_PER_DAY; |
69 import static java.time.LocalTime.NANOS_PER_HOUR; |
69 import static java.time.LocalTime.NANOS_PER_HOUR; |
70 import static java.time.LocalTime.NANOS_PER_MINUTE; |
70 import static java.time.LocalTime.NANOS_PER_MINUTE; |
71 import static java.time.LocalTime.NANOS_PER_SECOND; |
71 import static java.time.LocalTime.NANOS_PER_SECOND; |
72 import static java.time.LocalTime.SECONDS_PER_DAY; |
72 import static java.time.LocalTime.SECONDS_PER_DAY; |
|
73 import static java.time.temporal.ChronoField.NANO_OF_SECOND; |
73 |
74 |
74 import java.io.DataInput; |
75 import java.io.DataInput; |
75 import java.io.DataOutput; |
76 import java.io.DataOutput; |
76 import java.io.IOException; |
77 import java.io.IOException; |
77 import java.io.InvalidObjectException; |
78 import java.io.InvalidObjectException; |
78 import java.io.ObjectStreamException; |
79 import java.io.ObjectStreamException; |
79 import java.io.Serializable; |
80 import java.io.Serializable; |
|
81 import java.time.chrono.ChronoLocalDateTime; |
|
82 import java.time.chrono.IsoChronology; |
80 import java.time.format.DateTimeFormatter; |
83 import java.time.format.DateTimeFormatter; |
81 import java.time.format.DateTimeFormatters; |
|
82 import java.time.format.DateTimeParseException; |
84 import java.time.format.DateTimeParseException; |
83 import java.time.temporal.ChronoField; |
85 import java.time.temporal.ChronoField; |
84 import java.time.temporal.ChronoLocalDateTime; |
|
85 import java.time.temporal.ChronoUnit; |
86 import java.time.temporal.ChronoUnit; |
86 import java.time.temporal.ISOChrono; |
87 import java.time.temporal.Queries; |
87 import java.time.temporal.OffsetDateTime; |
|
88 import java.time.temporal.Temporal; |
88 import java.time.temporal.Temporal; |
89 import java.time.temporal.TemporalAccessor; |
89 import java.time.temporal.TemporalAccessor; |
90 import java.time.temporal.TemporalAdder; |
|
91 import java.time.temporal.TemporalAdjuster; |
90 import java.time.temporal.TemporalAdjuster; |
|
91 import java.time.temporal.TemporalAmount; |
92 import java.time.temporal.TemporalField; |
92 import java.time.temporal.TemporalField; |
93 import java.time.temporal.TemporalQuery; |
93 import java.time.temporal.TemporalQuery; |
94 import java.time.temporal.TemporalSubtractor; |
|
95 import java.time.temporal.TemporalUnit; |
94 import java.time.temporal.TemporalUnit; |
96 import java.time.temporal.ValueRange; |
95 import java.time.temporal.ValueRange; |
97 import java.time.zone.ZoneRules; |
96 import java.time.zone.ZoneRules; |
98 import java.util.Objects; |
97 import java.util.Objects; |
99 |
98 |
210 //----------------------------------------------------------------------- |
209 //----------------------------------------------------------------------- |
211 /** |
210 /** |
212 * Obtains an instance of {@code LocalDateTime} from year, month, |
211 * Obtains an instance of {@code LocalDateTime} from year, month, |
213 * day, hour and minute, setting the second and nanosecond to zero. |
212 * day, hour and minute, setting the second and nanosecond to zero. |
214 * <p> |
213 * <p> |
|
214 * This returns a {@code LocalDateTime} with the specified year, month, |
|
215 * day-of-month, hour and minute. |
215 * The day must be valid for the year and month, otherwise an exception will be thrown. |
216 * The day must be valid for the year and month, otherwise an exception will be thrown. |
216 * The second and nanosecond fields will be set to zero. |
217 * The second and nanosecond fields will be set to zero. |
217 * |
218 * |
218 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
219 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
219 * @param month the month-of-year to represent, not null |
220 * @param month the month-of-year to represent, not null |
220 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
221 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
221 * @param hour the hour-of-day to represent, from 0 to 23 |
222 * @param hour the hour-of-day to represent, from 0 to 23 |
222 * @param minute the minute-of-hour to represent, from 0 to 59 |
223 * @param minute the minute-of-hour to represent, from 0 to 59 |
223 * @return the local date-time, not null |
224 * @return the local date-time, not null |
224 * @throws DateTimeException if the value of any field is out of range |
225 * @throws DateTimeException if the value of any field is out of range, |
225 * @throws DateTimeException if the day-of-month is invalid for the month-year |
226 * or if the day-of-month is invalid for the month-year |
226 */ |
227 */ |
227 public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute) { |
228 public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute) { |
228 LocalDate date = LocalDate.of(year, month, dayOfMonth); |
229 LocalDate date = LocalDate.of(year, month, dayOfMonth); |
229 LocalTime time = LocalTime.of(hour, minute); |
230 LocalTime time = LocalTime.of(hour, minute); |
230 return new LocalDateTime(date, time); |
231 return new LocalDateTime(date, time); |
232 |
233 |
233 /** |
234 /** |
234 * Obtains an instance of {@code LocalDateTime} from year, month, |
235 * Obtains an instance of {@code LocalDateTime} from year, month, |
235 * day, hour, minute and second, setting the nanosecond to zero. |
236 * day, hour, minute and second, setting the nanosecond to zero. |
236 * <p> |
237 * <p> |
|
238 * This returns a {@code LocalDateTime} with the specified year, month, |
|
239 * day-of-month, hour, minute and second. |
237 * The day must be valid for the year and month, otherwise an exception will be thrown. |
240 * The day must be valid for the year and month, otherwise an exception will be thrown. |
238 * The nanosecond field will be set to zero. |
241 * The nanosecond field will be set to zero. |
239 * |
242 * |
240 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
243 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
241 * @param month the month-of-year to represent, not null |
244 * @param month the month-of-year to represent, not null |
242 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
245 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
243 * @param hour the hour-of-day to represent, from 0 to 23 |
246 * @param hour the hour-of-day to represent, from 0 to 23 |
244 * @param minute the minute-of-hour to represent, from 0 to 59 |
247 * @param minute the minute-of-hour to represent, from 0 to 59 |
245 * @param second the second-of-minute to represent, from 0 to 59 |
248 * @param second the second-of-minute to represent, from 0 to 59 |
246 * @return the local date-time, not null |
249 * @return the local date-time, not null |
247 * @throws DateTimeException if the value of any field is out of range |
250 * @throws DateTimeException if the value of any field is out of range, |
248 * @throws DateTimeException if the day-of-month is invalid for the month-year |
251 * or if the day-of-month is invalid for the month-year |
249 */ |
252 */ |
250 public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) { |
253 public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) { |
251 LocalDate date = LocalDate.of(year, month, dayOfMonth); |
254 LocalDate date = LocalDate.of(year, month, dayOfMonth); |
252 LocalTime time = LocalTime.of(hour, minute, second); |
255 LocalTime time = LocalTime.of(hour, minute, second); |
253 return new LocalDateTime(date, time); |
256 return new LocalDateTime(date, time); |
255 |
258 |
256 /** |
259 /** |
257 * Obtains an instance of {@code LocalDateTime} from year, month, |
260 * Obtains an instance of {@code LocalDateTime} from year, month, |
258 * day, hour, minute, second and nanosecond. |
261 * day, hour, minute, second and nanosecond. |
259 * <p> |
262 * <p> |
|
263 * This returns a {@code LocalDateTime} with the specified year, month, |
|
264 * day-of-month, hour, minute, second and nanosecond. |
260 * The day must be valid for the year and month, otherwise an exception will be thrown. |
265 * The day must be valid for the year and month, otherwise an exception will be thrown. |
261 * |
266 * |
262 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
267 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
263 * @param month the month-of-year to represent, not null |
268 * @param month the month-of-year to represent, not null |
264 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
269 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
265 * @param hour the hour-of-day to represent, from 0 to 23 |
270 * @param hour the hour-of-day to represent, from 0 to 23 |
266 * @param minute the minute-of-hour to represent, from 0 to 59 |
271 * @param minute the minute-of-hour to represent, from 0 to 59 |
267 * @param second the second-of-minute to represent, from 0 to 59 |
272 * @param second the second-of-minute to represent, from 0 to 59 |
268 * @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 |
273 * @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 |
269 * @return the local date-time, not null |
274 * @return the local date-time, not null |
270 * @throws DateTimeException if the value of any field is out of range |
275 * @throws DateTimeException if the value of any field is out of range, |
271 * @throws DateTimeException if the day-of-month is invalid for the month-year |
276 * or if the day-of-month is invalid for the month-year |
272 */ |
277 */ |
273 public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) { |
278 public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) { |
274 LocalDate date = LocalDate.of(year, month, dayOfMonth); |
279 LocalDate date = LocalDate.of(year, month, dayOfMonth); |
275 LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond); |
280 LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond); |
276 return new LocalDateTime(date, time); |
281 return new LocalDateTime(date, time); |
279 //----------------------------------------------------------------------- |
284 //----------------------------------------------------------------------- |
280 /** |
285 /** |
281 * Obtains an instance of {@code LocalDateTime} from year, month, |
286 * Obtains an instance of {@code LocalDateTime} from year, month, |
282 * day, hour and minute, setting the second and nanosecond to zero. |
287 * day, hour and minute, setting the second and nanosecond to zero. |
283 * <p> |
288 * <p> |
|
289 * This returns a {@code LocalDateTime} with the specified year, month, |
|
290 * day-of-month, hour and minute. |
284 * The day must be valid for the year and month, otherwise an exception will be thrown. |
291 * The day must be valid for the year and month, otherwise an exception will be thrown. |
285 * The second and nanosecond fields will be set to zero. |
292 * The second and nanosecond fields will be set to zero. |
286 * |
293 * |
287 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
294 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
288 * @param month the month-of-year to represent, from 1 (January) to 12 (December) |
295 * @param month the month-of-year to represent, from 1 (January) to 12 (December) |
289 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
296 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
290 * @param hour the hour-of-day to represent, from 0 to 23 |
297 * @param hour the hour-of-day to represent, from 0 to 23 |
291 * @param minute the minute-of-hour to represent, from 0 to 59 |
298 * @param minute the minute-of-hour to represent, from 0 to 59 |
292 * @return the local date-time, not null |
299 * @return the local date-time, not null |
293 * @throws DateTimeException if the value of any field is out of range |
300 * @throws DateTimeException if the value of any field is out of range, |
294 * @throws DateTimeException if the day-of-month is invalid for the month-year |
301 * or if the day-of-month is invalid for the month-year |
295 */ |
302 */ |
296 public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) { |
303 public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) { |
297 LocalDate date = LocalDate.of(year, month, dayOfMonth); |
304 LocalDate date = LocalDate.of(year, month, dayOfMonth); |
298 LocalTime time = LocalTime.of(hour, minute); |
305 LocalTime time = LocalTime.of(hour, minute); |
299 return new LocalDateTime(date, time); |
306 return new LocalDateTime(date, time); |
301 |
308 |
302 /** |
309 /** |
303 * Obtains an instance of {@code LocalDateTime} from year, month, |
310 * Obtains an instance of {@code LocalDateTime} from year, month, |
304 * day, hour, minute and second, setting the nanosecond to zero. |
311 * day, hour, minute and second, setting the nanosecond to zero. |
305 * <p> |
312 * <p> |
|
313 * This returns a {@code LocalDateTime} with the specified year, month, |
|
314 * day-of-month, hour, minute and second. |
306 * The day must be valid for the year and month, otherwise an exception will be thrown. |
315 * The day must be valid for the year and month, otherwise an exception will be thrown. |
307 * The nanosecond field will be set to zero. |
316 * The nanosecond field will be set to zero. |
308 * |
317 * |
309 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
318 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
310 * @param month the month-of-year to represent, from 1 (January) to 12 (December) |
319 * @param month the month-of-year to represent, from 1 (January) to 12 (December) |
311 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
320 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
312 * @param hour the hour-of-day to represent, from 0 to 23 |
321 * @param hour the hour-of-day to represent, from 0 to 23 |
313 * @param minute the minute-of-hour to represent, from 0 to 59 |
322 * @param minute the minute-of-hour to represent, from 0 to 59 |
314 * @param second the second-of-minute to represent, from 0 to 59 |
323 * @param second the second-of-minute to represent, from 0 to 59 |
315 * @return the local date-time, not null |
324 * @return the local date-time, not null |
316 * @throws DateTimeException if the value of any field is out of range |
325 * @throws DateTimeException if the value of any field is out of range, |
317 * @throws DateTimeException if the day-of-month is invalid for the month-year |
326 * or if the day-of-month is invalid for the month-year |
318 */ |
327 */ |
319 public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) { |
328 public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) { |
320 LocalDate date = LocalDate.of(year, month, dayOfMonth); |
329 LocalDate date = LocalDate.of(year, month, dayOfMonth); |
321 LocalTime time = LocalTime.of(hour, minute, second); |
330 LocalTime time = LocalTime.of(hour, minute, second); |
322 return new LocalDateTime(date, time); |
331 return new LocalDateTime(date, time); |
324 |
333 |
325 /** |
334 /** |
326 * Obtains an instance of {@code LocalDateTime} from year, month, |
335 * Obtains an instance of {@code LocalDateTime} from year, month, |
327 * day, hour, minute, second and nanosecond. |
336 * day, hour, minute, second and nanosecond. |
328 * <p> |
337 * <p> |
|
338 * This returns a {@code LocalDateTime} with the specified year, month, |
|
339 * day-of-month, hour, minute, second and nanosecond. |
329 * The day must be valid for the year and month, otherwise an exception will be thrown. |
340 * The day must be valid for the year and month, otherwise an exception will be thrown. |
330 * |
341 * |
331 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
342 * @param year the year to represent, from MIN_YEAR to MAX_YEAR |
332 * @param month the month-of-year to represent, from 1 (January) to 12 (December) |
343 * @param month the month-of-year to represent, from 1 (January) to 12 (December) |
333 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
344 * @param dayOfMonth the day-of-month to represent, from 1 to 31 |
334 * @param hour the hour-of-day to represent, from 0 to 23 |
345 * @param hour the hour-of-day to represent, from 0 to 23 |
335 * @param minute the minute-of-hour to represent, from 0 to 59 |
346 * @param minute the minute-of-hour to represent, from 0 to 59 |
336 * @param second the second-of-minute to represent, from 0 to 59 |
347 * @param second the second-of-minute to represent, from 0 to 59 |
337 * @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 |
348 * @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 |
338 * @return the local date-time, not null |
349 * @return the local date-time, not null |
339 * @throws DateTimeException if the value of any field is out of range |
350 * @throws DateTimeException if the value of any field is out of range, |
340 * @throws DateTimeException if the day-of-month is invalid for the month-year |
351 * or if the day-of-month is invalid for the month-year |
341 */ |
352 */ |
342 public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) { |
353 public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) { |
343 LocalDate date = LocalDate.of(year, month, dayOfMonth); |
354 LocalDate date = LocalDate.of(year, month, dayOfMonth); |
344 LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond); |
355 LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond); |
345 return new LocalDateTime(date, time); |
356 return new LocalDateTime(date, time); |
390 * |
401 * |
391 * @param epochSecond the number of seconds from the epoch of 1970-01-01T00:00:00Z |
402 * @param epochSecond the number of seconds from the epoch of 1970-01-01T00:00:00Z |
392 * @param nanoOfSecond the nanosecond within the second, from 0 to 999,999,999 |
403 * @param nanoOfSecond the nanosecond within the second, from 0 to 999,999,999 |
393 * @param offset the zone offset, not null |
404 * @param offset the zone offset, not null |
394 * @return the local date-time, not null |
405 * @return the local date-time, not null |
395 * @throws DateTimeException if the result exceeds the supported range |
406 * @throws DateTimeException if the result exceeds the supported range, |
|
407 * or if the nano-of-second is invalid |
396 */ |
408 */ |
397 public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) { |
409 public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) { |
398 Objects.requireNonNull(offset, "offset"); |
410 Objects.requireNonNull(offset, "offset"); |
|
411 NANO_OF_SECOND.checkValidValue(nanoOfSecond); |
399 long localSecond = epochSecond + offset.getTotalSeconds(); // overflow caught later |
412 long localSecond = epochSecond + offset.getTotalSeconds(); // overflow caught later |
400 long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY); |
413 long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY); |
401 int secsOfDay = (int)Math.floorMod(localSecond, SECONDS_PER_DAY); |
414 int secsOfDay = (int)Math.floorMod(localSecond, SECONDS_PER_DAY); |
402 LocalDate date = LocalDate.ofEpochDay(localEpochDay); |
415 LocalDate date = LocalDate.ofEpochDay(localEpochDay); |
403 LocalTime time = LocalTime.ofSecondOfDay(secsOfDay, nanoOfSecond); |
416 LocalTime time = LocalTime.ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + nanoOfSecond); |
404 return new LocalDateTime(date, time); |
417 return new LocalDateTime(date, time); |
405 } |
418 } |
406 |
419 |
407 //----------------------------------------------------------------------- |
420 //----------------------------------------------------------------------- |
408 /** |
421 /** |
409 * Obtains an instance of {@code LocalDateTime} from a temporal object. |
422 * Obtains an instance of {@code LocalDateTime} from a temporal object. |
410 * <p> |
423 * <p> |
411 * A {@code TemporalAccessor} represents some form of date and time information. |
424 * This obtains an offset time based on the specified temporal. |
412 * This factory converts the arbitrary temporal object to an instance of {@code LocalDateTime}. |
425 * A {@code TemporalAccessor} represents an arbitrary set of date and time information, |
413 * <p> |
426 * which this factory converts to an instance of {@code LocalDateTime}. |
414 * The conversion extracts and combines {@code LocalDate} and {@code LocalTime}. |
427 * <p> |
|
428 * The conversion extracts and combines the {@code LocalDate} and the |
|
429 * {@code LocalTime} from the temporal object. |
|
430 * Implementations are permitted to perform optimizations such as accessing |
|
431 * those fields that are equivalent to the relevant objects. |
415 * <p> |
432 * <p> |
416 * This method matches the signature of the functional interface {@link TemporalQuery} |
433 * This method matches the signature of the functional interface {@link TemporalQuery} |
417 * allowing it to be used as a query via method reference, {@code LocalDateTime::from}. |
434 * allowing it to be used as a query via method reference, {@code LocalDateTime::from}. |
418 * |
435 * |
419 * @param temporal the temporal object to convert, not null |
436 * @param temporal the temporal object to convert, not null |
422 */ |
439 */ |
423 public static LocalDateTime from(TemporalAccessor temporal) { |
440 public static LocalDateTime from(TemporalAccessor temporal) { |
424 if (temporal instanceof LocalDateTime) { |
441 if (temporal instanceof LocalDateTime) { |
425 return (LocalDateTime) temporal; |
442 return (LocalDateTime) temporal; |
426 } else if (temporal instanceof ZonedDateTime) { |
443 } else if (temporal instanceof ZonedDateTime) { |
427 return ((ZonedDateTime) temporal).getDateTime(); |
444 return ((ZonedDateTime) temporal).toLocalDateTime(); |
|
445 } else if (temporal instanceof OffsetDateTime) { |
|
446 return ((OffsetDateTime) temporal).toLocalDateTime(); |
428 } |
447 } |
429 try { |
448 try { |
430 LocalDate date = LocalDate.from(temporal); |
449 LocalDate date = LocalDate.from(temporal); |
431 LocalTime time = LocalTime.from(temporal); |
450 LocalTime time = LocalTime.from(temporal); |
432 return new LocalDateTime(date, time); |
451 return new LocalDateTime(date, time); |
438 //----------------------------------------------------------------------- |
457 //----------------------------------------------------------------------- |
439 /** |
458 /** |
440 * Obtains an instance of {@code LocalDateTime} from a text string such as {@code 2007-12-03T10:15:30}. |
459 * Obtains an instance of {@code LocalDateTime} from a text string such as {@code 2007-12-03T10:15:30}. |
441 * <p> |
460 * <p> |
442 * The string must represent a valid date-time and is parsed using |
461 * The string must represent a valid date-time and is parsed using |
443 * {@link java.time.format.DateTimeFormatters#isoLocalDateTime()}. |
462 * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME}. |
444 * |
463 * |
445 * @param text the text to parse such as "2007-12-03T10:15:30", not null |
464 * @param text the text to parse such as "2007-12-03T10:15:30", not null |
446 * @return the parsed local date-time, not null |
465 * @return the parsed local date-time, not null |
447 * @throws DateTimeParseException if the text cannot be parsed |
466 * @throws DateTimeParseException if the text cannot be parsed |
448 */ |
467 */ |
449 public static LocalDateTime parse(CharSequence text) { |
468 public static LocalDateTime parse(CharSequence text) { |
450 return parse(text, DateTimeFormatters.isoLocalDateTime()); |
469 return parse(text, DateTimeFormatter.ISO_LOCAL_DATE_TIME); |
451 } |
470 } |
452 |
471 |
453 /** |
472 /** |
454 * Obtains an instance of {@code LocalDateTime} from a text string using a specific formatter. |
473 * Obtains an instance of {@code LocalDateTime} from a text string using a specific formatter. |
455 * <p> |
474 * <p> |
533 * <li>{@code ERA} |
552 * <li>{@code ERA} |
534 * </ul> |
553 * </ul> |
535 * All other {@code ChronoField} instances will return false. |
554 * All other {@code ChronoField} instances will return false. |
536 * <p> |
555 * <p> |
537 * If the field is not a {@code ChronoField}, then the result of this method |
556 * If the field is not a {@code ChronoField}, then the result of this method |
538 * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)} |
557 * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} |
539 * passing {@code this} as the argument. |
558 * passing {@code this} as the argument. |
540 * Whether the field is supported is determined by the field. |
559 * Whether the field is supported is determined by the field. |
541 * |
560 * |
542 * @param field the field to check, null returns false |
561 * @param field the field to check, null returns false |
543 * @return true if the field is supported on this date-time, false if not |
562 * @return true if the field is supported on this date-time, false if not |
563 * The {@link #isSupported(TemporalField) supported fields} will return |
582 * The {@link #isSupported(TemporalField) supported fields} will return |
564 * appropriate range instances. |
583 * appropriate range instances. |
565 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
584 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
566 * <p> |
585 * <p> |
567 * If the field is not a {@code ChronoField}, then the result of this method |
586 * If the field is not a {@code ChronoField}, then the result of this method |
568 * is obtained by invoking {@code TemporalField.doRange(TemporalAccessor)} |
587 * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)} |
569 * passing {@code this} as the argument. |
588 * passing {@code this} as the argument. |
570 * Whether the range can be obtained is determined by the field. |
589 * Whether the range can be obtained is determined by the field. |
571 * |
590 * |
572 * @param field the field to query the range for, not null |
591 * @param field the field to query the range for, not null |
573 * @return the range of valid values for the field, not null |
592 * @return the range of valid values for the field, not null |
596 * {@code EPOCH_DAY} and {@code EPOCH_MONTH} which are too large to fit in |
615 * {@code EPOCH_DAY} and {@code EPOCH_MONTH} which are too large to fit in |
597 * an {@code int} and throw a {@code DateTimeException}. |
616 * an {@code int} and throw a {@code DateTimeException}. |
598 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
617 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
599 * <p> |
618 * <p> |
600 * If the field is not a {@code ChronoField}, then the result of this method |
619 * If the field is not a {@code ChronoField}, then the result of this method |
601 * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)} |
620 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
602 * passing {@code this} as the argument. Whether the value can be obtained, |
621 * passing {@code this} as the argument. Whether the value can be obtained, |
603 * and what the value represents, is determined by the field. |
622 * and what the value represents, is determined by the field. |
604 * |
623 * |
605 * @param field the field to get, not null |
624 * @param field the field to get, not null |
606 * @return the value for the field |
625 * @return the value for the field |
627 * The {@link #isSupported(TemporalField) supported fields} will return valid |
646 * The {@link #isSupported(TemporalField) supported fields} will return valid |
628 * values based on this date-time. |
647 * values based on this date-time. |
629 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
648 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
630 * <p> |
649 * <p> |
631 * If the field is not a {@code ChronoField}, then the result of this method |
650 * If the field is not a {@code ChronoField}, then the result of this method |
632 * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)} |
651 * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)} |
633 * passing {@code this} as the argument. Whether the value can be obtained, |
652 * passing {@code this} as the argument. Whether the value can be obtained, |
634 * and what the value represents, is determined by the field. |
653 * and what the value represents, is determined by the field. |
635 * |
654 * |
636 * @param field the field to get, not null |
655 * @param field the field to get, not null |
637 * @return the value for the field |
656 * @return the value for the field |
655 * as this date-time. |
674 * as this date-time. |
656 * |
675 * |
657 * @return the date part of this date-time, not null |
676 * @return the date part of this date-time, not null |
658 */ |
677 */ |
659 @Override |
678 @Override |
660 public LocalDate getDate() { |
679 public LocalDate toLocalDate() { |
661 return date; |
680 return date; |
662 } |
681 } |
663 |
682 |
664 /** |
683 /** |
665 * Gets the year field. |
684 * Gets the year field. |
666 * <p> |
685 * <p> |
667 * This method returns the primitive {@code int} value for the year. |
686 * This method returns the primitive {@code int} value for the year. |
668 * <p> |
687 * <p> |
669 * The year returned by this method is proleptic as per {@code get(YEAR)}. |
688 * The year returned by this method is proleptic as per {@code get(YEAR)}. |
670 * To obtain the year-of-era, use {@code get(YEAR_OF_ERA}. |
689 * To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}. |
671 * |
690 * |
672 * @return the year, from MIN_YEAR to MAX_YEAR |
691 * @return the year, from MIN_YEAR to MAX_YEAR |
673 */ |
692 */ |
674 public int getYear() { |
693 public int getYear() { |
675 return date.getYear(); |
694 return date.getYear(); |
795 |
814 |
796 //----------------------------------------------------------------------- |
815 //----------------------------------------------------------------------- |
797 /** |
816 /** |
798 * Returns an adjusted copy of this date-time. |
817 * Returns an adjusted copy of this date-time. |
799 * <p> |
818 * <p> |
800 * This returns a new {@code LocalDateTime}, based on this one, with the date-time adjusted. |
819 * This returns a {@code LocalDateTime}, based on this one, with the date-time adjusted. |
801 * The adjustment takes place using the specified adjuster strategy object. |
820 * The adjustment takes place using the specified adjuster strategy object. |
802 * Read the documentation of the adjuster to understand what adjustment will be made. |
821 * Read the documentation of the adjuster to understand what adjustment will be made. |
803 * <p> |
822 * <p> |
804 * A simple adjuster might simply set the one of the fields, such as the year field. |
823 * A simple adjuster might simply set the one of the fields, such as the year field. |
805 * A more complex adjuster might set the date to the last day of the month. |
824 * A more complex adjuster might set the date to the last day of the month. |
806 * A selection of common adjustments is provided in {@link java.time.temporal.Adjusters}. |
825 * A selection of common adjustments is provided in {@link java.time.temporal.Adjusters}. |
807 * These include finding the "last day of the month" and "next Wednesday". |
826 * These include finding the "last day of the month" and "next Wednesday". |
808 * Key date-time classes also implement the {@code TemporalAdjuster} interface, |
827 * Key date-time classes also implement the {@code TemporalAdjuster} interface, |
809 * such as {@link Month} and {@link java.time.temporal.MonthDay MonthDay}. |
828 * such as {@link Month} and {@link java.time.MonthDay MonthDay}. |
810 * The adjuster is responsible for handling special cases, such as the varying |
829 * The adjuster is responsible for handling special cases, such as the varying |
811 * lengths of month and leap years. |
830 * lengths of month and leap years. |
812 * <p> |
831 * <p> |
813 * For example this code returns a date on the last day of July: |
832 * For example this code returns a date on the last day of July: |
814 * <pre> |
833 * <pre> |
850 } |
869 } |
851 |
870 |
852 /** |
871 /** |
853 * Returns a copy of this date-time with the specified field set to a new value. |
872 * Returns a copy of this date-time with the specified field set to a new value. |
854 * <p> |
873 * <p> |
855 * This returns a new {@code LocalDateTime}, based on this one, with the value |
874 * This returns a {@code LocalDateTime}, based on this one, with the value |
856 * for the specified field changed. |
875 * for the specified field changed. |
857 * This can be used to change any supported field, such as the year, month or day-of-month. |
876 * This can be used to change any supported field, such as the year, month or day-of-month. |
858 * If it is not possible to set the value, because the field is not supported or for |
877 * If it is not possible to set the value, because the field is not supported or for |
859 * some other reason, an exception is thrown. |
878 * some other reason, an exception is thrown. |
860 * <p> |
879 * <p> |
868 * the matching method on {@link LocalDate#with(TemporalField, long) LocalDate} |
887 * the matching method on {@link LocalDate#with(TemporalField, long) LocalDate} |
869 * or {@link LocalTime#with(TemporalField, long) LocalTime}. |
888 * or {@link LocalTime#with(TemporalField, long) LocalTime}. |
870 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
889 * All other {@code ChronoField} instances will throw a {@code DateTimeException}. |
871 * <p> |
890 * <p> |
872 * If the field is not a {@code ChronoField}, then the result of this method |
891 * If the field is not a {@code ChronoField}, then the result of this method |
873 * is obtained by invoking {@code TemporalField.doWith(Temporal, long)} |
892 * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)} |
874 * passing {@code this} as the argument. In this case, the field determines |
893 * passing {@code this} as the argument. In this case, the field determines |
875 * whether and how to adjust the instant. |
894 * whether and how to adjust the instant. |
876 * <p> |
895 * <p> |
877 * This instance is immutable and unaffected by this method call. |
896 * This instance is immutable and unaffected by this method call. |
878 * |
897 * |
933 * <p> |
952 * <p> |
934 * This instance is immutable and unaffected by this method call. |
953 * This instance is immutable and unaffected by this method call. |
935 * |
954 * |
936 * @param dayOfMonth the day-of-month to set in the result, from 1 to 28-31 |
955 * @param dayOfMonth the day-of-month to set in the result, from 1 to 28-31 |
937 * @return a {@code LocalDateTime} based on this date-time with the requested day, not null |
956 * @return a {@code LocalDateTime} based on this date-time with the requested day, not null |
938 * @throws DateTimeException if the day-of-month value is invalid |
957 * @throws DateTimeException if the day-of-month value is invalid, |
939 * @throws DateTimeException if the day-of-month is invalid for the month-year |
958 * or if the day-of-month is invalid for the month-year |
940 */ |
959 */ |
941 public LocalDateTime withDayOfMonth(int dayOfMonth) { |
960 public LocalDateTime withDayOfMonth(int dayOfMonth) { |
942 return with(date.withDayOfMonth(dayOfMonth), time); |
961 return with(date.withDayOfMonth(dayOfMonth), time); |
943 } |
962 } |
944 |
963 |
948 * <p> |
967 * <p> |
949 * This instance is immutable and unaffected by this method call. |
968 * This instance is immutable and unaffected by this method call. |
950 * |
969 * |
951 * @param dayOfYear the day-of-year to set in the result, from 1 to 365-366 |
970 * @param dayOfYear the day-of-year to set in the result, from 1 to 365-366 |
952 * @return a {@code LocalDateTime} based on this date with the requested day, not null |
971 * @return a {@code LocalDateTime} based on this date with the requested day, not null |
953 * @throws DateTimeException if the day-of-year value is invalid |
972 * @throws DateTimeException if the day-of-year value is invalid, |
954 * @throws DateTimeException if the day-of-year is invalid for the year |
973 * or if the day-of-year is invalid for the year |
955 */ |
974 */ |
956 public LocalDateTime withDayOfYear(int dayOfYear) { |
975 public LocalDateTime withDayOfYear(int dayOfYear) { |
957 return with(date.withDayOfYear(dayOfYear), time); |
976 return with(date.withDayOfYear(dayOfYear), time); |
958 } |
977 } |
959 |
978 |
1021 * Truncation returns a copy of the original date-time with fields |
1040 * Truncation returns a copy of the original date-time with fields |
1022 * smaller than the specified unit set to zero. |
1041 * smaller than the specified unit set to zero. |
1023 * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit |
1042 * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit |
1024 * will set the second-of-minute and nano-of-second field to zero. |
1043 * will set the second-of-minute and nano-of-second field to zero. |
1025 * <p> |
1044 * <p> |
1026 * Not all units are accepted. The {@link ChronoUnit#DAYS days} unit and time |
1045 * The unit must have a {@linkplain TemporalUnit#getDuration() duration} |
1027 * units with an exact duration can be used, other units throw an exception. |
1046 * that divides into the length of a standard day without remainder. |
|
1047 * This includes all supplied time units on {@link ChronoUnit} and |
|
1048 * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception. |
1028 * <p> |
1049 * <p> |
1029 * This instance is immutable and unaffected by this method call. |
1050 * This instance is immutable and unaffected by this method call. |
1030 * |
1051 * |
1031 * @param unit the unit to truncate to, not null |
1052 * @param unit the unit to truncate to, not null |
1032 * @return a {@code LocalDateTime} based on this date-time with the time truncated, not null |
1053 * @return a {@code LocalDateTime} based on this date-time with the time truncated, not null |
1036 return with(date, time.truncatedTo(unit)); |
1057 return with(date, time.truncatedTo(unit)); |
1037 } |
1058 } |
1038 |
1059 |
1039 //----------------------------------------------------------------------- |
1060 //----------------------------------------------------------------------- |
1040 /** |
1061 /** |
1041 * Returns a copy of this date-time with the specified period added. |
1062 * Returns a copy of this date-time with the specified amount added. |
1042 * <p> |
1063 * <p> |
1043 * This method returns a new date-time based on this time with the specified period added. |
1064 * This returns a {@code LocalDateTime}, based on this one, with the specified amount added. |
1044 * The adder is typically {@link Period} but may be any other type implementing |
1065 * The amount is typically {@link Period} or {@link Duration} but may be |
1045 * the {@link TemporalAdder} interface. |
1066 * any other type implementing the {@link TemporalAmount} interface. |
1046 * The calculation is delegated to the specified adjuster, which typically calls |
1067 * <p> |
1047 * back to {@link #plus(long, TemporalUnit)}. |
1068 * The calculation is delegated to the amount object by calling |
1048 * <p> |
1069 * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free |
1049 * This instance is immutable and unaffected by this method call. |
1070 * to implement the addition in any way it wishes, however it typically |
1050 * |
1071 * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation |
1051 * @param adder the adder to use, not null |
1072 * of the amount implementation to determine if it can be successfully added. |
|
1073 * <p> |
|
1074 * This instance is immutable and unaffected by this method call. |
|
1075 * |
|
1076 * @param amountToAdd the amount to add, not null |
1052 * @return a {@code LocalDateTime} based on this date-time with the addition made, not null |
1077 * @return a {@code LocalDateTime} based on this date-time with the addition made, not null |
1053 * @throws DateTimeException if the addition cannot be made |
1078 * @throws DateTimeException if the addition cannot be made |
1054 * @throws ArithmeticException if numeric overflow occurs |
1079 * @throws ArithmeticException if numeric overflow occurs |
1055 */ |
1080 */ |
1056 @Override |
1081 @Override |
1057 public LocalDateTime plus(TemporalAdder adder) { |
1082 public LocalDateTime plus(TemporalAmount amountToAdd) { |
1058 return (LocalDateTime) adder.addTo(this); |
1083 return (LocalDateTime) amountToAdd.addTo(this); |
1059 } |
1084 } |
1060 |
1085 |
1061 /** |
1086 /** |
1062 * Returns a copy of this date-time with the specified period added. |
1087 * Returns a copy of this date-time with the specified amount added. |
1063 * <p> |
1088 * <p> |
1064 * This method returns a new date-time based on this date-time with the specified period added. |
1089 * This returns a {@code LocalDateTime}, based on this one, with the amount |
1065 * This can be used to add any period that is defined by a unit, for example to add years, months or days. |
1090 * in terms of the unit added. If it is not possible to add the amount, because the |
1066 * The unit is responsible for the details of the calculation, including the resolution |
1091 * unit is not supported or for some other reason, an exception is thrown. |
1067 * of any edge cases in the calculation. |
1092 * <p> |
|
1093 * If the field is a {@link ChronoUnit} then the addition is implemented here. |
|
1094 * Date units are added as per {@link LocalDate#plus(long, TemporalUnit)}. |
|
1095 * Time units are added as per {@link LocalTime#plus(long, TemporalUnit)} with |
|
1096 * any overflow in days added equivalent to using {@link #plusDays(long)}. |
|
1097 * <p> |
|
1098 * If the field is not a {@code ChronoUnit}, then the result of this method |
|
1099 * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)} |
|
1100 * passing {@code this} as the argument. In this case, the unit determines |
|
1101 * whether and how to perform the addition. |
1068 * <p> |
1102 * <p> |
1069 * This instance is immutable and unaffected by this method call. |
1103 * This instance is immutable and unaffected by this method call. |
1070 * |
1104 * |
1071 * @param amountToAdd the amount of the unit to add to the result, may be negative |
1105 * @param amountToAdd the amount of the unit to add to the result, may be negative |
1072 * @param unit the unit of the period to add, not null |
1106 * @param unit the unit of the amount to add, not null |
1073 * @return a {@code LocalDateTime} based on this date-time with the specified period added, not null |
1107 * @return a {@code LocalDateTime} based on this date-time with the specified amount added, not null |
1074 * @throws DateTimeException if the unit cannot be added to this type |
1108 * @throws DateTimeException if the addition cannot be made |
|
1109 * @throws ArithmeticException if numeric overflow occurs |
1075 */ |
1110 */ |
1076 @Override |
1111 @Override |
1077 public LocalDateTime plus(long amountToAdd, TemporalUnit unit) { |
1112 public LocalDateTime plus(long amountToAdd, TemporalUnit unit) { |
1078 if (unit instanceof ChronoUnit) { |
1113 if (unit instanceof ChronoUnit) { |
1079 ChronoUnit f = (ChronoUnit) unit; |
1114 ChronoUnit f = (ChronoUnit) unit; |
1235 return plusWithOverflow(date, 0, 0, 0, nanos, 1); |
1270 return plusWithOverflow(date, 0, 0, 0, nanos, 1); |
1236 } |
1271 } |
1237 |
1272 |
1238 //----------------------------------------------------------------------- |
1273 //----------------------------------------------------------------------- |
1239 /** |
1274 /** |
1240 * Returns a copy of this date-time with the specified period subtracted. |
1275 * Returns a copy of this date-time with the specified amount subtracted. |
1241 * <p> |
1276 * <p> |
1242 * This method returns a new date-time based on this time with the specified period subtracted. |
1277 * This returns a {@code LocalDateTime}, based on this one, with the specified amount subtracted. |
1243 * The subtractor is typically {@link Period} but may be any other type implementing |
1278 * The amount is typically {@link Period} or {@link Duration} but may be |
1244 * the {@link TemporalSubtractor} interface. |
1279 * any other type implementing the {@link TemporalAmount} interface. |
1245 * The calculation is delegated to the specified adjuster, which typically calls |
1280 * <p> |
1246 * back to {@link #minus(long, TemporalUnit)}. |
1281 * The calculation is delegated to the amount object by calling |
1247 * <p> |
1282 * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free |
1248 * This instance is immutable and unaffected by this method call. |
1283 * to implement the subtraction in any way it wishes, however it typically |
1249 * |
1284 * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation |
1250 * @param subtractor the subtractor to use, not null |
1285 * of the amount implementation to determine if it can be successfully subtracted. |
|
1286 * <p> |
|
1287 * This instance is immutable and unaffected by this method call. |
|
1288 * |
|
1289 * @param amountToSubtract the amount to subtract, not null |
1251 * @return a {@code LocalDateTime} based on this date-time with the subtraction made, not null |
1290 * @return a {@code LocalDateTime} based on this date-time with the subtraction made, not null |
1252 * @throws DateTimeException if the subtraction cannot be made |
1291 * @throws DateTimeException if the subtraction cannot be made |
1253 * @throws ArithmeticException if numeric overflow occurs |
1292 * @throws ArithmeticException if numeric overflow occurs |
1254 */ |
1293 */ |
1255 @Override |
1294 @Override |
1256 public LocalDateTime minus(TemporalSubtractor subtractor) { |
1295 public LocalDateTime minus(TemporalAmount amountToSubtract) { |
1257 return (LocalDateTime) subtractor.subtractFrom(this); |
1296 return (LocalDateTime) amountToSubtract.subtractFrom(this); |
1258 } |
1297 } |
1259 |
1298 |
1260 /** |
1299 /** |
1261 * Returns a copy of this date-time with the specified period subtracted. |
1300 * Returns a copy of this date-time with the specified amount subtracted. |
1262 * <p> |
1301 * <p> |
1263 * This method returns a new date-time based on this date-time with the specified period subtracted. |
1302 * This returns a {@code LocalDateTime}, based on this one, with the amount |
1264 * This can be used to subtract any period that is defined by a unit, for example to subtract years, months or days. |
1303 * in terms of the unit subtracted. If it is not possible to subtract the amount, |
1265 * The unit is responsible for the details of the calculation, including the resolution |
1304 * because the unit is not supported or for some other reason, an exception is thrown. |
1266 * of any edge cases in the calculation. |
1305 * <p> |
|
1306 * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated. |
|
1307 * See that method for a full description of how addition, and thus subtraction, works. |
1267 * <p> |
1308 * <p> |
1268 * This instance is immutable and unaffected by this method call. |
1309 * This instance is immutable and unaffected by this method call. |
1269 * |
1310 * |
1270 * @param amountToSubtract the amount of the unit to subtract from the result, may be negative |
1311 * @param amountToSubtract the amount of the unit to subtract from the result, may be negative |
1271 * @param unit the unit of the period to subtract, not null |
1312 * @param unit the unit of the amount to subtract, not null |
1272 * @return a {@code LocalDateTime} based on this date-time with the specified period subtracted, not null |
1313 * @return a {@code LocalDateTime} based on this date-time with the specified amount subtracted, not null |
1273 * @throws DateTimeException if the unit cannot be added to this type |
1314 * @throws DateTimeException if the subtraction cannot be made |
|
1315 * @throws ArithmeticException if numeric overflow occurs |
1274 */ |
1316 */ |
1275 @Override |
1317 @Override |
1276 public LocalDateTime minus(long amountToSubtract, TemporalUnit unit) { |
1318 public LocalDateTime minus(long amountToSubtract, TemporalUnit unit) { |
1277 return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); |
1319 return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); |
1278 } |
1320 } |
1470 * @param query the query to invoke, not null |
1512 * @param query the query to invoke, not null |
1471 * @return the query result, null may be returned (defined by the query) |
1513 * @return the query result, null may be returned (defined by the query) |
1472 * @throws DateTimeException if unable to query (defined by the query) |
1514 * @throws DateTimeException if unable to query (defined by the query) |
1473 * @throws ArithmeticException if numeric overflow occurs (defined by the query) |
1515 * @throws ArithmeticException if numeric overflow occurs (defined by the query) |
1474 */ |
1516 */ |
|
1517 @SuppressWarnings("unchecked") |
1475 @Override // override for Javadoc |
1518 @Override // override for Javadoc |
1476 public <R> R query(TemporalQuery<R> query) { |
1519 public <R> R query(TemporalQuery<R> query) { |
|
1520 if (query == Queries.localDate()) { |
|
1521 return (R) date; |
|
1522 } |
1477 return ChronoLocalDateTime.super.query(query); |
1523 return ChronoLocalDateTime.super.query(query); |
1478 } |
1524 } |
1479 |
1525 |
1480 /** |
1526 /** |
1481 * Adjusts the specified temporal object to have the same date and time as this object. |
1527 * Adjusts the specified temporal object to have the same date and time as this object. |
1521 * The calculation returns a whole number, representing the number of |
1567 * The calculation returns a whole number, representing the number of |
1522 * complete units between the two date-times. |
1568 * complete units between the two date-times. |
1523 * For example, the period in months between 2012-06-15T00:00 and 2012-08-14T23:59 |
1569 * For example, the period in months between 2012-06-15T00:00 and 2012-08-14T23:59 |
1524 * will only be one month as it is one minute short of two months. |
1570 * will only be one month as it is one minute short of two months. |
1525 * <p> |
1571 * <p> |
1526 * This method operates in association with {@link TemporalUnit#between}. |
1572 * There are two equivalent ways of using this method. |
1527 * The result of this method is a {@code long} representing the amount of |
1573 * The first is to invoke this method. |
1528 * the specified unit. By contrast, the result of {@code between} is an |
1574 * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}: |
1529 * object that can be used directly in addition/subtraction: |
|
1530 * <pre> |
1575 * <pre> |
1531 * long period = start.periodUntil(end, MONTHS); // this method |
1576 * // these two lines are equivalent |
1532 * dateTime.plus(MONTHS.between(start, end)); // use in plus/minus |
1577 * amount = start.periodUntil(end, MONTHS); |
|
1578 * amount = MONTHS.between(start, end); |
1533 * </pre> |
1579 * </pre> |
|
1580 * The choice should be made based on which makes the code more readable. |
1534 * <p> |
1581 * <p> |
1535 * The calculation is implemented in this method for {@link ChronoUnit}. |
1582 * The calculation is implemented in this method for {@link ChronoUnit}. |
1536 * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS}, |
1583 * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS}, |
1537 * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS}, |
1584 * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS}, |
1538 * {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES}, |
1585 * {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES}, |
1578 if (end.time.isBefore(time)) { |
1625 if (end.time.isBefore(time)) { |
1579 endDate = endDate.minusDays(1); |
1626 endDate = endDate.minusDays(1); |
1580 } |
1627 } |
1581 return date.periodUntil(endDate, unit); |
1628 return date.periodUntil(endDate, unit); |
1582 } |
1629 } |
1583 return unit.between(this, endDateTime).getAmount(); |
1630 return unit.between(this, endDateTime); |
1584 } |
1631 } |
1585 |
1632 |
1586 //----------------------------------------------------------------------- |
1633 //----------------------------------------------------------------------- |
1587 /** |
1634 /** |
1588 * Returns an offset date-time formed from this date-time and the specified offset. |
1635 * Combines this time with a date to create an {@code OffsetTime}. |
1589 * <p> |
1636 * <p> |
1590 * This combines this date-time with the specified offset to form an {@code OffsetDateTime}. |
1637 * This returns an {@code OffsetTime} formed from this time at the specified offset. |
1591 * All possible combinations of date-time and offset are valid. |
1638 * All possible combinations of date-time and offset are valid. |
1592 * <p> |
|
1593 * This instance is immutable and unaffected by this method call. |
|
1594 * |
1639 * |
1595 * @param offset the offset to combine with, not null |
1640 * @param offset the offset to combine with, not null |
1596 * @return the offset date-time formed from this date-time and the specified offset, not null |
1641 * @return the offset date-time formed from this date-time and the specified offset, not null |
1597 */ |
1642 */ |
1598 public OffsetDateTime atOffset(ZoneOffset offset) { |
1643 public OffsetDateTime atOffset(ZoneOffset offset) { |
1599 return OffsetDateTime.of(this, offset); |
1644 return OffsetDateTime.of(this, offset); |
1600 } |
1645 } |
1601 |
1646 |
1602 /** |
1647 /** |
1603 * Returns a zoned date-time formed from this date-time and the specified time-zone. |
1648 * Combines this time with a time-zone to create a {@code ZonedDateTime}. |
1604 * <p> |
1649 * <p> |
1605 * This creates a zoned date-time matching the input date-time as closely as possible. |
1650 * This returns a {@code ZonedDateTime} formed from this date-time at the |
|
1651 * specified time-zone. The result will match this date-time as closely as possible. |
1606 * Time-zone rules, such as daylight savings, mean that not every local date-time |
1652 * Time-zone rules, such as daylight savings, mean that not every local date-time |
1607 * is valid for the specified zone, thus the local date-time may be adjusted. |
1653 * is valid for the specified zone, thus the local date-time may be adjusted. |
1608 * <p> |
1654 * <p> |
1609 * The local date-time is resolved to a single instant on the time-line. |
1655 * The local date-time is resolved to a single instant on the time-line. |
1610 * This is achieved by finding a valid offset from UTC/Greenwich for the local |
1656 * This is achieved by finding a valid offset from UTC/Greenwich for the local |
1621 * <p> |
1667 * <p> |
1622 * To obtain the later offset during an overlap, call |
1668 * To obtain the later offset during an overlap, call |
1623 * {@link ZonedDateTime#withLaterOffsetAtOverlap()} on the result of this method. |
1669 * {@link ZonedDateTime#withLaterOffsetAtOverlap()} on the result of this method. |
1624 * To throw an exception when there is a gap or overlap, use |
1670 * To throw an exception when there is a gap or overlap, use |
1625 * {@link ZonedDateTime#ofStrict(LocalDateTime, ZoneOffset, ZoneId)}. |
1671 * {@link ZonedDateTime#ofStrict(LocalDateTime, ZoneOffset, ZoneId)}. |
1626 * <p> |
|
1627 * This instance is immutable and unaffected by this method call. |
|
1628 * |
1672 * |
1629 * @param zone the time-zone to use, not null |
1673 * @param zone the time-zone to use, not null |
1630 * @return the zoned date-time formed from this date-time, not null |
1674 * @return the zoned date-time formed from this date-time, not null |
1631 */ |
1675 */ |
1632 @Override |
1676 @Override |