Java Date Time Java Java API
Class:
java.time.LocalDate
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); }
}
Output2116-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); } }
Output2001-02-28
|