In an earlier article, we looked at different ways to get the current date and time in Java. In this article, you'll learn how to add days, months, and years to date using both Java 8 new date and time API and legacy Date
and Calendar
API.
Java 8 Date & Time API
A new date and time API was introduced in Java 8 to fix the flaws in the old java.util.Date
and java.util.Calendar
APIs. The new API provides utility methods like plusDays()
and minusDays()
to manipulate LocalDate
, LocalDateTime
, ZonedDateTime
, and OffsetDateTime
.
Add days to LocalDate
object
The LocalDate
class represents a date without time in ISO-8601 format (yyyy-MM-dd). The following example shows how you can add days, years, and months to an instance of LocalDate
:
// increment days by 7
LocalDate date = LocalDate.now();
System.out.println("Current Date: " + date);
date = date.plusDays(7);
System.out.println("Date after Increment: " + date);
// increment month, day, year
LocalDate date2 = LocalDate.of(2016, Month.AUGUST, 15);
System.out.println("Original Date: " + date2);
date2 = date2.plusDays(5).plusMonths(3).plusYears(1);
System.out.println("Date after Increment: " + date2);
// minus days, month
LocalDate date3 = LocalDate.parse("2019-12-25");
System.out.println("Original Date: " + date3);
date3 = date3.minusDays(5).minusMonths(5);
System.out.println("Date after Decrement: " + date3);
You should see the following output for the above code snippet:
Current Date: 2019-12-26
Date after Increment: 2020-01-02
Original Date: 2016-08-15
Date after Increment: 2017-11-20
Original Date: 2019-12-25
Date after Decrement: 2019-07-20
Add days to LocalDateTime
object
A LocalDateTime
represents date and time without timezone information in ISO-8601 format. To add or subtract days from an instance of LocalDateTime
, you can do the following:
// increment days by 15
LocalDateTime datetime = LocalDateTime.now();
System.out.println("Current Date & Time: " + datetime);
datetime = datetime.plusDays(15);
System.out.println("Date & Time after Increment: " + datetime);
// increment month, day, year
LocalDateTime datetime2 = LocalDateTime.of(2015, Month.AUGUST, 5, 12, 45);
System.out.println("Original Date & Time: " + datetime2);
datetime2 = datetime2.plusDays(20).plusMonths(10).plusYears(3);
System.out.println("Date & Time after Increment: " + datetime2);
// minus days, month
LocalDateTime datetime3 = LocalDateTime.parse("2019-12-25T15:18:25");
System.out.println("Original Date & Time: " + datetime3);
datetime3 = datetime3.minusDays(5).minusMonths(5);
System.out.println("Date & Time after Decrement: " + datetime3);
The output of the above code snippet looks like the below:
Current Date & Time: 2019-12-26T18:07:15.607
Date & Time after Increment: 2020-01-10T18:07:15.607
Original Date & Time: 2015-08-05T12:45
Date & Time after Increment: 2019-06-25T12:45
Original Date & Time: 2019-12-25T15:18:25
Date & Time after Decrement: 2019-07-20T15:18:25
Add days to to ZonedDateTime
object
The ZonedDateTime
represents a date and time with a timezone in ISO-8601 format (e.g 2016-12-15T10:15:30+01:00[Europe/Paris]). Here is an example that shows how to add and minus days from an instance of ZonedDateTime
:
// increment days by 25
ZonedDateTime datetime = ZonedDateTime.now(ZoneId.systemDefault());
System.out.println("Current Date & Time: " + datetime);
datetime = datetime.plusDays(25);
System.out.println("Date & Time after Increment: " + datetime);
// increment month, day, year
ZonedDateTime datetime2 = ZonedDateTime.of(2010, 5, 5, 12, 45, 0, 0, ZoneId.of("Asia/Karachi"));
System.out.println("Original Date & Time: " + datetime2);
datetime2 = datetime2.plusDays(5).plusMonths(2).plusYears(2);
System.out.println("Date & Time after Increment: " + datetime2);
// minus days, month
ZonedDateTime datetime3 = ZonedDateTime.parse("2016-12-15T10:15:30+01:00[Europe/Paris]");
System.out.println("Original Date & Time: " + datetime3);
datetime3 = datetime3.minusDays(15).minusMonths(8);
System.out.println("Date & Time after Decrement: " + datetime3);
The above code snippet will print the following on the console:
Current Date & Time: 2019-12-26T18:12:28.358+05:00[Asia/Karachi]
Date & Time after Increment: 2020-01-20T18:12:28.358+05:00[Asia/Karachi]
Original Date & Time: 2010-05-05T12:45+05:00[Asia/Karachi]
Date & Time after Increment: 2012-07-10T12:45+05:00[Asia/Karachi]
Original Date & Time: 2016-12-15T10:15:30+01:00[Europe/Paris]
Date & Time after Decrement: 2016-03-30T10:15:30+02:00[Europe/Paris]
Add days to OffsetDateTime
object
OffsetDateTime
is another class from Java 8 new date and time API that represents a date and time with an offset from UTC/Greenwich in the ISO-8601 format (e.g. 2017-12-30T23:15:30-05:00).
The following example demonstrates how you can add or minus days, months, and years from an object of OffsetDateTime
:
// increment days by 14
OffsetDateTime datetime = OffsetDateTime.now();
System.out.println("Current Date & Time: " + datetime);
datetime = datetime.plusDays(14);
System.out.println("Date & Time after Increment: " + datetime);
// increment month, day, year
OffsetDateTime datetime2 = OffsetDateTime.of(2014, 12, 15, 23, 45, 55, 0, ZoneOffset.of("+03:30"));
System.out.println("Original Date & Time: " + datetime2);
datetime2 = datetime2.plusDays(15).plusMonths(5).plusYears(4);
System.out.println("Date & Time after Increment: " + datetime2);
// minus days, month
OffsetDateTime datetime3 = OffsetDateTime.parse("2017-12-30T23:15:30-05:00");
System.out.println("Original Date & Time: " + datetime3);
datetime3 = datetime3.minusDays(10).minusMonths(6);
System.out.println("Date & Time after Decrement: " + datetime3);
Here is what the output looks like:
Current Date & Time: 2019-12-26T18:18:43.725+05:00
Date & Time after Increment: 2020-01-09T18:18:43.725+05:00
Original Date & Time: 2014-12-15T23:45:55+03:30
Date & Time after Increment: 2019-05-30T23:45:55+03:30
Original Date & Time: 2017-12-30T23:15:30-05:00
Date & Time after Decrement: 2017-06-20T23:15:30-05:00
Legacy Date
& Calendar
API
Before Java 8, java.util.Date
and java.util.Calendar
classes were used for handling dates and times. To add or minus days from an instance of Date
, you can use the Calendar.add()
method as shown below:
// date pattern
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
// increment current date by 7 days
Date date = new Date();
System.out.println("Current Date: " + formatter.format(date));
// use `Calendar` to add days
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, 7);
// print date after the increment
System.out.println("Date after Increment: " + formatter.format(c.getTime()));
The above code will output the following:
Current Date: 2019-12-26
Date after Increment: 2020-01-02
Here is another example that shows how you can parse a string to a date and then add or minus days, months, years, seconds, and hours from an instance of Date
:
try {
// date pattern
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
// parse string to date
Date date = formatter.parse("17-Jul-2018 05:15:45");
System.out.println("Original Date: " + formatter.format(date));
// use `Calendar` to add days, months, years
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, 7);
c.add(Calendar.MONTH, -2); // minus months
c.add(Calendar.YEAR, 1);
c.add(Calendar.HOUR, 2);
c.add(Calendar.MINUTE, 12);
c.add(Calendar.SECOND, 45);
// print date after the increment
System.out.println("Date after Increment: " + formatter.format(c.getTime()));
} catch (ParseException ex) {
ex.printStackTrace();
}
Now, if you execute the above code, you should see the following output:
Original Date: 17-Jul-2018 05:15:45
Date after Increment: 24-May-2019 07:28:30
Conclusion
In this guide, we looked at different ways to add and subtract different date and time units like days, months, and years from a date. Java 8 new date and time API provides plenty of utility methods for manipulating dates and times. We learned to use plusDays()
and minusDays()
methods to add or minus days from an instance of LocalDate
, LocalDateTime
, ZonedDateTime
, and OffsetDateTime
.
Finally, we also looked at how to use the Calendar.add()
method to add or subtract a specified amount of time from an instance of the legacy Date
class.
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.