Gillardo
Gillardo

Reputation: 9818

Convert Javascript/Json Date String to Java Date using Gson

I have a date object from javascript that is converted into a string in the format like this

2014-05-22T15:27:49.125Z

Now in my java file, i am using Gson and create my builder like so

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

To convert the date from the string value into a Date object, i am using this.

Date myDate = gson.fromJson(options.getString("dateAsString"), Date.class);

This seems to report this error?

05-22 16:29:19.484: W/System.err(24636): java.lang.IllegalArgumentException: Parse error: 2014-05-22T15:27:49.125Z

Any help appreciated

Upvotes: 1

Views: 2426

Answers (1)

Braj
Braj

Reputation: 46841

If the issue is with date format then try any one:

SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",Locale.US);

OR

SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);

Upvotes: 2

Related Questions