Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JSON date in the "mm dd yyyy" format

Tags:

json

android

In my application the Date Fiend JSON response comes as this type:

 "CreatedOn": "\/Date(1313572467987+0000)\/"

I want to convert this date in the "MM DD YYYY" format. How can I convert this?

like image 554
Hardik Gajjar Avatar asked Aug 18 '11 07:08

Hardik Gajjar


People also ask

Does JSON have a date type?

JSON does not have a built-in type for date/time values. The general consensus is to store the date/time value as a string in ISO 8601 format.

Does JSON support date format?

JSON does not directly support the date format and it stores it as String. However, as you have learned by now that mongo shell is a JavaScript interpreter and so in order to represent dates in JavaScript, JSON uses a specific string format ISODate to encode dates as string.


2 Answers

That date in your JSON response looks like a standard timestamp (e.g. number of milliseconds since January 1, 1970). If you parse out the timestamp something like:

    String timestamp = jsonValue.split("\\(")[1].split("\\+")[0];
    Date createdOn = new Date(Long.parseLong(timestamp));

Now you can use SimpleDateFormat to format a date string:

    SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy");
    String formattedDate = sdf.format(createdOn);

This is ignoring the timezone adjustment in that response (the '+0000') you might also want to parse out this value and add/remove the hours from your timestamp value before formatting..

like image 123
Jer Avatar answered Sep 29 '22 16:09

Jer


you can use SimpleDateFormat, so just try to search on the same.

Detailed example is here: http://www.helloandroid.com/tutorials/date-handling-android-development

like image 21
Paresh Mayani Avatar answered Sep 29 '22 16:09

Paresh Mayani