给定下面的try / catch块,如何在实际方法范围中返回return !dateToDate.before(dateFromDate) && dateFromDate.before(dateToDate);
值?它希望我回归虚假。我无法在我想要的地方包含正确的返回值,即return false
当前所在的位置。
public class YourDateValidator implements ConstraintValidator<ValidDates,
TypeToBeValidated> {
@Override
public void initialize(ValidDates constraintAnnotation) {
}
@Override
public boolean isValid(TypeToBeValidated value, ConstraintValidatorContext context) {
String dateTo = value.getDateTo();
String dateFrom = value.getDateFrom();
SimpleDateFormat formatter = new SimpleDateFormat(DateUtil.dateFormat);
Date dateToDate = null;
Date dateFromDate = null;
try {
dateFromDate = formatter.parse(dateFrom);
dateToDate = formatter.parse(dateTo);
return !dateToDate.before(dateFromDate) && dateFromDate.before(dateToDate);
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
}
所需代码:
@Override
public boolean isValid(TypeToBeValidated value, ConstraintValidatorContext context) {
String dateTo = value.getDateTo();
String dateFrom = value.getDateFrom();
SimpleDateFormat formatter = new SimpleDateFormat(DateUtil.dateFormat);
Date dateFromDate = formatter.parse(dateFrom);
Date dateToDate = formatter.parse(dateTo);
return !dateToDate.before(dateFromDate) && dateFromDate.before(dateToDate);
}
我无法在@Overriden isValid
类中抛出ParseException。但是我不能将返回值放在tryter的try / catch块中。我该怎么办?
答案 0 :(得分:2)
由于您必须捕获已检查的异常,因此无法实现所需的实现。如果您的目标是只从方法中获得单个返回点,那么您可以使用在try / catch块中指定的结果变量:
@Override
public boolean isValid(TypeToBeValidated value, ConstraintValidatorContext context) {
String dateTo = value.getDateTo();
String dateFrom = value.getDateFrom();
SimpleDateFormat formatter = new SimpleDateFormat(DateUtil.dateFormat);
Date dateToDate = null;
Date dateFromDate = null;
boolean valid = false;
try {
dateFromDate = formatter.parse(dateFrom);
dateToDate = formatter.parse(dateTo);
valid = !dateToDate.before(dateFromDate) && dateFromDate.before(dateToDate);
} catch (ParseException e) {
e.printStackTrace();
}
return valid;
}
或者,如果您真的想在方法之外处理异常,可以将其重新抛出为未经检查的异常:
@Override
public boolean isValid(TypeToBeValidated value, ConstraintValidatorContext context) {
String dateTo = value.getDateTo();
String dateFrom = value.getDateFrom();
SimpleDateFormat formatter = new SimpleDateFormat(DateUtil.dateFormat);
Date dateToDate = null;
Date dateFromDate = null;
try {
dateFromDate = formatter.parse(dateFrom);
dateToDate = formatter.parse(dateTo);
return !dateToDate.before(dateFromDate) && dateFromDate.before(dateToDate);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
答案 1 :(得分:1)
我建议你总是使用这样的东西:
@Override
public boolean isValid(TypeToBeValidated value, ConstraintValidatorContext context) {
String dateTo = value.getDateTo();
String dateFrom = value.getDateFrom();
SimpleDateFormat formatter = new SimpleDateFormat(DateUtil.dateFormat);
boolean isValid = false;
try {
Date dateFromDate = formatter.parse(dateFrom);
Date dateToDate = formatter.parse(dateTo);
isValid = dateFromDate.before(dateToDate);
if( isValid ) {
return true;
}
context.buildConstraintViolationWithTemplate(
String.format("Error: dateFromDate (%s) is not before dateToDate (%s)!",
dateFromDate.toString(),
dateToDate.toString()
)
)
.addConstraintViolation();
}
} catch (ParseException e) {
context.buildConstraintViolationWithTemplate(
String.format("Exception while validating dateFromDate (%s), dateToDate (%s)! Reason: %s",
dateFromDate.toString(),
dateToDate.toString(),
e.getMessage()
)
)
.addConstraintViolation();
}
return false;
}
在这种情况下,您将始终知道,为什么以及您的错位失败的数据。