Close

Java Date Time - LocalDate.plusYears() Examples

Java Date Time Java Java API 


Class:

java.time.LocalDate

java.lang.Objectjava.lang.Objectjava.time.LocalDatejava.time.LocalDatejava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.time.chrono.ChronoLocalDateChronoLocalDatejava.io.SerializableSerializableLogicBig

Method:

public LocalDate plusYears(long yearsToAdd)

This method returns a new instance of LocalDate by adding the specified amount to the year field. The original instance is not changed.

The specified yearsToAdd may be negative, in that years will be subtracted.

This method will throw DateTimeException if the result exceeds the supported date range.


Examples


package com.logicbig.example.localdate;

import java.time.LocalDate;
import java.time.Month;

public class PlusYearsExample {

public static void main (String... args) {
LocalDate d = LocalDate.of(2016, Month.NOVEMBER, 12);

LocalDate d2 = d.plusYears(100);
System.out.println(d2);

LocalDate d3 = d.plusYears(100);
System.out.println(d3);
}


}

Output

2116-11-12
2116-11-12




package com.logicbig.example.localdate;

import java.time.LocalDate;
import java.time.Month;

public class PlusYearsExample2 {

public static void main (String... args) {
LocalDate d = LocalDate.of(2000, Month.FEBRUARY, 29);
LocalDate d2 = d.plusYears(1);
System.out.println(d2);
}
}

Output

2001-02-28




See Also