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);
        }
  • 相关阅读:
    (QR14)带权的DAG节点排序
    数字组合
    最长连续不重复子序列
    树状数组
    归并排序
    差分
    前缀和
    64位整数乘法
    MySQL8 常用指令
    离线及实时实操架构
  • 原文地址:https://www.cnblogs.com/xinting/p/12536128.html
Copyright © 2011-2022 走看看