zoukankan      html  css  js  c++  java
  • springboot 日期参数前后台转换问题

    方式 一:

    在实体类上加@DatetimeFormat与@JsonFormat注解

    @DatetimeFormat:将前台日期字符串转换成Date格式 @DateTimeFormat(pattern="yyyy-MM-dd")

    @JsonFormat:将服务器端Date日期转换成指定字符串格式 @JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")

    两个需要同时加,否则会有时区的问题


    方式二:

    在applicition.properties中添加如下配置

    #时间戳统一转换
    spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
    spring.jackson.time-zone=GMT+8


    /**
    * 创建时间
    */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    Date createTime;
    /**
    * 修改时间
    */
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    Date updateTime;


    jdbc-url: jdbc:mysql://47.103.114.48:3306/box_mall?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT%2B8&useSSL=false&autoReconnect=true&failOverReadOnly=false
     
     
    @Bean
    public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    //设置日期格式
    ObjectMapper objectMapper = new ObjectMapper();
    SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd HH:ss");
    objectMapper.setDateFormat(smt);
    mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
    //设置中文编码格式
    List<MediaType> list = new ArrayList<MediaType>();
    list.add(MediaType.APPLICATION_JSON_UTF8);
    mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
    return mappingJackson2HttpMessageConverter;
    }
    小蚊子大人
  • 相关阅读:
    cnblogs blogStats All In One
    ESLint & vue template indent validate All In One
    vue & elementui 表单验证 bug All In One
    vue 表单验证 rule.message bug All In One
    vue 表单验证 rule message bug All In One
    Node.js & TypeScript error All In One
    VS2010如何调试IIS上的网站
    用LINQ查询XML并绑定给GridView显示
    SQLServer2008评估期已过解决方法
    ASP.NET给用户控件(.ascx)增加属性
  • 原文地址:https://www.cnblogs.com/ywsheng/p/14964288.html
Copyright © 2011-2022 走看看