博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC日期类型转换问题三大处理方法归纳
阅读量:6988 次
发布时间:2019-06-27

本文共 1485 字,大约阅读时间需要 4 分钟。

转自: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 {    @InitBinder    public void initBinder(WebDataBinder binder) {        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值,false:不能为空值    }}

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

注册全局转化器

 

具体的实现代码

public class DateConverter implements Converter
{ @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"%>       

Freemarker模版引擎方法:

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

你可能感兴趣的文章
收集网址
查看>>
python文本操作
查看>>
本周学习小结(01/04 - 07/04)
查看>>
精选CSDN的ACM-ICPC博文
查看>>
C语言的面向对象技术(转)
查看>>
C/C++读写二进制文件
查看>>
经典SQL语句(转载)
查看>>
#从零开始学SWIFT2.0# NO.5 可选变量和集合
查看>>
处理矩阵的行列转换
查看>>
java返回一个简单的日历
查看>>
java中包的命令行(cmd)操作详解
查看>>
git中fatal: Authentication failed的问题
查看>>
数学建模排版中加页码与首页不加页码问题
查看>>
matlab-1
查看>>
C# 用POST提交json数据
查看>>
cpu亲和性绑定
查看>>
变量 、缓冲值 、编码
查看>>
20160408javaweb之JDBC ---PreparedStatement
查看>>
2018.11.03-dtoj-2092-交通 (traffic)
查看>>
内置模块(二)
查看>>