Add Days To Date Java Example

Chapter: Date and Time Last Updated: 08-08-2021 18:50:21 UTC

Program:

            /* ............... START ............... */
                
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.text.ParseException;

public class AddDaysToDate {
	public static void main(String args[]) {
		String oldDate = "2021-02-16";
		System.out.println("Date before Addition: " + oldDate);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		// Parsing and setting date to Calendar
		try {
			c.setTime(sdf.parse(oldDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		// Adding 180 days to date.
		c.add(Calendar.DAY_OF_MONTH, 180);
		// Converting date to sting format.
		String newDate = sdf.format(c.getTime());
		// Displaying the new Date after addition of Days
		System.out.println("New date after addition: " + newDate);
	}
}


/* Output 
Date before Addition: 2021-02-16
New date after addition: 2021-08-15
*/

// Program to add days to current date.

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

public class AddingDateToCurrentDate {
	public static void main(String args[]) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		// This will give the current date and time.
		Calendar cal = Calendar.getInstance();
		System.out.println("Current Date: " + sdf.format(cal.getTime()));
		cal.add(Calendar.DAY_OF_MONTH, 180); // Will add 180 days
		String newDate = sdf.format(cal.getTime());

		System.out.println("New Date : " + newDate);
	}
}
/* Output
Current Date: 2021/08/08
New Date : 2022/02/04
*/
                /* ............... END ............... */
        

Notes:

  • In first program date is taking as string and we are paring the same using Calendar and SimpleDateFormat class.
  • Add function in calendar class is used to add the days to calendar object.
  • By using string format function calendar object has been again changed to string format and printing the same using System.out.println.
  • Please refer the first program for more details.
  • In second program Calendar.getInstance() will give the current date and same add function is used to add days to current date.
  • As like the other program we used string format fuction to format the calendar instance to string format.
Similar Programs Chapter Last Updated
Java Program to Calculate Time Difference In Hours Date and Time 20-09-2023
Java Code To Calculate Time Difference In Milliseconds Date and Time 20-09-2023
Java Program To Calculate Time Difference In Seconds Date and Time 20-09-2023
How to calculate time difference between two time zones in Java Date and Time 20-09-2023
Java Date And Time API To Create A Custom Calendar Date and Time 20-09-2023
How To Format Date And Time In Java Date and Time 16-09-2023
How Do You Get The First Day Of The Week In Java Date and Time 15-09-2023
Java Code To Change Date Format Date and Time 15-08-2023
Java Program To Find Day Of The Week For A Given Date Date and Time 10-08-2023
Java code to check if date is weekend Date and Time 29-06-2023
Java program to find number of days between two dates Date and Time 29-06-2023
Java Program To Get The Current Time In Different Time Zones Date and Time 22-06-2023
How To Add Working Days To A Date In Java Date and Time 22-06-2023
Java Program To Add A Specified Number Of Days To A Given Date Date and Time 22-06-2023
Write A Program To Check Leap Year In Java Date and Time 22-06-2023
Write A Program To Display Current Date And Time In Java Date and Time 22-06-2023
Java Date Format AM PM Date and Time 14-05-2023
Java Add Months To Date Date and Time 14-05-2023
Add Days To Date In Java example Date and Time 14-05-2023
Java Program To Calculate Days Between Two Dates Date and Time 16-04-2023
Java Program To Add Days To Date Date and Time 16-04-2023
How To Find Difference Between Two Dates In Java Date and Time 01-04-2023
Time Difference Between Two Timestamps In Java Date and Time 22-09-2018

1