C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programs

Java program to get current date and change date by adding given number of days

In this java program, we are going to learn how to get current date and change the system’s date by adding given number of days?
Submitted by IncludeHelp, on October 29, 2017

Get current date and change it by adding given number of days using java program.

To get current system date, we are using "get()" method of "Calendar" class by specifying, current month, date and year.

And to add number of days, we are using "Calendar.add()" method.

Program to get date and change by adding days in java

import java.util.Calendar;

public class ChangeDate 
{
	public static void main(String[] args) 
	{
		// creating object of calendar.
		Calendar now = Calendar.getInstance();

		// print the current date.
		System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) 
		+ "-"+ now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));

		//adding number of days 
		now.add(Calendar.DATE, 10);

		// print date after changement.
		System.out.println("Changed date : " + (now.get(Calendar.MONTH) + 1) 
		+ "-"+ now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
	}
}

Output

Current date : 10-28-2017
Changed date : 11-7-2017






COMMENTS