ant ant - 1 year ago 68
Java Question

How to compare dates in Java?

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?

Answer Source

Date has before and after methods and can be compared to each other.

You could also give Joda-Time a go.

How do I verify if today's date is in between date1 and date 3?

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