Code to reproduce:
public void futureBirthDate_failsValidation() {
Dog dog = new Dog();
dog.timeOfBirth = OffsetDateTime.of(LocalDate.MAX, LocalTime.MAX, ZoneOffset.UTC);
Validation.buildDefaultValidatorFactory().getValidator().validate(dog);
}
private static class Dog {
@Past
private OffsetDateTime timeOfBirth;
}
Issues found
- If date is too far in the past or in the future an error happens: ArithmeticException: long overflow
- Date comparison looses precision.
Explanation In PastValidatorForOffsetDateTime there is a code that converts Java8 Instant into long:
return value.toInstant().toEpochMilli() < now;
But in Java8 the date-related types are larger than long. So if too far in the past or in the future dates are used the overflow happens. Also, in Java8 time is more precise than milliseconds - it keeps track of nano-seconds as well. So current validator looses this precision. So current validator works ideologically incorrect with Java8 Time API. Instead of casting to long methods like before() and after() should be used. |