在作web开发的时候,页面传入的都是String类型,SpringMVC能够对一些基本的类型进行转换,可是对于日期类的转换可能就须要咱们配置。html
一、若是查询类使咱们本身写,那么在属性前面加上@DateTimeFormat(pattern = "yyyy-MM-dd") ,便可将String转换为Date类型,以下前端
@DateTimeFormat (pattern = "yyyy-MM-dd" )
private Date createTime;
|
二、若是咱们只负责web层的开发,就须要在controller中加入数据绑定:java
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
dateFormat.setLenient( false );
binder.registerCustomEditor(Date. class , new CustomDateEditor(dateFormat, true ));//true:容许输入空值,false:不能为空值
|
三、能够在系统中加入一个全局类型转换器web
实现转换器spring
?
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
dateFormat.setLenient( false );
try {
return dateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null ;
}
|
进行配置:json
< bean id = "conversionService" class = "org.springframework.format.support.FormattingConversionServiceFactoryBean" >
< property name = "converters" >
< list >
< bean class = "com.doje.XXX.web.DateConverter" />
</ list >
</ property >
</ bean >
|
< mvc:annotation-driven conversion-service = "conversionService" />
|
四、若是将日期类型转换为String在页面上显示,须要配合一些前端的技巧进行处理。后端
五、SpringMVC使用@ResponseBody返回json时,日期格式默认显示为时间戳。mvc
@Component ( "customObjectMapper" )
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper() {
CustomSerializerFactory factory = new CustomSerializerFactory();
factory.addGenericMapping(Date. class , new JsonSerializer<Date>() {
@Override
public void serialize(Date value, JsonGenerator jsonGenerator,
SerializerProvider provider) throws IOException, JsonProcessingException {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
jsonGenerator.writeString(sdf.format(value));
}
});
this .setSerializerFactory(factory);
}
}
|
配置以下:app
?
< mvc:annotation-driven >
< mvc:message-converters >
< bean class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
< property name = "objectMapper" ref = "customObjectMapper" ></ property >
</ bean >
</ mvc:message-converters >
</ mvc:annotation-driven >
|
六、date类型转换为json字符串时,返回的是long time值,若是须要返回指定的日期的类型的get方法上写上@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") ,便可将json返回的对象为指定的类型。ide
@DateTimeFormat (pattern= "yyyy-MM-dd HH:mm:ss" )
@JsonFormat (pattern= "yyyy-MM-dd HH:mm:ss" ,timezone = "GMT+8" )
public Date getCreateTime() {
return this .createTime;
}
|
以上就是本文的所有内容,但愿对你们的学习有所帮助,也但愿你们多多支持脚本之家。
经过注解来协助SpringMVC处理日期在先后端的传递问题
从前端向后端传递日期:
@DateTimeFormat(pattern="yyyy-MM-dd")
前台向后台传递字符串类型的日期参数时,须要经过此注解将字符串解析成日期类型,其中日期格式能够根据须要进行设置。
例子:若是后台接收的createDate为Java.util.Date类型,但前台传递过来的是2016-05-23,那么此时咱们须要使用@DateTimeFormat注解来修饰createDate字段,不然SpringMVC认为传递过来的是字符串与日期类型不匹配而报400错误(HTTP Status 400 The request sent by the client was syntactically incorrect)。
从后端向前端传递日期:
@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
SpringMVC向前端返回json格式的数据时,日期类型默认返回时间戳,那么咱们能够经过此注解将时间返回为固定格式的字符串。
使用此注解须要引入一下jar包(须要特别注意的是jackson最新的版本对此功能不兼容,所以建议选择2.6.1或者如下的版本):
1. <dependency>
2. <groupId>org.codehaus.jackson</groupId>
3. <artifactId>jackson-mapper-asl</artifactId>
4. <version>1.9.13</version>
5. </dependency>
6. <dependency>
7. <groupId>com.fasterxml.jackson.core</groupId>
8. <artifactId>jackson-core</artifactId>
9. <version>2.6.1</version>
10. </dependency>
11. <dependency>
12. <groupId>com.fasterxml.jackson.core</groupId>
13. <artifactId>jackson-databind</artifactId>
14. <version>2.6.1</version>
15. </dependency>