Java Calendar Day isSameDay(Calendar day1, Calendar day2)

Here you can find the source of isSameDay(Calendar day1, Calendar day2)

Description

Return true if both calenday date's are of the same day

License

Apache License

Parameter

Parameter Description
day1 a parameter
day2 a parameter

Declaration

public static boolean isSameDay(Calendar day1, Calendar day2) 

Method Source Code


//package com.java2s;
/*/*from   w w  w. j  a  v  a  2  s . co  m*/
 * Copyright 2007-2011 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 {
    /**
     * Return true if both calenday date's are of the same day
     * @param day1
     * @param day2
     * @return
     */
    public static boolean isSameDay(Calendar day1, Calendar day2) {
        return datePart(day1).compareTo(datePart(day2)) == 0;
    }

    /**
     * Return a calendars day-part only
     * @param date
     * @return
     */
    public static Calendar datePart(Calendar date) {
        return merge(date, null);
    }

    /**
     * Return a date's day part only
     * @param date
     * @return
     */
    public static Date datePart(Date date) {
        return merge(date, null);
    }

    /**
     * Merge two calendars, one with the day date component and the other
     * with a time component. The resutling calendar is of the date's
     * locale and timezone. If the time argument is null the method returns
     * only the date's date with 00:00:00 time. 
     * If the date is null current date is used
     * 
     * @param date
     * @param time
     * @return
     */
    public static Calendar merge(Calendar date, Calendar time) {
        Calendar resCal = Calendar.getInstance();
        if (date == null && time == null)
            return resCal;
        if (date != null)
            resCal = (Calendar) date.clone();
        resCal.clear();
        if (null == time) {
            resCal.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH));
            return resCal;
        }
        resCal.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH),
                time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE), time.get(Calendar.SECOND));
        return resCal;
    }

    /**
     * Merge a date's date part with another dates time part
     * @param date
     * @param time
     * @return
     * @deprecated
     */
    public static Date merge(Date date, Date time) {
        Calendar dateCal = Calendar.getInstance();
        dateCal.setTime(date);
        if (time == null)
            return merge(dateCal, null).getTime();
        Calendar timeCal = Calendar.getInstance();
        timeCal.setTime(time);
        return merge(dateCal, timeCal).getTime();
    }
}

Related

  1. isSameDay(Calendar cal1, Calendar cal2)
  2. isSameDay(Calendar cal1, Calendar cal2)
  3. isSameDay(Calendar cal1, Calendar cal2)
  4. isSameDay(Calendar cal1, Calendar cal2)
  5. isSameDay(Calendar cal1, Calendar cal2)
  6. isSameDay(Calendar dayOne, Calendar dayTwo)
  7. isSameDay(Calendar today, Date now)
  8. isSameDay(final Calendar cal1, final Calendar cal2)
  9. isSameDay(final Calendar date1, final Calendar date2)