How to Add Days in Java Calendar

In Java, we can add the days to the specified date or the current date of the system using the Java calendar class. The calendar class is an abstract class supported by the API of the Java utility which manipulates and operates with the dates and times. We are prohibited to employ a constructor to make an instance of it because it is an abstract class. It provides several practical interfaces that let us convert the dates between a certain point in time and some calendar fields. The calendar class inherits from the Object class and implements the Comparable interface.

Example 1:

Using the calendar class, the Java program updates the day to the current date. The calendar class adds only one day from the current day. Let’s construct the calendar class implementation to add a day by giving the date.

We utilize the calendar library from the utility class of Java and the date library to get the date of the system. Next, we implement the “program1” class to call the main() method. Here, we declare the “MyDate” variable which is set with the Date() class to get the precise millisecond-level instant in time. The current date along with the time are printed which the “MyDate” variable returns. Then, we call the calendar class and define its “calen” variable. The “calen” variable is assigned with the getInstance() method of the calendar class. The getInstance() method of the calendar class establishes a Unica Interact API instance that connects to the given runtime server.

Moreover, we use the setTime() method and pass the “MyDate” date variable as a parameter. This method sets the “MyDate” object display in milliseconds. After that, we utilize the add() method of the calendar to add the day in the current day of the calendar which only increments by a value of “1” since this value is provided as a parameter. Once the next day is set, we fetch the time of that day along with the date from the getTime() method and display it via the print method.

The following is a readout of the time and the system’s current date. Also, the next day after the current day is displayed along with the time:

Example 2:

We provided the Java program in the previous example to get the day of the calendar that comes after the current day. We can also accomplish the days of our choice by incrementing the value days within the add() method. Let’s have a look at the program where we get the day which comes two days from the current day.

We have a “calendar” class from the Java utility package. Then, we deploy the “Program2” class for the main() method where the calendar class program is implemented. Here, we create the calendar class variable “c” where the getInstance() method is called to get the server time. After that, we define a print statement in which we have a getTime() method to get a numeric value that represents the time in universal time for the current date. Next, we use the add method for the calendar variable “c” to add the date and time which increments by a value of “2” and returns the day which comes two days after the current day of the system. The getTime() method gets that day which represents the date along with the time.

Since the server’s current day is “Tuesday”, the day that comes two days after is “Thursday” as shown on the screen:

Example 3:

The procedures for decreasing a day to the specified date are the same as those for adding the days to a current date. In this situation, the format of a date is provided. The date is taken from the calendar and is then decreased by a certain number of days. Let’s have another Java program where the format is set for a date to get the previous day from the specified date.

We use the “SimpleDateFormat” class and the “ParseExecption” from the text package of Java to specify the format of the date and handle the exception that deals with the parsing of the calendar date into that format.  Furthermore, we alos use the calendar class from the Java utility class. We set a “program3” Java class where the main() method is defined. Here, we first create the “Df” variable where the SimpleDateFormat() method is invoked that matches with the given format of the date. After that, we establishe the “cd” instance for the getInstance() method where the date is set for the “Df” object.

We then print the date of the system using the getTime() method in the provided format. This is the day before decreasing the days. To decrease the days, we use the add() method again, but the add() method takes the DAY_OF_MONTH which is decremented by the value of “3” since the negative sign value is used with it. The getTime() method gets that day which we decremented and displayed on the screen.

The specified day is printed in the assigned format. The third day before the specified day is also fetched on the following screen:

Example 4:

It is not only the day which can be added to the calendar class in Java. The LocalDate class also has a feature to add or minus the days with the help of this class’s different methods. The number of days can be added or subtracted using the plusDays() and minusDays() methods, respectively.

We use the LocalDateTime class from the time module of Java. Then, the main() method is implemented by the “Program4” class in our program. We declare the “Today” variable of the LocalDateTime and call the now()method to retrieve the current time date of the system. After that, we have another variable, “Tomorrow”, where the plusDays() method of LocalDateTime adds the number of days to today’s day. We pass the numeric value of “1” which means that only one day is added to the specified day. After that, we use the minusDays() method to get to yesterday’s date. Then, we print all the returned days from the LocalDateTime methods.

Today’s date along with the time is returned from the now() method. Also, the yesterday and tomorrow dates are retrieved via the plusDays() and minusDays() method:


The Java calendar class makes the tasks like multiplying a Date by a certain number of days extremely challenging. We have ways to add the days into the date of our system or we can provide a date to add the days. The operations of the calendar class are performed with the provided examples where we added a certain number of days and got the previous days from the provided date and time.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.