Parse json date string in android

2019-01-26 14:03发布

I get a json string with number of milliseconds after 1970 from the server in my android app.

Looks like this: \/Date(1358157378910+0100)\/.

How can I parse this into a Java calendar object, or just get some date value from it? Should I start with regex and just get the millisecons? The server is .NET.

Thanks

7条回答
祖国的老花朵
2楼-- · 2019-01-26 15:02

The time seems to also have the timezone there so I would do something like this:

String timeString = json.substring(json.indexOf("(") + 1, json.indexOf(")"));
String[] timeSegments = timeString.split("\\+");
// May have to handle negative timezones
int timeZoneOffSet = Integer.valueOf(timeSegments[1]) * 36000; // (("0100" / 100) * 3600 * 1000)
int millis = Integer.valueOf(timeSegments[0]);
Date time = new Date(millis + timeZoneOffSet);
查看更多
登录 后发表回答