Java Program to add two dates

program two add two dates in Java

What is  a Calendar class?

The Calendar class in the Java programming language is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.

In this Article, we will write a program using different methods to add two dates in Java.

Steps to Add two dates: 

  1. Import the necessary classes for working with dates in Java. You can use the Java.util package, which includes the Date class.

  2. Create two Date objects that represent the dates you want to add. You can either initialize them with specific dates or use the current date by calling the new Date() constructor.

  3. Convert the Date objects to the long data type using the gettime() method. This method returns the number of milliseconds since January 1, 1970, which is a common way to represent dates in Java.

  4. Add the two long values representing the dates together to get the sum.

  5. Create a new Date object using the sum of the two dates in milliseconds. You can do this by passing the sum as an argument to the Date  constructor.

  6. Print the result, which will be the sum of the two dates.

Calendar Class:

The Calendar class also provides a number of methods for adding or subtracting time from a calendar, such as add() and roll(), and for comparing two calendars.

Example for the Program to add two dates:

Run
import java.util.Calendar;

public class Main{
  public static void main(String[] args){
    Calendar date1 = Calendar.getInstance();
    Calendar date2 = Calendar.getInstance();

    // Set the date for first calendar
    date1.set(Calendar.YEAR, 2022);
    date1.set(Calendar.MONTH, Calendar.JANUARY);
    date1.set(Calendar.DATE, 1);

    // Set the date for second calendar
    date2.set(Calendar.YEAR, 2022);
    date2.set(Calendar.MONTH, Calendar.FEBRUARY);
    date2.set(Calendar.DATE, 28);

    // Printing both of the dates before addition
    System.out.println("Value for Date1 before addition is : " + date1.getTime());
    System.out.println("Value for Date2 before addition is : " + date2.getTime());
    
    // Add the dates to both of the above dates
    date1.add(Calendar.DATE, 28);
    date2.add(Calendar.DATE, 2);

    // Printing both of the dates
    System.out.println("Value for Date1 after addition is : " + date1.getTime());
    System.out.println("Value for Date2 after addition is : " + date2.getTime());
  }
}
Output:

Value for Date1 before addition is : Sat Jan 01 11:02:29 GMT 2022
Value for Date2 before addition is : Mon Feb 28 11:02:29 GMT 2022
Value for Date1 after addition is : Sat Jan 29 11:02:29 GMT 2022
Value for Date2 after addition is : Wed Mar 02 11:02:29 GMT 2022

Explanation:

This program sets the dates for two calendars, date1 and date2, using the Calendar.set method. It then adds the number of days in date2 to date1 using the Calendar.add method. Finally, it prints out the resulting date by getTime method of Java calendar class from the date1 and date2 calendar using the Calendar.get method.

The output of this program will be “Sat Jan 29 11:02:29 GMT 2022”, which is the result of adding January 1st and 28 days together and second output is “Wed Mar 02 11:02:29 GMT 2022”, which is the result of adding Febraury 28th and 2 days together.

Example:

Run
import java.util.Calendar;

public class Main {
  public static void main(String[] args) {
    Calendar date1 = Calendar.getInstance();
    Calendar date2 = Calendar.getInstance();

    // Set the values for the first date
    date1.set(Calendar.YEAR, 2020);
    date1.set(Calendar.MONTH, Calendar.JANUARY);
    date1.set(Calendar.DAY_OF_MONTH, 20);

    // Set the values for the second date
    date2.set(Calendar.YEAR, 2021);
    date2.set(Calendar.MONTH, Calendar.FEBRUARY);
    date2.set(Calendar.DAY_OF_MONTH, 28);

    // Printing before adding 
    System.out.println("Before Adding : " + date1.getTime());
    
    // Add the two dates together
    date1.add(Calendar.DAY_OF_MONTH, date2.get(Calendar.DAY_OF_MONTH));
    date1.add(Calendar.MONTH, date2.get(Calendar.MONTH));

    // Print the result
    System.out.println("After Adding : " + date1.getTime());
  }
}
Output:

Before Adding : Mon Jan 20 11:22:24 GMT 2020
After Adding : Tue Mar 17 11:22:24 GMT 2020

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription