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

Problem statement

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

Java - Get current date and change date by adding given number of days

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 and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.