Spring 日期时间处理

1 Spring自身的支持

1.1 factory-bean

<bean id="dateFormat" class="java.text.SimpleDateFormat">
	<constructor-arg value="yyyy-MM-dd" />
</bean>
<bean  id="user" class="zxy.demo.springmvc.domain.UserInfo">
	<constructor-arg value="10" />
	<constructor-arg index="1" value="名字" />
	<constructor-arg index="2">
		<bean factory-bean="dateFormat" factory-method="parse">
			<constructor-arg value="2016-12-19" />
		</bean>
	</constructor-arg>
</bean>

作用于单个实体,用于xml文件配置。

1.2 DateTimeFormat

这是一个注解,完整类名是org.springframework.format.annotation.DateTimeFormat,用于http请求入参,只能作用于具体的实体对象,如下

@DateTimeFormat(pattern="yyyy-MM-dd")

1.3 InitBinder

只用于http请求入参,作用于全局,可搭配@Controller@ControllerAdvice使用,如下

@ControllerAdvice
public class MyControllerAdvice {
	@InitBinder
	public void initDate(WebDataBinder binder) {
//		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH");
//		dateFormat.setLenient(false);
//		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
		binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd")); // Spring 4.2之后的写法
	}
}

1.4 conversion-service

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean class="zxy.demo.springmvc.converter.DateConverter"/>
        </set>
    </property>
</bean>

<mvc:annotation-driven conversion-service="conversionService"/>
public class DateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String source) {
        // ...
    }
}

DateConverter是一个自定义类,实现接口org.springframework.core.convert.converter.Converter,重写convert()方法即可。作用于全局,用于http请求入参


2 说明

前面说的日期处理,没有一种是用于请求返回的,如果是要返回数据,并且使用json进行系列化的,那么SpringMVC支持的有jackson跟Gson,具体要看引入了哪个jar包,如果两个引入了,那么将以jackson为准,具体可看org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser这个类,关键看两个成员变量jackson2PresentgsonPresent,这是两个boolean值,根据这两个成员变量看关联的代码即可。

:由于一般接口返回的数据是json格式的,所以jackson跟Gson这两个包肯定是要引入一个的,不然请求会报错,如“org.springframework.core.convert.ConversionFailedException: Failed to convert from type xxx to type xxx for ...”,这时候引入两个包之中的一个就可以让对象序列化为Json进行传输了。同理xml格式的序列化也可参考AnnotationDrivenBeanDefinitionParser这个类进行相应的配置。

:下面的代码是基于Spring 4.3.8.RELEASE,其他版本应该也类似。


3 jackson

3.1 JsonFormat

这是一个注解,完整类名是com.fasterxml.jackson.annotation.JsonFormat,只能用于http请求返回,只能作用于具体的实体对象,如下

@JsonFormat(pattern = "yyyy-MM-dd HH:mm")

3.2 message-converters

作用于全局,只用于请求返回

<mvc:annotation-driven>
	<mvc:message-converters>
		<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
			<property name="objectMapper" >
				<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" p:simpleDateFormat="yyyy-MM-dd HH:mm:ss" />
			</property>
		</bean>
	</mvc:message-converters>
</mvc:annotation-driven>

4 Gson

4.1 message-converters

作用于全局,只用于请求返回

<mvc:annotation-driven>
	<mvc:message-converters>
		<bean class="org.springframework.http.converter.json.GsonHttpMessageConverter">
			<property name="gson">
				<bean class="org.springframework.http.converter.json.GsonFactoryBean" p:dateFormatPattern="yyyy-MM-dd HH:mm:ss" />
			</property>
		</bean>
	</mvc:message-converters>
</mvc:annotation-driven>

5 最后

如果作用域单个的跟作用于全局的都配置了,那么配置单个实体的将最终生效而不会用全局的配置。

对于使用message-converters的方式,如果还需要更多的配置参数,可以看spring-webmvc的AnnotationDrivenBeanDefinitionParser这个类跟spring-web的org.springframework.http.converter.json包下的类即可。

更多的日期时间处理方式,可参考官方文档。


6 参考

posted @ 2017-05-15 20:50  狼爷  阅读(3762)  评论(0编辑  收藏  举报