前台传输日期类型到后台,数据库取出日期类型按照自定义格式输出到前台

版权声明:转载说明出处,谢谢。 https://blog.csdn.net/Black_PL/article/details/80845740

前台传输到后台的日期为string类型,而后台domain定义日期为date类型

前台传给后台(保证传输过来的是日期的格式)

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

时间格式要跟前台传递的字符串完全对应,这样就可以保证时间前端的字符串在后台可以正常的解析成date 否则就是提示说无法把string转换成时间类型的错误。

在后台返回的domain对象中的时间类型的数据的字段上加上如下注解

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
指明时间的格式跟时区。

--------------------------------------------------------------------------------------------------------------------

数据库取出日期类型按照自定义格式输出到前台(解决数据库取出的时间跟我们想要的时间格式不一样)

用jsp的语法解决,引入jsp工具包

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<fmt:formatDate value="${model中的时间数据}" pattern="yyyy-MM-dd"/>



猜你喜欢

转载自blog.csdn.net/Black_PL/article/details/80845740