h2. Code to reproduce: {code} 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; }{code}
h2. Issues found
# If date is too far in the past or in the future an error happens: {{ArithmeticException: long overflow}} # Date comparison looses precision.
h2. Explanation In {{PastValidatorForOffsetDateTime}} there is a code that converts Java8 Instant into {{long}}: {code}return value.toInstant().toEpochMilli() < now;{code} 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.
h2. Workaround
For now I'll simply implement my own custom annotation and validator, but would be great to be able to use the standard annotation. |
|