1

Basically, MySQL DATETIME data type's format is this one: "2014-02-14 16:43:04" but when deserializing with Gson I get a Unparseable date: "2014-02-14 16:43:04".

I'm guessing I'm using an incorrect object for this, I'm using java.sql.Timestamp; what is the correct object for this, or is it that I need to configure some date format to Gson? if so, how to?

Edit: What happens with the setDateFormat() if I use DATE (yyyy-mm-dd) and also DATETIME(yyyy-mm-dd hh:mm:ss), will any of them be affected?

Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206

2 Answers2

4

Try using a Gson setDateFormat like this:

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

Pretty sure you need a regular Date object as well.

Durandal
  • 5,575
  • 5
  • 35
  • 49
  • if I use DATE (yyyy-mm-dd) and also DATETIME(yyyy-mm-dd hh:mm:ss), will any of them be affected? – Christopher Francisco Feb 18 '14 at 06:41
  • If you've got multiple date formats then you'll have to get a little more creative. You can write a custom deserializer like in this answer: http://stackoverflow.com/a/18337283/772385 – Durandal Feb 18 '14 at 06:43
1

You can define a custom dateformat for Gson by using the GsonBuilder class. This has a method setDateFormat where you can set your own format (which will be "yyyy-MM-dd HH:mm:ss" in your case).

Toon Borgers
  • 3,638
  • 1
  • 14
  • 22