Java Date Equal isEqual(Date date1, Date date2)

Here you can find the source of isEqual(Date date1, Date date2)

Description

Checks if date1 is equal date2.

License

Open Source License

Parameter

Parameter Description
date1 The first given date
date2 The second given date

Return

true if date1 is equal date2; false otherwise.

Declaration

public static boolean isEqual(Date date1, Date date2) 

Method Source Code

//package com.java2s;
/*/* w ww . ja va2s.  c o  m*/
 * Utility class for a easy way to handle Date
 * Copyright (C) 2012 Martin Absmeier, IT Consulting Services
 *
 *  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/>.
 */

import java.util.Calendar;
import java.util.Date;

import java.util.Locale;

public class Main {
    /**
     * Checks if date1 is equal date2. It is tested without time slice.
     * 
     * @param date1
     *            The first given date
     * @param date2
     *            The second given date
     * @return <code>true</code> if date1 is equal date2; <code>false</code>
     *         otherwise.
     */
    public static boolean isEqual(Date date1, Date date2) {
        int[] dIng1 = getDateIngredients(date1);
        int[] dIng2 = getDateIngredients(date2);
        if (dIng1[0] == dIng2[0] && dIng1[1] == dIng2[1] && dIng1[2] == dIng2[2]) {
            return true;
        }
        return false;
    }

    /**
     * Provides an array day, month and year.<br>
     * <code>
     * int[] ingredients = getDateIngredients(date);<br>
     * int day = res[0];<br>
     * int month = res[1];<br>
     * int year = res[2];<br>
     * <code>
     * 
     * @param date
     *            The given date.
     * @return The ingredients of date
     */
    public static int[] getDateIngredients(Date date) {
        int[] ingredients = new int[3];

        Calendar cal = createCalendar(date);
        ingredients[0] = cal.get(Calendar.DATE);
        ingredients[1] = cal.get(Calendar.MONTH);
        ingredients[2] = cal.get(Calendar.YEAR);

        return ingredients;
    }

    /**
     * Creates a calendar from given date.
     * 
     * @param date
     *            the given date
     * @return the created <code>Calendar</code>.
     */
    public static Calendar createCalendar(Date date) {
        Calendar cal = Calendar.getInstance(Locale.GERMANY);
        cal.setTimeInMillis(date.getTime());

        return cal;
    }
}

Related

  1. equalsIgnoreTime(final Date d1, final Date d2)
  2. equalsToDate(Date date1, Date date2, boolean checkTime)
  3. equalsWoTime(final Date date1, final Date date2)
  4. firstLessOrEqualSecond(Date firstDate, Date secondDate)
  5. hourEqual(Date date1, Date date2)
  6. isEqualByDay(Date date1, Date date2)
  7. isEqualDate(Date date1, Date date2)
  8. isEqualField(int field, Date d1, Date d2)
  9. isLowerEqualDate(Date date)