0

I want to parse this json string into date

"startDateTime":"2014-08-10T20:08:45.0218Z"

and then parse it to another date format.

I thought using this:

Gson gson = new GsonBuilder().setDateFormat("dd/MM HH:mm ???").create;

but I'm not sure how what is the format of "2014-08-10T20:08:45.0218Z"

is it yyyy-mm-dd ???

Elad Benda2
  • 13,852
  • 29
  • 82
  • 157

2 Answers2

2

2014-08-10T20:08:45.0218Z

This looks like an ISO 8601 date

Date and time expressed according to ISO 8601:
Combined date and time in UTC: ... 2014-08-20T19:23:25Z

so

yyyy-MM-dd'T'HH:mm:ss.SSSZ

should do it.

Joda time also provides ISODateTimeFormat since this is a common format.

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • Joda time? built in lib in java? – Elad Benda2 Aug 22 '14 at 18:08
  • @user1065869, Joda time is a decent date/time library that many (myself included) prefer to Java's horribly broken `java.util.{Data,Calendar}` APIs. See the [docs](http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html). [Java 8](http://java.dzone.com/articles/introducing-new-date-and-time) may get a significantly revamped date/time core library based in part on Joda time. – Mike Samuel Aug 22 '14 at 18:18
  • thanks. I'll read about it. Can you please see my question here: http://stackoverflow.com/questions/25449502/gsonbuilder-setdateformat-doesnt-change-the-string-that-i-get-from-the-s maybe I the date format doesn't fit? – Elad Benda2 Aug 22 '14 at 18:24
1
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String date = sdf.format(new Date());
System.out.println(date);
Date d = sdf.parse(date);
Jan Vladimir Mostert
  • 12,380
  • 15
  • 80
  • 137