0 votes
1 view
in Java by (3.4k points)
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?

1 Answer

0 votes
by (46.1k points)

The date must before and after methods and can be compared to each other as mentioned

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 provide Joda-Time ago, but remark that:

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

Back-ports are prepared for Java 6 and 7 as great as Android

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...