110

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
  • 54,930
  • 75
  • 197
  • 265
Gnaniyar Zubair
  • 7,816
  • 22
  • 58
  • 71

11 Answers11

284

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
  • 18,596
  • 10
  • 58
  • 70
142

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
  • 513,304
  • 74
  • 731
  • 1,106
48

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
  • 78,683
  • 26
  • 106
  • 112
  • 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
15

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
  • 2,833
  • 1
  • 18
  • 28
  • 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
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
  • 262,936
  • 84
  • 758
  • 1,028
10

Another option

min.getTime() <= d.getTime() && d.getTime() <= max.getTime()
David
  • 1,231
  • 1
  • 9
  • 9
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
  • 5,894
  • 12
  • 40
  • 55
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
  • 8,974
  • 5
  • 66
  • 76
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
  • 74,514
  • 30
  • 136
  • 188
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
  • 85,564
  • 27
  • 164
  • 223
user54579
  • 4,428
  • 4
  • 22
  • 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
  • 41,736
  • 17
  • 78
  • 110