Skip to content

Commit 8180274

Browse files
Dassi orleandomaibin
authored andcommitted
BAEL-1074 Difference between two dates in Java (eugenp#2493)
* Different types of bean injection in Spring * Difference between two dates in java * Update README.md * Simple clean of difference between dates * Clean my test article
1 parent c12ba86 commit 8180274

6 files changed

Lines changed: 203 additions & 1 deletion

File tree

‎core-java/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,6 @@
141141
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
142142
- [How to Get the Last Element of a Stream in Java?](http://www.baeldung.com/java-stream-last-element)
143143
- [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char)
144+
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
145+
- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro)
146+
- [Migrating to the New Java 8 Date Time API](http://www.baeldung.com/migrating-to-java-8-date-time-api)

‎core-java/pom.xml‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@
186186
<artifactId>fscontext</artifactId>
187187
<version>${fscontext.version}</version>
188188
</dependency>
189+
<dependency>
190+
<groupId>joda-time</groupId>
191+
<artifactId>joda-time</artifactId>
192+
<version>${joda-time.version}</version>
193+
</dependency>
194+
<dependency>
195+
<groupId>com.darwinsys</groupId>
196+
<artifactId>hirondelle-date4j</artifactId>
197+
<version>${hirondelle-date4j.version}</version>
198+
</dependency>
189199
</dependencies>
190200

191201
<build>
@@ -408,6 +418,8 @@
408418
<grep4j.version>1.8.7</grep4j.version>
409419
<lombok.version>1.16.12</lombok.version>
410420
<fscontext.version>4.6-b01</fscontext.version>
421+
<joda-time.version>2.9.9</joda-time.version>
422+
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
411423

412424
<!-- testing -->
413425
<org.hamcrest.version>1.3</org.hamcrest.version>
@@ -416,9 +428,9 @@
416428
<assertj.version>3.6.1</assertj.version>
417429
<avaitility.version>1.7.0</avaitility.version>
418430

431+
419432
<!-- maven plugins -->
420433
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
421434
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
422-
423435
</properties>
424436
</project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung;
2+
3+
import org.joda.time.DateTime;
4+
import org.junit.Test;
5+
6+
import java.text.ParseException;
7+
import java.text.SimpleDateFormat;
8+
import java.time.Duration;
9+
import java.time.ZonedDateTime;
10+
import java.util.Date;
11+
import java.util.Locale;
12+
import java.util.TimeZone;
13+
import java.util.concurrent.TimeUnit;
14+
15+
import static org.junit.Assert.assertEquals;
16+
17+
public class DateDiffUnitTest {
18+
19+
@Test
20+
public void givenTwoDatesBeforeJava8_whenDifferentiating_thenWeGetSix() throws ParseException {
21+
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
22+
Date firstDate = sdf.parse("06/24/2017");
23+
Date secondDate = sdf.parse("06/30/2017");
24+
25+
long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
26+
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
27+
28+
assertEquals(diff, 6);
29+
}
30+
31+
@Test
32+
public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() {
33+
ZonedDateTime now = ZonedDateTime.now();
34+
ZonedDateTime sixDaysBehind = now.minusDays(6);
35+
36+
Duration duration = Duration.between(now, sixDaysBehind);
37+
long diff = Math.abs(duration.toDays());
38+
39+
assertEquals(diff, 6);
40+
}
41+
42+
@Test
43+
public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix() {
44+
DateTime now = DateTime.now();
45+
DateTime sixDaysBehind = now.minusDays(6);
46+
47+
org.joda.time.Duration duration = new org.joda.time.Duration(now, sixDaysBehind);
48+
long diff = Math.abs(duration.getStandardDays());
49+
50+
assertEquals(diff, 6);
51+
}
52+
53+
@Test
54+
public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() {
55+
hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault());
56+
hirondelle.date4j.DateTime sixDaysBehind = now.minusDays(6);
57+
58+
long diff = Math.abs(now.numDaysFrom(sixDaysBehind));
59+
60+
assertEquals(diff, 6);
61+
}
62+
63+
}

‎java-difference-date/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Relevant articles:
2+
3+
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
4+
- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro)
5+
- [Migrating to the New Java 8 Date Time API](http://www.baeldung.com/migrating-to-java-8-date-time-api)

‎java-difference-date/pom.xml‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>java-difference-date</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>java-difference-date</name>
12+
<description>Difference between two dates in java</description>
13+
14+
<parent>
15+
<groupId>com.baeldung</groupId>
16+
<artifactId>parent-modules</artifactId>
17+
<version>1.0.0-SNAPSHOT</version>
18+
</parent>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>joda-time</groupId>
23+
<artifactId>joda-time</artifactId>
24+
<version>${joda-time.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.darwinsys</groupId>
28+
<artifactId>hirondelle-date4j</artifactId>
29+
<version>${hirondelle-date4j.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>junit</groupId>
33+
<artifactId>junit</artifactId>
34+
<version>${junit.version}</version>
35+
<scope>test</scope>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-compiler-plugin</artifactId>
44+
<configuration>
45+
<target>1.8</target>
46+
<target>1.8</target>
47+
</configuration>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
52+
<properties>
53+
<java.version>1.8</java.version>
54+
<junit.version>4.12</junit.version>
55+
<joda-time.version>2.9.9</joda-time.version>
56+
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
57+
</properties>
58+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung;
2+
3+
import org.joda.time.DateTime;
4+
import org.junit.Test;
5+
6+
import java.text.ParseException;
7+
import java.text.SimpleDateFormat;
8+
import java.time.Duration;
9+
import java.time.ZonedDateTime;
10+
import java.util.Date;
11+
import java.util.Locale;
12+
import java.util.TimeZone;
13+
import java.util.concurrent.TimeUnit;
14+
15+
import static org.junit.Assert.assertEquals;
16+
17+
public class DateDiffTest {
18+
@Test
19+
public void givenTwoDatesBeforeJava8_whenDifferentiating_thenWeGetSix() throws ParseException {
20+
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
21+
Date firstDate = sdf.parse("06/24/2017");
22+
Date secondDate = sdf.parse("06/30/2017");
23+
24+
long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
25+
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
26+
27+
assertEquals(diff, 6);
28+
}
29+
30+
@Test
31+
public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() {
32+
ZonedDateTime now = ZonedDateTime.now();
33+
ZonedDateTime sixDaysBehind = now.minusDays(6);
34+
35+
Duration duration = Duration.between(now, sixDaysBehind);
36+
long diff = Math.abs(duration.toDays());
37+
38+
assertEquals(diff, 6);
39+
}
40+
41+
@Test
42+
public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix() {
43+
DateTime now = DateTime.now();
44+
DateTime sixDaysBehind = now.minusDays(6);
45+
46+
org.joda.time.Duration duration = new org.joda.time.Duration(now, sixDaysBehind);
47+
long diff = Math.abs(duration.getStandardDays());
48+
49+
assertEquals(diff, 6);
50+
}
51+
52+
@Test
53+
public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() {
54+
hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault());
55+
hirondelle.date4j.DateTime sixDaysBehind = now.minusDays(6);
56+
57+
long diff = Math.abs(now.numDaysFrom(sixDaysBehind));
58+
59+
assertEquals(diff, 6);
60+
}
61+
}

0 commit comments

Comments
 (0)