Java Calendar Day isSameDay(Calendar today, Date now)

Here you can find the source of isSameDay(Calendar today, Date now)

Description

Returns a boolean indicating whether the given Date is the same day as the day in the calendar.

License

Open Source License

Parameter

Parameter Description
today the Calendar representing a date, must not be null.
now the date to compare to, must not be null

Return

true if the calendar and date represent the same day in the given calendar.

Declaration

public static boolean isSameDay(Calendar today, Date now) 

Method Source Code

//package com.java2s;
/*/*from  w  w w .  j  a  v  a2  s  .c o m*/
 * #%L
 * The AIBench Plugin Manager Plugin
 * %%
 * Copyright (C) 2006 - 2016 Daniel Glez-Pe?a and Florentino Fdez-Riverola
 * %%
 * This program 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 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 General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

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

public class Main {
    /**
     * Returns a boolean indicating whether the given Date is the same day as
     * the day in the calendar. Calendar and date are unchanged by the check.
     *
     * @param today the Calendar representing a date, must not be null.
     * @param now the date to compare to, must not be null
     * @return true if the calendar and date represent the same day in the
     *   given calendar.
     */
    public static boolean isSameDay(Calendar today, Date now) {
        Calendar temp = (Calendar) today.clone();
        startOfDay(temp);
        Date start = temp.getTime();
        temp.setTime(now);
        startOfDay(temp);
        return start.equals(temp.getTime());
    }

    /**
     * Adjust the given calendar to the first millisecond of the given date.
     * that is all time fields cleared. The Date of the adjusted Calendar is
     * returned. 
     * 
     * @param calendar calendar to adjust.
     * @param date the Date to use.
     * @return the start of the day of the given date
     */
    public static Date startOfDay(Calendar calendar, Date date) {
        calendar.setTime(date);
        startOfDay(calendar);
        return calendar.getTime();
    }

    /**
     * Adjust the given calendar to the first millisecond of the current day.
     * that is all time fields cleared.
     * 
     * @param calendar calendar to adjust.
     */
    public static void startOfDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
    }
}

Related

  1. isSameDay(Calendar cal1, Calendar cal2)
  2. isSameDay(Calendar cal1, Calendar cal2)
  3. isSameDay(Calendar cal1, Calendar cal2)
  4. isSameDay(Calendar day1, Calendar day2)
  5. isSameDay(Calendar dayOne, Calendar dayTwo)
  6. isSameDay(final Calendar cal1, final Calendar cal2)
  7. isSameDay(final Calendar date1, final Calendar date2)
  8. isSunday(GregorianCalendar date)
  9. isTheSameDay(Calendar dayOne, Calendar dayTwo)