]> git.mxchange.org Git - jcore-utils.git/blobdiff - src/org/mxchange/jcoreutils/dates/DateUtils.java
Continued:
[jcore-utils.git] / src / org / mxchange / jcoreutils / dates / DateUtils.java
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 () {
+       }
+}