Javatpoint Logo
Javatpoint Logo

Java Date Add Days

In Java, we use the Calendar abstract class to work with the date and time. It has several useful interfaces which allow us to convert date between a specific instance in time and a set of calendar fields such as DAY, MONTH, YEAR, HOUR, etc. The Calendar class implements the Comparable interface and inherits the Object class.

Declaration of the Calendar class

In Java, we can add a single day or days to the given date or current date using Calendar class. Let's understand how we can add days to the date.

Adding Days to the given Date using Calendar class

In order to add days to the given date by using the Calendar class, we use the following steps:

  1. We import all the require packages and classes such as SimpleDateFormat, Calendar
  2. Create instance of the SimpleDateFormat and Calendar By using these instances, we parse the date in which we need to add days.
  3. Add the given date to the calendar by using setTime() method of calendar class.
  4. Use the add() method of the calendar class to add days to the date. The add method() takes two parameter, i.e., calendar field and amount of time that needs to be added.
  5. Get the new date from the calendar and set the format of the SimpleDateFormat class to show the calculated new date on the screen.

DateExample.java

Output:

Java Date Add Days

Add Days to the current date using Calendar class

The steps to add days to the current date are same as the steps to add days to the given date. There is no need to define a date in this case. We get the date from the calendar here and then add days to that particular date.

Let's take an example and understand how we can add days to the current date.

DateExample1.java

Output:

Java Date Add Days

Add Days to date without using Calendar class

It is very easy to add days to the date without using the Calendar class. We use the LocalDate class of the java.time package. The LocalDate class provides several methods for calculating and manipulating the current date.

Let's take an example and understand how we can use LocalDate class to calculate date in Java.

DateExample2.java

Output:

Java Date Add Days
Next TopicJava Plot





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA