370 D minusDays(long daysToSubtract) { |
370 D minusDays(long daysToSubtract) { |
371 return (daysToSubtract == Long.MIN_VALUE ? ((ChronoLocalDateImpl<D>)plusDays(Long.MAX_VALUE)).plusDays(1) : plusDays(-daysToSubtract)); |
371 return (daysToSubtract == Long.MIN_VALUE ? ((ChronoLocalDateImpl<D>)plusDays(Long.MAX_VALUE)).plusDays(1) : plusDays(-daysToSubtract)); |
372 } |
372 } |
373 |
373 |
374 //----------------------------------------------------------------------- |
374 //----------------------------------------------------------------------- |
375 /** |
375 @Override |
376 * {@inheritDoc} |
376 public long until(Temporal endExclusive, TemporalUnit unit) { |
377 * @throws DateTimeException {@inheritDoc} |
377 Objects.requireNonNull(endExclusive, "endExclusive"); |
378 * @throws ArithmeticException {@inheritDoc} |
378 ChronoLocalDate end = getChronology().date(endExclusive); |
379 */ |
|
380 @Override |
|
381 public long until(Temporal endDateTime, TemporalUnit unit) { |
|
382 Objects.requireNonNull(endDateTime, "endDateTime"); |
|
383 Objects.requireNonNull(unit, "unit"); |
|
384 if (endDateTime instanceof ChronoLocalDate == false) { |
|
385 throw new DateTimeException("Unable to calculate amount as objects are of two different types"); |
|
386 } |
|
387 ChronoLocalDate end = (ChronoLocalDate) endDateTime; |
|
388 if (getChronology().equals(end.getChronology()) == false) { |
|
389 throw new DateTimeException("Unable to calculate amount as objects have different chronologies"); |
|
390 } |
|
391 if (unit instanceof ChronoUnit) { |
379 if (unit instanceof ChronoUnit) { |
392 switch ((ChronoUnit) unit) { |
380 switch ((ChronoUnit) unit) { |
393 case DAYS: return daysUntil(end); |
381 case DAYS: return daysUntil(end); |
394 case WEEKS: return daysUntil(end) / 7; |
382 case WEEKS: return daysUntil(end) / 7; |
395 case MONTHS: return monthsUntil(end); |
383 case MONTHS: return monthsUntil(end); |
399 case MILLENNIA: return monthsUntil(end) / 12000; |
387 case MILLENNIA: return monthsUntil(end) / 12000; |
400 case ERAS: return end.getLong(ERA) - getLong(ERA); |
388 case ERAS: return end.getLong(ERA) - getLong(ERA); |
401 } |
389 } |
402 throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); |
390 throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); |
403 } |
391 } |
404 return unit.between(this, endDateTime); |
392 Objects.requireNonNull(unit, "unit"); |
|
393 return unit.between(this, end); |
405 } |
394 } |
406 |
395 |
407 private long daysUntil(ChronoLocalDate end) { |
396 private long daysUntil(ChronoLocalDate end) { |
408 return end.toEpochDay() - toEpochDay(); // no overflow |
397 return end.toEpochDay() - toEpochDay(); // no overflow |
409 } |
398 } |