yangys
2024-10-30 25db770e621f1259b8d5b7fd514207f7481c2d0f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;
        }
    }
}