@Slf4j
public class MyConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
log.info("start convert");
if (null == source || "".equals(source)) {
return null;
}
String pattern = "";
if (source.matches("^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$")) {
pattern = "yyyy-MM-dd";
} else if (source.matches("^(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$")) {
pattern = "yyyy-MM-dd";
} else if (source.matches("^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$")) {
pattern = "yyyy-MM-dd HH:mm:dd";
} else {
throw new IllegalArgumentException("not match");
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
try {
Date parse = simpleDateFormat.parse(source);
return parse;
} catch (ParseException e) {
//e.printStackTrace();
throw new IllegalArgumentException(e.getMessage());
}
}
}