BAEL-3224 - Checking if 2 Java Dates are in the same day

This commit is contained in:
Al Niyas 2019-11-13 23:02:04 +03:00
parent ac75af110b
commit 7034761130
3 changed files with 141 additions and 12 deletions

View File

@ -14,4 +14,25 @@
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<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>
</dependencies>
<properties>
<joda-time.version>2.10</joda-time.version>
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
</properties>
</project>

View File

@ -0,0 +1,55 @@
package com.baeldung.date.comparison;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.apache.commons.lang3.time.DateUtils;
import hirondelle.date4j.DateTime;
public class DateComparisonUtils {
public static boolean isSameDayUsingLocalDate(Date date1, Date date2) {
LocalDate localDate1 = date1.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
LocalDate localDate2 = date2.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
return localDate1.isEqual(localDate2);
}
public static boolean isSameDayUsingSimpleDateFormat(Date date1, Date date2) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
return fmt.format(date1)
.equals(fmt.format(date2));
}
public static boolean isSameDayUsingCalendar(Date date1, Date date2) {
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date2);
return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH) && calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH);
}
public static boolean isSameDayUsingApacheCommons(Date date1, Date date2) {
return DateUtils.isSameDay(date1, date2);
}
public static boolean isSameDayUsingJoda(Date date1, Date date2) {
org.joda.time.LocalDate localDate1 = new org.joda.time.LocalDate(date1);
org.joda.time.LocalDate localDate2 = new org.joda.time.LocalDate(date2);
return localDate1.equals(localDate2);
}
public static boolean isSameDayUsingDate4j(Date date1, Date date2) {
DateTime dateObject1 = DateTime.forInstant(date1.getTime(), TimeZone.getDefault());
DateTime dateObject2 = DateTime.forInstant(date2.getTime(), TimeZone.getDefault());
return dateObject1.isSameDayAs(dateObject2);
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.date.comparison;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import org.junit.jupiter.api.Test;
public class DateComparisonUtilsUnitTest {
private Date day1Morning = toDate(LocalDateTime.of(2019, 10, 19, 6, 30, 40));
private Date day1Evening = toDate(LocalDateTime.of(2019, 10, 19, 18, 30, 50));
private Date day2Morning = toDate(LocalDateTime.of(2019, 10, 20, 6, 30, 50));
private Date toDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault())
.toInstant());
}
@Test
void givenDatesWithDifferentTime_whenIsSameDay_thenReturnsTrue() {
assertTrue(DateComparisonUtils.isSameDayUsingLocalDate(day1Morning, day1Evening));
assertTrue(DateComparisonUtils.isSameDayUsingSimpleDateFormat(day1Morning, day1Evening));
assertTrue(DateComparisonUtils.isSameDayUsingCalendar(day1Morning, day1Evening));
assertTrue(DateComparisonUtils.isSameDayUsingApacheCommons(day1Morning, day1Evening));
assertTrue(DateComparisonUtils.isSameDayUsingJoda(day1Morning, day1Evening));
assertTrue(DateComparisonUtils.isSameDayUsingDate4j(day1Morning, day1Evening));
}
@Test
void givenDates_whenIsDifferentDay_thenReturnsFalse() {
assertFalse(DateComparisonUtils.isSameDayUsingLocalDate(day1Morning, day2Morning));
assertFalse(DateComparisonUtils.isSameDayUsingLocalDate(day1Evening, day2Morning));
assertFalse(DateComparisonUtils.isSameDayUsingSimpleDateFormat(day1Morning, day2Morning));
assertFalse(DateComparisonUtils.isSameDayUsingSimpleDateFormat(day1Evening, day2Morning));
assertFalse(DateComparisonUtils.isSameDayUsingCalendar(day1Morning, day2Morning));
assertFalse(DateComparisonUtils.isSameDayUsingCalendar(day1Evening, day2Morning));
assertFalse(DateComparisonUtils.isSameDayUsingApacheCommons(day1Morning, day2Morning));
assertFalse(DateComparisonUtils.isSameDayUsingApacheCommons(day1Evening, day2Morning));
assertFalse(DateComparisonUtils.isSameDayUsingJoda(day1Morning, day2Morning));
assertFalse(DateComparisonUtils.isSameDayUsingJoda(day1Evening, day2Morning));
assertFalse(DateComparisonUtils.isSameDayUsingDate4j(day1Morning, day2Morning));
assertFalse(DateComparisonUtils.isSameDayUsingDate4j(day1Evening, day2Morning));
}
}