@Override public Instant plus(long amountToAdd, PeriodUnit unit) { if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plus(amountToAdd / 1000000, (amountToAdd % 1000000) * 1000); case MILLIS: return plusMillis(amountToAdd); case SECONDS: return plusSeconds(amountToAdd); case MINUTES: return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_MINUTE)); case HOURS: return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_HOUR)); case HALF_DAYS: return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_DAY / 2)); case DAYS: return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_DAY)); } throw new DateTimeException("Unsupported unit: " + unit.getName()); } return unit.doPlus(this, amountToAdd); }
/** * Returns a copy of this {@code LocalTime} with the time truncated. * <p> * Truncating the time returns a copy of the original time with fields smaller than the specified unit set * to zero. For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit will set the * second-of-minute and nano-of-second field to zero. * <p> * Not all units are accepted. The {@link ChronoUnit#DAYS days} unit and time units with an exact duration * can be used, other units throw an exception. * <p> * This instance is immutable and unaffected by this method call. * * @param unit the unit to truncate to, not null * @return a {@code LocalTime} based on this time with the time truncated, not null * @throws DateTimeException if unable to truncate */ public LocalTime truncatedTo(PeriodUnit unit) { if (unit == ChronoUnit.NANOS) { return this; } else if (unit == ChronoUnit.DAYS) { return MIDNIGHT; } else if (unit.isDurationEstimated()) { throw new DateTimeException("Unit must not have an estimated duration"); } long nod = toNanoOfDay(); long dur = unit.getDuration().toNanos(); if (dur >= NANOS_PER_DAY) { throw new DateTimeException("Unit must not be a date unit"); } nod = (nod / dur) * dur; return ofNanoOfDay(nod); }
@Override public ChronoDateTimeImpl<C> plus(long amountToAdd, PeriodUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000); case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000000); case SECONDS: return plusSeconds(amountToAdd); case MINUTES: return plusMinutes(amountToAdd); case HOURS: return plusHours(amountToAdd); case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is // multiple of 2) } return with(this.date.plus(amountToAdd, unit), this.time); } return this.date.getChrono().ensureChronoLocalDateTime(unit.doPlus(this, amountToAdd)); }
@Override public long periodUntil(DateTime endDateTime, PeriodUnit unit) { if (endDateTime instanceof ChronoZonedDateTime == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } @SuppressWarnings("unchecked") ChronoZonedDateTime<C> end = (ChronoZonedDateTime<C>) endDateTime; if (getDate().getChrono().equals(end.getDate().getChrono()) == false) { throw new DateTimeException("Unable to calculate period between two different chronologies"); } if (unit instanceof ChronoUnit) { end = end.withZoneSameInstant(this.offset); return this.dateTime.periodUntil(end.getDateTime(), unit); } return unit.between(this, endDateTime).getAmount(); }
@Override public Year plus(long amountToAdd, PeriodUnit unit) { if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case YEARS: return plusYears(amountToAdd); case DECADES: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 10)); case CENTURIES: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 100)); case MILLENNIA: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 1000)); case ERAS: return with(ERA, Jdk8Methods.safeAdd(getLong(ERA), amountToAdd)); } throw new DateTimeException("Unsupported unit: " + unit.getName()); } return unit.doPlus(this, amountToAdd); }
@Override public long periodUntil(DateTime endDateTime, PeriodUnit unit) { if (endDateTime instanceof Year == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } Year end = (Year) endDateTime; if (unit instanceof ChronoUnit) { long yearsUntil = ((long) end.year) - this.year; // no overflow switch ((ChronoUnit) unit) { case YEARS: return yearsUntil; case DECADES: return yearsUntil / 10; case CENTURIES: return yearsUntil / 100; case MILLENNIA: return yearsUntil / 1000; case ERAS: return end.getLong(ERA) - getLong(ERA); } throw new DateTimeException("Unsupported unit: " + unit.getName()); } return unit.between(this, endDateTime).getAmount(); }
@Test(groups = { "tck" }, dataProvider = "calendars") public void test_badPlusPeriodUnitChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDate date = chrono.date(refDate); for (Chrono[] clist : data_of_calendars()) { Chrono chrono2 = clist[0]; ChronoLocalDate<?> date2 = chrono2.date(refDate); PeriodUnit adjuster = new FixedPeriodUnit(date2); if (chrono != chrono2) { try { ChronoLocalDate<?> notreached = date.plus(1, adjuster); Assert.fail("PeriodUnit.doPlus plus should have thrown a ClassCastException" + date.getClass() + ", can not be cast to " + date2.getClass()); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDate<?> result = date.plus(1, adjuster); assertEquals(result, date2, "WithAdjuster failed to replace date"); } } }
@Test(groups = { "tck" }, dataProvider = "calendars") public void test_badMinusPeriodUnitChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDate date = chrono.date(refDate); for (Chrono[] clist : data_of_calendars()) { Chrono chrono2 = clist[0]; ChronoLocalDate<?> date2 = chrono2.date(refDate); PeriodUnit adjuster = new FixedPeriodUnit(date2); if (chrono != chrono2) { try { ChronoLocalDate<?> notreached = date.minus(1, adjuster); Assert.fail("PeriodUnit.doAdd minus should have thrown a ClassCastException" + date.getClass() + ", can not be cast to " + date2.getClass()); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDate<?> result = date.minus(1, adjuster); assertEquals(result, date2, "WithAdjuster failed to replace date"); } } }
@Test(groups = { "tck" }, dataProvider = "calendars") public void test_badPlusPeriodUnitChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); for (Chrono[] clist : data_of_calendars()) { Chrono chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); PeriodUnit adjuster = new FixedPeriodUnit(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.plus(1, adjuster); Assert.fail("PeriodUnit.doPlus plus should have thrown a ClassCastException" + cdt + ", can not be cast to " + cdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDateTime<?> result = cdt.plus(1, adjuster); assertEquals(result, cdt2, "WithAdjuster failed to replace date"); } } }
@Test(groups = { "tck" }, dataProvider = "calendars") public void test_badPlusPeriodUnitChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); for (Chrono[] clist : data_of_calendars()) { Chrono chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); PeriodUnit adjuster = new FixedPeriodUnit(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.plus(1, adjuster); Assert.fail("PeriodUnit.doAdd plus should have thrown a ClassCastException" + cdt + ", can not be cast to " + cdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDateTime<?> result = cdt.plus(1, adjuster); assertEquals(result, cdt2, "WithAdjuster failed to replace date"); } } }
@Test(groups = { "tck" }, dataProvider = "calendars") public void test_badPlusPeriodUnitChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDate date = chrono.date(refDate); for (Chrono[] clist : data_of_calendars()) { Chrono chrono2 = clist[0]; ChronoLocalDate<?> date2 = chrono2.date(refDate); PeriodUnit adjuster = new FixedPeriodUnit(date2); if (chrono != chrono2) { try { ChronoLocalDate<?> notreached = date.plus(1, adjuster); Assert.fail("PeriodUnit.doAdd plus should have thrown a ClassCastException" + date.getClass() + ", can not be cast to " + date2.getClass()); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDate<?> result = date.plus(1, adjuster); assertEquals(result, date2, "WithAdjuster failed to replace date"); } } }
@Test(groups = { "tck" }, dataProvider = "calendars") public void test_badMinusPeriodUnitChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); for (Chrono[] clist : data_of_calendars()) { Chrono chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); PeriodUnit adjuster = new FixedPeriodUnit(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.minus(1, adjuster); Assert.fail("PeriodUnit.doPlus minus should have thrown a ClassCastException" + cdt.getClass() + ", can not be cast to " + cdt2.getClass()); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDateTime<?> result = cdt.minus(1, adjuster); assertEquals(result, cdt2, "WithAdjuster failed to replace date"); } } }
@Test(groups = { "tck" }, dataProvider = "calendars") public void test_badMinusPeriodUnitChrono(Chrono chrono) { LocalDate refDate = LocalDate.of(1900, 1, 1); ChronoLocalDateTime cdt = chrono.date(refDate).atTime(LocalTime.NOON); for (Chrono[] clist : data_of_calendars()) { Chrono chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); PeriodUnit adjuster = new FixedPeriodUnit(cdt2); if (chrono != chrono2) { try { ChronoLocalDateTime<?> notreached = cdt.minus(1, adjuster); Assert.fail("PeriodUnit.doAdd minus should have thrown a ClassCastException" + cdt.getClass() + ", can not be cast to " + cdt2.getClass()); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDateTime<?> result = cdt.minus(1, adjuster); assertEquals(result, cdt2, "WithAdjuster failed to replace date"); } } }
@Override public long periodUntil(DateTime endDateTime, PeriodUnit unit) { if (endDateTime instanceof OffsetTime == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } if (unit instanceof ChronoUnit) { OffsetTime end = (OffsetTime) endDateTime; long nanosUntil = end.toEpochNano() - toEpochNano(); // no overflow switch ((ChronoUnit) unit) { case NANOS: return nanosUntil; case MICROS: return nanosUntil / 1000; case MILLIS: return nanosUntil / 1000000; case SECONDS: return nanosUntil / NANOS_PER_SECOND; case MINUTES: return nanosUntil / NANOS_PER_MINUTE; case HOURS: return nanosUntil / NANOS_PER_HOUR; case HALF_DAYS: return nanosUntil / (12 * NANOS_PER_HOUR); } throw new DateTimeException("Unsupported unit: " + unit.getName()); } return unit.between(this, endDateTime).getAmount(); }
@Override public YearMonth plus(long amountToAdd, PeriodUnit unit) { if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case MONTHS: return plusMonths(amountToAdd); case QUARTER_YEARS: return plusYears(amountToAdd / 256).plusMonths((amountToAdd % 256) * 3); // no overflow (256 is // multiple of 4) case HALF_YEARS: return plusYears(amountToAdd / 256).plusMonths((amountToAdd % 256) * 6); // no overflow (256 is // multiple of 2) case YEARS: return plusYears(amountToAdd); case DECADES: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 10)); case CENTURIES: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 100)); case MILLENNIA: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 1000)); case ERAS: return with(ERA, Jdk8Methods.safeAdd(getLong(ERA), amountToAdd)); } throw new DateTimeException("Unsupported unit: " + unit.getName()); } return unit.doPlus(this, amountToAdd); }
@Override public long periodUntil(DateTime endDateTime, PeriodUnit unit) { if (endDateTime instanceof YearMonth == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } YearMonth end = (YearMonth) endDateTime; if (unit instanceof ChronoUnit) { long monthsUntil = end.getEpochMonth() - getEpochMonth(); // no overflow switch ((ChronoUnit) unit) { case MONTHS: return monthsUntil; case QUARTER_YEARS: return monthsUntil / 3; case HALF_YEARS: return monthsUntil / 6; case YEARS: return monthsUntil / 12; case DECADES: return monthsUntil / 120; case CENTURIES: return monthsUntil / 1200; case MILLENNIA: return monthsUntil / 12000; case ERAS: return end.getLong(ERA) - getLong(ERA); } throw new DateTimeException("Unsupported unit: " + unit.getName()); } return unit.between(this, endDateTime).getAmount(); }
@Override public long periodUntil(DateTime endDateTime, PeriodUnit unit) { if (endDateTime instanceof Instant == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } Instant end = (Instant) endDateTime; if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case NANOS: return nanosUntil(end); case MICROS: return nanosUntil(end) / 1000; case MILLIS: return Jdk8Methods.safeSubtract(end.toEpochMilli(), toEpochMilli()); case SECONDS: return secondsUntil(end); case MINUTES: return secondsUntil(end) / SECONDS_PER_MINUTE; case HOURS: return secondsUntil(end) / SECONDS_PER_HOUR; case HALF_DAYS: return secondsUntil(end) / (12 * SECONDS_PER_HOUR); case DAYS: return secondsUntil(end) / (SECONDS_PER_DAY); } throw new DateTimeException("Unsupported unit: " + unit.getName()); } return unit.between(this, endDateTime).getAmount(); }
@Override public long periodUntil(DateTime endDateTime, PeriodUnit unit) { if (endDateTime instanceof OffsetDateTime == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } if (unit instanceof ChronoUnit) { OffsetDateTime end = (OffsetDateTime) endDateTime; end = end.withOffsetSameInstant(this.offset); return this.dateTime.periodUntil(end.dateTime, unit); } return unit.between(this, endDateTime).getAmount(); }
/** * Returns a copy of this period with the specified period added. * <p> * The specified unit must be one of the supported units from {@link ChronoUnit}, {@code YEARS}, * {@code MONTHS} or {@code DAYS} or be a time unit with an {@link PeriodUnit#isDurationEstimated() exact * duration}. Other units throw an exception. * <p> * This instance is immutable and unaffected by this method call. * * @param amount the amount to add, positive or negative * @param unit the unit that the amount is expressed in, not null * @return a {@code Period} based on this period with the requested amount added, not null * @throws ArithmeticException if numeric overflow occurs */ public Period plus(long amount, PeriodUnit unit) { Jdk7Methods.Objects_requireNonNull(unit, "unit"); if (unit instanceof ChronoUnit) { if (unit == YEARS || unit == MONTHS || unit == DAYS || unit.isDurationEstimated() == false) { if (amount == 0) { return this; } switch ((ChronoUnit) unit) { case NANOS: return plusNanos(amount); case MICROS: return plusNanos(Jdk8Methods.safeMultiply(amount, 1000L)); case MILLIS: return plusNanos(Jdk8Methods.safeMultiply(amount, 1000000L)); case SECONDS: return plusSeconds(amount); case MINUTES: return plusMinutes(amount); case HOURS: return plusHours(amount); case HALF_DAYS: return plusNanos(Jdk8Methods.safeMultiply(amount, 12 * NANOS_PER_HOUR)); case DAYS: return plusDays(amount); case MONTHS: return plusMonths(amount); case YEARS: return plusYears(amount); default : throw new DateTimeException("Unsupported unit: " + unit.getName()); } } } if (unit.isDurationEstimated()) { throw new DateTimeException("Unsupported unit: " + unit.getName()); } return plusNanos(Duration.of(amount, unit).toNanos()); }
/** * Returns a copy of this duration with the specified duration added. * <p> * The duration amount is measured in terms of the specified unit. Only a subset of units are accepted by * this method. The unit must either have an {@link PeriodUnit#isDurationEstimated() exact duration} or be * {@link ChronoUnit#DAYS} which is treated as 24 hours. Other units throw an exception. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToAdd the amount of the period, measured in terms of the unit, positive or negative * @param unit the unit that the period is measured in, must have an exact duration, not null * @return a {@code Duration} based on this duration with the specified duration added, not null * @throws ArithmeticException if numeric overflow occurs */ public Duration plus(long amountToAdd, PeriodUnit unit) { Jdk7Methods.Objects_requireNonNull(unit, "unit"); if (unit == DAYS) { return plus(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_DAY), 0); } if (unit.isDurationEstimated()) { throw new DateTimeException("Unit must not have an estimated duration"); } if (amountToAdd == 0) { return this; } if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plusSeconds((amountToAdd / (1000000L * 1000)) * 1000).plusNanos( (amountToAdd % (1000000L * 1000)) * 1000); case MILLIS: return plusMillis(amountToAdd); case SECONDS: return plusSeconds(amountToAdd); } return plusSeconds(Jdk8Methods.safeMultiply(unit.getDuration().seconds, amountToAdd)); } Duration duration = unit.getDuration().multipliedBy(amountToAdd); return plusSeconds(duration.getSeconds()).plusNanos(duration.getNano()); }
/** * Returns a copy of this time with the specified period added. * <p> * This method returns a new time based on this time with the specified period added. This can be used to * add any period that is defined by a unit, for example to add hours, minutes or seconds. The unit is * responsible for the details of the calculation, including the resolution of any edge cases in the * calculation. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToAdd the amount of the unit to add to the result, may be negative * @param unit the unit of the period to add, not null * @return a {@code LocalTime} based on this time with the specified period added, not null * @throws DateTimeException if the unit cannot be added to this type */ @Override public LocalTime plus(long amountToAdd, PeriodUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plusNanos((amountToAdd % MICROS_PER_DAY) * 1000); case MILLIS: return plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000000); case SECONDS: return plusSeconds(amountToAdd); case MINUTES: return plusMinutes(amountToAdd); case HOURS: return plusHours(amountToAdd); case HALF_DAYS: return plusHours((amountToAdd % 2) * 12); case DAYS: return this; } throw new DateTimeException("Unsupported unit: " + unit.getName()); } return unit.doPlus(this, amountToAdd); }
@Override public long periodUntil(DateTime endDateTime, PeriodUnit unit) { if (endDateTime instanceof LocalTime == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } LocalTime end = (LocalTime) endDateTime; if (unit instanceof ChronoUnit) { long nanosUntil = end.toNanoOfDay() - toNanoOfDay(); // no overflow switch ((ChronoUnit) unit) { case NANOS: return nanosUntil; case MICROS: return nanosUntil / 1000; case MILLIS: return nanosUntil / 1000000; case SECONDS: return nanosUntil / NANOS_PER_SECOND; case MINUTES: return nanosUntil / NANOS_PER_MINUTE; case HOURS: return nanosUntil / NANOS_PER_HOUR; case HALF_DAYS: return nanosUntil / (12 * NANOS_PER_HOUR); } throw new DateTimeException("Unsupported unit: " + unit.getName()); } return unit.between(this, endDateTime).getAmount(); }
/** * Returns a copy of this date with the specified period added. * <p> * This method returns a new date based on this date with the specified period added. This can be used to * add any period that is defined by a unit, for example to add years, months or days. The unit is * responsible for the details of the calculation, including the resolution of any edge cases in the * calculation. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToAdd the amount of the unit to add to the result, may be negative * @param unit the unit of the period to add, not null * @return a {@code LocalDate} based on this date with the specified period added, not null * @throws DateTimeException if the unit cannot be added to this type */ @Override public LocalDate plus(long amountToAdd, PeriodUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case DAYS: return plusDays(amountToAdd); case WEEKS: return plusWeeks(amountToAdd); case MONTHS: return plusMonths(amountToAdd); case QUARTER_YEARS: return plusYears(amountToAdd / 256).plusMonths((amountToAdd % 256) * 3); // no overflow (256 is // multiple of 4) case HALF_YEARS: return plusYears(amountToAdd / 256).plusMonths((amountToAdd % 256) * 6); // no overflow (256 is // multiple of 2) case YEARS: return plusYears(amountToAdd); case DECADES: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 10)); case CENTURIES: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 100)); case MILLENNIA: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 1000)); case ERAS: return with(ERA, Jdk8Methods.safeAdd(getLong(ERA), amountToAdd)); } throw new DateTimeException("Unsupported unit: " + unit.getName()); } return unit.doPlus(this, amountToAdd); }
@Override public long periodUntil(DateTime endDateTime, PeriodUnit unit) { if (endDateTime instanceof LocalDate == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } LocalDate end = (LocalDate) endDateTime; if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case DAYS: return daysUntil(end); case WEEKS: return daysUntil(end) / 7; case MONTHS: return monthsUntil(end); case QUARTER_YEARS: return monthsUntil(end) / 3; case HALF_YEARS: return monthsUntil(end) / 6; case YEARS: return monthsUntil(end) / 12; case DECADES: return monthsUntil(end) / 120; case CENTURIES: return monthsUntil(end) / 1200; case MILLENNIA: return monthsUntil(end) / 12000; case ERAS: return end.getLong(ERA) - getLong(ERA); } throw new DateTimeException("Unsupported unit: " + unit.getName()); } return unit.between(this, endDateTime).getAmount(); }
/** * Returns a copy of this date-time with the specified period added. * <p> * This method returns a new date-time based on this date-time with the specified period added. This can be * used to add any period that is defined by a unit, for example to add years, months or days. The unit is * responsible for the details of the calculation, including the resolution of any edge cases in the * calculation. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToAdd the amount of the unit to add to the result, may be negative * @param unit the unit of the period to add, not null * @return a {@code LocalDateTime} based on this date-time with the specified period added, not null * @throws DateTimeException if the unit cannot be added to this type */ @Override public LocalDateTime plus(long amountToAdd, PeriodUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000); case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000000); case SECONDS: return plusSeconds(amountToAdd); case MINUTES: return plusMinutes(amountToAdd); case HOURS: return plusHours(amountToAdd); case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12); // no overflow (256 is // multiple of 2) } return with(this.date.plus(amountToAdd, unit), this.time); } return unit.doPlus(this, amountToAdd); }
@Override public long periodUntil(DateTime endDateTime, PeriodUnit unit) { if (endDateTime instanceof ZonedDateTime == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } if (unit instanceof ChronoUnit) { ZonedDateTime end = (ZonedDateTime) endDateTime; end = end.withZoneSameInstant(this.offset); return this.dateTime.periodUntil(end.dateTime, unit); } return unit.between(this, endDateTime).getAmount(); }
@Override public ChronoZonedDateTime<C> plus(long amountToAdd, PeriodUnit unit) { if (unit instanceof ChronoUnit) { return with(this.dateTime.plus(amountToAdd, unit)); } return getDate().getChrono().ensureChronoZonedDateTime(unit.doPlus(this, amountToAdd)); // / TODO: // Generics // replacement // Risk! }
@Override public long periodUntil(DateTime endDateTime, PeriodUnit unit) { if (endDateTime instanceof OffsetDate == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } if (unit instanceof ChronoUnit) { OffsetDate end = (OffsetDate) endDateTime; long offsetDiff = end.offset.getTotalSeconds() - this.offset.getTotalSeconds(); LocalDate endLocal = end.date.plusDays(Jdk8Methods.floorDiv(-offsetDiff, SECONDS_PER_DAY)); return this.date.periodUntil(endLocal, unit); } return unit.between(this, endDateTime).getAmount(); }
@Override public long periodUntil(DateTime endDateTime, PeriodUnit unit) { if (endDateTime instanceof ChronoLocalDate == false) { throw new DateTimeException("Unable to calculate period between objects of two different types"); } ChronoLocalDate<?> end = (ChronoLocalDate<?>) endDateTime; if (getChrono().equals(end.getChrono()) == false) { throw new DateTimeException("Unable to calculate period between two different chronologies"); } if (unit instanceof ChronoUnit) { return LocalDate.from(this).periodUntil(end, unit); // TODO: this is wrong } return unit.between(this, endDateTime).getAmount(); }