How to compare dates in Java?

comparison, date, java

Solution

Date has before and after methods and can be compared to each other as follows:

if(todayDate.after(historyDate) && todayDate.before(futureDate)) {
    // In between
}

For an inclusive comparison:

if(!historyDate.after(todayDate) && !futureDate.before(todayDate)) {
    /* historyDate <= todayDate <= futureDate */ 
}

You could also give Joda-Time a go, but note that:

Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java.time (JSR-310).

Back-ports are available for Java 6 and 7 as well as Android.

Problem

How do I compare dates in between in Java? Example: date1 is `22-02-2010` date2 is `07-04-2010` today date3 is `25-12-2010` `date3` is always greater than `date1` and `date2` is always today. How do I verify if today's date is in between date1 and date 3?

Original source

Related problems