Java Date Between isBetween(Date check, Date from, Date to)

Here you can find the source of isBetween(Date check, Date from, Date to)

Description

Used to check if a date is within a given range

License

Apache License

Parameter

Parameter Description
check is the date to check
from is the beginning range of the Date/Time
to is the ending range of the Date/Time

Declaration

public static boolean isBetween(Date check, Date from, Date to) 

Method Source Code

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

import java.util.*;

public class Main {
    private static boolean bypassTodayCheck;

    /**/*from w  w  w  . j  a va2  s. c o  m*/
     * Used to check if a date is within a given range
     * 
     * @param check is the date to check
     * @param from is the beginning range of the Date/Time
     * @param to is the ending range of the Date/Time
     * @return
     */
    public static boolean isBetween(Date check, Date from, Date to) {
        if (bypassTodayCheck)
            return true;

        if (null == check || null == from || null == to)
            return false;

        Calendar start = new GregorianCalendar();
        start.setTime(from);
        start.set(Calendar.HOUR, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        start.set(Calendar.MILLISECOND, 0);
        start.add(Calendar.SECOND, -1);
        Calendar end = new GregorianCalendar();
        end.setTime(to);
        end.set(Calendar.HOUR, 23);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        end.set(Calendar.MILLISECOND, 999);
        end.add(Calendar.MILLISECOND, 1);
        Calendar checkCal = new GregorianCalendar();
        checkCal.setTime(check);
        checkCal.set(Calendar.HOUR, 12);
        return start.before(checkCal) && end.after(checkCal);
    }
}

Related

  1. getBetweenDates(Date fromDate, Date toDate )
  2. getBetweenDays(String strFromDate, String strToDate)
  3. getBetweenMonths(Date date1, Date date2)
  4. getBetweenTime(Date begin, Date end, int field)
  5. getBetweenWorkDate(int amount, Date beginDate)
  6. isBetween(Date date, Date d1, Date d2)