--- /dev/null
+/*
+ * Copyright (C) 2022 Roland Häder<roland@mxchange.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcoreutils.dates;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Objects;
+import org.mxchange.jcoreutils.comparable.ComparableUtils;
+
+/**
+ * A utility class for comparing java.util.Date instances
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class DateUtils {
+
+ /**
+ * Compares both date instances for differences in year, month or day (of
+ * month)
+ * <p>
+ * @param date1 Left date instance
+ * @param date2 Right date instance
+ * <p>
+ * @return Comparison value
+ */
+ public static int compareYearMonthDay (final Date date1, final Date date2) {
+ // Is anything null or the same?
+ if (Objects.equals(date1, date2)) {
+ // Both are the same
+ return 0;
+ } else if (null == date1) {
+ // First one is null
+ return -1;
+ } else if (null == date2) {
+ // Second one is null
+ return 1;
+ }
+
+ // Init both calendar instances
+ final Calendar calendar1 = Calendar.getInstance();
+ calendar1.setTime(date1);
+ final Calendar calendar2 = Calendar.getInstance();
+ calendar1.setTime(date2);
+
+ // Compare year, month and day only
+ final int comparators[] = {
+ // Compare years
+ Integer.compare(calendar1.get(Calendar.YEAR), calendar2.get(Calendar.YEAR)),
+ // Compare months
+ Integer.compare(calendar1.get(Calendar.MONTH), calendar2.get(Calendar.MONTH)),
+ // Compare days
+ Integer.compare(calendar1.get(Calendar.DAY_OF_MONTH), calendar2.get(Calendar.DAY_OF_MONTH))
+ };
+
+ // Check all
+ return ComparableUtils.checkAll(comparators);
+ }
+
+ /**
+ * No instances from utility classes
+ */
+ private DateUtils () {
+ }
+}
--- /dev/null
+/*
+ * Copyright (C) 2022 Roland Häder<roland@mxchange.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcoreutils.test.dates;
+
+import java.util.Calendar;
+import java.util.Date;
+import org.mxchange.jcoreutils.dates.DateUtils;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ * Test cases for DateUtils utilities class
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class DateUtilsTest {
+
+ /**
+ * Default constructor
+ */
+ public DateUtilsTest () {
+ }
+
+ @Test (description = "Compares two different java.util.Date instances")
+ public void testCompareDifferentDates () {
+ // Init date instance
+ final Date date1 = new Date();
+
+ // Get calendar and add a month
+ final Calendar calendar = Calendar.getInstance();
+ calendar.setTime(new Date());
+ calendar.add(Calendar.MONTH, 1);
+
+ // Should always be 1
+ Assert.assertEquals(DateUtils.compareYearMonthDay(date1, calendar.getTime()), 1);
+ }
+
+ @Test (description = "Compares left null with a java.util.Date instance")
+ public void testCompareLeftNullDate () {
+ // Init instance
+ final Date date = new Date();
+
+ // Should always be zero
+ Assert.assertEquals(DateUtils.compareYearMonthDay(null, date), -1);
+ }
+
+ @Test (description = "Compares right null with a java.util.Date instance")
+ public void testCompareRightNullDate () {
+ // Init instance
+ final Date date = new Date();
+
+ // Should always be 1
+ Assert.assertEquals(DateUtils.compareYearMonthDay(date, null), 1);
+ }
+
+ @Test (description = "Compares the same java.util.Date instances")
+ public void testCompareSameInstances () {
+ // Init instance
+ final Date date = new Date();
+
+ // Should always be -1
+ Assert.assertEquals(DateUtils.compareYearMonthDay(date, date), 0);
+ }
+
+}