Java 8 Add Days to Current Date Example
1. Introduction
Java 8 added a new set of packages to provide a comprehensive date-time model. Java 8 Date-Time API is a JSR-310 implementation. In this example, I will use classes from the java.time and java.time.format packages to demonstrate how to add days to the current date.
java.time
is the base package which includes a set of date-time classes: LocalDate, LocalDateTime, Instant, Period, Duration, Clock, ZonedDateTime, etc. These classes use the calendar system defined in ISO-8601 as the default calendar. Each class has anow()
method to get the current date and common utility operations: plus, minus, format, etc.java.time.format
includes classes for formatting and parsing date and time objects.
2. Technologies used
The example code in this article was built and run using:
- Java 1.8.101
- Maven 3.3.9
- Eclipse Oxygen
- JUnit 4.12
3. Maven Project
3.1 Dependency
Add Junit to the pom.xml.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>zheng.jcg.demo</groupId> <artifactId>java8-date</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
3.2 Add Days with LocalDate
LocalDate
is a date class without a time-zone in the ISO-8601 calendar system, such as 2018-10-25.
In this step, I will demonstrate how to add days to the current date using LocalDate
in two steps:
- Get the current date via the
now
method. - Add days via the
plusDays
andplus
methods.
AddDays_LocalDate.java
package com.zheng.demo; import java.time.LocalDate; import java.time.Period; import java.time.ZoneId; import java.time.temporal.ChronoUnit; public class AddDays_LocalDate { private LocalDate currentDate; public AddDays_LocalDate() { super(); currentDate = LocalDate.now(); } public AddDays_LocalDate(ZoneId zoneId) { currentDate = LocalDate.now(zoneId); } public LocalDate addDays_by_plusDays(Long days) { return currentDate.plusDays(days.longValue()); } public LocalDate addDays_by_plus(Long days) { return currentDate.plus(Period.ofDays(days.intValue())); } public LocalDate addDays_by_plus_2(Long days) { return currentDate.plus(days.longValue(), ChronoUnit.DAYS); } public LocalDate substractDays_by_minusDays(Long days) { return currentDate.minusDays(days.longValue()); } public LocalDate getCurrentDate() { return currentDate; } public void setCurrentDate(LocalDate currentDate) { this.currentDate = currentDate; } }
3.3 Add Days with LocalDateTime
LocalDateTime
is a date-time class without a time-zone in the ISO-8601 calendar system, such as 2018-10-25T10:15:30.
In this step, I will demonstrate how to add days to the current date using LocalDateTime
via plus
and plusDays
.
AddDays_LocalDateTime.java
package com.zheng.demo; import java.time.LocalDateTime; import java.time.Period; import java.time.ZoneId; import java.time.temporal.ChronoUnit; public class AddDays_LocalDateTime { private LocalDateTime currentDate; public AddDays_LocalDateTime() { super(); currentDate = LocalDateTime.now(); } public AddDays_LocalDateTime(ZoneId zoneId) { currentDate = LocalDateTime.now(zoneId); } public LocalDateTime addDays_by_plusDays(Long days) { return currentDate.plusDays(days.longValue()); } public LocalDateTime addDays_by_plus(Long days) { return currentDate.plus(Period.ofDays( days.intValue())); } public LocalDateTime addDays_by_plus_2(Long days) { return currentDate.plus(days.longValue(), ChronoUnit.DAYS); } public LocalDateTime substractDays_by_minusDays(Long days) { return currentDate.minusDays(days.longValue()); } public LocalDateTime getCurrentDate() { return currentDate; } public void setCurrentDate(LocalDateTime currentDate) { this.currentDate = currentDate; } }
3.4 Add Days with ZonedDateTime
ZonedDateTime
is a date-time class with a time-zone in the ISO-8601 calendar system, such as 2018-10-25T10:15:30+01:00 US/Central.
In this step, I will demonstrate how to add days to the current date using ZonedDateTime
via plus
and plusDays
.
AddDays_ZonedDateTime.java
package com.zheng.demo; import java.time.Period; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; public class AddDays_ZonedDateTime { private ZonedDateTime currentDate; public AddDays_ZonedDateTime() { super(); currentDate = ZonedDateTime.now(); } public ZonedDateTime addDays_by_plusDays(Long days) { return currentDate.plusDays(days.longValue()); } public ZonedDateTime addDays_by_plus(Long days) { return currentDate.plus(Period.ofDays(days.intValue())); } public ZonedDateTime addDays_by_plus_2(Long days) { return currentDate.plus(days.longValue(), ChronoUnit.DAYS); } public ZonedDateTime substractDays_by_minusDays(Long days) { return currentDate.minusDays(days.longValue()); } public ZonedDateTime getCurrentDate() { return currentDate; } public void setCurrentDate(ZonedDateTime currentDate) { this.currentDate = currentDate; } }
3.5 Add Days with Instant
Instant models a single instantaneous point on the timeline.
In this step, I will demonstrate how to add days to the current date using Instant
via plus
.
AddDays_Instant.java
package com.zheng.demo; import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; public class AddDays_Instant { private Instant currentDate; public AddDays_Instant() { super(); currentDate = Instant.now(); } public Instant addDays_by_plus_2(Long days) { return currentDate.plus(days.longValue(), ChronoUnit.DAYS); } public Instant addDays_by_plus(Long days) { return currentDate.plus(Duration.ofDays(days.longValue())); } public Instant substractDaysWithMinus(Long days) { return currentDate.minus(days.longValue(), ChronoUnit.DAYS); } public Instant getCurrentDate() { return currentDate; } public void setCurrentDate(Instant currentDate) { this.currentDate = currentDate; } }
3.6 Add Days with Clock
Clock
provides access to the current instant, date, and time.
In this step, I will demonstrate how to add days to the current date using Clock
via instant
.
AddDays_Clock.java
package com.zheng.demo; import java.time.Clock; import java.time.Instant; import java.time.temporal.ChronoUnit; public class AddDays_Clock { private Instant currentDate; public AddDays_Clock() { super(); currentDate = Clock.systemUTC().instant(); } public Instant addDays(Long days) { return currentDate.plus(days.longValue(), ChronoUnit.DAYS); } public Instant substractDays(Long days) { return currentDate.minus(days.longValue(), ChronoUnit.DAYS); } public Instant getCurrentDate() { return currentDate; } public void setCurrentDate(Instant currentDate) { this.currentDate = currentDate; } }
4. JUnit Tests
4.1 AddDays_LocalDateTest
I will use the Java 8 DateTimeFormatter
to format the LocalDate
as "yyyy/MM/dd"
and test it by adding days to the current date.
AddDays_LocalDateTest.java
package com.zheng.demo; import static org.junit.Assert.assertTrue; import java.time.LocalDate; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import org.junit.Before; import org.junit.Test; public class AddDays_LocalDateTest { private static final int DAYS = 10; private static final String ISO8601_DATE_FORMAT = "yyyy/MM/dd"; private static final DateTimeFormatter dateFormat8 = DateTimeFormatter.ofPattern(ISO8601_DATE_FORMAT); private AddDays_LocalDate defaultZoneDate = new AddDays_LocalDate(); private AddDays_LocalDate cstZoneDate = new AddDays_LocalDate(ZoneId.of("US/Central")); @Before public void setup() { System.out.println("*****\nOriginal defaultZoneDate : " + dateFormat8.format(defaultZoneDate.getCurrentDate())); System.out.println("*****\nOriginal cstZoneDate : " + dateFormat8.format(cstZoneDate.getCurrentDate())); System.out.println("-----"); } @Test public void test_addDays_by_plusDays() { for (int i = 0; i < DAYS; i++) { LocalDate plusDate = defaultZoneDate.addDays_by_plusDays(Long.valueOf(i)); assertTrue( i == plusDate.getDayOfYear() - defaultZoneDate.getCurrentDate().getDayOfYear()); System.out.println(" Plus " + i + " days = " + dateFormat8.format(plusDate)); } } @Test public void test_substractDays_by_minusDays() { for (int i = 0; i < DAYS; i++) { LocalDate minusDate = defaultZoneDate.substractDays_by_minusDays(Long.valueOf(i)); assertTrue( i == defaultZoneDate.getCurrentDate().getDayOfYear() - minusDate.getDayOfYear()); System.out.println(" Minus " + i + " days = " + dateFormat8.format(minusDate)); } } }
4.2 AddDays_LocalDateTimeTest
I will use the Java 8 DateTimeFormatter
to format the LocalDateTime
as "yyyy/MM/dd HH:mm:ss"
and test it by adding days to the current date.
AddDays_LocalDateTimeTest.java
package com.zheng.demo; import static org.junit.Assert.assertTrue; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import org.junit.Before; import org.junit.Test; public class AddDays_LocalDateTimeTest { private static final int DAYS = 10; private static final String ISO8601_DATE_FORMAT = "yyyy/MM/dd HH:mm:ss"; private static final DateTimeFormatter dateFormat8 = DateTimeFormatter.ofPattern(ISO8601_DATE_FORMAT); private AddDays_LocalDateTime defaultZoneDate = new AddDays_LocalDateTime(); private AddDays_LocalDateTime cstZoneDate = new AddDays_LocalDateTime(ZoneId.of("US/Central")); @Before public void setup() { System.out.println("*****\nOriginal defaultZoneDate : " + dateFormat8.format(defaultZoneDate.getCurrentDate())); System.out.println("*****\nOriginal cstZoneDate : " + dateFormat8.format(cstZoneDate.getCurrentDate())); System.out.println("-----"); } @Test public void plus_number_of_days() { for (int i = 0; i < DAYS; i++) { LocalDateTime plusDate = defaultZoneDate.addDays_by_plusDays(Long.valueOf(i)); assertTrue( i == plusDate.getDayOfYear() - defaultZoneDate.getCurrentDate().getDayOfYear()); System.out.println("Plus " + i + " = " + dateFormat8.format(plusDate)); } } @Test public void minus_number_of_days() { for (int i = 0; i < DAYS; i++) { LocalDateTime minusDate = defaultZoneDate.substractDays_by_minusDays(Long.valueOf(i)); assertTrue( i == defaultZoneDate.getCurrentDate().getDayOfYear() - minusDate.getDayOfYear()); System.out.println("Minus" + i + " = " + dateFormat8.format(minusDate)); } } }
4.3 AddDays_ZondedDateTimeTest
I will use the Java 8 ZonedTimeFormatter
to format the ZondedDateTime
and test it by adding days to the current date.
AddDays_ZondedDateTimeTest.java
package com.zheng.demo; import static org.junit.Assert.assertTrue; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import org.junit.Before; import org.junit.Test; public class AddDays_ZonedDateTimeTest { private static final int DAYS = 10; private static final DateTimeFormatter dateFormat8 = DateTimeFormatter.ISO_OFFSET_DATE_TIME; private AddDays_ZonedDateTime defaultZoneDate = new AddDays_ZonedDateTime(); @Before public void setup() { System.out.println("*****\nOriginal defaultZoneDate : " + dateFormat8.format(defaultZoneDate.getCurrentDate())); System.out.println("-----"); } @Test public void add_number_of_days_byPlusDays() { for (int i = 1; i < DAYS; i++) { ZonedDateTime plusDate = defaultZoneDate.addDays_by_plusDays(Long.valueOf(i)); assertTrue( i == plusDate.getDayOfYear() - defaultZoneDate.getCurrentDate().getDayOfYear()); System.out.println("PlusDays " + i + " = " + dateFormat8.format(plusDate)); } } @Test public void add_number_of_days_byPlus() { for (int i = 1; i < DAYS; i++) { ZonedDateTime plusDate = defaultZoneDate.addDays_by_plus(Long.valueOf(i)); assertTrue( i == plusDate.getDayOfYear() - defaultZoneDate.getCurrentDate().getDayOfYear()); System.out.println("Plus " + i + "= " + dateFormat8.format(plusDate)); } } }
4.4 AddDays_InstantTest
I will add days to the current date with Instant
.
AddDays_InstantTest.java
package com.zheng.demo; import java.time.Instant; import org.junit.Before; import org.junit.Test; public class AddDays_InstantTest { private AddDays_Instant defaultZoneDate = new AddDays_Instant(); @Before public void setup() { System.out.println("*****\nOriginal defaultZoneDate : " + defaultZoneDate.getCurrentDate()); System.out.println("-----"); } @Test public void plus_number_of_days() { for (int i = 0; i < 10; i++) { Instant plusDate = defaultZoneDate.addDays_by_plus(Long.valueOf(i)); System.out.println("Plus " + i + " = " + plusDate); } } @Test public void minus_number_of_days() { for (int i = 0; i < 10; i++) { Instant minusDate = defaultZoneDate.substractDaysWithMinus(Long.valueOf(i)); System.out.println("Minus" + i + "= " + minusDate); } } }
4.5 AddDays_ClockTest
I will add days to the current date with the Clock
.
AddDays_ClockTest.java
package com.zheng.demo; import java.time.Instant; import org.junit.Before; import org.junit.Test; public class AddDays_ClockTest { private AddDays_Clock defaultZoneDate = new AddDays_Clock(); @Before public void setup() { System.out.println("*****\nOriginal defaultZoneDate : " + defaultZoneDate.getCurrentDate()); System.out.println("-----"); } @Test public void plus_number_of_days() { for (int i = 1; i < 10; i++) { Instant plusDate = defaultZoneDate.addDays(Long.valueOf(i)); System.out.println("defaultZoneDate Plus " + i + " = " + plusDate); } } @Test public void minus_number_of_days() { for (int i = 1; i < 10; i++) { Instant minusDate = defaultZoneDate.substractDays(Long.valueOf(i)); System.out.println("defaultZoneDate Minus" + i + "= " + minusDate); } } }
5. Demo
Execute mvn clean install
and capture the output:
Tests Output
-------------- T E S T S ------------------------------------------------------- Running com.zheng.demo.AddDays_ClockTest ***** Original defaultZoneDate : 2018-10-25T19:30:20.773Z ----- defaultZoneDate Minus1= 2018-10-24T19:30:20.773Z defaultZoneDate Minus2= 2018-10-23T19:30:20.773Z defaultZoneDate Minus3= 2018-10-22T19:30:20.773Z defaultZoneDate Minus4= 2018-10-21T19:30:20.773Z defaultZoneDate Minus5= 2018-10-20T19:30:20.773Z defaultZoneDate Minus6= 2018-10-19T19:30:20.773Z defaultZoneDate Minus7= 2018-10-18T19:30:20.773Z defaultZoneDate Minus8= 2018-10-17T19:30:20.773Z defaultZoneDate Minus9= 2018-10-16T19:30:20.773Z ***** Original defaultZoneDate : 2018-10-25T19:30:20.824Z ----- defaultZoneDate Plus 1 = 2018-10-26T19:30:20.824Z defaultZoneDate Plus 2 = 2018-10-27T19:30:20.824Z defaultZoneDate Plus 3 = 2018-10-28T19:30:20.824Z defaultZoneDate Plus 4 = 2018-10-29T19:30:20.824Z defaultZoneDate Plus 5 = 2018-10-30T19:30:20.824Z defaultZoneDate Plus 6 = 2018-10-31T19:30:20.824Z defaultZoneDate Plus 7 = 2018-11-01T19:30:20.824Z defaultZoneDate Plus 8 = 2018-11-02T19:30:20.824Z defaultZoneDate Plus 9 = 2018-11-03T19:30:20.824Z Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.154 sec Running com.zheng.demo.AddDays_InstantTest ***** Original defaultZoneDate : 2018-10-25T19:30:20.829Z ----- Minus0= 2018-10-25T19:30:20.829Z Minus1= 2018-10-24T19:30:20.829Z Minus2= 2018-10-23T19:30:20.829Z Minus3= 2018-10-22T19:30:20.829Z Minus4= 2018-10-21T19:30:20.829Z Minus5= 2018-10-20T19:30:20.829Z Minus6= 2018-10-19T19:30:20.829Z Minus7= 2018-10-18T19:30:20.829Z Minus8= 2018-10-17T19:30:20.829Z Minus9= 2018-10-16T19:30:20.829Z ***** Original defaultZoneDate : 2018-10-25T19:30:20.831Z ----- Plus 0 = 2018-10-25T19:30:20.831Z Plus 1 = 2018-10-26T19:30:20.831Z Plus 2 = 2018-10-27T19:30:20.831Z Plus 3 = 2018-10-28T19:30:20.831Z Plus 4 = 2018-10-29T19:30:20.831Z Plus 5 = 2018-10-30T19:30:20.831Z Plus 6 = 2018-10-31T19:30:20.831Z Plus 7 = 2018-11-01T19:30:20.831Z Plus 8 = 2018-11-02T19:30:20.831Z Plus 9 = 2018-11-03T19:30:20.831Z Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.014 sec Running com.zheng.demo.AddDays_LocalDateTest ***** Original defaultZoneDate : 2018/10/25 ***** Original cstZoneDate : 2018/10/25 ----- Plus 0 days = 2018/10/25 Plus 1 days = 2018/10/26 Plus 2 days = 2018/10/27 Plus 3 days = 2018/10/28 Plus 4 days = 2018/10/29 Plus 5 days = 2018/10/30 Plus 6 days = 2018/10/31 Plus 7 days = 2018/11/01 Plus 8 days = 2018/11/02 Plus 9 days = 2018/11/03 ***** Original defaultZoneDate : 2018/10/25 ***** Original cstZoneDate : 2018/10/25 ----- Minus 0 days = 2018/10/25 Minus 1 days = 2018/10/24 Minus 2 days = 2018/10/23 Minus 3 days = 2018/10/22 Minus 4 days = 2018/10/21 Minus 5 days = 2018/10/20 Minus 6 days = 2018/10/19 Minus 7 days = 2018/10/18 Minus 8 days = 2018/10/17 Minus 9 days = 2018/10/16 Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.048 sec Running com.zheng.demo.AddDays_LocalDateTimeTest ***** Original defaultZoneDate : 2018/10/25 14:30:20 ***** Original cstZoneDate : 2018/10/25 14:30:20 ----- Minus0 = 2018/10/25 14:30:20 Minus1 = 2018/10/24 14:30:20 Minus2 = 2018/10/23 14:30:20 Minus3 = 2018/10/22 14:30:20 Minus4 = 2018/10/21 14:30:20 Minus5 = 2018/10/20 14:30:20 Minus6 = 2018/10/19 14:30:20 Minus7 = 2018/10/18 14:30:20 Minus8 = 2018/10/17 14:30:20 Minus9 = 2018/10/16 14:30:20 ***** Original defaultZoneDate : 2018/10/25 14:30:20 ***** Original cstZoneDate : 2018/10/25 14:30:20 ----- Plus 0 = 2018/10/25 14:30:20 Plus 1 = 2018/10/26 14:30:20 Plus 2 = 2018/10/27 14:30:20 Plus 3 = 2018/10/28 14:30:20 Plus 4 = 2018/10/29 14:30:20 Plus 5 = 2018/10/30 14:30:20 Plus 6 = 2018/10/31 14:30:20 Plus 7 = 2018/11/01 14:30:20 Plus 8 = 2018/11/02 14:30:20 Plus 9 = 2018/11/03 14:30:20 Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.065 sec Running com.zheng.demo.AddDays_ZonedDateTimeTest ***** Original defaultZoneDate : 2018-10-25T14:30:20.876-05:00 ----- PlusDays 1 = 2018-10-26T14:30:20.876-05:00 PlusDays 2 = 2018-10-27T14:30:20.876-05:00 PlusDays 3 = 2018-10-28T14:30:20.876-05:00 PlusDays 4 = 2018-10-29T14:30:20.876-05:00 PlusDays 5 = 2018-10-30T14:30:20.876-05:00 PlusDays 6 = 2018-10-31T14:30:20.876-05:00 PlusDays 7 = 2018-11-01T14:30:20.876-05:00 PlusDays 8 = 2018-11-02T14:30:20.876-05:00 PlusDays 9 = 2018-11-03T14:30:20.876-05:00 ***** Original defaultZoneDate : 2018-10-25T14:30:20.882-05:00 ----- Plus 1= 2018-10-26T14:30:20.882-05:00 Plus 2= 2018-10-27T14:30:20.882-05:00 Plus 3= 2018-10-28T14:30:20.882-05:00 Plus 4= 2018-10-29T14:30:20.882-05:00 Plus 5= 2018-10-30T14:30:20.882-05:00 Plus 6= 2018-10-31T14:30:20.882-05:00 Plus 7= 2018-11-01T14:30:20.882-05:00 Plus 8= 2018-11-02T14:30:20.882-05:00 Plus 9= 2018-11-03T14:30:20.882-05:00 Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.08 sec Results : Tests run: 10, Failures: 0, Errors: 0, Skipped: 0
6. Java 8 Add Days to Current Date Example- Summary
In this example, we demonstrated how to add days to the current date via the classes: LocalDate
, LocalDateTime
, ZonedDateTime
, Instant
, and Clock
.
7. Download the Source Code
This example consists of a Maven project to add days to the current date using Java 8 Date-Time API.
You can download the full source code of this example here: Java 8 Add days to Current Date Example