无意中发现对于时间字符串转Date类,根本不用自己去写转换类,spring mvc已经实现了该功能,还是基于注解的,轻松省事,使用 org.springframework.format.support.FormattingConversionServiceFactoryBean
之后,只要在vo里加注解就行了
使用时要引入一个类库joda-time-n.n.jar
Java代码
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date dateRangeStart;// 通行日期范围开始
@DateTimeFormat(pattern="yyyy-MM-dd") 可将形如1980-0-01的字符串转换到Date类
@NumberFormat(pattern="#,###.##") 可将形如4,500.00的字符串转换成long类型
怎么注册呢?
Java代码
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
conversion-service="conversionService" />
使用 的话,默认就启用FormattingConversionServiceFactoryBean了,所以上面的配置也省了。
但是基本不用,因为