Categories
date java

add 3 days to the current date in java

In this post we will learn to add 3 days to the current date in java
While we working with dates we will come up with adding or subtracting from the current date

In Java there are lot of method for Date. So it is little bit difficult to remember all the things.

In this example we used Calendar.java and Date.java from java.util package
In below example we added 3 days

Syntax
object.add(Calendar.DAY_OF_YEAR, 1)
Example
import java.util.Calendar;
import java.util.Date;

public class AddDaysToDate {

	public static void main(String[] args) {
		try {
			Calendar calender = Calendar.getInstance();
			Date beforeAdding = calender.getTime();
			System.out.println("Printing the value before adding date ::: " + beforeAdding);

			// Adding the 3 days below
			calender.add(Calendar.DAY_OF_YEAR, 3);
			Date afterAdding = calender.getTime();
			System.out.println("Printing the value after adding date  ::: " + afterAdding);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
Output
Printing the value before adding date ::: Thu Feb 13 17:00:21 IST 2020
Printing the value after adding date  ::: Sun Feb 16 17:00:21 IST 2020
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/AddDaysToDate.java

Related Articles

convert String to date in java with example

compare two dates in java example

 

Leave a Reply

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