]> git.mxchange.org Git - jcore-utils.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Fri, 4 Nov 2022 17:06:57 +0000 (18:06 +0100)
committerRoland Häder <roland@mxchange.org>
Sat, 5 Nov 2022 02:00:31 +0000 (03:00 +0100)
- added DateUtils class for comparing Date objects with each other
- currently only year, month and day (of month) are compared
- added unit test for above class

src/org/mxchange/jcoreutils/dates/DateUtils.java [new file with mode: 0644]
test/org/mxchange/jcoreutils/test/dates/DateUtilsTest.java [new file with mode: 0644]

diff --git a/src/org/mxchange/jcoreutils/dates/DateUtils.java b/src/org/mxchange/jcoreutils/dates/DateUtils.java
new file mode 100644 (file)
index 0000000..1b2d3a6
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * 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 () {
+       }
+}
diff --git a/test/org/mxchange/jcoreutils/test/dates/DateUtilsTest.java b/test/org/mxchange/jcoreutils/test/dates/DateUtilsTest.java
new file mode 100644 (file)
index 0000000..35a27de
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * 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);
+       }
+
+}