117

How can I check if a date is between two other dates, in the case where all three dates are represented by instances of java.util.Date?

Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
Gnaniyar Zubair
  • 8,114
  • 23
  • 61
  • 72

11 Answers11

289

This might be a bit more readable:

Date min, max;   // assume these are set to something
Date d;          // the date in question

return d.after(min) && d.before(max);
Nathan Feger
  • 19,122
  • 11
  • 62
  • 71
  • 3
    A very healthy 9 seconds faster, sir. – Tetsujin no Oni May 19 '09 at 14:23
  • 78
    between can include the endpoints, in which case you could use return !d.before(min) && !d.after(max); – Patrick McDonald May 19 '09 at 14:28
  • 1
    @Tetsujin no Oni sorry man, its a good thing I don't have a big deliverable due in a couple weeks that is both helped and hurt by SO. :) – Nathan Feger May 19 '09 at 14:35
  • @PatrickMcDonald you can write this other way too: !(d.before(min) || d.after(max)) – krossovochkin Dec 23 '14 at 07:41
  • @NathanFeger, one more point I want to the `after()` and `before()` both are exclude type which means `after()` doesn't give the result for of `d` day, I have same scenario where I have to include `min` and `max` date in report by `if((date.after(fromDate) || date.compareTo(fromDate)==0) && (date.before(toDate) || date.compareTo(toDate)==0 ))` it doesn't give any result. Please suggest good approach – MegaBytes Jan 22 '15 at 20:05
  • 3
    Is this better than the "selected solved answer"? – Christian Moen Jan 21 '17 at 23:01
  • @ChristianMoen yes. However I'm not sure if java 8 changes this, I'm a little rusty now. – Nathan Feger Jan 23 '17 at 20:24
150

If you don't know the order of the min/max values

Date a, b;   // assume these are set to something
Date d;      // the date in question

return a.compareTo(d) * d.compareTo(b) > 0;

If you want the range to be inclusive

return a.compareTo(d) * d.compareTo(b) >= 0;

You can treat null as unconstrained with

if (a == null) {
    return b == null || d.compareTo(b) < 0;
} else if (b == null) {
    return a.compareTo(d) < 0;
} else {
    return a.compareTo(d) * d.compareTo(b) > 0;
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
50

Like so:

Date min, max;   // assume these are set to something
Date d;          // the date in question

return d.compareTo(min) >= 0 && d.compareTo(max) <= 0;

You can use > instead of >= and < instead of <= to exclude the endpoints from the sense of "between."

Jason Cohen
  • 81,399
  • 26
  • 107
  • 114
  • Thanks for your immediate Response. – Gnaniyar Zubair May 19 '09 at 14:23
  • 1
    This doesn't always work. I have had problems using compareTo on Date objects. Jan 1 2006 was apparently after Apr 1 2006 in one of my programs. – KitsuneYMG May 19 '09 at 14:28
  • @KitsuneYMG: I tried the following code and is working as expected: `Date date1 = new Date(2006, 1, 1); // Jan 1,2006 Date date2 = new Date(2006, 4, 1); // Apr 1, 2006 System.out.println(date1.compareTo(date2)); // -1 System.out.println(date2.compareTo(date1)); // 1`. Can you share here what is that *not working* for you? – Gnanam Mar 02 '11 at 06:26
  • 1
    This is the **exact** Answer for `between`. Others are for `within` the date. – Mr. Mak Feb 19 '17 at 11:30
17

Between dates Including end points can be written as

public static boolean isDateInBetweenIncludingEndPoints(final Date min, final Date max, final Date date){
    return !(date.before(min) || date.after(max));
}

NB: as @Ponmudi pointed out in the comments, this solution may not work if the dates are different at miilliseconds level.

manoj
  • 3,391
  • 2
  • 20
  • 30
  • 1
    Note that after() and before() may not work as expected when date matches. https://stackoverflow.com/questions/52246234/why-does-javas-date-after-return-true-when-the-date-is-actually-earlier – Ponmudi VN Sep 02 '21 at 04:55
  • how can I use this inside a java8 filter – Coding world Jun 27 '22 at 06:51
12

Here's a couple ways to do this using the Joda-Time 2.3 library.

One way is to use the simple isBefore and isAfter methods on DateTime instances. By the way, DateTime in Joda-Time is similar in concept to a java.util.Date (a moment in time on the timeline of the Universe) but includes a time zone.

Another way is to build an Interval in Joda-Time. The contains method tests if a given DateTime occurs within the span of time covered by the Interval. The beginning of the Interval is inclusive, but the endpoint is exclusive. This approach is known as "Half-Open", symbolically [).

See both ways in the following code example.

Convert the java.util.Date instances to Joda-Time DateTime instances. Simply pass the Date instance to constructor of DateTime. In practice you should also pass a specific DateTimeZone object rather than rely on JVM’s default time zone.

DateTime dateTime1 = new DateTime( new java.util.Date() ).minusWeeks( 1 );
DateTime dateTime2 = new DateTime( new java.util.Date() );
DateTime dateTime3 = new DateTime( new java.util.Date() ).plusWeeks( 1 );

Compare by testing for before/after…

boolean is1After2 = dateTime1.isAfter( dateTime2 );
boolean is2Before3 = dateTime2.isBefore( dateTime3 );

boolean is2Between1And3 = ( ( dateTime2.isAfter( dateTime1 ) ) && ( dateTime2.isBefore( dateTime3 ) ) );

Using the Interval approach instead of isAfter/isBefore…

Interval interval = new Interval( dateTime1, dateTime3 );
boolean intervalContainsDateTime2 = interval.contains( dateTime2 );

Dump to console…

System.out.println( "DateTimes: " + dateTime1 + " " + dateTime1 + " " + dateTime1 );
System.out.println( "is1After2 " + is1After2 );
System.out.println( "is2Before3 " + is2Before3 );
System.out.println( "is2Between1And3 " + is2Between1And3 );
System.out.println( "intervalContainsDateTime2 " + intervalContainsDateTime2 );

When run…

DateTimes: 2014-01-22T20:26:14.955-08:00 2014-01-22T20:26:14.955-08:00 2014-01-22T20:26:14.955-08:00
is1After2 false
is2Before3 true
is2Between1And3 true
intervalContainsDateTime2 true
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
10

Another option

min.getTime() <= d.getTime() && d.getTime() <= max.getTime()
David
  • 1,261
  • 1
  • 9
  • 10
4

Here you go:

public static void main(String[] args) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

        String oeStartDateStr = "04/01/";
        String oeEndDateStr = "11/14/";

        Calendar cal = Calendar.getInstance();
        Integer year = cal.get(Calendar.YEAR);

        oeStartDateStr = oeStartDateStr.concat(year.toString());
        oeEndDateStr = oeEndDateStr.concat(year.toString());

        Date startDate = sdf.parse(oeStartDateStr);
        Date endDate = sdf.parse(oeEndDateStr);
        Date d = new Date();
        String currDt = sdf.format(d);


        if((d.after(startDate) && (d.before(endDate))) || (currDt.equals(sdf.format(startDate)) ||currDt.equals(sdf.format(endDate)))){
            System.out.println("Date is between 1st april to 14th nov...");
        }
        else{
            System.out.println("Date is not between 1st april to 14th nov...");
        }
    }
Akshay Lokur
  • 6,680
  • 13
  • 43
  • 62
2

Here's how to find whether today is between 2 months:

private boolean isTodayBetween(int from, int to) {
    if (from < 0 || to < 0 || from > Calendar.DECEMBER || to > Calendar.DECEMBER) {
        throw new IllegalArgumentException("Invalid month provided: from = " + from + " to = " + to);
    }
    Date now = new Date();
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(now);
    int thisMonth = cal.get(Calendar.MONTH);
    if (from > to) {
        to = to + Calendar.DECEMBER;
        thisMonth = thisMonth + Calendar.DECEMBER;
    }
    if (thisMonth >= from && thisMonth <= to) {
        return true;
    }
    return false;
}

and call it like:

isTodayBetween(Calendar.OCTOBER, Calendar.MARCH)
ACV
  • 9,964
  • 5
  • 76
  • 81
2
import java.util.Date;

public class IsDateBetween {

public static void main (String[] args) {

          IsDateBetween idb=new IsDateBetween("12/05/2010"); // passing your Date
 }
 public IsDateBetween(String dd) {

       long  from=Date.parse("01/01/2000");  // From some date

       long to=Date.parse("12/12/2010");     // To Some Date

       long check=Date.parse(dd);

       int x=0;

      if((check-from)>0 && (to-check)>0)
      {
             x=1;
      }

 System.out.println ("From Date is greater Than  ToDate : "+x);
}   

}
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
2

you can use getTime() and compare the returned long UTC values.

EDIT if you are sure you'll not have to deal with dates before 1970, not sure how it will behave in that case.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
user54579
  • 4,588
  • 4
  • 23
  • 19
2

You might want to take a look at Joda Time which is a really good API for dealing with date/time. Even though if you don't really need it for the solution to your current question it is bound to save you pain in the future.

willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111