package com.qianwen.smartman.common.validator.date;
|
|
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.util.StrUtil;
|
import javax.validation.ConstraintValidator;
|
import javax.validation.ConstraintValidatorContext;
|
|
public class DateValueValidator implements ConstraintValidator<DateValue, String> {
|
private Boolean required;
|
private String format;
|
|
public void initialize(DateValue constraintAnnotation) {
|
this.required = Boolean.valueOf(constraintAnnotation.required());
|
this.format = constraintAnnotation.format();
|
}
|
|
public boolean isValid(String dateValue, ConstraintValidatorContext context) {
|
if (StrUtil.isEmpty(dateValue)) {
|
if (this.required.booleanValue()) {
|
return false;
|
}
|
return true;
|
}
|
try {
|
DateUtil.parse(dateValue, this.format);
|
return true;
|
} catch (Exception e) {
|
return false;
|
}
|
}
|
}
|