在Java中将Json日期时间转换为字符串

3
我有以下几个列在mysql中: CreatedTime (时间戳), UpdatedTime (时间戳), Status (文本类型), AutoID (自动编号)。
通过我的服务,我得到了以下的JSON格式数据。
 {
        "CreatedTime": "\/Date(1457646424000)\/",
        "UpdatedTime": "\/Date(1457647761000)\/",
        "Status": "Open",
        "AutoID": 1
 }

我想将CreatedTime和UpdatedTime转换成如下格式:
public String ConvertJsonDateTime(String jsondate)
{
    if (jsondate != "" && !jsondate.equals("") && jsondate != null && jsondate != "null") {
        jsondate = jsondate.replace("/Date(", "").replace(")/", "");
        long time = Long.parseLong(jsondate);
        Date d = new Date(time);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d).toString();
    } else {
        return "";
    }
}

但是它返回了错误的细节。例如,在表格中,CreatedTime2016-03-10 14:47:04,但是函数ConvertJsonDateTime返回为2016-03-11 03:17:04。我该如何修复这个问题?

1
可能你需要为你的 SimpleDateFormat 设置正确的 TimeZone - Erich Kitzmueller
1个回答

0

你需要设置正确的时区

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT")); // GMT or any timezone

另外,正如Bart所说,你永远不应该使用==!=来比较字符串,而是应该始终使用equals()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接