Close

Java Date Time - LocalDate.plus() 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

Methods:

public LocalDate plus(TemporalAmount amountToAdd)

Returns a new LocalDate instance with the specified amount added to this local date instance. The amount provided is typically Period. Duration will throw exception.


public LocalDate plus(long amountToAdd,

TemporalUnit unit)

This method returns a new instance of LocalDate, with the provided amount in terms of the provided unit added to this instance of local date. The original instance is not changed. If the provided unit is not supported an exception will be thrown.


Examples


package com.logicbig.example.localdate;

import java.time.LocalDate;
import java.time.Period;

public class PlusExample {

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

LocalDate d = LocalDate.of(2005, 02, 10);

LocalDate d2 = d.plus(Period.ofDays(300));
System.out.println(d2);

LocalDate d3 = d.plus(Period.of(5, 2, 3));
System.out.println(d3);

LocalDate d4 = d.plus(Period.ofDays(19));
System.out.println(d4);
}

}

Output

2005-12-07
2010-04-13
2005-03-01




package com.logicbig.example.localdate;

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;

public class PlusExample2 {

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

LocalDate d = LocalDate.of(2001, 11, 22);

LocalDate d2 = d.plus(500, ChronoUnit.DAYS);
System.out.println(d2);

LocalDate d3 = d.plus(50, ChronoUnit.WEEKS);
System.out.println(d3);


}


}

Output

2003-04-06
2002-11-07




package com.logicbig.example.localdate;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class PlusExample3 {

public static void main(String... args) {
LocalDate d = LocalDate.of(2001, 11, 22);
LocalDate d2 = d.plus(500, ChronoUnit.HALF_DAYS);
System.out.println(d2);
}
}

Output

Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: HalfDays
at java.time.LocalDate.plus(LocalDate.java:1247)
at com.logicbig.example.localdate.PlusExample3.main(PlusExample3.java:15)
... 6 more




See Also