Simple clean of difference between dates

This commit is contained in:
Dassi Orleando
2017-08-25 09:46:35 +01:00
parent 348ff5d403
commit a663b3ccde
6 changed files with 92 additions and 44 deletions

View File

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

View File

@@ -5,55 +5,56 @@ 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 DateDiffTest {
@Test
public void givenTwoDatesBeforeJava8SeparatedBySixDays_whenCallingDiffOnThem_thenWeGetSix() throws ParseException {
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");
DateDiff dateDiff = new DateDiff();
long diff = dateDiff.beforeJava8diff(firstDate, secondDate);
long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
assertEquals(diff, 6);
}
@Test
public void givenTheCurrentDateAndSixDaysBehindInJava8_whenCallingDiffOnThem_thenWeGetSix() {
public void givenTwoDatesInJava8_whenDifferentiating_thenWeGetSix() {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime sixDaysBehind = now.minusDays(6);
DateDiff dateDiff = new DateDiff();
long diff = dateDiff.fromJava8Diff(now, sixDaysBehind);
Duration duration = Duration.between(now, sixDaysBehind);
long diff = Math.abs(duration.toDays());
assertEquals(diff, 6);
}
@Test
public void givenTheCurrentDateAndSixDaysBehindInJodaTime_whenCallingDiffOnThem_thenWeGetSix() {
public void givenTwoDatesInJodaTime_whenDifferentiating_thenWeGetSix() {
DateTime now = DateTime.now();
DateTime sixDaysBehind = now.minusDays(6);
DateDiff dateDiff = new DateDiff();
long diff = dateDiff.fromJodaTime(now, sixDaysBehind);
org.joda.time.Duration duration = new org.joda.time.Duration(now, sixDaysBehind);
long diff = Math.abs(duration.getStandardDays());
assertEquals(diff, 6);
}
@Test
public void givenTheCurrentDateAndSixDaysBehindInDate4j_whenCallingDiffOnThem_thenWeGetSix() {
public void givenTwoDatesInDate4j_whenDifferentiating_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);
long diff = Math.abs(now.numDaysFrom(sixDaysBehind));
assertEquals(diff, 6);
}