Close

Java Date Time - YearMonth.plus() Examples

Java Date Time Java Java API 


Class:

java.time.YearMonth

java.lang.Objectjava.lang.Objectjava.time.YearMonthjava.time.YearMonthjava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.lang.ComparableComparablejava.io.SerializableSerializableLogicBig

Methods:

public YearMonth plus(TemporalAmount amountToAdd)

Returns a copy of this year-month with the specified amount added.



public YearMonth plus(long amountToAdd,
                      TemporalUnit unit)

Returns a copy of this year-month with the specified amount added to the specified unit.


Examples


package com.logicbig.example.yearmonth;

import java.time.Period;
import java.time.YearMonth;

public class PlusExample {

public static void main(String... args) {
YearMonth y = YearMonth.of(2010, 11);
System.out.println(y);

YearMonth y2 = y.plus(Period.of(5, 3, 0));
System.out.println(y2);
}
}

Output

2010-11
2016-02




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




See Also