Home  >  Q&A  >  body text

java - 转换json格式时间乱码问题

http://img.mukewang.com/58468...

private static String dateFormat;
private static SerializeConfig mapping = new SerializeConfig();
static {
    dateFormat = "yyyy-MM-dd HH:mm:ss";
    mapping.put(Date.class, new SimpleDateFormatSerializer(dateFormat));
}
String jsonString = JSONObject.toJSONString(deal,mapping,SerializerFeature.WriteDateUseDateFormat);

这个方法是把jsonString放入redis中,但有两个方法同时放一个redis中,这两个方法的deal实体类中的属性还不相同,其他代码都一样,不知道是不是这个问题,希望有大神给解释一下

伊谢尔伦伊谢尔伦2765 days ago691

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-18 10:28:14

    It seems that the thread of SimpleDateFormat is not safe, and multi-thread concurrency will cause this

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:28:14

    It doesn’t feel necessary to be so complicated, it’s very simple to use fastjson

            Map<String, Object> map = new HashMap<>();
            map.put("createTime", new Date());
            map.put("name", "simeon");
            String jstr = JSON.toJSONStringWithDateFormat(map, "yyyy-MM-dd HH:mm:ss");

    reply
    0
  • Cancelreply