SpringMVC对日期类型的转换示例

所属分类: 软件编程 / java 阅读数: 38
收藏 0 赞 0 分享

在做web开发的时候,页面传入的都是String类型,SpringMVC可以对一些基本的类型进行转换,但是对于日期类的转换可能就需要我们配置。

1、如果查询类使我们自己写,那么在属性前面加上@DateTimeFormat(pattern = "yyyy-MM-dd")  ,即可将String转换为Date类型,如下

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

2、如果我们只负责web层的开发,就需要在controller中加入数据绑定:

@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、可以在系统中加入一个全局类型转换器

实现转换器

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

进行配置:

<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、如果将日期类型转换为String在页面上显示,需要配合一些前端的技巧进行处理。

5、SpringMVC使用@ResponseBody返回json时,日期格式默认显示为时间戳。

@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类型转换为json字符串时,返回的是long time值,如果需要返回指定的日期的类型的get方法上写上@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") ,即可将json返回的对象为指定的类型。

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多