GSON tip: Setting ISO 8601 Date Time format with nano second precision


Today, I had to parse an ISO 8601 date that was returned as part of a JSON response from a REST service. With default settings GSON can’t parse the date like 2015-09-22T17:21:32.70856998Z. To make it work, use the code shown below.

Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                .setPrettyPrinting()
                .create();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s