Java Date Equal areEqual(Date date1, Date date2)

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

Description

Compares if two dates are equal up to second resolution.

License

Open Source License

Parameter

Parameter Description
date1 a parameter
date2 a parameter

Return

true if both dates are equal to the second or both dates are null.

Declaration

public static boolean areEqual(Date date1, Date date2) 

Method Source Code

//package com.java2s;
/*/*from w  ww.java2  s  .  co m*/
 * $Id: DateUtils.java,v 1.4 2005/10/10 18:02:45 rbair Exp $
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

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

public class Main {
    /**
     * Compares if two dates are equal up to second resolution. Milliseconds are
     * not considered as these can be stripped if the date value is serialized
     * through a web service, etc. If one of the dates is null, false is
     * returned. If both dates are null, true is returned.
     *
     * @param date1
     * @param date2
     * @return true if both dates are equal to the second or both dates are
     * null.
     */
    public static boolean areEqual(Date date1, Date date2) {
        boolean result = false;
        if (date1 == null && date2 == null) {
            // Both dates are null
            result = true;
        } else if (date1 == null || date2 == null) {
            // One of the dates is not null
            result = false;
        } else {
            // Consider dates up to second resolution. 
            Calendar cal1 = Calendar.getInstance();
            Calendar cal2 = Calendar.getInstance();
            cal1.setTime(date1);
            cal2.setTime(date2);
            result = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                    && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)
                    && cal1.get(Calendar.HOUR_OF_DAY) == cal2.get(Calendar.HOUR_OF_DAY)
                    && cal1.get(Calendar.MINUTE) == cal2.get(Calendar.MINUTE)
                    && cal1.get(Calendar.SECOND) == cal2.get(Calendar.SECOND);
        }
        return result;

    }
}

Related

  1. dateEquals(Date dtFirst, Date dtSecond)
  2. datesEqualToSecond(Date date1, Date date2)
  3. equal(Date date, Date other)
  4. equalDateByDay(Date date1, Date date2)