Java Date Between isBetween(Date date, Date d1, Date d2)

Here you can find the source of isBetween(Date date, Date d1, Date d2)

Description

Check if date is between d1 and d2

License

Apache License

Parameter

Parameter Description
date the target date, not null
d1 lower threadhold, if null then no lower threadhold
d2 higher threadhold, if null then no higher threadhold

Exception

Parameter Description
NullPointerException if date is null

Return

true if it is between the two threadhold, false other wise

Declaration

public static boolean isBetween(Date date, Date d1, Date d2) 

Method Source Code

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

import java.util.*;

public class Main {
    /**/*  w w  w  .  j av  a  2 s . c o  m*/
     * Check if date is between d1 and d2
     * @param date the target date, not null
     * @param d1 lower threadhold, if null then no lower threadhold
     * @param d2 higher threadhold, if null then no higher threadhold
     * @return true if it is between the two threadhold, false other wise
     * @throws NullPointerException if date is null
     */
    public static boolean isBetween(Date date, Date d1, Date d2) {
        long time = date.getTime();
        long time1;
        if (d1 != null) {
            time1 = d1.getTime();
        } else {
            time1 = Long.MIN_VALUE;
        }

        long time2;
        if (d2 != null) {
            time2 = d2.getTime();
        } else {
            time2 = Long.MAX_VALUE;
        }
        return time > Math.min(time1, time2) && time < Math.max(time1, time2);
    }
}

Related

  1. getBetweenDays(String strFromDate, String strToDate)
  2. getBetweenMonths(Date date1, Date date2)
  3. getBetweenTime(Date begin, Date end, int field)
  4. getBetweenWorkDate(int amount, Date beginDate)
  5. isBetween(Date check, Date from, Date to)