转自:http://blog.csdn.net/chenleixing/article/details/45190371

方法1.在实体中加入日期格式化注解

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

方法2.在controller中加入数据绑定代码

package com.fyh.www.pojo.user;import java.text.SimpleDateFormat;
import java.util.Date;import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;public class LoginController {@InitBinderpublic void initBinder(WebDataBinder binder) {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值,false:不能为空值}
}

方法3.注册一个全局日期类型转化器

注册全局转化器

    <mvc:annotation-driven conversion-service="conversionService"/><!-- 设置Converter转换器 --><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><!-- 设置多个转换器 --><property name="converters"><list><bean class="com.fyh.www.common.mvcConverter.CustomTrimConverter"></bean></list></property></bean>

具体的实现代码

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

后台date类型到前台String类型

JSP模版引擎方法:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>   <fmt:formatDate value="${job.jobtime }" pattern="yyyy-MM-dd HH:mm:ss"/>  

Freemarker模版引擎方法:

    <input id="receiveAppTime" name="receiveAppTime" type="text" value="${(bean.receiveAppTime?string('yyyy-MM-dd'))!}" />  

转载于:https://www.cnblogs.com/YLQBL/p/8493680.html

SpringMVC日期类型转换问题三大处理方法归纳相关推荐

  1. SpringMVC日期类型转换问题处理方法归纳

    前言 我们在SpringMVC开发中,可能遇到比较多的问题就是前台与后 台实体类之间日期转换处理的问题了,说问题也不大,但很多人开发中经常会遇到这个问题,有时很令人头疼,有时间问题暴露的不是很明显,然 ...

  2. SpringMVC 日期类型转换

    全局日期类型转换配置 配置SpringMVC配置文件 <bean id="conversionService" class="org.springframework ...

  3. SpringMVC 之类型转换Converter 源代码分析

    SpringMVC 之类型转换Converter 源代码分析 最近研究SpringMVC的类型转换器,在以往我们需要 SpringMVC 为我们自动进行类型转换的时候都是用的PropertyEdito ...

  4. 深度盘点:Python 变量类型转换的 6 种方法

    大家好,今天我来给大家介绍 Python 变量类型转换的 6 种方法.梳理不易,喜欢记得点赞.收藏.关注. [注]完整版代码.数据.技术交流,文末获取 一.变量类型及转换 对于变量的数据类型而言,Pa ...

  5. SpringMVC 之类型转换Converter详解转载

    SpringMVC之类型转换Converter详解 本文转载 http://www.tuicool.com/articles/uUjaum 1.1     目录 1.1      目录 1.2     ...

  6. Java进阶学习第十六天(Struts框架:配置、数据处理、自动封装、日期类型转换、文件上传)

    一.Struts2配置 1.Struts2的Action开发的几种方式 ① 方式1:继承ActionSupport(一般使用方法) public class UserAction extends Ac ...

  7. springboot springmvc 抛出全局异常解决方法

    springboot springmvc 抛出全局异常解决方法 参考文章: (1)springboot springmvc 抛出全局异常解决方法 (2)https://www.cnblogs.com/ ...

  8. java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明)...

    转载地址:http://www.devba.com/index.php/archives/4581.html java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明); ...

  9. 分数化成有限小数的方法_[153]各种循环小数化成分数的方法归纳

    各种循环小数化成分数的方法归纳 一.纯循环小数化分数 从小数点后面第一位就循环的小数叫做纯循环小数.怎样把它化为分数呢?看下面例题. 例1把纯循环小数化分数: 从以上例题可以看出,纯循环小数的小数部分 ...

最新文章

  1. Google Test(GTest)使用方法和源码解析——概况
  2. java中拼写xml
  3. openshift学习_在OpenShift上将JMS与JBoss A-MQ结合使用。 学习了有关远程客户端和加密的经验。...
  4. 内置函数之sorted,filter,map
  5. Solr Facet技术的应用与研究
  6. 如何在 5 分钟内读懂区块链的架构思维?
  7. vb升级工作笔记001---VB.NET升级到VB.NET 随时更新
  8. edge浏览器识别ip地址为手机号的解决办法
  9. 《Linux内核设计与实现》读书笔记 - 目录 (完结)【转】
  10. 联想用u盘重装系统步骤_保姆级重装系统教程在此
  11. angularJs解决跨域问题-最简单的完美实例
  12. case when then end.sql
  13. 地图poi数据库下载
  14. GJB438C-2021规范详解其二
  15. 游戏测试用例及游戏测试bug详解
  16. C4D预设如何安装?
  17. 今生梦一场,思念你的殇
  18. requests使用cookie模拟登陆豆瓣
  19. 妇科癌症中的肠道菌群:病因、治疗潜力
  20. 还在花钱抢票?12306 已屏蔽60款抢票软件!

热门文章

  1. boost::posix_time模块实现本地时间和 UTC 之间的转换的测试程序
  2. GDCM:衍生系列DeriveSeries的测试程序
  3. boost::contract模块日历的测试程序
  4. ITK:写一个TIFF图像
  5. ITK:在不复制数据的情况下,从图像创建样本列表。
  6. ITK:设置默认的线程数
  7. VTK:可视化之ColorSeriesPatches
  8. VTK:直线网格之RGrid
  9. VTK:Points之ExtractEnclosedPoints
  10. VTK:几何对象之Sphere