/** * Returns a copy of this year with the specified number of years subtracted. * <p> * This instance is immutable and unaffected by this method call. * * @param yearsToSubtract the years to subtract, may be negative * @return a {@code Year} based on this year with the period subtracted, not null * @throws DateTimeException if the result exceeds the supported year range */ public Year minusYears(long yearsToSubtract) { return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract)); }
/** * Returns a copy of this year with the specified number of years subtracted. * <p> * This instance is immutable and unaffected by this method call. * * @param yearsToSubtract the years to subtract, may be negative * @return a {@code Year} based on this year with the period subtracted, not null * @throws DateTimeException if the result exceeds the supported year range */ public Year minusYears(long yearsToSubtract) { return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract)); }
Year thisYear = Year.from( today ); Year nextYear = thisYear.plusYears( 1 ); Year lastYear = thisYear.minusYears( 1 );
/** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override public Year plus(long amountToAdd, TemporalUnit 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 UnsupportedTemporalTypeException("Unsupported unit: " + unit); } return unit.addTo(this, amountToAdd); }
/** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override public Year plus(long amountToAdd, TemporalUnit 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 UnsupportedTemporalTypeException("Unsupported unit: " + unit); } return unit.addTo(this, amountToAdd); }