JSON解析的DateTime Android的中 [英] Json DateTime Parsing In Android

查看:275
本文介绍了JSON解析的DateTime Android的中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的API给出了这样的结果:

My API Gives This Result:

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

我分析我的JSON像这样

I Parse My JSON Like This

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();
} 

但它不工作,我想是应该计算所经过的时间,因为用户发布的帖子,结果就是我们在JSON日期减去当前的数据,然后将其转换以分钟/小时

But Its Not Working, what I want is It should calculate the time elapsed since the User posted the Post,
i.e we have to subtract the current data with the date in JSON, and then convert that in Minutes / Hours

推荐答案

好吧,我希望你找到这个有用的:

Well I hope that you find this usefull:

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

但我说,在你的JSON日期格式这真是ackwards,采取的谷歌,对于正确,STANDAR,解析JSON。

But I'm saying, that date format in your JSON It's really ackwards, take the example of google, for a correct, standar, parsed JSON.

这篇关于JSON解析的DateTime Android的中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆