我有以下几个列在mysql中:
CreatedTime (时间戳),
UpdatedTime (时间戳),
Status (文本类型),
AutoID (自动编号)。
通过我的服务,我得到了以下的JSON格式数据。
我想将CreatedTime和UpdatedTime转换成如下格式:
但是它返回了错误的细节。例如,在表格中,
通过我的服务,我得到了以下的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 "";
}
}
但是它返回了错误的细节。例如,在表格中,
CreatedTime
为2016-03-10 14:47:04
,但是函数ConvertJsonDateTime
返回为2016-03-11 03:17:04
。我该如何修复这个问题?
SimpleDateFormat
设置正确的TimeZone
。 - Erich Kitzmueller==
或!=
来比较字符串:https://dev59.com/DnRB5IYBdhLWcg3wyqEd。并且在表达式中将`jsondate!=null`放在最左边:如果它为空,`!jsondate.equals("")`将抛出NPE。 - Bart Kiers