Fix illegal date issues (e.g. year out of bounds)

This commit is contained in:
Reinhard Pointner 2016-10-05 03:04:02 +08:00
parent 55d06dc87d
commit eab8c2dccc
1 changed files with 17 additions and 11 deletions

View File

@ -9,6 +9,7 @@ import java.time.chrono.ChronoLocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
import java.time.format.TextStyle; import java.time.format.TextStyle;
import java.time.temporal.ChronoField;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -197,25 +198,30 @@ public class DateMatcher {
public static class DateFilter implements Predicate<ChronoLocalDate> { public static class DateFilter implements Predicate<ChronoLocalDate> {
public final ChronoLocalDate lowerBound; public final ChronoLocalDate min;
public final ChronoLocalDate upperBound; public final ChronoLocalDate max;
public DateFilter(ChronoLocalDate lowerBound, ChronoLocalDate upperBound) { private final int minYear;
this.lowerBound = lowerBound; private final int maxYear;
this.upperBound = upperBound;
public DateFilter(ChronoLocalDate min, ChronoLocalDate max) {
this.min = min;
this.max = max;
this.minYear = min.get(ChronoField.YEAR);
this.maxYear = max.get(ChronoField.YEAR);
} }
@Override @Override
public boolean test(ChronoLocalDate date) { public boolean test(ChronoLocalDate date) {
return date.isAfter(lowerBound) && date.isBefore(upperBound); return date.isAfter(min) && date.isBefore(max);
}
public boolean acceptDate(int year, int month, int day) {
return test(LocalDate.of(year, month, day));
} }
public boolean acceptYear(int year) { public boolean acceptYear(int year) {
return test(LocalDate.of(year, 1, 1)); return minYear < year && year < maxYear;
}
public boolean acceptDate(int year, int month, int day) {
return acceptYear(year) && test(LocalDate.of(year, month, day));
} }
} }