亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringMVC對(duì)日期類型的轉(zhuǎn)換示例

 更新時(shí)間:2017年02月14日 16:22:10   作者:ngulc  
本篇文章主要介紹了SpringMVC對(duì)日期類型的轉(zhuǎn)換示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

在做web開發(fā)的時(shí)候,頁(yè)面?zhèn)魅氲亩际荢tring類型,SpringMVC可以對(duì)一些基本的類型進(jìn)行轉(zhuǎn)換,但是對(duì)于日期類的轉(zhuǎn)換可能就需要我們配置。

1、如果查詢類使我們自己寫,那么在屬性前面加上@DateTimeFormat(pattern = "yyyy-MM-dd")  ,即可將String轉(zhuǎn)換為Date類型,如下

@DateTimeFormat(pattern = "yyyy-MM-dd") 
private Date createTime; 

2、如果我們只負(fù)責(zé)web層的開發(fā),就需要在controller中加入數(shù)據(jù)綁定:

@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:不能為空值

3、可以在系統(tǒng)中加入一個(gè)全局類型轉(zhuǎn)換器

實(shí)現(xiàn)轉(zhuǎn)換器

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; 
}

進(jìn)行配置:

<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" /> 

4、如果將日期類型轉(zhuǎn)換為String在頁(yè)面上顯示,需要配合一些前端的技巧進(jìn)行處理。

5、SpringMVC使用@ResponseBody返回json時(shí),日期格式默認(rèn)顯示為時(shí)間戳。

@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); 
 } 
}

配置如下:

<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> 

6、date類型轉(zhuǎn)換為json字符串時(shí),返回的是long time值,如果需要返回指定的日期的類型的get方法上寫上@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") ,即可將json返回的對(duì)象為指定的類型。

@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; 
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論