diff --git a/java-dates-2/pom.xml b/java-dates-2/pom.xml
index c1419514ef..08b2076d03 100644
--- a/java-dates-2/pom.xml
+++ b/java-dates-2/pom.xml
@@ -20,7 +20,13 @@
joda-time
${joda-time.version}
-
+
+
+ org.apache.commons
+ commons-lang3
+ ${commons-lang3.version}
+
+
commons-validator
@@ -63,6 +69,7 @@
3.6.1
2.10
+ 3.9
1.6
1.9
1.9
diff --git a/java-dates-2/src/main/java/com/baeldung/date/comparison/DateTimeComparisonUtils.java b/java-dates-2/src/main/java/com/baeldung/date/comparison/DateTimeComparisonUtils.java
new file mode 100644
index 0000000000..4d732df795
--- /dev/null
+++ b/java-dates-2/src/main/java/com/baeldung/date/comparison/DateTimeComparisonUtils.java
@@ -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));
+ }
+}
diff --git a/java-dates-2/src/main/java/com/baeldung/date/comparison/LegacyDateComparisonUtils.java b/java-dates-2/src/main/java/com/baeldung/date/comparison/LegacyDateComparisonUtils.java
new file mode 100644
index 0000000000..9519bc316a
--- /dev/null
+++ b/java-dates-2/src/main/java/com/baeldung/date/comparison/LegacyDateComparisonUtils.java
@@ -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);
+ }
+}
diff --git a/java-dates-2/src/test/java/com/baeldung/date/comparison/DateTimeComparisonUtilsUnitTest.java b/java-dates-2/src/test/java/com/baeldung/date/comparison/DateTimeComparisonUtilsUnitTest.java
new file mode 100644
index 0000000000..0db0d1e8d7
--- /dev/null
+++ b/java-dates-2/src/test/java/com/baeldung/date/comparison/DateTimeComparisonUtilsUnitTest.java
@@ -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));
+ }
+}
\ No newline at end of file
diff --git a/java-dates-2/src/test/java/com/baeldung/date/comparison/Java8DateTimeApiGeneralComparisonsUnitTest.java b/java-dates-2/src/test/java/com/baeldung/date/comparison/Java8DateTimeApiGeneralComparisonsUnitTest.java
new file mode 100644
index 0000000000..ff51476e7c
--- /dev/null
+++ b/java-dates-2/src/test/java/com/baeldung/date/comparison/Java8DateTimeApiGeneralComparisonsUnitTest.java
@@ -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));
+ }
+}
\ No newline at end of file
diff --git a/java-dates-2/src/test/java/com/baeldung/date/comparison/LegacyDateComparisonUtilsUnitTest.java b/java-dates-2/src/test/java/com/baeldung/date/comparison/LegacyDateComparisonUtilsUnitTest.java
new file mode 100644
index 0000000000..a501cb8888
--- /dev/null
+++ b/java-dates-2/src/test/java/com/baeldung/date/comparison/LegacyDateComparisonUtilsUnitTest.java
@@ -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));
+ }
+}
\ No newline at end of file