Java Date Equal dateEquals(Date dtFirst, Date dtSecond)

Here you can find the source of dateEquals(Date dtFirst, Date dtSecond)

Description

Check if two dates equals regardless of the time.

License

Open Source License

Parameter

Parameter Description
dtFirst - first date to compare, can be null
dtSecond - second date to compare, can be null

Return

boolean - true if two dates equals regardless of what the time is

Declaration

public static boolean dateEquals(Date dtFirst, Date dtSecond) 

Method Source Code

//package com.java2s;
/*/*from w ww .ja  v a  2 s .  com*/
 * Copyright (C) 2003 - 2013 OpenSubsystems.com/net/org and its owners. All rights reserved.
 * 
 * This file is part of OpenSubsystems.
 *
 * OpenSubsystems is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

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

public class Main {
    /**
     * Check if two dates equals regardless of the time. Two null dates are equal. 
     * Null and not null dates are not equal.
     * 
     * @param  dtFirst - first date to compare, can be null
     * @param  dtSecond - second date to compare, can be null 
     * @return boolean - true if two dates equals regardless of what the time is 
     */
    public static boolean dateEquals(Date dtFirst, Date dtSecond) {
        boolean bReturn;

        // If they are the same object, they are equals
        bReturn = (dtFirst == dtSecond);
        if (!bReturn) {
            if (dtFirst == null) {
                // Two null dates are the same
                bReturn = (dtSecond == null);
            } else {
                if (dtSecond != null) {
                    Calendar compCalendar;
                    int iEra;
                    int iYear;
                    int iMonth;
                    int iDay;

                    compCalendar = Calendar.getInstance();
                    compCalendar.setTime(dtFirst);
                    iEra = compCalendar.get(Calendar.ERA);
                    iYear = compCalendar.get(Calendar.YEAR);
                    iMonth = compCalendar.get(Calendar.MONTH);
                    iDay = compCalendar.get(Calendar.DATE);
                    compCalendar.setTime(dtSecond);

                    bReturn = ((iEra == compCalendar.get(Calendar.ERA))
                            && (iYear == compCalendar.get(Calendar.YEAR))
                            && (iMonth == compCalendar.get(Calendar.MONTH))
                            && (iDay == compCalendar.get(Calendar.DATE)));
                }
            }
        }

        return bReturn;
    }
}

Related

  1. areEqual(Date date1, Date date2)
  2. datesEqualToSecond(Date date1, Date date2)
  3. equal(Date date, Date other)
  4. equalDateByDay(Date date1, Date date2)
  5. equalDates(Date first, Date second)