Java Date Compare by Day isSameDay(Date a, Date b)

Here you can find the source of isSameDay(Date a, Date b)

Description

is Same Day

License

Open Source License

Declaration

public static boolean isSameDay(Date a, Date b) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w w  w.ja  va  2 s .  c o m*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

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

public class Main {
    public static boolean isSameDay(Date a, Date b) {
        a = truncDate(a);
        b = truncDate(b);
        return equals(a, b);
    }

    /**
     * truncate the date to a day with time 00:00:00.000
     */
    public static Date truncDate(Date d) {
        if (d == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        truncCalendar(c);
        return new Date(c.getTime().getTime());
    }

    public static boolean equals(Date a, Date b) {
        return a == b || (a != null && b != null && a.compareTo(b) == 0);
    }

    /**
     * truncate the calendar to a day with time 00:00:00.000
     */
    public static void truncCalendar(Calendar c) {
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
    }
}

Related

  1. isSameDay(Date a, Date b)
  2. isSameDay(Date d1, Date d2)
  3. isSameDay(Date d1, Date d2)
  4. isSameDay(Date d1, Date d2)