package com.ruoyi.common.validator.date;


import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 时间格式校验
 *
 * @author
 * @date
 */
public class TimeStrVerifyValidator implements ConstraintValidator<TimeStrVerify, String> {

    @Override
    public void initialize(TimeStrVerify constraintAnnotation) {
    }

    @Override
    public boolean isValid(String timeStr, ConstraintValidatorContext context) {
        //校验时间格式 如9:00
        try {
            String[] time = timeStr.split(":");
            int hour = Integer.parseInt(time[0]);
            int minute = Integer.parseInt(time[1]);
            if (hour < 0 || hour >= 24) {
                return false;
            }
            if (minute < 0 || minute >= 60) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }

    }
}
