22

I'm getting an exception using Gson to deserialize json.

java.text.ParseException: Unparseable date: "2011-10-19T23:30:00-04:00"

The 23:30:00 part does seem strange to me. Is this an invalid date?

LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
  • possible duplicate of [GSON deserializing key-value to custom object](http://stackoverflow.com/questions/5845822/gson-deserializing-key-value-to-custom-object) – Matt Ball Oct 19 '11 at 04:40
  • Thanks @MattBall! Could you provide an example of how I might plug in custom deserialization when I'm deserializing a class (with nested classes inside), of which one of the fields is this Date object? I'm a bit confused about how to do it. – LuxuryMode Oct 19 '11 at 04:58
  • The question of how to implement such custom deserialization was covered in http://stackoverflow.com/questions/7883615/custom-deserialization-in-gson. This thread looks like it should be closed. – Programmer Bruce Nov 12 '11 at 07:21

1 Answers1

58

You have to define the date Format in the GsonBuilder, something like this.

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

Regards!

aram.salinas
  • 616
  • 7
  • 4
  • 4
    your answer keeps on giving. Was deserializing a .net date in a json string into a java Date field and was getting a parsing exception. I tripped over your answer here and it solved my problem. thanks. – Dean Blakely Aug 27 '13 at 21:12