narayana1208 / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

date format problem #199

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What is the expected output? What do you see instead?
expected:'2010-02-02' actually:{"nanos":0} or null

What version of the product are you using? On what operating system?
version=1.4   system=windows 7

Please provide any additional information below.
when I select a date field from the database and output the string with
Gson,and the date field displays follow :{"nanos":0} or null, but my
database has date data in it.

Original issue reported on code.google.com by wuguangjian on 21 Mar 2010 at 7:22

GoogleCodeExporter commented 9 years ago
Сonfirm this bug:
------------
Gson g = new GsonBuilder().create();//setDateFormat("'Date('yyyy-MM-
dd'T'HH:mm:ss.SSSZ')'").create();
throw new Exception(g.toJson(item.shiftBegin) + "----" + 
item.shiftBegin.toString());
------------
"{nanos:0}----2010-05-03 00:00:00.0"

Original comment by saph...@gmail.com on 5 May 2010 at 6:49

GoogleCodeExporter commented 9 years ago
It's not bug :)
returning field from database is "java.sql.Timestamp"
solution:

...
    static private class sqlTimestampConverter implements JsonSerializer<Timestamp> {
        static SimpleDateFormat sdf = new SimpleDateFormat("'Date('yyyy-MM-
dd'T'HH:mm:ss.SSSZ')'");

        @Override
        public JsonElement serialize(Timestamp src, Type srcType, 
JsonSerializationContext context) {
            return new JsonPrimitive(sdf.format(src));
        }
    }
...
        GsonBuilder gson = new GsonBuilder().setDateFormat("'Date('yyyy-MM-
dd'T'HH:mm:ss.SSSZ')'");
        gson.registerTypeAdapter(Timestamp.class, new sqlTimestampConverter());

Original comment by saph...@gmail.com on 7 May 2010 at 8:39

GoogleCodeExporter commented 9 years ago
Thanks saphsys.

Original comment by limpbizkit on 3 Sep 2010 at 6:34