BAEL-1074 Separate difference on date and datetime (#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
This commit is contained in:
Dassi orleando 2017-08-28 05:55:59 +01:00 committed by maibin
parent e1e6550a49
commit c5d77f424d
1 changed files with 29 additions and 10 deletions

View File

@ -1,12 +1,12 @@
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.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
@ -30,8 +30,8 @@ public class DateDiffUnitTest {
@Test
public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime sixDaysBehind = now.minusDays(6);
LocalDate now = LocalDate.now();
LocalDate sixDaysBehind = now.minusDays(6);
Duration duration = Duration.between(now, sixDaysBehind);
long diff = Math.abs(duration.toDays());
@ -40,16 +40,36 @@ public class DateDiffUnitTest {
}
@Test
public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix() {
DateTime now = DateTime.now();
DateTime sixDaysBehind = now.minusDays(6);
public void givenTwoDateTimesInJava8_whenDifferentiating_thenWeGetSix() {
LocalDateTime now = LocalDateTime.now();
LocalDateTime sixMinutesBehind = now.minusMinutes(6);
org.joda.time.Duration duration = new org.joda.time.Duration(now, sixDaysBehind);
long diff = Math.abs(duration.getStandardDays());
Duration duration = Duration.between(now, sixMinutesBehind);
long diff = Math.abs(duration.toMinutes());
assertEquals(diff, 6);
}
@Test
public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix() {
org.joda.time.LocalDate now = org.joda.time.LocalDate.now();
org.joda.time.LocalDate sixDaysBehind = now.minusDays(6);
org.joda.time.Period period = new org.joda.time.Period(now, sixDaysBehind);
long diff = Math.abs(period.getDays());
assertEquals(diff, 6);
}
@Test
public void givenTwoDateTimesInJodaTime_whenDifferentiating_thenWeGetSix() {
org.joda.time.LocalDateTime now = org.joda.time.LocalDateTime.now();
org.joda.time.LocalDateTime sixMinutesBehind = now.minusMinutes(6);
org.joda.time.Period period = new org.joda.time.Period(now, sixMinutesBehind);
long diff = Math.abs(period.getDays());
}
@Test
public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() {
hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault());
@ -59,5 +79,4 @@ public class DateDiffUnitTest {
assertEquals(diff, 6);
}
}