-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DateTypeAdapter deserialization fails based on user's 24 hour preference #935
Comments
I was about to file the same issue. In versions below 2.5 the exception is this one: You get it this way:
The problem is that in Android |
Can you try using this type adapter: https://github.com/google/gson/blob/master/extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java |
+1'ing this post as I spent the morning tracking down an exception being thrown due to this as well. As per the above suggestion, I took UtcDateTypeAdapter.java and then added in the case to handle and parse out |
iander123: This adapter gives the same error |
I am facing the same issue with DateTypeAdapter throwing a JsonSyntaxException. I have a Java Date Object like this "Sat May 13 00:00:00 GMT+02:00 2017" and save this via Gson via new GsonBuilder().create().toJson(date) with the following json result "May 13, 2017 00:00:00". If I want to retrieve my Date object from json I get the JsonSyntaxException: com.google.gson.JsonSyntaxException: May 9, 2017 00:00:00 Any solutions to for this? |
@wman1980 I modified this adapter here - see lines 241+. The file itself won't run as-is because I pulled out some project specific logging includes, but you should be able to re-use the chunk of code I added. |
@ssawchenko that works. Thanks. |
@ssawchenko may i know where can i put that adapter in my application and how to specify our date object in my class . Please reply ASAP. Thanks in advance. |
This problem can be solved by using a Custom TypeAdapter. Example:
|
Is there any official solution to this problem by GSON library? |
Yes. Avoid the built-in support for Dates. |
I had to use my own deserializer, because the UtcDateTypeAdapter added timezone differences to the date, e.g. +1 hour etc. So I am using now my own:
Maybe it helps. |
2021 and there is still no fix, this is ridiculous... |
@wman1980 I've tried your solution and it works. I've created GsonProvider and use it instead of doing
Usage:
|
One more thing, I've needed to add Locale.US inside try 24 hour format, like this:
Otherwise it seems that for some users it still won't work (I guess depending on their locale) |
The proposed The reason appears to be that |
I've encountered a bug affecting (de)serialization of dates on Android devices using gson 2.7.
Description/Stacktrace
When trying to deserialize this date (
Sep 26, 2016 18:13:24
), after switching my device time preferences from 24 hour to 12 hour, usingDateTypeAdapter.deserializeToDate
, the following exception was thrown:Steps to reproduce
Reproduced on a Nexus 5X running Android 7.0. I have also seen this crash reported from varying devices and Android versions.
java.util.Date
object at the following time:Sep 26, 2016 18:13:24
(any date should work, but I will use this date for the example).DateTypeAdapter.write
, observe that the output is either the string above orSep 26, 2016 6:13:24 PM
depending on your device's 24 hour time settings.DateTypeAdapter.deserializeToDate
and observe the exception above being thrown.If you are using the 24-hour format
DateTypeAdapter.deserializeToDate
->Sep 26, 2016 18:13:24
-> works as expectedDateTypeAdapter.deserializeToDate
->Sep 26, 2016 6:13:24 PM
-> throws an exceptionIf you are using the 12-hour format
DateTypeAdapter.deserializeToDate
->Sep 26, 2016 18:13:24
-> throws an exceptionDateTypeAdapter.deserializeToDate
->Sep 26, 2016 6:13:24 PM
-> works as expectedThat the ISO8601Utils cannot deserialize this string does not seem to be the issue. The problem seems to be that the
DateFormat
objectenUsFormat
is used by default to serialize the Date object. Then when it deserializes it, it tries to format it using this same DateFormat, if that fails it's falling back to another DateFormat and finally the ISO8601Utils, which also will fail because it's not meant to handle this format. When the 24-hour time preference is switched, the DateFormat object no longer can correctly format the serialized string, and therefore the adapter falls back to ISO8601Utils which throws an error.DefaultDateTypeAdapter
also appears to have the same problem asDateTypeAdapter
as it is contains the same (de)serialization logic forjava.util.Date
objects.A workaround I am using is to create a TypeAdapter to handle dates and to use ISO8601Utils directly rather than default to using DateFormat so that the serialized string will be agnostic of the user's time preferences.
The text was updated successfully, but these errors were encountered: