关于java时间的判断

//获取开始时间一年后的时间
        Date oneYearLaterDate = DateUtils.addYears(DateUtil.parseDateString(settledDateBegin,"yyyy-MM-dd"),1);
        int beginToEnd = settledDateBegin.compareTo(settledDateEnd);
        int endToOneYearLater = settledDateEnd.compareTo(DateUtil.format(oneYearLaterDate,"yyyy-MM-dd"));
        if (beginToEnd > 0 || endToOneYearLater > 0){
            return  Result.fail(ErrorEnum.DATE_TIME_ERROR.getCode(),ErrorEnum.DATE_TIME_ERROR.getMessage());
        }
//查询一个时间段内的数据,需要把结束时间加一天
 Date date = DateUtil.parseDateString(dto.getSettledDateEnd(),"yyyy-MM-dd");
        dto.setSettledDateEnd(DateUtil.format(DateUtils.addDays(date, 1),"yyyy-MM-dd"));
//SQL
//时间小于当前时间减一天
 
 //时间小于于当前时间
 
 //在开始结束时间内的数据(代码中结束时间增加一天)
  = str_to_date(#{settledDateBegin}, '%Y-%m-%d')
        AND str_to_date(settled_date, '%Y-%m-%d')< str_to_date(#{settledDateEnd}, '%Y-%m-%d') ]]>
        //开始时间在结束时间后面
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Constants.DATE_FORMATTER);
        LocalDate startDate = LocalDate.parse(request.getStartDate(), formatter);
        LocalDate endDate = LocalDate.parse(request.getEndDate(), formatter);
        if (startDate.isAfter(endDate)) {
            return ResultGenerator.fail(BasicError.END_DATE_BEFORE_START_DATE);
        }
            //只能添加两天以内
            DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(Constants.DATE_FORMATTER);//"uuuu-MM-dd"
            LocalDate guideDate = LocalDate.parse(customerGuide.getGuideDate(), dateFormatter);
            if (!LocalDate.now().minusDays(3L).isBefore(guideDate)) {
                return ResultGenerator.fail(BasicError.TIME_OVERDUE);
            }
            //带看时间是否符合规范  必须整时整刻钟等
            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(Constants.TIME_FORMAT);// "HH:mm"
            LocalTime guideTime = LocalTime.parse(customerGuide.getGuideTime(), timeFormatter);
            if (!GuideTimeEnum.getKeys().contains(guideTime.getMinute())) {
                return ResultGenerator.fail(BasicError.TIME_NOT_PROFILE);
            }
 @CustomDate(required = true, message = "日期格式错误YYYY-MM-DD")
 @Pattern(regexp = "[0-9]{2}:[0-9]{2}", message = "时间格式错误HH:MM")
  
  //校验时间注解 @CustomDate
  import org.apache.commons.lang3.StringUtils;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Documented
@Constraint(validatedBy = {CustomDate.Validator.class})
@Target({FIELD, METHOD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
public @interface CustomDate {

    boolean required() default false;

    String message() default "";

    String format() default "uuuu-MM-dd";

    Class[] groups() default {};

    Class[] payload() default {};

    class Validator implements ConstraintValidator {

        private CustomDate customDate;

        @Override
        public void initialize(CustomDate customDate) {
            this.customDate = customDate;
        }

        @Override
        public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
            if (StringUtils.isEmpty(value) || "".equals(value.trim())) {
                return !this.customDate.required();
            } else {
                String pattern = customDate.format();
                if (pattern.length() != value.length()) {
                    return false;
                }
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern).withResolverStyle(ResolverStyle.STRICT);
                try {
                    LocalDate.parse(value, dateTimeFormatter);
                } catch (Exception e) {
                    return false;
                }
                return true;
            }
        }
    }
}

你可能感兴趣的:(Java)