Java program to add days to current date

Java program to add days to the current date. The following Java program is to add one day to the current date. Adding days to the present date can be done using a different method.

Method 1: Using LocalDate object: LocalDate ld=LocalDate.now(); The above code is used to obtain the current date. Use plusDays(1) method to add one day to the current date.

Method 2: Using Calendar object: Create a calendar object and use getInstance() method to get the current date. Add one day to CalendarDate using (Calendar.Date, 1) and get the new date using getTime() method.

Method 3: Using Date object Using Date object the next date is obtained using the below code, Date tmw=new Date(tdy.getTime()+(1000*60*60*24));

Java add days:


package com.candidjava.time;

import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

public class AddDate 
{
public static void main(String[] args) 
{
	//add one day to LocalDate Object
	LocalDate ld=LocalDate.now();	//get current date
	ld=ld.plusDays(1);	//add one day to current date
	System.out.println(ld);
		
	// add one day to calendar object
	Calendar c=Calendar.getInstance();
	c.add(Calendar.DATE, 1);	//add one day to calendar object
	System.out.println(c.getTime()); //print added date
	
	
	// add one day to date object
	Date tdy=new Date();
	Date tmw=new Date(tdy.getTime()+(1000*60*60*24)); //get tomorrow date
	System.out.println(tmw);
}
}

Output:

2019-01-08
Tue Jan 08 13:01:02 IST 2019
Tue Jan 08 13:01:02 IST 2019

 

Leave a Reply

Your email address will not be published. Required fields are marked *