public String hello(User user, Model model)throws JsonProcessingException { ObjectMapper obj = new ObjectMapper (); obj.configure (SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false); obj.setDateFormat (new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss")); return obj.writeValueAsString (new Date ()); }
第一种方法:
使用原来的日期时间类来格式化我们的时间
1 2 3
public String hello(User user, Model model)throws JsonProcessingException { returnnew ObjectMapper ().writeValueAsString (new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss").format(new Date())); }
第二种方法:
配置我们的objectMapper;
1 2 3 4 5 6
public String hello(User user, Model model)throws JsonProcessingException { ObjectMapper obj = new ObjectMapper (); obj.configure (SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false); obj.setDateFormat (new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss")); return obj.writeValueAsString (new Date ()); }