Returns a copy of this year-month with the specified amount added to the specified unit.
package com.logicbig.example.yearmonth;
import java.time.YearMonth;
import java.time.temporal.ChronoUnit;
import java.time.temporal.UnsupportedTemporalTypeException;
public class PlusExample2 {
public static void main(String... args) {
YearMonth y = YearMonth.of(2012, 4);
System.out.println(y);
for (ChronoUnit chronoUnit : ChronoUnit.values()) {
try {
YearMonth y2 = y.plus(1, chronoUnit);
System.out.printf("%10s: %s%n", chronoUnit.name(), y2);
} catch (UnsupportedTemporalTypeException e) {
System.out.printf("%10s: not supported%n", chronoUnit.name());
} catch (Exception e) {
System.out.printf("%10s: error: %s%n", chronoUnit.name(), e.getMessage());
}
}
}
}
Output
2012-04
NANOS: not supported
MICROS: not supported
MILLIS: not supported
SECONDS: not supported
MINUTES: not supported
HOURS: not supported
HALF_DAYS: not supported
DAYS: not supported
WEEKS: not supported
MONTHS: 2012-05
YEARS: 2013-04
DECADES: 2022-04
CENTURIES: 2112-04
MILLENNIA: 3012-04
ERAS: error: Invalid value for Era (valid values 0 - 1): 2
FOREVER: not supported