Json DateTime在Android中解析

Rob*_*Ray 7 datetime android json android-parser

我的API给出了这个结果:

{
    "Posts": [{
        "UseName": "Robert Ray",
        "DateTime": "\/Date(1376841597000)\/",
        "PostText": "Welcome!!\u003cbr\u003eThis Is my Text"
    }]
}
Run Code Online (Sandbox Code Playgroud)

我像这样解析我的JSON

try {
    Posts = json.getJSONArray(TAG_POSTS);

    for(int i = 0; i < Posts.length(); i++){
        JSONObject c = Posts.getJSONObject(i);
        String post_text = c.getString("PostText"); 

        String strDate = "2013-05-15T10:00:00-0700";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        Date date = (Date) dateFormat.parse(c.getString("DateTime"));
    }
} catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
} catch (ParseException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
} 
Run Code Online (Sandbox Code Playgroud)

但它不工作,我想要的是它应该计算自用户发布帖子以来经过的时间,
即我们必须用JSON中的日期减去当前数据,然后在几分钟/小时内转换它

4gu*_*71n 5

好吧,我希望你觉得这很有用:

String ackwardDate = "/Date(1376841597000)/";
//Dirty convertion
Calendar calendar = Calendar.getInstance();
String ackwardRipOff = ackwardDate.replace("/Date(", "").replace(")/", "");
Long timeInMillis = Long.valueOf(ackwardRipOff);
calendar.setTimeInMillis(timeInMillis);
System.out.println(calendar.getTime().toGMTString()); //Prints 18 Aug 2013 15:59:57 GMT
Run Code Online (Sandbox Code Playgroud)

但我是说,你的 JSON 中的日期格式真的很糟糕,以google为例,对于正确的、标准的、解析的 JSON。