Java Day End isInRange(Date myDate, Date dateStart, Date dateEnd)

Here you can find the source of isInRange(Date myDate, Date dateStart, Date dateEnd)

Description

Is the date given in date range

License

Open Source License

Parameter

Parameter Description
myDate Date to compare
dateStart Range Start Date
dateEnd Range End Date

Declaration

public static boolean isInRange(Date myDate, Date dateStart, Date dateEnd) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*  w  w  w  . j  ava 2 s.  c o  m*/
     * Is the date given in date range
     * @param myDate Date to compare
     * @param dateStart Range Start Date
     * @param dateEnd Range End Date
     * @return
     */
    public static boolean isInRange(Date myDate, Date dateStart, Date dateEnd) {
        return isInSameDayOrAfter(myDate, dateStart) && isInSameDayOrBefore(myDate, dateEnd);
    }

    public static boolean isInSameDayOrAfter(Date d1, Date d2) {
        if (d1 == null || d2 == null)
            return false;

        if (isInSameDay(d1, d2) || d1.after(d2))
            return true;
        return false;
    }

    public static boolean isInSameDayOrBefore(Date d1, Date d2) {
        if (d1 == null || d2 == null)
            return false;

        if (isInSameDay(d1, d2) || d1.before(d2))
            return true;
        return false;
    }

    /** Returns true if the Dates are in the same day. Ignores time.
     * @param d1 Day one compared to
     * @param d2 Day two
     * @return
     */
    public static boolean isInSameDay(Date d1, Date d2) {
        if (d1 == null || d2 == null) {
            return false;
        }

        GregorianCalendar cal1 = new GregorianCalendar();
        cal1.setTime(d1);
        GregorianCalendar cal2 = new GregorianCalendar();
        cal2.setTime(d2);

        return (cal1.get(Calendar.DATE) == cal2.get(Calendar.DATE)
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
                && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR));
    }
}

Related

  1. isEndOfMonth(Date date)
  2. isEndOfMonth(Date nowday)
  3. isEndOfSeason(Date date)
  4. isEqualYMD(Date begindate, Date enddate)
  5. isIncludeDay(Date start, Date end, Date target)
  6. isInWorkDay(Calendar start)
  7. isMoreThenMonth(Date startDate, Date enDate)
  8. isNowBetween(Date start, Date end)
  9. isSameDay(Calendar cal1, Calendar cal2)