Javatpoint Logo
Javatpoint Logo

How to add 2 dates in Java?

Adding two dates is a common task in programming, especially when dealing with time-based calculations. In Java, there are several ways to add two dates together, depending on the specific requirements of the program. In this article, we will explore some of the different methods for adding two dates in Java.

Method 1: Using the Calendar Class

One way to add two dates in Java is to use the Calendar class, which provides a set of methods for working with dates and times. To add two dates using the Calendar class, we first create two Calendar objects representing the dates we want to add, and then use the add() method to perform the addition.

Here is an example program that adds two dates using the Calendar class:

DateAdditionExample.java

Output:

Date 1: Sat Apr 17 10:46:26 EDT 2023
Date 2: Sat Apr 24 10:46:26 EDT 2023

In this program, we first create two Date objects representing the current date and time. We then create two Calendar objects using the getInstance() method of the Calendar class, which returns a Calendar object initialized with the current date and time in the default time zone and locale. Next, we set the Calendar objects to the two dates using the setTime() method. We then add 7 days to the first date and 14 days to the second date using the add() method, which adds the specified amount to the given field of the Calendar object.

Finally, we print the results using the getTime() method of the Calendar class, which returns a Date object representing the time value of the Calendar object.

Another way to add two dates in Java is to use the LocalDate class, which represents a date without a time zone or time of day. To add two dates using the LocalDate class, we first create two LocalDate objects representing the dates we want to add, and then use the plus() method to perform the addition.

Here is an example program that adds two dates using the LocalDate class:

DateAdditionExample.java

Output:

Date 1: 2023-04-10
Date 2: 2023-04-20
Result: 2024-05-17

In this program, we first create two LocalDate objects representing the current date using the now() method, and the second date using the parse() method of the LocalDate class, which parses a string representing a date in the specified format.

Next, we add 7 days, 1 month, and 1 year to the first date using the plus() method, which returns a new LocalDate object representing the result of the addition.

Finally, we print the results using the toString() method of the LocalDate class, which returns a string representing the value of the LocalDate object.

Method 3: Using the Date and SimpleDateFormat Classes

A third way to add two dates in Java is to use the Date and SimpleDateFormat classes, which provide methods for working with dates and times in a specific format. To add two dates using these classes, we first create two Date objects representing the dates we want to add, and then use the SimpleDateFormat class to parse and format the dates as strings. We can then add the dates together by concatenating the strings, and then parse the result back into a Date object using the SimpleDateFormat class.

Here is an example program that adds two dates using the Date and SimpleDateFormat classes:

DateAdditionExample.java

Output:

Date 1: Sun Apr 10 11:15:57 EDT 2023
Date 2: Sun Apr 10 11:15:57 EDT 2023
Result: Thu Apr 27 00:00:00 EDT 2023

In this program, we first create two Date objects representing the current date and time. We then create a SimpleDateFormat object with the pattern "yyyy-MM-dd", which represents a date in the format "year-month-day".

Next, we format the first date as a string using the format() method of the SimpleDateFormat class, and we set the second date as a string directly. We then add the two dates together by concatenating the strings with a space separator.

Finally, we parse the result string back into a Date object using the parse() method of the SimpleDateFormat class, and we print the results using the toString() method of the Date class.

In this section, we have explored three different methods for adding two dates in Java. The first method uses the Calendar class, which provides a set of methods for working with dates and times. The second method uses the LocalDate class, which represents a date without a time zone or time of day. The third method uses the Date and SimpleDateFormat classes, which provide methods for working with dates and times in a specific format.







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