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);
        }
  • 相关阅读:
    Vue.js进阶知识点总结
    测试初学
    gitee简单命令使用
    Ubuntu学习之alias命令
    Python turtle.right与turtle.setheading的区别
    Python-对Pcap文件进行处理,获取指定TCP流
    第八次作业
    第七次作业
    第八周作业
    第七周作业
  • 原文地址:https://www.cnblogs.com/xinting/p/12536128.html
Copyright © 2011-2022 走看看