
17

JSR-303 Bean Validation - Date String Validation
source link: http://www.blogjava.net/paulwong/archive/2021/02/25/435810.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

JSR-303 Bean Validation - Date String Validation
@Past @Future只针对Date类型的验证,如果是String类型的验证,则不适用。其实可以新加一个方法返回Date类型,再配合@Future@Past 进行验证。
@Future(message = "Invalid CN_ID_INFO.EXPIRE_DATE.")
private LocalDate getValidExpireDate() {
try {
return LocalDate.parse(this.dateString, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
} catch (Exception e) {
return null;
}
}
此方法对dateString进行解释,返回LocalDate,如果dateString为空或格式错误,则返回空,再配合@Future 进行是否未来日期的验证。private LocalDate getValidExpireDate() {
try {
return LocalDate.parse(this.dateString, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
} catch (Exception e) {
return null;
}
}
posted on 2021-02-25 09:44 paulwong 阅读(80) 评论(0) 编辑 收藏 所属分类: J2SE 、SPRING 、SPRING BOOT 、Bean Validation
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK