I have a class with three pairs of Date
fields.
public static final class Filter {
private final Date startDateFrom;
private final Date startDateTo;
private final Date endDateFrom;
private final Date endDateTo;
private final Date regDateFrom;
private final Date regDateTo;
public Filter(Date startDateFrom, Date startDateTo,
Date endDateFrom, Date endDateTo,
Date regDateFrom, Date regDateTo) {
this.startDateFrom = startDateFrom;
this.startDateTo = startDateTo;
this.endDateFrom = endDateFrom;
this.endDateTo = endDateTo;
this.regDateFrom = regDateFrom;
this.regDateTo = regDateTo;
}
}
So I need to check that at least one pair has both not null
Dates and check that DateFrom
is before or equals DateTo
for all not-null dates.
I've made this to check if there is at least one pair:
public boolean allDatesMissing() {
if (startDateFrom != null && startDateTo != null) {
return false;
} else if (endDateFrom != null && endDateTo != null) {
return false;
} else if (regDateFrom != null && regDateTo != null) {
return false;
}
return true;
}
And this to check fromDate
is before toDate
:
public void checkDates(Date from, Date to) throws RequestException{
if (from != null) {
if (to != null) {
if (from.after(to)) {
throw new MyException(INCORRECT_DATES_PERIOD);
}
} else {
throw new MyException(MISSING_PARAMETER);
}
} else if (to != null){
throw new MyException(MISSING_PARAMETER);
}
}
Is there any simplier way to do the same?