gst*_*low 4 java validation date spring-mvc hibernate-validator
我知道存在注释@Future.
如果我使用此注释注释字段
@Future
private Date date;
Run Code Online (Sandbox Code Playgroud)
日期必须在将来意味着在当前时刻之后.
现在我需要在当前时刻之后至少24小时验证该日期.
我该怎么做?
bee*_*jay 12
AfterTomorrow.java:
@Target({ FIELD, METHOD, PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = AfterTomorrowValidator.class)
@Documented
public @interface AfterTomorrow {
String message() default "{AfterTomorrow.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Run Code Online (Sandbox Code Playgroud)
AfterTomorrowValidator.java:
public class AfterTomorrowValidator
implements ConstraintValidator<AfterTomorrow, Date> {
public final void initialize(final AfterTomorrow annotation) {}
public final boolean isValid(final Date value,
final ConstraintValidatorContext context) {
Calendar c = Calendar.getInstance();
c.setTime(value);
c.add(Calendar.DATE, 1);
return value.after(c.getTime());
}
}
Run Code Online (Sandbox Code Playgroud)
此外,您可以添加默认AfterTomorrow.message邮件ValidationMessages.properties
最后,注释你的领域:
@AfterTomorrow
private Date date;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3914 次 |
| 最近记录: |