Reputation: 957
My standard method for checking if a date is between two dates in Java looks like this:
public static boolean isWithinRange(Date date, Date startDate, Date endDate) {
return !(date.before(startDate) || date.after(endDate));
}
I want to add support for null values on startDate or endDate (meaning user hasn't entered a date. If startDate is null I only want to check endDate and if endDate is null I only want to check startDate and if both null then it is true. My current solution looks like this:
public static boolean isWithinRange(Date date, Date startDate, Date endDate) {
if (startDate == null && endDate == null) {
return true;
}
if (!(startDate != null || !date.after(endDate))) {
return true;
}
if (!(endDate != null || !date.before(startDate))) {
return true;
}
return !(date.before(startDate) || date.after(endDate));
}
alternative more readable example:
public static boolean isWithinRange(Date date, Date startDate, Date endDate) {
if (startDate == null && endDate == null) {
return true;
}
if (startDate == null && date.before(endDate))) {
return true;
}
if (endDate == null && date.after(startDate))) {
return true;
}
return date.after(startDate) && date.before(endDate));
}
But it feels like really bad code. Is there any other way to deal with this?
Upvotes: 5
Views: 5253
Reputation: 311228
Java 8's enhances to the Comaparator
interface provide a pretty elegant way of doing this:
private static final Comparator<Date> NULL_SAFE_BEFORE =
Comparator.nullsFirst(Comparator.naturalOrder());
private static final Comparator<Date> NULL_SAFE_AFTER =
Comparator.nullsLast(Comparator.naturalOrder());
public static boolean isWithinRange(Date date, Date startDate, Date endDate) {
return NULL_SAFE_BEFORE.compare(startDate, date) < 0 &&
NULL_SAFE_AFTER.compare(date, endDate) < 0;
}
Upvotes: 3
Reputation: 328598
How about:
return (startDate == null || !date.before(startDate))
&& (endDate == null || !date.after(endDate));
This uses the fact that these two statements are equivalent:
!(date.before(startDate) || date.after(endDate))
!date.before(startDate) && !date.after(endDate)
And the fact that ||
is a short-circuit, which prevents NullPointerExceptions.
Upvotes: 11
Reputation: 4039
This should be equivalent:
public static boolean isWithinRange(Date date, Date startDate, Date endDate) {
return !(startDate != null && date.before(startDate) || endDate != null && date.after(endDate));
}
Upvotes: 1