SpringMVC json automatically converts the date type to long, springmvcjson
This morning, I encountered a strange problem. I sent a request directly to the background and returned the page information. The displayed time is normal, for example, 15:42:11. 0, but the time displayed in the information obtained through AJAX is actually a timestamp.
First, I checked whether the time type in the collection returned by the background is a timestamp. When the result is hit with a breakpoint, the time displayed in the collection is also normal. After I press f8, check that the JSON file sent back by the browser is actually a timestamp. I don't believe it, so I repeat it several times. It is true that the set is displayed normally, and a timestamp is generated when a response is sent to the front-end, so I added the json annotation (@JsonFormat
(pattern=
"yyyy-MM-dd HH:mm:ss"
,timezone =
"GMT+8"
)
), But I can't use it, because at present this product does not use the ROM framework, that is, there is no entity. After being depressed, I tried to format it myself.
private List<Map<String,Object>> formatDate(List<Map<String,Object>> list){ List<Map<String,Object>> l = new ArrayList<Map<String,Object>>(); for (Map<String, Object> map : list) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if(map.get("add_time")!=null){ String add_time = map.get("add_time").toString(); try { Date date = sdf.parse(add_time); map.put("add_time", sdf.format(date)); } catch (ParseException e) { e.printStackTrace(); } } l.add(map); } return l; }
^ _ ^, The format is not converted after completion. After careful check, I found that the time, minute, and second in the original set is still 15:42:11 in milliseconds. 0. If JSON is formatted in milliseconds, the time is not automatically converted to the timestamp.
This problem has been solved successfully!