SpringMVC自定义日期类型的数据转换器

此方法适用于springmvc4零配置
1.
/**
* springmvc String转换成Date
* @author test
*
*/
public class StringToDate implements Converter<String, Date> {

private static final Logger logger = LoggerFactory.getLogger(StringToDate.class);

@Override
public Date convert(String source) {

try {
return DateUtils.parseDate(source);
} catch (ParseException e) {
logger.warn("can not {} convert to time", source);
return new Date();
}
}

}
2.

/**
* 重写addFormatters方法,添加转换器
*/
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToDate());
}

你可能感兴趣的:(springMVC)