2

I am getting following exception -

com.google.gson.JsonSyntaxException: 04/03/2017 10:39:55.000
at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:107)

I have set the date format as -

Gson gson = new GsonBuilder().setDateFormat("MM/dd/yyyy hh:mm:ss a").create();

And I don't have control on the date format. How can I setup GsonBuilder to handle 2 different time formats so that the above exception can be avoided?

EDIT - In another instance the Time will appear as 04/03/2017 10:39:55 AM That's why I need to handle 2 different time formats.

Saikat
  • 14,222
  • 20
  • 104
  • 125

1 Answers1

3

The date format that you supplied ("MM/dd/yyyy hh:mm:ss a") does not match the date format of your input (04/03/2017 10:39:55.000). Your input has milliseconds after the seconds, and no AM/PM notation. Correct dateformat is:

setDateFormat("MM/dd/yyyy hh:mm:ss.SSS");
1615903
  • 32,635
  • 12
  • 70
  • 99