Browse Source

【新增】新增月份校验器,时间校验器,日期或日期时间校验器,日期或月份校验器,日期或时间校验器,月份或日期时间校验器

就是那个锅 4 năm trước cách đây
mục cha
commit
0db4d6429b
12 tập tin đã thay đổi với 609 bổ sung6 xóa
  1. 57 0
      guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/dateordatetime/DateOrDateTimeValue.java
  2. 65 0
      guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/dateordatetime/DateOrDateTimeValueValidator.java
  3. 57 0
      guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/dateormonth/DateOrMonthValue.java
  4. 67 0
      guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/dateormonth/DateOrMonthValueValidator.java
  5. 2 2
      guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/dateortime/DateOrTimeValue.java
  6. 4 4
      guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/dateortime/DateOrTimeValueValidator.java
  7. 57 0
      guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/month/MonthValue.java
  8. 60 0
      guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/month/MonthValueValidator.java
  9. 57 0
      guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/mothordatetime/MonthOrDateTimeValue.java
  10. 67 0
      guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/mothordatetime/MonthOrDateTimeValueValidator.java
  11. 57 0
      guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/time/TimeValue.java
  12. 59 0
      guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/time/TimeValueValidator.java

+ 57 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/dateordatetime/DateOrDateTimeValue.java

@ -0,0 +1,57 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.validation.dateordatetime;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
 * 校验日期或时间格式 yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss
 *
 * @author xuyuxiang
 * @date 2020/5/26 14:48
 */
@Documented
@Constraint(validatedBy = DateOrDateTimeValueValidator.class)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface DateOrDateTimeValue {
    String message() default "日期或时间格式不正确,正确格式应为yyyy-MM-dd或yyyy-MM-dd HH:mm:ss";
    Class[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    @Target({ElementType.FIELD, ElementType.PARAMETER})
    @Retention(RUNTIME)
    @Documented
    @interface List {
        DateOrDateTimeValue[] value();
    }
}

+ 65 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/dateordatetime/DateOrDateTimeValueValidator.java

@ -0,0 +1,65 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.validation.dateordatetime;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
 * 校验日期或时间格式 yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss
 *
 * @author xuyuxiang
 * @date 2020/5/26 14:48
 */
public class DateOrDateTimeValueValidator implements ConstraintValidator<DateOrDateTimeValue, String> {
    @Override
    public boolean isValid(String dateValue, ConstraintValidatorContext context) {
        //为空则放过,因为在此校验之前会加入@NotNull或@NotBlank校验
        if (ObjectUtil.isEmpty(dateValue)) {
            return true;
        }
        //长度不对直接返回
        if (dateValue.length() != DatePattern.NORM_DATE_PATTERN.length() &&
                dateValue.length() != DatePattern.NORM_DATETIME_PATTERN.length()) {
            return false;
        }
        try {
            DateUtil.parseDate(dateValue);
            return true;
        } catch (Exception dateParseException) {
            try {
                DateUtil.parseDateTime(dateValue);
                return true;
            } catch (Exception dateTimeParseException) {
                return false;
            }
        }
    }
}

+ 57 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/dateormonth/DateOrMonthValue.java

@ -0,0 +1,57 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.validation.dateormonth;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
 * 校验日期格式 yyyy-MM-dd 或 yyyy-MM
 *
 * @author xuyuxiang
 * @date 2020/5/26 14:48
 */
@Documented
@Constraint(validatedBy = DateOrMonthValueValidator.class)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface DateOrMonthValue {
    String message() default "日期格式不正确,正确格式应为yyyy-MM-dd或yyyy-MM";
    Class[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    @Target({ElementType.FIELD, ElementType.PARAMETER})
    @Retention(RUNTIME)
    @Documented
    @interface List {
        DateOrMonthValue[] value();
    }
}

+ 67 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/dateormonth/DateOrMonthValueValidator.java

@ -0,0 +1,67 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.validation.dateormonth;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
 * 校验日期格式 yyyy-MM-dd 或 yyyy-MM
 *
 * @author xuyuxiang
 * @date 2020/5/26 14:48
 */
public class DateOrMonthValueValidator implements ConstraintValidator<DateOrMonthValue, String> {
    public static final String NORM_MONTH_PATTERN = "yyyy-MM";
    @Override
    public boolean isValid(String dateValue, ConstraintValidatorContext context) {
        //为空则放过,因为在此校验之前会加入@NotNull或@NotBlank校验
        if (ObjectUtil.isEmpty(dateValue)) {
            return true;
        }
        //长度不对直接返回
        if (dateValue.length() != DatePattern.NORM_DATE_PATTERN.length() &&
                dateValue.length() != NORM_MONTH_PATTERN.length()) {
            return false;
        }
        try {
            DateUtil.parseDate(dateValue);
            return true;
        } catch (Exception dateParseException) {
            try {
                DateUtil.parse(dateValue, NORM_MONTH_PATTERN);
                return true;
            } catch (Exception monthParseException) {
                return false;
            }
        }
    }
}

+ 2 - 2
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/dateortime/DateOrTimeValue.java

@ -31,7 +31,7 @@ import java.lang.annotation.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
 * 校验日期或时间格式 yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss
 * 校验日期或时间格式 yyyy-MM-dd 或 HH:mm:ss
 *
 * @author xuyuxiang
 * @date 2020/5/26 14:48
@ -42,7 +42,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Retention(RetentionPolicy.RUNTIME)
public @interface DateOrTimeValue {
    String message() default "日期或时间格式不正确,正确格式应为yyyy-MM-dd或yyyy-MM-dd HH:mm:ss";
    String message() default "日期或时间格式不正确,正确格式应为yyyy-MM-dd 或 HH:mm:ss";
    Class[] groups() default {};

+ 4 - 4
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/dateortime/DateOrTimeValueValidator.java

@ -32,7 +32,7 @@ import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
 * 校验日期或时间格式 yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss
 * 校验日期或时间格式 yyyy-MM-dd 或 HH:mm:ss
 *
 * @author xuyuxiang
 * @date 2020/5/26 14:48
@ -47,7 +47,7 @@ public class DateOrTimeValueValidator implements ConstraintValidator<DateOrTimeV
        }
        //长度不对直接返回
        if (dateValue.length() != DatePattern.NORM_DATE_PATTERN.length() &&
                dateValue.length() != DatePattern.NORM_DATETIME_PATTERN.length()) {
                dateValue.length() != DatePattern.NORM_TIME_PATTERN.length()) {
            return false;
        }
        try {
@ -55,9 +55,9 @@ public class DateOrTimeValueValidator implements ConstraintValidator<DateOrTimeV
            return true;
        } catch (Exception dateParseException) {
            try {
                DateUtil.parseDateTime(dateValue);
                DateUtil.parseTime(dateValue);
                return true;
            } catch (Exception dateTimeParseException) {
            } catch (Exception timeParseException) {
                return false;
            }
        }

+ 57 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/month/MonthValue.java

@ -0,0 +1,57 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.validation.month;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
 * 校验日期格式 yyyy-MM
 *
 * @author xuyuxiang
 * @date 2020/5/26 14:48
 */
@Documented
@Constraint(validatedBy = MonthValueValidator.class)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface MonthValue {
    String message() default "日期格式不正确,正确格式应为yyyy-MM";
    Class[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    @Target({ElementType.FIELD, ElementType.PARAMETER})
    @Retention(RUNTIME)
    @Documented
    @interface List {
        MonthValue[] value();
    }
}

+ 60 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/month/MonthValueValidator.java

@ -0,0 +1,60 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.validation.month;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
 * 校验日期格式 yyyy-MM
 *
 * @author xuyuxiang
 * @date 2020/5/26 14:48
 */
public class MonthValueValidator implements ConstraintValidator<MonthValue, String> {
    public static final String NORM_MONTH_PATTERN = "yyyy-MM";
    @Override
    public boolean isValid(String dateValue, ConstraintValidatorContext context) {
        //为空则放过,因为在此校验之前会加入@NotNull或@NotBlank校验
        if (ObjectUtil.isEmpty(dateValue)) {
            return true;
        }
        //长度不对直接返回
        if (dateValue.length() != NORM_MONTH_PATTERN.length()) {
            return false;
        }
        try {
            DateUtil.parse(dateValue, NORM_MONTH_PATTERN);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

+ 57 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/mothordatetime/MonthOrDateTimeValue.java

@ -0,0 +1,57 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.validation.mothordatetime;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
 * 校验日期格式 yyyy-MM 或 yyyy-MM-dd HH:mm:ss
 *
 * @author xuyuxiang
 * @date 2020/5/26 14:48
 */
@Documented
@Constraint(validatedBy = MonthOrDateTimeValueValidator.class)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface MonthOrDateTimeValue {
    String message() default "日期格式不正确,正确格式应为yyyy-MM或yyyy-MM-dd HH:mm:ss";
    Class[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    @Target({ElementType.FIELD, ElementType.PARAMETER})
    @Retention(RUNTIME)
    @Documented
    @interface List {
        MonthOrDateTimeValue[] value();
    }
}

+ 67 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/mothordatetime/MonthOrDateTimeValueValidator.java

@ -0,0 +1,67 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.validation.mothordatetime;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
 * 校验日期格式 yyyy-MM 或 yyyy-MM-dd HH:mm:ss
 *
 * @author xuyuxiang
 * @date 2020/5/26 14:48
 */
public class MonthOrDateTimeValueValidator implements ConstraintValidator<MonthOrDateTimeValue, String> {
    public static final String NORM_MONTH_PATTERN = "yyyy-MM";
    @Override
    public boolean isValid(String dateValue, ConstraintValidatorContext context) {
        //为空则放过,因为在此校验之前会加入@NotNull或@NotBlank校验
        if (ObjectUtil.isEmpty(dateValue)) {
            return true;
        }
        //长度不对直接返回
        if (dateValue.length() != DatePattern.NORM_DATETIME_PATTERN.length() &&
                dateValue.length() != NORM_MONTH_PATTERN.length()) {
            return false;
        }
        try {
            DateUtil.parseDateTime(dateValue);
            return true;
        } catch (Exception dateTimeParseException) {
            try {
                DateUtil.parse(dateValue, NORM_MONTH_PATTERN);
                return true;
            } catch (Exception monthParseException) {
                return false;
            }
        }
    }
}

+ 57 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/time/TimeValue.java

@ -0,0 +1,57 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.validation.time;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
 * 校验日期格式 HH:mm:ss
 *
 * @author xuyuxiang
 * @date 2020/5/26 14:48
 */
@Documented
@Constraint(validatedBy = TimeValueValidator.class)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface TimeValue {
    String message() default "日期格式不正确,正确格式应为HH:mm:ss";
    Class[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    @Target({ElementType.FIELD, ElementType.PARAMETER})
    @Retention(RUNTIME)
    @Documented
    @interface List {
        TimeValue[] value();
    }
}

+ 59 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/time/TimeValueValidator.java

@ -0,0 +1,59 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.validation.time;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
 * 校验日期格式 HH:mm:ss
 *
 * @author xuyuxiang
 * @date 2020/5/26 14:48
 */
public class TimeValueValidator implements ConstraintValidator<TimeValue, String> {
    @Override
    public boolean isValid(String dateValue, ConstraintValidatorContext context) {
        //为空则放过,因为在此校验之前会加入@NotNull或@NotBlank校验
        if (ObjectUtil.isEmpty(dateValue)) {
            return true;
        }
        //长度不对直接返回
        if (dateValue.length() != DatePattern.NORM_TIME_PATTERN.length()) {
            return false;
        }
        try {
            DateUtil.parseTime(dateValue);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}