Close

Java Date Time - Year.plus() Examples

Java Date Time Java Java API 


Class:

java.time.Year

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

Methods:

public Year plus(TemporalAmount amountToAdd)

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



public Year plus(long amountToAdd,
                 TemporalUnit unit)

Returns a copy of this year with the specified amount for the specified temporal unit added.


Examples


package com.logicbig.example.year;

import java.time.Period;
import java.time.Year;

public class PlusExample {

public static void main(String... args) {
Year y = Year.of(2011);
System.out.println(y);

Year y2 = y.plus(Period.ofYears(4));
System.out.println(y2);

Year y3 = y.plus(Period.ofYears(-40));
System.out.println(y3);
}
}

Output

2011
2015
1971




package com.logicbig.example.year;

import java.time.DateTimeException;
import java.time.Year;
import java.time.temporal.ChronoUnit;
import java.time.temporal.UnsupportedTemporalTypeException;

public class PlusExample2 {

public static void main(String... args) {

Year y = Year.of(2017);
System.out.println(y);

for (ChronoUnit chronoUnit : ChronoUnit.values()) {
try {
Year 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

2017
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: not supported
YEARS: 2018
DECADES: 2027
CENTURIES: 2117
MILLENNIA: 3017
ERAS: error: Invalid value for Era (valid values 0 - 1): 2
FOREVER: not supported




See Also