/** * Returns a copy of this {@code YearMonth} with the month-of-year altered. * <p> * This instance is immutable and unaffected by this method call. * * @param month the month-of-year to set in the returned year-month, from 1 (January) to 12 (December) * @return a {@code YearMonth} based on this year-month with the requested month, not null * @throws DateTimeException if the month-of-year value is invalid */ public YearMonth withMonth(int month) { MONTH_OF_YEAR.checkValidValue(month); return with(year, month); }
/** * Returns a copy of this {@code YearMonth} with the year altered. * <p> * This instance is immutable and unaffected by this method call. * * @param year the year to set in the returned year-month, from MIN_YEAR to MAX_YEAR * @return a {@code YearMonth} based on this year-month with the requested year, not null * @throws DateTimeException if the year value is invalid */ public YearMonth withYear(int year) { YEAR.checkValidValue(year); return with(year, month); }
/** * Returns a copy of this year-month with the specified period in years added. * <p> * This instance is immutable and unaffected by this method call. * * @param yearsToAdd the years to add, may be negative * @return a {@code YearMonth} based on this year-month with the years added, not null * @throws DateTimeException if the result exceeds the supported range */ public YearMonth plusYears(long yearsToAdd) { if (yearsToAdd == 0) { return this; } int newYear = YEAR.checkValidIntValue(year + yearsToAdd); // safe overflow return with(newYear, month); }
YearMonth jan = currentYearMonth.with(Month.JANUARY); YearMonth feb = currentYearMonth.with(Month.FEBRUARY); YearMonth mar = currentYearMonth.with(Month.MARCH); YearMonth apr = currentYearMonth.with(Month.APRIL); YearMonth may = currentYearMonth.with(Month.MAY); YearMonth jun = currentYearMonth.with(Month.JUNE); YearMonth jul = currentYearMonth.with(Month.JULY); YearMonth aug = currentYearMonth.with(Month.AUGUST); YearMonth sep = currentYearMonth.with(Month.SEPTEMBER); YearMonth oct = currentYearMonth.with(Month.OCTOBER); YearMonth nov = currentYearMonth.with(Month.NOVEMBER); YearMonth dec = currentYearMonth.with(Month.DECEMBER);
YearMonth jan = currentYearMonth.with(Month.JANUARY); YearMonth feb = currentYearMonth.with(Month.FEBRUARY); YearMonth mar = currentYearMonth.with(Month.MARCH); YearMonth apr = currentYearMonth.with(Month.APRIL); YearMonth may = currentYearMonth.with(Month.MAY); YearMonth jun = currentYearMonth.with(Month.JUNE); YearMonth jul = currentYearMonth.with(Month.JULY); YearMonth aug = currentYearMonth.with(Month.AUGUST); YearMonth sep = currentYearMonth.with(Month.SEPTEMBER); YearMonth oct = currentYearMonth.with(Month.OCTOBER); YearMonth nov = currentYearMonth.with(Month.NOVEMBER); YearMonth dec = currentYearMonth.with(Month.DECEMBER);
/** * Returns a copy of this year-month with the specified period in months added. * <p> * This instance is immutable and unaffected by this method call. * * @param monthsToAdd the months to add, may be negative * @return a {@code YearMonth} based on this year-month with the months added, not null * @throws DateTimeException if the result exceeds the supported range */ public YearMonth plusMonths(long monthsToAdd) { if (monthsToAdd == 0) { return this; } long monthCount = year * 12L + (month - 1); long calcMonths = monthCount + monthsToAdd; // safe overflow int newYear = YEAR.checkValidIntValue(Jdk8Methods.floorDiv(calcMonths, 12)); int newMonth = Jdk8Methods.floorMod(calcMonths, 12) + 1; return with(newYear, newMonth); }
/** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override public YearMonth plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case MONTHS: return plusMonths(amountToAdd); 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); }