Skip to content

Commit c5d77f4

Browse files
Dassi orleandomaibin
authored andcommitted
BAEL-1074 Separate difference on date and datetime (eugenp#2511)
* 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 * Improve dates diff: for dates and datetimes
1 parent e1e6550 commit c5d77f4

1 file changed

Lines changed: 28 additions & 9 deletions

File tree

‎core-java/src/test/java/com/baeldung/DateDiffUnitTest.java‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.baeldung;
22

3-
import org.joda.time.DateTime;
43
import org.junit.Test;
54

65
import java.text.ParseException;
76
import java.text.SimpleDateFormat;
87
import java.time.Duration;
9-
import java.time.ZonedDateTime;
8+
import java.time.LocalDate;
9+
import java.time.LocalDateTime;
1010
import java.util.Date;
1111
import java.util.Locale;
1212
import java.util.TimeZone;
@@ -30,26 +30,46 @@ public void givenTwoDatesBeforeJava8_whenDifferentiating_thenWeGetSix() throws P
3030

3131
@Test
3232
public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() {
33-
ZonedDateTime now = ZonedDateTime.now();
34-
ZonedDateTime sixDaysBehind = now.minusDays(6);
33+
LocalDate now = LocalDate.now();
34+
LocalDate sixDaysBehind = now.minusDays(6);
3535

3636
Duration duration = Duration.between(now, sixDaysBehind);
3737
long diff = Math.abs(duration.toDays());
3838

3939
assertEquals(diff, 6);
4040
}
4141

42+
@Test
43+
public void givenTwoDateTimesInJava8_whenDifferentiating_thenWeGetSix() {
44+
LocalDateTime now = LocalDateTime.now();
45+
LocalDateTime sixMinutesBehind = now.minusMinutes(6);
46+
47+
Duration duration = Duration.between(now, sixMinutesBehind);
48+
long diff = Math.abs(duration.toMinutes());
49+
50+
assertEquals(diff, 6);
51+
}
52+
4253
@Test
4354
public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix() {
44-
DateTime now = DateTime.now();
45-
DateTime sixDaysBehind = now.minusDays(6);
55+
org.joda.time.LocalDate now = org.joda.time.LocalDate.now();
56+
org.joda.time.LocalDate sixDaysBehind = now.minusDays(6);
4657

47-
org.joda.time.Duration duration = new org.joda.time.Duration(now, sixDaysBehind);
48-
long diff = Math.abs(duration.getStandardDays());
58+
org.joda.time.Period period = new org.joda.time.Period(now, sixDaysBehind);
59+
long diff = Math.abs(period.getDays());
4960

5061
assertEquals(diff, 6);
5162
}
5263

64+
@Test
65+
public void givenTwoDateTimesInJodaTime_whenDifferentiating_thenWeGetSix() {
66+
org.joda.time.LocalDateTime now = org.joda.time.LocalDateTime.now();
67+
org.joda.time.LocalDateTime sixMinutesBehind = now.minusMinutes(6);
68+
69+
org.joda.time.Period period = new org.joda.time.Period(now, sixMinutesBehind);
70+
long diff = Math.abs(period.getDays());
71+
}
72+
5373
@Test
5474
public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() {
5575
hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault());
@@ -59,5 +79,4 @@ public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() {
5979

6080
assertEquals(diff, 6);
6181
}
62-
6382
}

0 commit comments

Comments
 (0)