How can I determine if a date is between two dates in Java?
JavaDateJava Problem Overview
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
?
Java Solutions
Solution 1 - Java
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);
Solution 2 - Java
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;
}
Solution 3 - Java
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."
Solution 4 - Java
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.
Solution 5 - Java
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
Solution 6 - Java
Another option
min.getTime() <= d.getTime() && d.getTime() <= max.getTime()
Solution 7 - Java
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...");
}
}
Solution 8 - Java
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.
Solution 9 - Java
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.
Solution 10 - Java
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);
}
}
Solution 11 - Java
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)