Java Program to Add Two Dates

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

Any time represented in a Date object will not function since the Java epoch is 1970. Your Dates will therefore begin in 1970, and when two Date objects are added, the total is off by around 1970 years. Thus, we'll use the calendar in this article to add two dates, moreover, we'll also see how to add two dates without using a calender in Java

Introduction

To operate with the date and time in Java, we utilize the abstract class called Calendar

It provides several helpful interfaces that enable us to convert dates between a certain point in time and a collection of calendar fields like DAY, MONTH, YEAR, HOUR, etc.

The Comparable interface is implemented by the Calendar class, which also derives from the Object class

To understand this example, you should know about the following Java programming topics:

  1. Java Class and Objects
  2. Java Basic Input and Output

Let us discuss these briefly before moving on

1. Java Class and Objects

In Java, an object is both a physical and a logical thing, whereas a class is solely a conceptual entity.

It is a thing that has state and behavior. It could be intellectual or physical (tangible and intangible)

Pen, for instance, is an object. Reynolds is its name, and its state is noted as being white Writing is its behavior because it is utilized for writing.

A class's instances are objects. A class serves as a model or blueprint from which new objects can be made. Therefore, a class's instance (or result) is an object.

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity and can't be physical.

A class in Java can contain:

  • Fields
  • Methods
  • Constructors
  • Blocks
  • Nested class and interface

Syntax to declare a class:

Know more about objects and classes in Java here

2. Java Basic Input and Output

Java makes advantage of the stream idea to speed up I/O. All of the classes needed for input and output operations are included in the java.io package.

A data stream is a collection of data. A stream in Java is made up of bytes. It resembles a flowing stream of water, which is why it is called a stream.

Three streams are automatically established for us in Java. Each of these streams is connected to the console.

  1. System.out: the standard output stream
  2. System.in: standard input stream
  3. System.err: standard error stream

In Java, we can simply use

or

or

to send output to standard output (screen).

Here,

  1. System is a class
  2. out is a public static field that accepts output data.

Learn more about Input and Output in Java here

Java Program to Add Two Dates

We can declare Calendar class as

Java's Calendar class allows us to add a single day or several days to a given date or the current date.

Let's understand how we can add days to the date

Adding Days to the Given Date Using Calendar Class

The steps below are used to use the Calendar class to add days to the specified date:

We import all necessary classes and packages, including Calendar, SimpleDateFormat, and SimpleDateFormat instances. We parse the date to which we need to add days using these instances

Using the calendar class's setTime() method, add the specified date to the calendar

To add days to the date, use the calendar class's add() method. The calendar field and the amount of time that has to be added are the two parameters for the add() method function.

To display the computed new date on the screen, get the new date from the calendar and change the SimpleDateFormat class's format.

Add Days to the Current Date Using Calendar Class

The steps to add days to the current date are the 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

Output

Explanation In the above program, c1 and c2 store the current date. Then, we simply clone c1 and add c2's to each DateTime property one after the other.

As you can see, we've added 1 to the months. This is because months start with 0 in Java.

Alternatively, you can also use Joda for time/date operations in Java.

Add Days to Date Without Using Calendar Class

Without utilizing the Calendar class, it is relatively simple to include days in the date. We make use of the Java.time package's LocalDate class. The LocalDate class offers several ways to determine and work with the current date.

Let's look at an illustration to better understand how the LocalDate class in Java can be used to compute dates.

Output

Conclusion

  1. To operate with the date and time in Java, we utilize the abstract class called Calendar. It provides several helpful interfaces that enable us to convert dates between a certain point in time and a collection of calendar fields like DAY, MONTH, YEAR, HOUR, etc.
  2. The Comparable interface is implemented by the Calendar class, which also derives from the Object class
  3. There are ways by which without utilizing the Calendar class, it is relatively simple to include days in date. We make use of the Java.time package's LocalDate class. The LocalDate class` offers several ways to determine and work with the current date.
  4. To add days to the date, use the calendar class's add() method.