Java Date Equal equalsIgnoreTime(final Date d1, final Date d2)

Here you can find the source of equalsIgnoreTime(final Date d1, final Date d2)

Description

Two dates are equal if they have the same date (differing time values will be ignored) or if d1 and d2 are both null.

License

Apache License

Parameter

Parameter Description
d1 First date for comparison
d2 Second date for comparison

Return

True, if equal. False, else.

Declaration

public static boolean equalsIgnoreTime(final Date d1, final Date d2) 

Method Source Code


//package com.java2s;
/*// ww w.j a v  a2  s  .c  o  m
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

public class Main {
    /**
     * <p>
     * Two dates are equal if they have the same date (differing time values will 
     * be ignored) or if <code>d1</code> and <code>d2</code> are both null.
     * </p>
     * @param d1 First date for comparison
     * @param d2 Second date for comparison
     * @return
     * True, if equal. False, else.
     */
    public static boolean equalsIgnoreTime(final Date d1, final Date d2) {
        if ((d1 == null) && (d2 == null)) {
            return true;
        }

        if (d1 != null && d2 != null) {
            Calendar c1 = Calendar.getInstance();
            c1.setTimeInMillis(d1.getTime());

            Calendar c2 = Calendar.getInstance();
            c2.setTimeInMillis(d2.getTime());

            if (c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH)
                    && c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH)
                    && c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) {
                return true;
            }
        }

        return false;
    }
}

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)
  5. equalDates(Date first, Date second)
  6. equalsToDate(Date date1, Date date2, boolean checkTime)
  7. equalsWoTime(final Date date1, final Date date2)
  8. firstLessOrEqualSecond(Date firstDate, Date secondDate)
  9. hourEqual(Date date1, Date date2)