zoukankan      html  css  js  c++  java
  • Spring MVC对日期处理的不友好问题

    一、后台日期类型解析到前端

    1.在springmvc的配置文件中添加这个.annotation-driven在配置文件中只配置一次     (此方法全局作用)
    <mvc:annotation-driven>
    <mvc:message-converters>
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="objectMapper">
    <bean class="com.fasterxml.jackson.databind.ObjectMapper">
    <property name="dateFormat">
    <bean class="java.text.SimpleDateFormat">
    <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
    </bean>
    </property>
    </bean>
    </property>
    </bean>
    </mvc:message-converters>
    </mvc:annotation-driven>

    2.直接在字段上写上  
    @JsonFormat(pattern="yyyy-MM-dd",timezone="GMT-8")
    private Date inputtime;

    二、前端日期类型传到后台

    1.在Controller类中添加这个方法      (此方法全局作用)
    @InitBinder
    public void initBinder(WebDataBinder binder){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }


    2.直接在字段上添加
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date beginDate;

  • 相关阅读:
    Orcad Pspice仿真
    AD导入Allegro brd文件(导入后找不到PCB的解决方法)
    VJTAG转VME DTB
    win10 非Unicode应用程序显示设置
    MFC多文档视图编程总结
    VC MFC开发示例下载
    FPGA仿真及时序约束分析
    VMWARE Thin APP
    VPX技术基础概论
    SecureCRT脚本(VBS)运行
  • 原文地址:https://www.cnblogs.com/wang--lei/p/6943376.html
Copyright © 2011-2022 走看看