Java Calendar Day isTheSameDay(Calendar dayOne, Calendar dayTwo)

Here you can find the source of isTheSameDay(Calendar dayOne, Calendar dayTwo)

Description

Check if the calendars are referring to the same day

License

Apache License

Parameter

Parameter Description
dayOne the day one
dayTwo the day two

Return

true if the days are equal

Declaration

public static boolean isTheSameDay(Calendar dayOne, Calendar dayTwo) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Main {
    /**/*  w  ww.  ja va 2 s.  c  o m*/
     * Check if the calendars are referring to the same day
     * 
     * @param dayOne
     *            the day one
     * @param dayTwo
     *            the day two
     * @return true if the days are equal
     */
    public static boolean isTheSameDay(Calendar dayOne, Calendar dayTwo) {
        boolean result = false;
        if (dayOne == null || dayTwo == null) {
            result = false;
        } else {
            result = (dayTwo.get(Calendar.DAY_OF_YEAR) == dayOne.get(Calendar.DAY_OF_YEAR))
                    && (dayTwo.get(Calendar.YEAR) == dayOne.get(Calendar.YEAR));
        }
        return result;
    }

    /**
     * Check if the dates are referring to the same day
     * 
     * @param dayOne
     *            the day one
     * @param dayTwo
     *            the day two
     * @return true if the days are equal
     */
    public static boolean isTheSameDay(Date dayOne, Date dayTwo) {
        return isTheSameDay(dayOne, dayTwo, null);
    }

    /**
     * Check if the dates are referring to the same day and add set the timezone
     * 
     * @param dayOne
     *            the day one with respect to the timezone
     * @param dayTwo
     *            the day two with respect to the timezone
     * @param userTimeZone
     *            for timezone specific comparison; can be null
     * @return true if the days are equal
     */

    public static boolean isTheSameDay(Date dayOne, Date dayTwo, TimeZone userTimeZone) {

        Calendar one = new GregorianCalendar();
        Calendar two = new GregorianCalendar();

        one.setTime(dayOne);
        two.setTime(dayTwo);
        if (userTimeZone != null) {
            one.setTimeZone(userTimeZone);
            two.setTimeZone(userTimeZone);
        }
        return isTheSameDay(one, two);
    }
}

Related

  1. isSameDay(Calendar dayOne, Calendar dayTwo)
  2. isSameDay(Calendar today, Date now)
  3. isSameDay(final Calendar cal1, final Calendar cal2)
  4. isSameDay(final Calendar date1, final Calendar date2)
  5. isSunday(GregorianCalendar date)
  6. isToday(Calendar creationDate)
  7. isToday(Calendar date)
  8. isToday(Calendar now, Calendar then)
  9. isTodayOff(String days, Calendar calendar)