在作web開發的時候,頁面傳入的都是String類型,SpringMVC能夠對一些基本的類型進行轉換,可是對於日期類的轉換可能就須要咱們配置。html
一、若是查詢類使咱們本身寫,那麼在屬性前面加上@DateTimeFormat(pattern = "yyyy-MM-dd") ,便可將String轉換爲Date類型,以下前端
1
2
|
@DateTimeFormat
(pattern =
"yyyy-MM-dd"
)
private
Date createTime;
|
二、若是咱們只負責web層的開發,就須要在controller中加入數據綁定:java
1
2
3
4
5
|
@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
1
2
3
4
5
6
7
8
9
10
11
12
|
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
1
2
3
4
5
6
7
|
<
bean
id
=
"conversionService"
class
=
"org.springframework.format.support.FormattingConversionServiceFactoryBean"
>
<
property
name
=
"converters"
>
<
list
>
<
bean
class
=
"com.doje.XXX.web.DateConverter"
/>
</
list
>
</
property
>
</
bean
>
|
1
|
<
mvc:annotation-driven
conversion-service
=
"conversionService"
/>
|
四、若是將日期類型轉換爲String在頁面上顯示,須要配合一些前端的技巧進行處理。後端
五、SpringMVC使用@ResponseBody返回json時,日期格式默認顯示爲時間戳。mvc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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
1
2
3
4
5
6
7
|
<
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
1
2
3
4
5
|
@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或者如下的版本):
本文轉自http://blog.csdn.net/kylinah/article/details/53101068 感謝做者