Java Date Between betweenInDay(Date date, Date start, Date end)

Here you can find the source of betweenInDay(Date date, Date start, Date end)

Description

check id a date in between two other date, but date in range.

License

LGPL

Parameter

Parameter Description
date a parameter
start a parameter
end a parameter

Declaration

public static boolean betweenInDay(Date date, Date start, Date end) 

Method Source Code

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

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

public class Main {
    /**/*from ww w  . ja  v a2 s .  c  om*/
     * check id a date in between two other date, but date in range.
     * 
     * @param date
     * @param start
     * @param end
     * @return
     */
    public static boolean betweenInDay(Date date, Date start, Date end) {
        return isAfterOrEqualForDay(date, start) && isBeforeOrEqualForDay(date, end);
    }

    /**
     * check if date is after other date, if the day is the same it is ok.
     */
    public static boolean isAfterOrEqualForDay(Date date, Date ref) {
        if (ref == null) {
            return true;
        }
        Calendar calDate = Calendar.getInstance();
        calDate.setTime(date);
        Calendar calRef = Calendar.getInstance();
        calRef.setTime(ref);
        if (calDate.get(Calendar.YEAR) < calRef.get(Calendar.YEAR)) {
            return false;
        } else if (calDate.get(Calendar.YEAR) == calRef.get(Calendar.YEAR)) {
            if (calDate.get(Calendar.DAY_OF_YEAR) < calRef.get(Calendar.DAY_OF_YEAR)) {
                return false;
            }
        }
        return true;
    }

    /**
     * check if date is after other date, if the day is the same it is ok.
     */
    public static boolean isBeforeOrEqualForDay(Date date, Date ref) {
        if (ref == null) {
            return true;
        }
        Calendar calDate = Calendar.getInstance();
        calDate.setTime(date);
        Calendar calRef = Calendar.getInstance();
        calRef.setTime(ref);
        if (calDate.get(Calendar.YEAR) > calRef.get(Calendar.YEAR)) {
            return false;
        } else if (calDate.get(Calendar.YEAR) == calRef.get(Calendar.YEAR)) {
            if (calDate.get(Calendar.DAY_OF_YEAR) > calRef.get(Calendar.DAY_OF_YEAR)) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. between(Date kezdet, Date veg, Date datum)
  2. between(Date since, Date until, Date dateIn, Date dateOut)
  3. between(Date smdate, Date bdate)
  4. betweenDate(String from, String to)
  5. betweenHour(Date date, int hour)
  6. betweenStartDateAndEndDate(Date startDate, Date endDate)
  7. betweenStrict(Date kezdet, Date datum, Date veg)
  8. betweenTwoDates(Date date1, Date date2)
  9. daysBetween(Date d1)