zoukankan      html  css  js  c++  java
  • spring boot jackson 时间转换

    Entity

    @Data
    public class ExampleLeave  implements Serializable {
        private String id;
        private String title;
        private String leaveType;
        @DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
        private Date startDate;
        @DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
        private Date endDate;
        private BigDecimal count;
        private String reason;
        private int status;
    }

    工具类

    public class JsonResult implements Serializable {
      
    
        public static <T> T convertValue(String content, Class<T> toValueType ) {
            ObjectMapper mapper = objectMapper();
            mapper.setDateFormat(new SimpleDateFormat(DateFormat.DEFAULT_PATTERN.getValue()));
            try {
                return mapper.readValue(content, toValueType);
            } catch (IOException e) {
                e.printStackTrace();
                log.error(e.toString());
                return null;
            }
        }
        private static ObjectMapper objectMapper(){
            ObjectMapper mapper = new ObjectMapper();
            mapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            return mapper;
        }
    }

    Controller

        @RequestMapping(value = "/save", method = RequestMethod.POST)
        @ResponseBody
        public void save(String  leave) {
           ExampleLeave exampleLeave= JsonResult.convertValue(leave,ExampleLeave.class);
        }
  • 相关阅读:
    SpringCloud-sleuth-zipkin链路追踪
    关于encodeURI() 踩的坑
    兄弟ifream的方法调用
    jq为什么能用$操作
    js获取一周的日期范围
    layui中实现上传图片压缩
    input预览上传图片
    js获取地址栏参数
    计算两天之间的天数差
    文字始终均匀分布整个div
  • 原文地址:https://www.cnblogs.com/xinting/p/12536128.html
Copyright © 2011-2022 走看看