0

Hi in a JSON response i have the two dates, "2016-03-29T00:00:00+01:00" and "2016-03-27T00:00:00Z" in this structure something like [{date:2016-03-29T00:00:00+01:00},{date:2016-03-27T00:00:00Z}] how to parse this using Gson or GsonBuilder. Please keep in mind using android.

Thanks

user1796624
  • 3,665
  • 7
  • 38
  • 65
  • 1
    these are iso8601 compliant time stamps there is 660kb library for parsing strings like these called joda time gson is a json parser not a timestamp parser – Bhargav Dec 16 '15 at 17:49
  • 1
    Possible duplicate of [Converting ISO 8601-compliant String to java.util.Date](http://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date) – 1615903 Dec 16 '15 at 18:03
  • If this is your json structure then your json is not a valid json.. – Chintan Soni Dec 17 '15 at 05:16

2 Answers2

0

these are iso8601 compliant time stamps there is 660kb library for parsing strings like these called joda time gson is a json parser not a timestamp parser, you can take a look at this answer for converting iso8601 time stamps to java.util.date , both the question and the answer explain in detail how to do this.

If the format of the date string isn't going to change then for now you could try this.

SimpleDateFormat ISO8601DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
Date date = ISO8601DATEFORMAT.parse(date);

I personally had been using the format yyyy-MM-dd'T'HH:mm:ss.SSSZ for quite a while to parse iso8601 timestamps, but have now moved to using joda time.

Community
  • 1
  • 1
Bhargav
  • 8,118
  • 6
  • 40
  • 63
0

There is setDateFormat() method in GsonBuilder where you can specify date format you need

Yrgl
  • 643
  • 1
  • 6
  • 19