Date Parser Exception

1,149 views
Skip to first unread message

郑蔚

unread,
Aug 31, 2012, 6:58:29 AM8/31/12
to googl...@googlegroups.com
the code is:
Gson gson = new GsonBuilder().setDateFormat("EEE MMM dd HH:mm:ss Z yyyy").create();
Date d = gson.fromJson("Tue May 31 17:46:55 +0800 2011", Date.class);
 
I use these code in my android project , get the JsonSyntaxException.
But when i use these date format with SimpleDateFormat , it's works.
Any one have idea where is the problem?

the trackstack: 

Exception in thread "main" com.google.gson.JsonSyntaxException: Tue

at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:107)

at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:82)

at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:35)

at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)

at com.google.gson.Gson.fromJson(Gson.java:795)

at com.google.gson.Gson.fromJson(Gson.java:761)

at com.google.gson.Gson.fromJson(Gson.java:710)

at com.google.gson.Gson.fromJson(Gson.java:682)

at com.xiami.TestMain.main(TestMain.java:19)

Caused by: java.text.ParseException: Unparseable date: "Tue"

at java.text.DateFormat.parse(DateFormat.java:337)

at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:105)

... 8 more



 

Brandon Mintern

unread,
Aug 31, 2012, 1:31:35 PM8/31/12
to googl...@googlegroups.com
Could it be as simple as missing double quotes *within* the date
string? Shouldn't that be:

Date d = gson.fromJson("\"Tue May 31 17:46:55 +0800 2011\"", Date.class);

> --
> You received this message because you are subscribed to the Google Groups
> "google-gson" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-gson/-/Q8Q_AFLH1bkJ.
> To post to this group, send email to googl...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-gson...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.

郑蔚

unread,
Sep 2, 2012, 10:47:18 PM9/2/12
to googl...@googlegroups.com
It works in the example code .
but I need to parse the date string from some api  which I can not modify .
any other ways ? Or, I just to use the  custom deserializer ?
Reply all
Reply to author
Forward
0 new messages