spring mvc 时间格式转换

在前后台传值时常常出现时间格式转换出错问题。

解决:jsp页面往 action 传值时出现的问题。

原因,jsp页面的时间格式是个字符串格式,后台是DATE格式,会出现格式不匹配

1、找个地方创建一个类让他实现 Converter接口,两个泛型代表,String类型转换成date类型

[java]  view plain  copy
  1. //Converter<S, T>  
  2. //S:source,需要转换的源的类型  
  3. //T:target,需要转换的目标类型  
  4. public class DateConverter implements Converter<String, Date> {  
  5.   
  6.     @Override  
  7.     public Date convert(String source) {  
  8.         try {  
  9.             // 把字符串转换为日期类型  
  10.             SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");  
  11.             Date date = simpleDateFormat.parse(source);  
  12.   
  13.             return date;  
  14.         } catch (ParseException e) {  
  15.             // TODO Auto-generated catch block  
  16.             e.printStackTrace();  
  17.         }  
  18.         // 如果转换异常则返回空  
  19.         return null;  
  20.     }  
  21. }  
2、配置  Converter

在spring mvc 的配置文件中添加配置

[java]  view plain  copy
  1. <!-- 配置注解驱动 -->  
  2. <!-- 如果配置此标签,可以不用配置... -->  
  3. <mvc:annotation-driven conversion-service="conversionService" />  
  4.   
  5. <!-- 转换器配置 -->  
  6. <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">  
  7.     <property name="converters">  
  8.         <set>  
  9.                         <!-- 这个类名是刚刚创建好的类的全路径 -->  
  10.                 <bean class="cn.itcast.springmvc.converter.DateConverter" />  
  11. </set>  
  12. </property>  
  13. </bean>  

解决:action往 jsp页面传值时出现的问题。

原因,jsp页面的时间格式是个字符串格式,后台是DATE格式,会出现格式不匹配

1、同样找个地方创建一个类

继承    JsonSerializer<Date>
[java]  view plain  copy
  1. package com.cn.config;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import java.text.SimpleDateFormat;  
  6. import java.util.Date;  
  7.   
  8. import org.codehaus.jackson.JsonGenerator;  
  9. import org.codehaus.jackson.JsonProcessingException;  
  10. import org.codehaus.jackson.map.JsonSerializer;  
  11. import org.codehaus.jackson.map.SerializerProvider;  
  12.   
  13. public class viewObjectMapper extends JsonSerializer<Date>{  
  14.   
  15.       @Override    
  16.         public void serialize(Date value, JsonGenerator jgen, SerializerProvider arg2)    
  17.                 throws IOException, JsonProcessingException {    
  18.             // TODO Auto-generated method stub    
  19.             SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");    
  20.             String formattedDate = formatter.format(value);    
  21.             jgen.writeString(formattedDate);    
  22.                 
  23.         }    
  24.   
  25.       
  26.        
  27.       
  28. }  

2、在需要传递的实体类 参数的get方法上添加注解使用刚刚写好的类

注解一定要写在get方法上

[java]  view plain  copy
  1. public class UserInfo {    
  2.     private String name;    
  3.     private int id;    
  4.     private Date birthday;    
  5.         
  6.     @JsonSerialize(using = viewObjectMapper.class)    
  7.     public Date getBirthday() {    
  8.         return birthday;    
  9.     }    
  10.     
  11.     public void setBirthday(Date birthday) {    
  12.         this.birthday = birthday;    
  13.     }    
  14.     
  15.     public String getName() {    
  16.         return name;    
  17.     }    
  18.     
  19.     public void setName(String name) {    
  20.         this.name = name;    
  21.     }    
  22.     
  23.     public int getId() {    
  24.         return id;    
  25.     }    
  26.     
  27.     public void setId(int id) {    
  28.         this.id = id;    
  29.     }    
  30.     
  31.         
  32. }    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值