Java Data Type How to - Add days and year to local date








Question

We would like to know how to add days and year to local date.

Answer

import java.time.LocalDate;
import java.time.LocalDateTime;
//  w w w  . j  a va 2 s  . c  om
public class Main {

  public static void main(String[] args) {
    LocalDate tomorrow = LocalDate.now().plusDays(1);
    System.out.println(tomorrow);
    
    LocalDate yesterday = LocalDate.now().minusDays(1);
    System.out.println(yesterday);
    
    LocalDate lastWeek = LocalDate.now().minusWeeks(1);
    System.out.println(lastWeek);
    
    LocalDate nextYear = LocalDate.now().plusYears(1);
    System.out.println(nextYear);
    
    LocalDateTime inThreeHoursAndTwentyMinutes = LocalDateTime.now().plusHours(3).plusMinutes(20);
    System.out.println(inThreeHoursAndTwentyMinutes);
  }
}

The code above generates the following result.