Difference between two dates in java

This commit is contained in:
Dassi Orleando 2017-08-24 21:04:23 +01:00
parent 3a4cddb740
commit 28bb7fc8d3
5 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,3 @@
## Relevant articles:
- [Constructor Dependency Injection in Spring](http://www.baeldung.com/constructor-injection-in-spring)

View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>com.baeldung</groupId>
<artifactId>java-difference-date</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>java-difference-date</name>
<description>Difference between two dates in java</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<target>1.8</target>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<java.version>1.8</java.version>
<junit.version>4.12</junit.version>
<joda-time.version>2.9.9</joda-time.version>
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
</properties>
</project>

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -55,6 +55,7 @@
<module>immutables</module>
<module>jackson</module>
<module>java-difference-date</module>
<module>java-cassandra</module>
<module>javaslang</module>
<module>javax-servlets</module>