diff --git a/java-difference-date/README.md b/java-difference-date/README.md new file mode 100644 index 0000000000..45f8216849 --- /dev/null +++ b/java-difference-date/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [Constructor Dependency Injection in Spring](http://www.baeldung.com/constructor-injection-in-spring) \ No newline at end of file diff --git a/java-difference-date/pom.xml b/java-difference-date/pom.xml new file mode 100644 index 0000000000..388753de90 --- /dev/null +++ b/java-difference-date/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + com.baeldung + java-difference-date + 0.0.1-SNAPSHOT + jar + + java-difference-date + Difference between two dates in java + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + joda-time + joda-time + ${joda-time.version} + + + com.darwinsys + hirondelle-date4j + ${hirondelle-date4j.version} + + + junit + junit + ${junit.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + 1.8 + 4.12 + 2.9.9 + 1.5.1 + + diff --git a/java-difference-date/src/main/java/com/baeldung/DateDiff.java b/java-difference-date/src/main/java/com/baeldung/DateDiff.java new file mode 100644 index 0000000000..08fc02fab7 --- /dev/null +++ b/java-difference-date/src/main/java/com/baeldung/DateDiff.java @@ -0,0 +1,30 @@ +package com.baeldung; + +import org.joda.time.DateTime; + +import java.time.Duration; +import java.time.ZonedDateTime; +import java.util.Date; +import java.util.concurrent.TimeUnit; + +public class DateDiff { + public long beforeJava8diff(Date firstDate, Date secondDate){ + long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime()); + return TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS); + } + + public long fromJava8Diff(ZonedDateTime firstDate, ZonedDateTime secondDate){ + Duration duration = Duration.between(firstDate, secondDate); + return Math.abs(duration.toDays()); + } + + public long fromJodaTime(DateTime firstDate, DateTime secondDate){ + org.joda.time.Duration duration = new org.joda.time.Duration(firstDate, secondDate); + return Math.abs(duration.getStandardDays()); + } + + public long fromDate4j(hirondelle.date4j.DateTime firstDate, hirondelle.date4j.DateTime secondDate){ + long diff = firstDate.numDaysFrom(secondDate); + return Math.abs(diff); + } +} \ No newline at end of file diff --git a/java-difference-date/src/test/java/com/baeldung/DateDiffTest.java b/java-difference-date/src/test/java/com/baeldung/DateDiffTest.java new file mode 100644 index 0000000000..fe2b507cf3 --- /dev/null +++ b/java-difference-date/src/test/java/com/baeldung/DateDiffTest.java @@ -0,0 +1,60 @@ +package com.baeldung; + +import org.joda.time.DateTime; +import org.junit.Test; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.ZonedDateTime; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; + +import static org.junit.Assert.assertEquals; + +public class DateDiffTest { + @Test + public void givenTwoDatesBeforeJava8SeparatedBySixDays_whenCallingDiffOnThem_thenWeGetSix() throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH); + Date firstDate = sdf.parse("06/24/2017"); + Date secondDate = sdf.parse("06/30/2017"); + + DateDiff dateDiff = new DateDiff(); + long diff = dateDiff.beforeJava8diff(firstDate, secondDate); + + assertEquals(diff, 6); + } + + @Test + public void givenTheCurrentDateAndSixDaysBehindInJava8_whenCallingDiffOnThem_thenWeGetSix() { + ZonedDateTime now = ZonedDateTime.now(); + ZonedDateTime sixDaysBehind = now.minusDays(6); + + DateDiff dateDiff = new DateDiff(); + long diff = dateDiff.fromJava8Diff(now, sixDaysBehind); + + assertEquals(diff, 6); + } + + @Test + public void givenTheCurrentDateAndSixDaysBehindInJodaTime_whenCallingDiffOnThem_thenWeGetSix() { + DateTime now = DateTime.now(); + DateTime sixDaysBehind = now.minusDays(6); + + DateDiff dateDiff = new DateDiff(); + long diff = dateDiff.fromJodaTime(now, sixDaysBehind); + + assertEquals(diff, 6); + } + + @Test + public void givenTheCurrentDateAndSixDaysBehindInDate4j_whenCallingDiffOnThem_thenWeGetSix() { + hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault()); + hirondelle.date4j.DateTime sixDaysBehind = now.minusDays(6); + + DateDiff dateDiff = new DateDiff(); + long diff = dateDiff.fromDate4j(now, sixDaysBehind); + + assertEquals(diff, 6); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index eef32f884e..d2d9ae3987 100644 --- a/pom.xml +++ b/pom.xml @@ -55,6 +55,7 @@ immutables jackson + java-difference-date java-cassandra javaslang javax-servlets