SpringMVC日期类型转换问题的几种处理方法

来源:互联网 发布:iphone6splus精仿淘宝 编辑:程序博客网 时间:2024/05/20 16:09

方法一:实体类中加日期格式化注解

  1. @DateTimeFormat(pattern = "yyyy-MM-dd")  
  2. private Date receiveAppTime;  
如上,在对应的属性上,加上指定日期格式的注解,,轻松解决问题!

方法二:控制器Action中加入一段数据绑定代码

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

下面的可以同时日期和时间
// 自定义类型转换器  @InitBinder  public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {            binder.registerCustomEditor(Date.class,              new CustomDateEditor(new SimpleDateFormat("YYYY-MM-DD hh:mm:ss"), true));  } 

方法四:适合页面把日期类型转换成字符串且JSP,Freemark页面

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

匹配时间最详细的还是这个:yyyy-MM-dd HH:mm:ss
出来的结果就是类似:2019-07-07 18:34:01



阅读全文
0 0
原创粉丝点击