Java Date Compare by Day isSameDay(final Date date1, final Date date2)

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

Description

Returns true if both of the dates represent the same day.

License

Open Source License

Parameter

Parameter Description
date1 first date
date2 second date

Return

day comparison result

Declaration

public static boolean isSameDay(final Date date1, final Date date2) 

Method Source Code

//package com.java2s;
/*//from   ww  w .j  ava2s . c  o  m
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library 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.
 *
 * WebLookAndFeel 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Returns true if both of the dates represent the same day.
     *
     * @param date1 first date
     * @param date2 second date
     * @return day comparison result
     */
    public static boolean isSameDay(final Date date1, final Date date2) {
        return isSameDay(date1.getTime(), date2.getTime());
    }

    /**
     * Returns true if both of the time represent the same day.
     *
     * @param time1 first time
     * @param time2 second time
     * @return day comparison result
     */
    public static boolean isSameDay(final Long time1, final Long time2) {
        final Calendar calendar = Calendar.getInstance();

        // Saving calendar time
        final Date tmp = calendar.getTime();

        calendar.setTimeInMillis(time1);
        final boolean same = isSameDay(calendar, time2);

        // Restoring calendar time
        calendar.setTime(tmp);

        return same;
    }

    /**
     * Returns true if date contained in Calendar and specified date both represent the same day.
     *
     * @param calendar calendar
     * @param date     date
     * @return day comparison result
     */
    public static boolean isSameDay(final Calendar calendar, final Date date) {
        return isSameDay(calendar, date.getTime());
    }

    /**
     * Returns true if date contained in Calendar and specified time both represent the same day.
     *
     * @param calendar calendar
     * @param date     date
     * @return day comparison result
     */
    public static boolean isSameDay(final Calendar calendar, final Long date) {
        // Saving calendar time
        final long time = calendar.getTimeInMillis();

        final int year = calendar.get(Calendar.YEAR);
        final int month = calendar.get(Calendar.MONTH);
        final int day = calendar.get(Calendar.DAY_OF_MONTH);

        calendar.setTimeInMillis(date);
        final boolean sameDay = year == calendar.get(Calendar.YEAR) && month == calendar.get(Calendar.MONTH)
                && day == calendar.get(Calendar.DAY_OF_MONTH);

        // Restoring calendar time
        calendar.setTimeInMillis(time);

        return sameDay;
    }
}

Related

  1. isSameDay(Date date1, Date date2)
  2. isSameDay(Date date1, Date date2)
  3. isSameDay(Date date1, Date date2)
  4. isSameDay(Date day1, Date day2)
  5. isSameDay(Date lastUpgradeTime)
  6. isSameDayWithToday(Date date)
  7. sameDay(Date date1, Date date2)
  8. sameDay(Date date1, Date date2)
  9. sameDay(Date date1, Date date2)