Java Date Time - LocalDateTime plus(long amountToAdd, TemporalUnit unit) example








LocalDateTime plus(long amountToAdd, TemporalUnit unit) returns a copy of this date-time with the specified amount added.

Syntax

plus has the following syntax.

public LocalDateTime plus(long amountToAdd,   TemporalUnit unit)

Example

The following example shows how to use plus.

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
// ww  w .j  a  v  a  2  s  . co  m
public class Main {
  public static void main(String[] args) {
    LocalDateTime a = LocalDateTime.of(2014, 6, 30, 12, 00);
    
    LocalDateTime t = a.plus(10,ChronoUnit.DAYS);
    
    System.out.println(t);
  }
}

The code above generates the following result.





Example 2

Add years and months to a date

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
/*from   w  w  w. j ava  2 s. c  o m*/
public class Main {
  public static void main(String[] args) {
    
    LocalDateTime timePoint = LocalDateTime.now();
    LocalDateTime newDateTime = timePoint.plus(3, ChronoUnit.WEEKS)
        .plus(3, ChronoUnit.YEARS);
    
    System.out.println(newDateTime);
  }
}

The code above generates the following result.