Simple clean of difference between dates
This commit is contained in:
parent
348ff5d403
commit
a663b3ccde
|
@ -141,3 +141,6 @@
|
||||||
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
|
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
|
||||||
- [How to Get the Last Element of a Stream in Java?](http://www.baeldung.com/java-stream-last-element)
|
- [How to Get the Last Element of a Stream in Java?](http://www.baeldung.com/java-stream-last-element)
|
||||||
- [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char)
|
- [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char)
|
||||||
|
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
|
||||||
|
- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro)
|
||||||
|
- [Migrating to the New Java 8 Date Time API](http://www.baeldung.com/migrating-to-java-8-date-time-api)
|
||||||
|
|
|
@ -186,6 +186,16 @@
|
||||||
<artifactId>fscontext</artifactId>
|
<artifactId>fscontext</artifactId>
|
||||||
<version>${fscontext.version}</version>
|
<version>${fscontext.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>joda-time</groupId>
|
||||||
|
<artifactId>joda-time</artifactId>
|
||||||
|
<version>${joda-time.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.darwinsys</groupId>
|
||||||
|
<artifactId>hirondelle-date4j</artifactId>
|
||||||
|
<version>${hirondelle-date4j.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -408,6 +418,8 @@
|
||||||
<grep4j.version>1.8.7</grep4j.version>
|
<grep4j.version>1.8.7</grep4j.version>
|
||||||
<lombok.version>1.16.12</lombok.version>
|
<lombok.version>1.16.12</lombok.version>
|
||||||
<fscontext.version>4.6-b01</fscontext.version>
|
<fscontext.version>4.6-b01</fscontext.version>
|
||||||
|
<joda-time.version>2.9.9</joda-time.version>
|
||||||
|
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
||||||
|
|
||||||
<!-- testing -->
|
<!-- testing -->
|
||||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||||
|
@ -416,9 +428,9 @@
|
||||||
<assertj.version>3.6.1</assertj.version>
|
<assertj.version>3.6.1</assertj.version>
|
||||||
<avaitility.version>1.7.0</avaitility.version>
|
<avaitility.version>1.7.0</avaitility.version>
|
||||||
|
|
||||||
|
|
||||||
<!-- maven plugins -->
|
<!-- maven plugins -->
|
||||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||||
|
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.joda.time.DateTime;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class DateDiffUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoDatesBeforeJava8_whenDifferentiating_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");
|
||||||
|
|
||||||
|
long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
|
||||||
|
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
|
assertEquals(diff, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() {
|
||||||
|
ZonedDateTime now = ZonedDateTime.now();
|
||||||
|
ZonedDateTime sixDaysBehind = now.minusDays(6);
|
||||||
|
|
||||||
|
Duration duration = Duration.between(now, sixDaysBehind);
|
||||||
|
long diff = Math.abs(duration.toDays());
|
||||||
|
|
||||||
|
assertEquals(diff, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix() {
|
||||||
|
DateTime now = DateTime.now();
|
||||||
|
DateTime sixDaysBehind = now.minusDays(6);
|
||||||
|
|
||||||
|
org.joda.time.Duration duration = new org.joda.time.Duration(now, sixDaysBehind);
|
||||||
|
long diff = Math.abs(duration.getStandardDays());
|
||||||
|
|
||||||
|
assertEquals(diff, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() {
|
||||||
|
hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault());
|
||||||
|
hirondelle.date4j.DateTime sixDaysBehind = now.minusDays(6);
|
||||||
|
|
||||||
|
long diff = Math.abs(now.numDaysFrom(sixDaysBehind));
|
||||||
|
|
||||||
|
assertEquals(diff, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,30 +0,0 @@
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,55 +5,56 @@ import org.junit.Test;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.Duration;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class DateDiffTest {
|
public class DateDiffTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenTwoDatesBeforeJava8SeparatedBySixDays_whenCallingDiffOnThem_thenWeGetSix() throws ParseException {
|
public void givenTwoDatesBeforeJava8_whenDifferentiating_thenWeGetSix() throws ParseException {
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
|
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
|
||||||
Date firstDate = sdf.parse("06/24/2017");
|
Date firstDate = sdf.parse("06/24/2017");
|
||||||
Date secondDate = sdf.parse("06/30/2017");
|
Date secondDate = sdf.parse("06/30/2017");
|
||||||
|
|
||||||
DateDiff dateDiff = new DateDiff();
|
long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
|
||||||
long diff = dateDiff.beforeJava8diff(firstDate, secondDate);
|
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
assertEquals(diff, 6);
|
assertEquals(diff, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTheCurrentDateAndSixDaysBehindInJava8_whenCallingDiffOnThem_thenWeGetSix() {
|
public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() {
|
||||||
ZonedDateTime now = ZonedDateTime.now();
|
ZonedDateTime now = ZonedDateTime.now();
|
||||||
ZonedDateTime sixDaysBehind = now.minusDays(6);
|
ZonedDateTime sixDaysBehind = now.minusDays(6);
|
||||||
|
|
||||||
DateDiff dateDiff = new DateDiff();
|
Duration duration = Duration.between(now, sixDaysBehind);
|
||||||
long diff = dateDiff.fromJava8Diff(now, sixDaysBehind);
|
long diff = Math.abs(duration.toDays());
|
||||||
|
|
||||||
assertEquals(diff, 6);
|
assertEquals(diff, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTheCurrentDateAndSixDaysBehindInJodaTime_whenCallingDiffOnThem_thenWeGetSix() {
|
public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix() {
|
||||||
DateTime now = DateTime.now();
|
DateTime now = DateTime.now();
|
||||||
DateTime sixDaysBehind = now.minusDays(6);
|
DateTime sixDaysBehind = now.minusDays(6);
|
||||||
|
|
||||||
DateDiff dateDiff = new DateDiff();
|
org.joda.time.Duration duration = new org.joda.time.Duration(now, sixDaysBehind);
|
||||||
long diff = dateDiff.fromJodaTime(now, sixDaysBehind);
|
long diff = Math.abs(duration.getStandardDays());
|
||||||
|
|
||||||
assertEquals(diff, 6);
|
assertEquals(diff, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTheCurrentDateAndSixDaysBehindInDate4j_whenCallingDiffOnThem_thenWeGetSix() {
|
public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() {
|
||||||
hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault());
|
hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault());
|
||||||
hirondelle.date4j.DateTime sixDaysBehind = now.minusDays(6);
|
hirondelle.date4j.DateTime sixDaysBehind = now.minusDays(6);
|
||||||
|
|
||||||
DateDiff dateDiff = new DateDiff();
|
long diff = Math.abs(now.numDaysFrom(sixDaysBehind));
|
||||||
long diff = dateDiff.fromDate4j(now, sixDaysBehind);
|
|
||||||
|
|
||||||
assertEquals(diff, 6);
|
assertEquals(diff, 6);
|
||||||
}
|
}
|
||||||
|
|
1
pom.xml
1
pom.xml
|
@ -76,7 +76,6 @@
|
||||||
<module>immutables</module>
|
<module>immutables</module>
|
||||||
|
|
||||||
<module>jackson</module>
|
<module>jackson</module>
|
||||||
<module>java-difference-date</module>
|
|
||||||
<!-- <module>java-cassandra</module> -->
|
<!-- <module>java-cassandra</module> -->
|
||||||
<module>vavr</module>
|
<module>vavr</module>
|
||||||
<module>javax-servlets</module>
|
<module>javax-servlets</module>
|
||||||
|
|
Loading…
Reference in New Issue