Date and Time

Java date add days to given date

public final class LocalDate extends Object

public LocalDate plusDays(long daysToAdd)

Returns a copy of this LocalDate with the specified number of days added. This method adds the specified amount to the days field incrementing the month and year fields as necessary to ensure the result remains valid. The result is only invalid if the maximum/minimum year is exceeded.For example, 2008-12-31 plus one day would result in 2009-01-01.

public LocalDate plusMonths(long monthsToAdd)

Returns a copy of this LocalDate with the specified number of months added.This method adds the specified amount to the months field in three steps:

  1. Add the input months to the month-of-year field
  2. Check if the resulting date would be invalid
  3. Adjust the day-of-month to the last valid day if necessary

For example, 2007-03-31 plus one month would result in the invalid date 2007-04-31. Instead of returning an invalid result, the last valid day of the month, 2007-04-30, is selected instead.

public LocalDate plusWeeks(long weeksToAdd)

Returns a copy of this LocalDate with the specified number of weeks added. This method adds the specified amount in weeks to the days field incrementing the month and year fields as necessary to ensure the result remains valid. The result is only invalid if the maximum/minimum year is exceeded. For example, 2008-12-31 plus one week would result in 2009-01-07.

public LocalDate plusYears(long yearsToAdd)

Returns a copy of this LocalDate with the specified number of years added.This method adds the specified amount to the years field in three steps:

  1. Add the input years to the year field
  2. Check if the resulting date would be invalid
  3. Adjust the day-of-month to the last valid day if necessary

For example, 2008-02-29 (leap year) plus one year would result in the invalid date 2009-02-29 (standard year). Instead of returning an invalid result, the last valid day of the month, 2009-02-28, is selected instead.

package com.candidjava.datetime;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;


public class JavaAddDate {
	public static void main(String[] args) {
		//Using LocalDateTime Class
		LocalDate date=LocalDate.now();
		System.out.println("Using LocalDate time class:");
		System.out.println("Current Date: "+date);
	    date=date.plusDays(7);
		System.out.println("Date after incrementing days:"+date);
		date=date.plusMonths(5);
		System.out.println("Date after incrementing months:"+date);
		date=date.plusYears(7);
		System.out.println("Date after incrementing years:"+date);
		date=date.plusWeeks(1);
		System.out.println("Date after incrementing weeks:"+date);
		System.out.println("------------------------------------------");
		LocalDate newdate=LocalDate.of(2020, 8, 27);
		System.out.println("Date:"+newdate);
		newdate=newdate.plusDays(5).plusMonths(1).plusYears(1);
		System.out.println("Incremented Date:"+newdate);
		System.out.println("------------------------------------------");
		//Using Calendar Class
		SimpleDateFormat dateFormat=new SimpleDateFormat("dd-MM-yyyy");
		Calendar calendar=Calendar.getInstance();
		System.out.println("Using Calendar Class:");
		System.out.println("Current Date:"+dateFormat.format(calendar.getTime()));
		calendar.add(Calendar.DAY_OF_MONTH, 2);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.YEAR, 1);
		String incrementedDate=dateFormat.format(calendar.getTime());
		System.out.println("Incremented Date:"+incrementedDate);
		System.out.println("------------------------------------------");


	}

}

OUTPUT

Using LocalDate time class:
Current Date: 2020-11-18
Date after incrementing days:2020-11-25
Date after incrementing months:2021-04-25
Date after incrementing years:2028-04-25
Date after incrementing weeks:2028-05-02
------------------------------------------
Date:2020-08-27
Incremented Date:2021-10-01
------------------------------------------
Using Calendar Class:
Current Date:18-11-2020
Incremented Date:20-12-2021
------------------------------------------