Json DateTime Parsing In Android
Asked Answered
C

4

7

My API Gives This Result:

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

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

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

Curl answered 17/11, 2013 at 20:55 Comment(3)
This JSON is malformed. I expect this throws a JSON exception?Knockabout
yes it throws exception!! can you please help me fixing the issue?Curl
i have corrected the Json, Please have a look and help if possible @popovitsCurl
S
5

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

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.

Schmidt answered 18/11, 2013 at 2:9 Comment(2)
Thanks It Worked!! if possible please let me know how to convert the time In AM/PM Format and what if i have to convert it in other Time Zones.Curl
You know, this solution is not perfect. Let's say you get the string in some field like: "I met a great chick. Date(which was set to tomorrow morning) didn't went that great though." This is just a sample, but I hope you get what is problematic here. Consider using some better tool for parsing the JSON, e.g. gson, which will do most of the heavy-lifting for you and only thing for you will be actually using correct regex to get the date from the field.Dimerous
C
2

Well I hope that you find this usefull:

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);
}
Constancy answered 28/2, 2014 at 14:19 Comment(0)
V
1

This will work For Sure

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
Vallombrosa answered 29/10, 2014 at 9:1 Comment(0)
R
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();
          }
Rhnegative answered 20/7, 2015 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.