springmvc时间date转换器(处理多种时间格式)
首先定义一个时间转换器的类DateAdvice,在springmvc中DateFormat不能是数组,所以就重写CustomDateEditor类并且命名为
MyCustomDateEditor类
@ControllerAdvice
public class DateAdvice {
@InitBinder
public void init(WebDataBinder binder){
DateFormat[] dateFormat={
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyy/MM/dd"),
new SimpleDateFormat("yyyy.MM.dd"),
new SimpleDateFormat("yyyy年MM月dd日")
};
MyCustomDateEditor customDateEditor = new MyCustomDateEditor(dateFormat,true);
binder.registerCustomEditor(Date.class,customDateEditor);
}
}
接下来就对MyCustomDateEditor进行修改
public class MyCustomDateEditor extends PropertyEditorSupport {
private final DateFormat[] dateFormat;
private final boolean allowEmpty;
private final int exactDateLength;
/**
* Create a new CustomDateEditor instance, using the given DateFormat
* for parsing and rendering.
* <p>The "allowEmpty" parameter states if an empty String should
* be allowed for parsing, i.e. get interpreted as null value.
* Otherwise, an IllegalArgumentException gets thrown in that case.
* @param dateFormat the DateFormat to use for parsing and rendering
* @param allowEmpty if empty strings should be allowed
*/
public MyCustomDateEditor(DateFormat[] dateFormat, boolean allowEmpty) {
this.dateFormat = dateFormat;
this.allowEmpty = allowEmpty;
this.exactDateLength = -1;
}
/**
* Create a new CustomDateEditor instance, using the given DateFormat
* for parsing and rendering.
* <p>The "allowEmpty" parameter states if an empty String should
* be allowed for parsing, i.e. get interpreted as null value.
* Otherwise, an IllegalArgumentException gets thrown in that case.
* <p>The "exactDateLength" parameter states that IllegalArgumentException gets
* thrown if the String does not exactly match the length specified. This is useful
* because SimpleDateFormat does not enforce strict parsing of the year part,
* not even with {@code setLenient(false)}. Without an "exactDateLength"
* specified, the "01/01/05" would get parsed to "01/01/0005". However, even
* with an "exactDateLength" specified, prepended zeros in the day or month
* part may still allow for a shorter year part, so consider this as just
* one more assertion that gets you closer to the intended date format.
* @param dateFormat the DateFormat to use for parsing and rendering
* @param allowEmpty if empty strings should be allowed
* @param exactDateLength the exact expected length of the date String
*/
public MyCustomDateEditor(DateFormat[] dateFormat, boolean allowEmpty, int exactDateLength) {
this.dateFormat = dateFormat;
this.allowEmpty = allowEmpty;
this.exactDateLength = exactDateLength;
}
/**
* Parse the Date from the given text, using the specified DateFormat.
*/
@Override
public void setAsText(@Nullable String text) throws IllegalArgumentException {
if (this.allowEmpty && !StringUtils.hasText(text)) {
// Treat empty String as null value.
setValue(null);
}
else if (text != null && this.exactDateLength >= 0 && text.length() != this.exactDateLength) {
throw new IllegalArgumentException(
"Could not parse date: it is not exactly" + this.exactDateLength + "characters long");
}
else {
boolean flag = false;
for (DateFormat df : dateFormat) {
try {
setValue(df.parse(text));
flag = true;
break;
} catch (ParseException ex) {
continue;
}
}
if (flag == false) {
throw new IllegalArgumentException("Could not parse date: ");
}
}
}
/**
* Format the Date as String, using the specified DateFormat.
*/
@Override
public String getAsText() {
Date value = (Date) getValue();
for (DateFormat df : dateFormat) {
try {
return df.format(value);
} catch (Exception e) {
continue;
}
}
return "";
}
}
注意:主要是为了把DateFormat转为数组,这样就进行了多种日期转换