8-SpringMVC对日期类型处理

package com.gzcgxt.date;

import java.text.SimpleDateFormat;
import java.util.Date;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gzcgxt.domain.User;

/**
 * 
 * <p>Title: DateController</p>
 * <p>Description: </p>
 * <p>Company: www.baidu.com</p> 
 * @author    刘诗华 微信:15390725037
 * @date    2019-7-16下午7:41:12
 * @version 1.0
 * 400 数据类型转换错误
 * HTTP Status 400 -
   type Status report message
   description The request sent by the client was syntactically incorrect.
   Apache Tomcat/7.0.57
 */
@Controller
@RequestMapping("/date")
public class DateController {

    /**
     * 第一种:直接注入date类型
     * <p>Title: date1</p>
     * <p>Description: </p>
     * @param d
     * @return
     * 我们得在形参上面设置注解标签
     * @DateTimeFormat(pattern="yyyy-MM-dd")
     */
    @RequestMapping("/date1")
    public String date1(@DateTimeFormat(pattern="yyyy-MM-dd") Date d)
    {
        System.out.println(d);
        return "";
    }
    
    /**
     * 第二种:在domain对象属性上面设置格式
     * <p>Title: date2</p>
     * <p>Description: </p>
     * @param u
     * @return
     * 
     * @Data
        @NoArgsConstructor
        @AllArgsConstructor
        public class User {
            private Integer id;
            private String    name;
            private String    password;
            
            @DateTimeFormat(pattern="yyyy-MM-dd")
            private Date date;
        }
        
     */
    @RequestMapping("/date2")
    public String date2(User u)
    {
        System.out.println(12);
        System.out.println(u);
        return "";
    }
    
    
    /**
     * 第三种:使用SpringMVC数据绑定组件 webdatabind
     * <p>Title: date3</p>
     * <p>Description: </p>
     * @return
     */
    @RequestMapping("/date3")
    public String date3(User u)
    {
        System.out.println(u);
        return "";
    }
    
    @InitBinder
    public void initBindDateType(WebDataBinder binder)
    {
        //时间格式对象
        SimpleDateFormat sdf=new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd");
        //new CustomDateEditor(sdf, true)  是否允许为空
        binder.registerCustomEditor(Date.class,new CustomDateEditor(sdf, true));
    }
    
}

 

通用型日期格式

package com.gzcgxt.date;

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.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

/**
 * 通用时间格式处理 在项目中都可以使用 不用单独配置某个字段格式
 * <p>Title: DateTimeFormatController</p>
 * <p>Description: </p>
 * <p>Company: www.baidu.com</p> 
 * @author    刘诗华 微信:15390725037
 * @date    2019-7-16下午7:57:12
 * @version 1.0
 * <context:component-scan base-package="com.gzcgxt"/>  一定要放在这个包下面,才可以扫描到
 */

@ControllerAdvice
public class DateTimeFormatAdvice {
    
    @InitBinder
    public void initBindDateType(WebDataBinder binder)
    {
        //时间格式对象
        SimpleDateFormat sdf=new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd");
        //new CustomDateEditor(sdf, true)  是否允许为空
        binder.registerCustomEditor(Date.class,new CustomDateEditor(sdf, true));
    }
    
}

 

在jsp前台显示date格式

引入jsp标签库 在tomcat服务器中

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<!--引入标签库-->
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Date格式化</title>
</head>
<body>

${u}
<br/><br/>

时间一:<fmt:formatDate value="${u.date}" pattern="yyyy/MM/dd"/><br/>
时间二:<fmt:formatDate value="${u.date}" pattern="yyyy-MM-dd"/><br/>
时间三:<fmt:formatDate value="${u.date}" pattern="yyyy:MM:dd"/><br/>

</body>
</html>

 

 

 

后台以json格式返回数据类型,会转换成时间戳 这样子我们还要在前台再次转换一下 比较麻烦,所以我们要在返回的时间就处理好显示格式

package com.gzcgxt.date;

import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gzcgxt.domain.User;

@Controller
public class JsonDateController {
    
    @RequestMapping("/getUser")
    @ResponseBody
    public User getUser()
    {
        User u=new User();
        u.setId(28);
        u.setName("刘诗华");
        u.setPassword("123456");
        u.setDate(new Date());
        return u;
    }
}

 

 

解决方案 在User对象上面加注解标签 

package com.gzcgxt.domain;

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Integer id;
    private String    name;
    private String    password;
    
    //在这里,设置东八区时间  时间才会显示正确
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")  
    private Date date;
}

 

posted @ 2019-07-16 20:00  往事只能回味---  阅读(453)  评论(0)    收藏  举报