Merge pull request #7660 from drazen-nikolic/feature/BAEL-3140-comparing-dates

BAEL-3140: Comparing Dates in Java - examples.
This commit is contained in:
Erik Pragt 2019-09-18 09:10:23 +10:00 committed by GitHub
commit 76b69c3b00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 272 additions and 1 deletions

View File

@ -21,6 +21,12 @@
<version>${joda-time.version}</version> <version>${joda-time.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-validator/commons-validator --> <!-- https://mvnrepository.com/artifact/commons-validator/commons-validator -->
<dependency> <dependency>
<groupId>commons-validator</groupId> <groupId>commons-validator</groupId>
@ -63,6 +69,7 @@
<!-- testing --> <!-- testing -->
<assertj.version>3.6.1</assertj.version> <assertj.version>3.6.1</assertj.version>
<joda-time.version>2.10</joda-time.version> <joda-time.version>2.10</joda-time.version>
<commons-lang3.version>3.9</commons-lang3.version>
<commons-validator.version>1.6</commons-validator.version> <commons-validator.version>1.6</commons-validator.version>
<maven.compiler.source>1.9</maven.compiler.source> <maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target> <maven.compiler.target>1.9</maven.compiler.target>

View File

@ -0,0 +1,35 @@
package com.baeldung.date.comparison;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import static java.time.temporal.ChronoUnit.*;
public class DateTimeComparisonUtils {
public static boolean isSameDay(LocalDateTime timestamp, LocalDate localDateToCompare) {
return timestamp.toLocalDate().isEqual(localDateToCompare);
}
public static boolean isSameDay(LocalDateTime timestamp, LocalDateTime timestampToCompare) {
return timestamp.truncatedTo(DAYS).isEqual(timestampToCompare.truncatedTo(DAYS));
}
public static boolean isSameHour(LocalDateTime timestamp, LocalDateTime timestampToCompare) {
return timestamp.truncatedTo(HOURS).isEqual(timestampToCompare.truncatedTo(HOURS));
}
public static boolean isSameMinute(LocalDateTime timestamp, LocalDateTime timestampToCompare) {
return timestamp.truncatedTo(MINUTES).isEqual(timestampToCompare.truncatedTo(MINUTES));
}
public static boolean isSameHour(ZonedDateTime zonedTimestamp, ZonedDateTime zonedTimestampToCompare) {
return zonedTimestamp.truncatedTo(HOURS).isEqual(zonedTimestampToCompare.truncatedTo(HOURS));
}
public static boolean isSameHour(ZonedDateTime zonedDateTime, LocalDateTime localDateTime, ZoneId zoneId) {
return isSameHour(zonedDateTime, localDateTime.atZone(zoneId));
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.date.comparison;
import org.apache.commons.lang3.time.DateUtils;
import java.util.Calendar;
import java.util.Date;
public class LegacyDateComparisonUtils {
public static boolean isSameDay(Date date, Date dateToCompare) {
return DateUtils.isSameDay(date, dateToCompare);
}
public static boolean isSameHour(Date date, Date dateToCompare) {
return DateUtils.truncatedEquals(date, dateToCompare, Calendar.HOUR);
}
}

View File

@ -0,0 +1,75 @@
package com.baeldung.date.comparison;
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
class DateTimeComparisonUtilsUnitTest {
@Test
void givenLocalDateTimes_whenIsSameDay_thenCompareTrue() {
LocalDateTime firstTimestamp = LocalDateTime.of(2019, 8, 10, 11, 00, 0);
LocalDateTime secondTimestamp = firstTimestamp.plusHours(5);
LocalDateTime thirdTimestamp = firstTimestamp.plusDays(1);
assertThat(DateTimeComparisonUtils.isSameDay(firstTimestamp, secondTimestamp), is(true));
assertThat(DateTimeComparisonUtils.isSameDay(secondTimestamp, thirdTimestamp), is(false));
}
@Test
void givenLocalDateAndLocalDateTime_whenIsSameDay_thenCompareTrue() {
LocalDate localDate = LocalDate.of(2019, 8, 10);
LocalDateTime localDateTime = LocalDateTime.of(2019, 8, 10, 11, 30, 0);
assertThat(DateTimeComparisonUtils.isSameDay(localDateTime, localDate), is(true));
}
@Test
void givenLocalDateTimes_whenIsSameHour_thenCompareTrue() {
LocalDateTime firstTimestamp = LocalDateTime.of(2019, 8, 10, 8, 00, 0);
LocalDateTime secondTimestamp = firstTimestamp.plusMinutes(15);
LocalDateTime thirdTimestamp = firstTimestamp.plusHours(2);
assertThat(DateTimeComparisonUtils.isSameHour(firstTimestamp, secondTimestamp), is(true));
assertThat(DateTimeComparisonUtils.isSameHour(secondTimestamp, thirdTimestamp), is(false));
}
@Test
void givenLocalDateTimes_whenIsSameMinute_thenCompareTrue() {
LocalDateTime firstTimestamp = LocalDateTime.of(2019, 8, 10, 8, 15, 0);
LocalDateTime secondTimestamp = firstTimestamp.plusSeconds(30);
LocalDateTime thirdTimestamp = firstTimestamp.plusMinutes(5);
assertThat(DateTimeComparisonUtils.isSameMinute(firstTimestamp, secondTimestamp), is(true));
assertThat(DateTimeComparisonUtils.isSameMinute(secondTimestamp, thirdTimestamp), is(false));
}
@Test
void givenZonedDateTimes_whenIsSameHour_thenCompareTrue() {
ZonedDateTime zonedTimestamp = ZonedDateTime.of(2019, 8, 10, 8, 0, 0, 30,
ZoneId.of("America/New_York"));
ZonedDateTime zonedTimestampToCompare = ZonedDateTime.of(2019, 8, 10, 14, 0, 0, 0,
ZoneId.of("Europe/Berlin"));
assertThat(DateTimeComparisonUtils.isSameHour(zonedTimestamp, zonedTimestampToCompare), is(true));
}
@Test
void givenZonedDateTimeAndLocalDateTime_whenIsSameHour_thenCompareTrue() {
ZonedDateTime zonedTimestamp = ZonedDateTime.of(2019, 8, 10, 8, 15, 0, 0,
ZoneId.of("America/New_York"));
LocalDateTime localTimestamp = LocalDateTime.of(2019, 8, 10, 14, 20, 0);
ZoneId zoneId = ZoneId.of("Europe/Berlin");
assertThat(DateTimeComparisonUtils.isSameHour(zonedTimestamp, localTimestamp, zoneId), is(true));
}
}

View File

@ -0,0 +1,83 @@
package com.baeldung.date.comparison;
import org.junit.Test;
import java.time.*;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class Java8DateTimeApiGeneralComparisonsUnitTest {
@Test
public void givenLocalDates_whenComparing_thenAssertsPass() {
LocalDate firstDate = LocalDate.of(2019, 8, 10);
LocalDate secondDate = LocalDate.of(2019, 7, 1);
LocalDate thirdDate = LocalDate.of(2019, 7, 1); // same date as secondDate
assertThat(firstDate.isAfter(secondDate), is(true));
assertThat(firstDate.isBefore(secondDate), is(false));
assertThat(firstDate.isEqual(secondDate), is(false));
assertThat(firstDate.equals(secondDate), is(false));
assertThat(firstDate.compareTo(secondDate), is(1));
assertThat(secondDate.compareTo(firstDate), is(-1));
assertThat(secondDate.isAfter(thirdDate), is(false));
assertThat(secondDate.isBefore(thirdDate), is(false));
assertThat(secondDate.isEqual(thirdDate), is(true));
assertThat(secondDate.equals(thirdDate), is(true));
assertThat(secondDate.compareTo(thirdDate), is(0));
}
@Test
public void givenLocalDateTimes_whenComparing_thenAssertsPass() {
LocalDateTime firstTimestamp = LocalDateTime.of(2019, 8, 10, 11, 30, 0);
LocalDateTime secondTimestamp = LocalDateTime.of(2019, 8, 10, 11, 15, 0);
LocalDateTime thirdTimestamp = LocalDateTime.of(2019, 8, 10, 11, 15, 0); // same as secondTimestamp
assertThat(firstTimestamp.isAfter(secondTimestamp), is(true));
assertThat(firstTimestamp.isBefore(secondTimestamp), is(false));
assertThat(firstTimestamp.isEqual(secondTimestamp), is(false));
assertThat(firstTimestamp.equals(secondTimestamp), is(false));
assertThat(firstTimestamp.compareTo(secondTimestamp), is(1));
assertThat(secondTimestamp.compareTo(firstTimestamp), is(-1));
assertThat(secondTimestamp.isAfter(thirdTimestamp), is(false));
assertThat(secondTimestamp.isBefore(thirdTimestamp), is(false));
assertThat(secondTimestamp.isEqual(thirdTimestamp), is(true));
assertThat(secondTimestamp.compareTo(thirdTimestamp), is(0));
}
@Test
public void givenZonedDateTimes_whenComparing_thenAssertsPass() {
ZonedDateTime timeInNewYork = ZonedDateTime.of(2019, 8, 10, 8, 0, 0, 0,
ZoneId.of("America/New_York"));
ZonedDateTime timeInBerlin = ZonedDateTime.of(2019, 8, 10, 14, 0, 0, 0,
ZoneId.of("Europe/Berlin"));
assertThat(timeInNewYork.isAfter(timeInBerlin), is(false));
assertThat(timeInNewYork.isBefore(timeInBerlin), is(false));
assertThat(timeInNewYork.isEqual(timeInBerlin), is(true));
assertThat(timeInNewYork.equals(timeInBerlin), is(false));
assertThat(timeInNewYork.compareTo(timeInBerlin), is(-1));
}
@Test
public void givenLocalTimes_whenComparing_thenAssertsPass() {
LocalTime firstTime = LocalTime.of(8, 30);
LocalTime secondTime = LocalTime.of(9, 45);
assertThat(firstTime.isAfter(secondTime), is(false));
assertThat(firstTime.isBefore(secondTime), is(true));
assertThat(firstTime.equals(secondTime), is(false));
assertThat(firstTime.compareTo(secondTime), is(-1));
}
}

View File

@ -0,0 +1,54 @@
package com.baeldung.date.comparison;
import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
class LegacyDateComparisonUtilsUnitTest {
private Date toDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
@Test
void givenDatesWithDifferentHours_whenIsSameDay_thenReturnsTrue() {
Date firstDate = toDate(LocalDateTime.of(2019, 8, 10, 11, 00, 00));
Date secondDate = toDate(LocalDateTime.of(2019, 8, 10, 12, 00, 00));
Date thirdDate = toDate(LocalDateTime.of(2019, 8, 15, 12, 00, 00));
assertThat(LegacyDateComparisonUtils.isSameDay(firstDate, secondDate), is(true));
assertThat(LegacyDateComparisonUtils.isSameDay(secondDate, thirdDate), is(false));
}
@Test
void givenDatesWithingSameHour_whenIsSameHour_thenReturnsTrue() {
Date firstDate = toDate(LocalDateTime.of(2019, 8, 10, 11, 00, 00));
Date secondDate = toDate(LocalDateTime.of(2019, 8, 10, 11, 15, 00));
Date thirdDate = toDate(LocalDateTime.of(2019, 8, 10, 12, 00, 00));
assertThat(LegacyDateComparisonUtils.isSameHour(firstDate, secondDate), is(true));
assertThat(LegacyDateComparisonUtils.isSameHour(secondDate, thirdDate), is(false));
}
@Test
void givenDates__whenComparing_thenAssertsPass() {
Date firstDate = toDate(LocalDateTime.of(2019, 8, 10, 0, 00, 00));
Date secondDate = toDate(LocalDateTime.of(2019, 8, 15, 0, 00, 00));
Date thirdDate = toDate(LocalDateTime.of(2019, 8, 15, 0, 00, 00)); // same date as secondDate
assertThat(firstDate.after(secondDate), is(false));
assertThat(firstDate.before(secondDate), is(true));
assertThat(firstDate.compareTo(secondDate), is(-1));
assertThat(firstDate.equals(secondDate), is(false));
assertThat(thirdDate.after(secondDate), is(false));
assertThat(thirdDate.before(secondDate), is(false));
assertThat(thirdDate.compareTo(secondDate), is(0));
assertThat(thirdDate.equals(secondDate), is(true));
}
}