未来至少24小时的休眠验证器注释 [英] Annotation for hibernate validator for a date at least 24 hours in the future
本文介绍了未来至少24小时的休眠验证器注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道存在@Future注释。
I know that exist annotation @Future.
如果我使用此注释注释字段
If I annotate field with this annotation
@Future
private Date date;
日期必须在当前时刻之后的将来。
date must be in future means after current moment.
现在,我需要验证该日期是否在当前时刻之后至少24小时。
我该怎么做?
Now I need to validate that date was at least 24 hours after current moment.
How can I make it?
推荐答案
AfterTomorrow.java:
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 {};
}
AfterTomorrowValidator.java:
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());
}
}
此外,您还可以添加默认的<$ c $在 ValidationMessages.properties
Additionally, you can add the default AfterTomorrow.message
message in ValidationMessages.properties
最后,对字段进行注释:
Finally, annotate your field:
@AfterTomorrow
private Date date;
这篇关于未来至少24小时的休眠验证器注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文