<!-- 自定义转换器 -->
<bean id="conversionService" class="org.springframework.format.support.DefaultFormattingConversionService">
<property name="converters">
<list>
<bean class="org.converter.DateConverter"></bean>
</list>
</property>
</bean>
<!-- 注册转化器 -->
<mvc:annotation-driven conversion-service="conversionService" />
注意:class 也可以为: org.springframework.context.support.ConversionServiceFactoryBean
package org.converter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.core.convert.converter.Converter;
public class DateConverter implements Converter<String, Date> {
private static final List<String> formarts = new ArrayList<String>(4);
static{
formarts.add("yyyy-MM");
formarts.add("yyyy-MM-dd");
formarts.add("yyyy-MM-dd hh:mm");
formarts.add("yyyy-MM-dd hh:mm:ss");
}
public Date convert(String source) {
String value = source.trim();
if ("".equals(value)) {
return null;
}
if(source.matches("^\\d{4}-\\d{1,2}$")){
return parseDate(source, formarts.get(0));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
return parseDate(source, formarts.get(1));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){
return parseDate(source, formarts.get(2));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
return parseDate(source, formarts.get(3));
}else {
throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
}
}
/**
* 功能描述:格式化日期
*
* @param dateStr
* String 字符型日期
* @param format
* String 格式
* @return Date 日期
*/
public Date parseDate(String dateStr, String format) {
Date date=null;
try {
DateFormat dateFormat = new SimpleDateFormat(format);
date = (Date) dateFormat.parse(dateStr);
} catch (Exception e) {
}
return date;
}
public static void main(String[] args) {
System.err.println(new DateConverter().convert("2014-04"));
}
}
来源:oschina
链接:https://my.oschina.net/u/162199/blog/343052