Reputation: 165
I am trying to check if a particular date falls within a certain date range. I came across Joda Time interval in Java. But it works as end time exclusive.
So is there an alternative which functions as end time inclusive
Upvotes: 5
Views: 1885
Reputation: 79580
I recommend you use modern Date-Time API*.
Quoted below is a notice at the home page of Joda-Time:
Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.
You can use !date.isAfter
where date
is a reference to LocalDate
e.g.
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate start = LocalDate.of(2021, 5, 10);
LocalDate end = LocalDate.of(2021, 6, 10);
for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) {
// ...
}
}
}
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Upvotes: 8