BAEL-1897: Code check-on for (http://jira.baeldung.com/browse/BAEL-1897) How to increment a Date by one day (#4615)

* abh.swar@gmail.com: Code check-in for article on Spring WebFlux
1. EmailWebClient is the client that subscribes to the data from WebFlux server
2. EmailGenerator generates one email per second randomly
3. EmailHandler and EmailRouter deal with handling of request of the subscriber
4. Email is the POJO for data transmitted by the server

* Code check-on for (http://jira.baeldung.com/browse/BAEL-1897) How to increment a Date by one day
- Added code and test to increment date by one day using java 8 and joda-time
- Added joda-time 2.10 dependency in pom.xml

* Revert "abh.swar@gmail.com: Code check-in for article on Spring WebFlux 1. EmailWebClient is the client that subscribes to the data from WebFlux server 2. EmailGenerator generates one email per second randomly 3. EmailHandler and EmailRouter deal with handling of request of the subscriber 4. Email is the POJO for data transmitted by the server"

This reverts commit 6254ad9

* Code check-on for (http://jira.baeldung.com/browse/BAEL-1897) How to increment a Date by one day
- Added code and test to increment date by one day using java.util.Calendar and ApacheCommons and unit tests for it
- Used properties for ${joda.version}
- Formatted the code using IntelliJ formatter
- Renamed DateIncrementerTest to DateIncrementerUnitTest
- Changed test method names to follow _given_when_then convention

* Code check-on for (http://jira.baeldung.com/browse/BAEL-1897) How to increment a Date by one day
- Removed unnecessary comment

* Code check-on for (http://jira.baeldung.com/browse/BAEL-1897) How to increment a Date by one day
- Corrected the order of parameters of assertEquals() method
This commit is contained in:
Abhinayak Swar 2018-07-13 00:40:06 +05:45 committed by Predrag Maric
parent 34f4a6bdb9
commit ed872e6118
3 changed files with 103 additions and 0 deletions

View File

@ -94,6 +94,11 @@
<artifactId>streamex</artifactId>
<version>${streamex.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda.version}</version>
</dependency>
</dependencies>
<build>
@ -233,6 +238,7 @@
<vavr.version>0.9.0</vavr.version>
<protonpack.version>1.13</protonpack.version>
<streamex.version>0.6.5</streamex.version>
<joda.version>2.10</joda.version>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<avaitility.version>1.7.0</avaitility.version>

View File

@ -0,0 +1,63 @@
package com.baeldung.datetime.modify;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Logger;
public class DateIncrementer {
private static final Logger log = Logger.getLogger(DateIncrementer.class.getName());
private static final int INCREMENT_BY_IN_DAYS = 1;
public static String addOneDay(String date) {
return LocalDate
.parse(date)
.plusDays(INCREMENT_BY_IN_DAYS)
.toString();
}
public static String addOneDayJodaTime(String date) {
DateTime dateTime = new DateTime(date);
return dateTime
.plusDays(INCREMENT_BY_IN_DAYS)
.toString("yyyy-MM-dd");
}
public static String addOneDayCalendar(String date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date));
c.add(Calendar.DATE, 1);
return sdf.format(c.getTime());
}
public static String addOneDayApacheCommons(String date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date incrementedDate = DateUtils.addDays(sdf.parse(date), 1);
return sdf.format(incrementedDate);
}
public static void main(String[] args) throws ParseException {
String date = LocalDate
.now()
.toString();
log.info("Current date = " + date);
String incrementedDateJava8 = DateIncrementer.addOneDay(date);
log.info("Date incremented by one day using (Java 8): " + incrementedDateJava8);
String incrementedDateJodaTime = DateIncrementer.addOneDayJodaTime(date);
log.info("Date incremented by one day using (Joda-Time): " + incrementedDateJodaTime);
String incrementedDateCalendar = addOneDayCalendar(date);
log.info("Date incremented by one day using (java.util.Calendar): " + incrementedDateCalendar);
String incrementedDateApacheCommons = addOneDayApacheCommons(date);
log.info("Date incremented by one day using (Apache Commons DateUtils): " + incrementedDateApacheCommons);
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.datetime.modify;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class DateIncrementerUnitTest {
private final static String DATE_TO_INCREMENT = "2018-07-03";
private final static String EXPECTED_DATE = "2018-07-04";
@Test
public void givenDate_whenUsingJava8_thenAddOneDay() throws Exception {
String incrementedDate = DateIncrementer.addOneDay(DATE_TO_INCREMENT);
assertEquals(EXPECTED_DATE, incrementedDate);
}
@Test
public void givenDate_whenUsingJodaTime_thenAddOneDay() throws Exception {
String incrementedDate = DateIncrementer.addOneDayJodaTime(DATE_TO_INCREMENT);
assertEquals(EXPECTED_DATE, incrementedDate);
}
@Test
public void givenDate_whenUsingCalendar_thenAddOneDay() throws Exception {
String incrementedDate = DateIncrementer.addOneDayCalendar(DATE_TO_INCREMENT);
assertEquals(EXPECTED_DATE, incrementedDate);
}
@Test
public void givenDate_whenUsingApacheCommons_thenAddOneDay() throws Exception {
String incrementedDate = DateIncrementer.addOneDayApacheCommons(DATE_TO_INCREMENT);
assertEquals(EXPECTED_DATE, incrementedDate);
}
}