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这个类,关键看两个成员变量jackson2Present跟gsonPresent,这是两个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 参考
- http://docs.spring.io/spring/docs/4.3.8.RELEASE/spring-framework-reference/html/validation.html#format-CustomFormatAnnotations
- http://docs.spring.io/spring/docs/4.3.8.RELEASE/spring-framework-reference/html/validation.html#core-convert-ConversionService-API
- http://docs.spring.io/spring/docs/4.3.8.RELEASE/spring-framework-reference/html/mvc.html#mvc-config-conversion
- http://docs.spring.io/spring/docs/4.3.8.RELEASE/spring-framework-reference/html/mvc.html#mvc-config-message-converters
Spring 日期时间处理的更多相关文章
- Springboot 关于日期时间格式化处理方式总结
项目中使用LocalDateTime系列作为DTO中时间的数据类型,但是SpringMVC收到参数后总报错,为了配置全局时间类型转换,尝试了如下处理方式. 注:本文基于Springboot2.x测试, ...
- MySQL 日期时间类型怎么选?千万不要乱用!
构建数据库写程序避免不了使用日期和时间,对于数据库来说,有多种日期时间字段可供选择,如 timestamp 和 datetime 以及使用 int 来存储 unix timestamp. 不仅新手,包 ...
- 全网最全!彻底弄透Java处理GMT/UTC日期时间
目录 前言 本文提纲 版本约定 正文 Date类型实现 时区/偏移量TimeZone 设置默认时区 让人恼火的夏令时 Date时区无关性 读取字符串为Date类型 SimpleDateFormat格式 ...
- 一文告诉你Java日期时间API到底有多烂
前言 你好,我是A哥(YourBatman). 好看的代码,千篇一律!难看的代码,卧槽卧槽~其实没有什么代码是"史上最烂"的,要有也只有"史上更烂". 日期是商 ...
- JAVA中计算两个日期时间的差值竟然也有这么多门道
上半年春招的时候,作为面试官,对于面试表现的不错的同学会要求其写一小段代码看看.题目很简单: 给定一个日期,然后计算下距离今天相差的天数. 本以为这么个问题就是用来活跃面试氛围的,但是结果却让人大跌眼 ...
- EasyUI datagrid 日期时间格式化
EasyUI datagrid中显示日期时间时,会显示为以下不太直观的数值: 添加以下JavaScript脚本,然后在field中添加 formatter: DateTimeFormatter 即可. ...
- POCO库——Foundation组件之日期时间DateTime
日期时间DateTime:内部提供多个设计计时器.日期.时区.时间戳等: Clock.h :Clock时钟计时类,_clock:Int64类型时钟值,CLOCKVAL_MIN.CLOCKVAL_MAX ...
- db2 日期时间格式
db2日期和时间常用汇总 1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2 ...
- Angularjs在控制器(controller.js)的js代码中使用过滤器($filter)格式化日期/时间实例
Angularjs内置的过滤器(filter)为我们的数据信息格式化提供了比较强大的功能,比如:格式化时间,日期.格式化数字精度.语言本地化.格式化货币等等.但这些过滤器一般都是在VIEW中使用的,比 ...
随机推荐
- js中实现继承的几种方式
首先我们了解,js中的继承是主要是由原型链实现的.那么什么是原型链呢? 由于每个实例中都有一个指向原型对象的指针,如果一个对象的原型对象,是另一个构造函数的实例,这个对象的原型对象就会指向另一个对象的 ...
- XJOI1689相连的城市
相连的城市 n个城市中,某些城市间有道路互相连接.给出与每个城市相邻的城市有多少个,请输出城市间的邻接矩阵. 输入格式: 第一行输入一个正整数n,表示城市的个数. 第二行输入n个用空格隔开的非负整数, ...
- git commit -m与-am的区别
前面的话 使用git commit -am是不是就可以完全不使用git add命令呢?不是 理论 要了解git commit -m与git commit -am的区别,首先要明白它们的定义 字面解释的 ...
- 老李推荐:第6章1节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览 2
事件要到那里去? 每个事件源处理类都维护着一个自己的事件队列, 在Monkey中叫做CommandQueue,里面装的是每个具体的MonkeyEvent事件.当来自网络的字串命令被翻译成对应的Monk ...
- get你想象不到的技能
1.取消选取.防止复制 <body selectStart="return false"> </body> 2.不允许粘贴 <body onpaste ...
- 基于Spring开发——自定义标签及其解析
1. XML Schema 1.1 最简单的标签 一个最简单的标签,形式如: <bf:head-routing key="1" value="1" to= ...
- selenium实例:unittest框架+PO开发模式
这是<selenium2+python学习总结>的升级版. 1. 项目结构 2. 项目代码 1) globalparameter.py # ...
- Entity Framework Code First在Oracle下的伪实现
为什么要说是伪实现,因为还做不到类似MsSql中那样完全的功能.Oralce中的数据库还是要我们自己手动去创建的.这里,我们舍掉了Model First中的EDMX文件,自己在代码里面写模型与映射关系 ...
- ios 获取当前ViewController
- (UIViewController*)getpresentVC:(UIWindow *)window{ if ([window.rootViewController class]==[UITabB ...
- 500. Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...