7

我的 API 给出了这个结果:

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

我像这样解析我的 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();
} 

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

4

4 回答 4

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

但我是说,你的 JSON 中的日期格式真的是 ackwards,以google为例,以获得正确的、标准的、解析的 JSON。

于 2013-11-18T02:09:16.963 回答
2

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

public static Date JsonDateToDate(String jsonDate)
{
    //  "/Date(1321867151710+0100)/"
    int idx1 = jsonDate.indexOf("(");
    int idx2 = jsonDate.indexOf(")") - 5;
    String s = jsonDate.substring(idx1+1, idx2);
    long l = Long.valueOf(s);
    return new Date(l);
}
于 2014-02-28T14:19:51.653 回答
1

这肯定会奏效

String date = "/Date(1376841597000)/";
Calendar calendar = Calendar.getInstance();
String datereip = date.replace("/Date(", "").replace(")/", "");
Long timeInMillis = Long.valueOf(datereip);
calendar.setTimeInMillis(timeInMillis);
System.out.println(calendar.getTime().toString("dd-MMM-yyyy h:mm tt"));//It Will Be in format 29-OCT-2014 2:26 PM
于 2014-10-29T09:01:15.060 回答
1
  public static  String ConvertJsonDate()
          {

            String jsondate="\/Date(1427959670000)\/";
              jsondate=jsondate.replace("/Date(", "").replace(")/", "");
                long time = Long.parseLong(jsondate);
                Date d= new Date(time);
            Log.d("Convertd date is:"+new SimpleDateFormat("dd/MM/yyyy").format(d).toString());

                return new SimpleDateFormat("MM/dd/yyyy").format(d).toString();
          }
于 2015-07-20T09:04:26.300 回答