This question already has an answer here:
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?
This method worked for me:
Update for Java 8 and later
isAfter()
isBefore()
isEqual()
compareTo()
These methods exists in
LocalDate
,LocalTime
, andLocalDateTime
classes.Those classes are built into Java 8 and later. Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
You can use
Date.getTime()
which:This means you can compare them just like numbers:
Try this
Use getTime() to get the numeric value of the date, and then compare using the returned values.