您应该为此提议定义自定义注释。像下面这样:
@Target(ElementType.FIELD)
@Retention(RUNTIME)
@Constraint(validatedBy = DateValidator.class)
@Documented
public @interface IsAfter{
String message() default "{message.key}";
String current();
}
并像这样定义一个自定义验证器:
public class DateValidator implements ConstraintValidator<IsAfter, LocalDate> {
String validDate;
@Override
public void initialize(IsAfter constraintAnnotation) {
validDate = constraintAnnotation.current();
}
@Override
public boolean isValid(LocalDate date, ConstraintValidatorContext constraintValidatorContext) {
String[] splitDate = validDate.split("-");
return date.isAfter(LocalDate.of(Integer.valueOf(splitDate[0]), Integer.valueOf(splitDate[1]), Integer.valueOf(splitDate[2])));
}
}
并使用此注释:
@Past(message="date of birth must be less than today")
@IsAfter(current = "1900-01-01")
@DateTimeFormate( pattern="yyyy-MM-dd")
private Date dateOfBirth;
注意:我没有测试过这个注释!