Adding days in Calendar in Java

Using the Java calendar class, we can add days to either the specified date or the system’s current date. The calendar class is an abstract class supported by the Java Application Programming Interface utility, which manipulates and operates with dates and times. Since it is an abstract class, you cannot employ a constructor and make an instance out of it.

The calendar class inherits from the object class and implements the comparable interface. It also provides several practical interfaces that allow us to convert the dates between specific time points and calendar fields. This article will illustrate various ways to add days to a calendar in Java.

Adding days in Java Calendar

Method 1: How to add one day to a date by using the calendar class

The calendar class is a class for dates and times. One can utilize this class and add a day to a given date. To add a day to a date, you first need to get the current date using the getInstance() method of the calendar class. Once you get the current date, you will be required to set that date using the setTime() method. After that, you will have to utilize the “add()” method of the Calendar class to add a day. The syntax for this method is:

Date date = new Date()
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add (Calendar.DATE, 1);
date = cal.getTime();
System.out.println("Adding One Day: "+ date);

Explanation of the syntax

To add a day to a date using this method, you first have to create a Date class object. After that, you will have to make a Calendar class instance and set the date to the specified date using the getInstance() method. After that, set the date by using the Calendar class instance.

Invoke a seTime() method, then pass a date object in it as an argument. Once that is done, add a day to the date by passing 1 as an argument to add it to the “Calendar.DATE”. In the date object, we will get the date and the time of the next day by utilizing the getTime() method. Lastly, we will be required to print the value of the date object by invoking the System.out.println() method.
Let us take a look at the code snippet below:

import java . util . Calendar;
import java . util . Date;
public class example {
       public static void main(String[] args) {
               Date date = new Date();
	            System.out.println("Todays Date: "+ date);
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                cal.add (Calendar.DATE, 1);
                date = cal.getTime();
                System.out.println("Adding One Day: "+ date);
        }
}

Explanation of the code

In the code above, we have utilized the calendar library from the utility class of Java and the date library to get the system’s date. Next, we implemented the “example” class to call the main() method. In the main method, we declared the “date” variable, which is set with the Date() class to get the precise millisecond level instant in time.

The current date and time are printed and returned by the “date” variable. After that, we call the Calendar class and define its “cal” variable. The “cal” 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.

In addition, we use the setTime() method and pass the “date” variable as a parameter. This method sets the “date” object to display in milliseconds. After that, we use the add() method, which is used if the calendar class is to add the day in the current day of the calendar that only increases by a value of 1. The value 1, is provided as a parameter. Once the next day is set, we fetch the time of that day and the date from the getTime() method. It is then displayed via the print method

The output of the code is :

Todays Date: Mon Jan 16 08:37:35 UTC 2023
Adding One Day: Tue Jan 17 08:37:35 UTC 2023

The output above indicates that a day was successfully added to a date using the Calendar class.

Method 2: How to add the days of your choice by using the Calendar class

Let us look at a program that enables us to get the day two days after the current day.

import java . util . Calendar;
import java . util . Date;
public class program {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        System.out.println("current date = " + cal.getTime());
        cal.add(Calendar . DATE, 2);
        System.out.println("new date = " + cal.getTime());
    }
}

Explanation of the code

The program above has a “Calendar” class from the utility package and the date library that enables us to get the system’s date. We then deployed the “program” class for the main() method where the calendar class program is to be implemented. In the main method, we create the calendar class variable “cal,” where the getInstance () method is called to get the server time.

Once that is done, we define a print statement with a getTime() method to get a numeric value representing the time in universal time for the current date. After that, we use the add method for the calendar variable “cal” to add the date and time, which will increment by a value of “2” and return the day that comes two days after the current day of the system. Lastly, the getTime() method gets that day, representing the date and time. Since the present day of the server is “Monday,” the day that comes after two days is “Wednesday,” as shown on the screen:
The output of the code is:

current date = Mon Jan 16 09:25:33 UTC 2023
new date = Wed Jan 18 09:25:33 UTC 2023

Method 3: How to add a day to a given date by using the calendar class

Let us look at the program written below, where we are adding a day to the date “2003 – 12 – 31” while using the calendar class.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.text.ParseException;
class Example{
   public static void main(String args[]){
// give the date in String format
	String oldDate = "2003-12-31";  
	System.out.println("Date before Addition: "+oldDate);
// specify the date format that matches the given date
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	Calendar cal = Calendar.getInstance();
	try{
// set the date to the given date
	   cal.setTime(sdf.parse(oldDate));
	}catch(ParseException e){
		e.printStackTrace();
	 }
// the number of days to be added
	cal.add(Calendar.DAY_OF_MONTH, 1);
// the date after you have added the days to the given date  
	String newDate = sdf.format(cal.getTime());  
// displaying the new date after the addition of days
	System.out.println("Date after Addition: "+newDate);
   }
}

The output of the code is:

Date before Addition: 2003-12-31
Date after Addition: 2004-01-01

The java Calendar class enables us to add days that were in the past to our system. In such a situation, the format of the date is provided. The date is then taken from the calendar and decreased by a certain number of days.

Let us have a Java program where the format is set for a date to get the previous day from the specified date.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.text.ParseException;
class Example{
   public static void main(String args[]){
 	SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
	Calendar cd = Calendar.getInstance();
	String Df = df.format(cd.getTime());
	System.out.println(Df + "is the current day");
	cd.add(Calendar.DAY_OF_MONTH, -3);
	String Dt = df.format(cd.getTime());
    System.out.println(Dt + "is a date three days before");
   }
}

Explanation of the code

In the code above, we use the “SimpleDateFormat” class and the “ParseException,” which are from the Java text package, to specify the date format and handle the exception that deals with the parsing of the calendar into that format. In addition, we use the Calendar class from the Java utility class.

After that, we set an “example” class where the main() method is defined. In the primary process, we create the Df variable where the SimpleDateFormat() method is invoked and ensure it matches the given date format. After that, we establish the CD instance for the getInstance() method, where the date is set for the Df object.

Once that is done, we print the system’s date using the getTime() method in the provided format. This refers to the day before reducing the days. To reduce the days, we again utilize the add() method. However, the add() method takes the DAY_OF_MONTH, which is then decremented by 3 due to the negative sign. Lastly, the getTime() method gets the decremented day and displays it on the screen.
The output of the code is:

2023-08-16is the current day
2023-08-13is a date three days before

Conclusion

The Java Calendar class makes the task of adding days to the date of our systems more manageable. The operations of the calendar class are performed with examples where we added a certain number of days and got the previous days or the days ahead. I hope this article has been of great help. Thank you for reading.

Similar Posts

Leave a Reply

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