最近在做一个项目的时候,我在输入端使用日期控件输入标准的日期格式,然后数据库也设置成了日期类型date,想让其展示成2018-05-08,而程序好像和我作对,结果展示成了2018-05-08 00:00:00.0。很是苦恼,于是查了很多资料,总结在这里。
目标:我想在我的系统中日期和日期时间共同存在。
第一步引入jar:
<!-- jackson begin -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- jackson end -->
<!-- 阿里巴巴json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- 日期帮助类,很强大 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
第二步:在我们spring.xml中配置:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<!-- 下载使用 -->
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ResourceRegionHttpMessageConverter"/>
<!-- json -->
<ref bean="jacksonMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
我们在第二步中,有一个jacksonObjectMapper没有找到对应的bean。所以我们的程序,必须建立一个bean Class,来与之对应bean引用
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* 解决Jackson 差8小时的问题
*/
@Component("jacksonObjectMapper")
public class JacksonObjectMapper extends ObjectMapper {
private static final long serialVersionUID = 4288193147502386170L;
private static final Locale CHINA = Locale.CHINA;
public JacksonObjectMapper() {
this.setLocale(CHINA);
this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", CHINA));
this.setTimeZone(TimeZone.getTimeZone("GMT+8"));
}
}
必须配置@Component("jacksonObjectMapper") 这个与xml中bean的引用相同,不然程序会报错。不知道的,请自行Spring的知识
第三步:
还没完哟,我们知道我们从前端输入的时候都是Sting类型,怎么把他转换成Date类型呢?此时要借助Controller中initBinder方法。我们可以让自己的controller继承一个公共的Controller类,这样自己的Controller就拥有这个方法了,就会自己进行初始化对日期/数字等类型都可以在这里设置。
@InitBinder
public void initBinder(ServletRequestDataBinder binder) {
/**
* 自动转换日期类型的字段格式
*/
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}
第四步:
我们在第三步中只控制了Springmvc的输入,那么输出呢,是不是也需要设置呢?我们知道SpringMVC中输出是基于模型输出的,那么就需要在模型层配置。
于是:
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
private Date StateTime;
这里必须要timezone,因为程序有时差之分,切记。
配置了这些我们解决了问题么,发现还没有。老天啊,要不要这么折磨我,于是查资料,发现实体类中的Date有两种来源
java.util.Date; 和 java.sql.Date; 二者区别自行百度。
于是将java.util.Date更换为java.sql.Date,轻松解决问题。
第五步总结:
在我的程序中,我将日期使用java.sql.Date,
将日期时间使用java.sql.Timestamp