轉載至:http://blog.csdn.net/lantianzhange/article/details/40920933html
在Spring MVC中存在兩大類的類型轉換,一類是Json,一個是Spring的Binder轉換。java
JSON:web
使用Json轉換時,能夠以下使用:spring
- public class Test {
-
- private Date createdate;
-
- @JsonSerialize(using = DateYMDHMSJsonSerializer.class)
- public Date getCreatedate() {
- return createdate;
- }
-
- @JsonDeserialize(using = DateYMDHMSJsonDeserializer.class)
- public void setCreatedate(Date createdate) {
- this.createdate = createdate;
- }
- }
能夠看到這裏使用了兩個Json轉換的註解:json
第一個@JsonSerialize是轉換爲字符串,主要是後臺傳遞給前臺時的日期格式;mvc
第二個@JsonDeserialize是轉換字符串爲日期類型,主要是從前臺日後臺傳遞時的日期。ide
兩個具體轉換類的實現:工具
- /**
- * Description: 日期轉換 - "yyyy-MM-dd HH:mm:ss"
- * Author: liuzh
- * Update: liuzh(2014-04-17 10:59)
- */
- public class DateYMDHMSJsonSerializer extends JsonSerializer<Date>{
- @Override
- public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
- try {
- jsonGenerator.writeString(DateUtil.formatDate(date, DateUtil.DATE_FORMAT_TIME_T));
- } catch (BusinessException e) {
- jsonGenerator.writeString(String.valueOf(date.getTime()));
- }
- }
- }
- /**
- * Description: 日期轉換 - "yyyy-MM-dd HH:mm:ss"
- * Author: liuzh
- * Update: liuzh(2014-04-17 10:59)
- */
- public class DateYMDHMSJsonDeserializer extends JsonDeserializer<Date> {
- @Override
- public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
- try {
- return DateUtil.formatStringToDate(jp.getText(), DateUtil.DATE_FORMAT_TIME_T);
- } catch (BusinessException e) {
- return new Date(jp.getLongValue());
- }
- }
- }
其中DateUtil是一個對日期格式轉換的工具類,使用的SimpleDateFormat進行轉換。this
Binder:spa
這種類型轉換的時候,使用的是Spring的參數綁定,代碼以下:
- /**
- * Description: 全局類型轉換
- * Author: liuzh
- * Update: liuzh(2014-05-26 13:08)
- */
- public class GlobalDataBinder implements WebBindingInitializer {
- /**
- * 智能日期轉換,針對四種格式日期:
- * 1.2014-05-26
- * 2.1401951570548
- * 3.2014-05-26 00:00
- * 4.2014-05-26 00:00:00
- */
- private class SmartDateEditor extends PropertyEditorSupport {
- /**
- * 根據2014-05-26 00:00:00長度來判斷選擇哪一種轉換方式
- */
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- if (text == null || text.length() == 0) {
- setValue(null);
- } else {
- try {
- if (text.length() == 10) {
- setValue(DateUtil.formatStringToDate(text, DateUtil.DATE_FORMAT_YYYYMMDD));
- } else if (text.length() == 13) {
- setValue(new Date(Long.parseLong(text)));
- } else if (text.length() == 16) {
- setValue(DateUtil.formatStringToDate(text, DateUtil.DATE_FORMAT_TIME_R));
- } else if (text.length() == 19) {
- setValue(DateUtil.formatStringToDate(text, DateUtil.DATE_FORMAT_TIME_T));
- } else {
- throw new IllegalArgumentException("轉換日期失敗: 日期長度不符合要求!");
- }
- } catch (Exception ex) {
- throw new IllegalArgumentException("轉換日期失敗: " + ex.getMessage(), ex);
- }
- }
- }
-
- /**
- * 轉換爲日期字符串
- */
- @Override
- public String getAsText() {
- Date value = (Date) getValue();
- String dateStr = null;
- if (value == null) {
- return "";
- } else {
- try {
- dateStr = DateUtil.formatDate(value, DateUtil.DATE_FORMAT_TIME_T);
- if (dateStr.endsWith(" 00:00:00")) {
- dateStr = dateStr.substring(0, 10);
- } else if (dateStr.endsWith(":00")) {
- dateStr = dateStr.substring(0, 16);
- }
- return dateStr;
- } catch (Exception ex) {
- throw new IllegalArgumentException("轉換日期失敗: " + ex.getMessage(), ex);
- }
- }
- }
- }
-
- @Override
- public void initBinder(WebDataBinder binder, WebRequest request) {
- //日期格式轉換
- binder.registerCustomEditor(Date.class, new SmartDateEditor());
- }
-
- }
這裏對日期類型進行了一些判斷來特殊處理。該類須要在Spring的xml進行配置:
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <property name="webBindingInitializer">
- <bean class="com.easternie.sys.common.GlobalDataBinder"/>
- </property>
- </bean>
經過這種配置以後,Spring就能對日期進行自由轉換了。