question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DateTypeAdapter deserialization fails based on user's 24 hour preference

See original GitHub issue

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, using DateTypeAdapter.deserializeToDate, the following exception was thrown:

java.lang.NumberFormatException: Invalid number: Sep 
    at com.google.gson.internal.bind.util.ISO8601Utils.parseInt(ISO8601Utils.java:311)
    at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:129)

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.

  1. Create a 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).
  2. Serialize the date object using DateTypeAdapter.write, observe that the output is either the string above or Sep 26, 2016 6:13:24 PM depending on your device’s 24 hour time settings.
  3. Go to device date & time settings.
  4. Switch to using 24-hour format (or vice versa)
  5. Try to deserialize the date string using 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 expected
  • DateTypeAdapter.deserializeToDate -> Sep 26, 2016 6:13:24 PM -> throws an exception

If you are using the 12-hour format

  • DateTypeAdapter.deserializeToDate -> Sep 26, 2016 18:13:24 -> throws an exception
  • DateTypeAdapter.deserializeToDate -> Sep 26, 2016 6:13:24 PM -> works as expected

That the ISO8601Utils cannot deserialize this string does not seem to be the issue. The problem seems to be that the DateFormat object enUsFormat 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 as DateTypeAdapter as it is contains the same (de)serialization logic for java.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.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
bercikcommented, May 8, 2021

2021 and there is still no fix, this is ridiculous…

1reaction
bercikcommented, May 26, 2021

One more thing, I’ve needed to add Locale.US inside try 24 hour format, like this:

        // Try 24 hour
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss", Locale.US);
            Date result = formatter.parse(date);
            return result;
        } catch (ParseException e) {
            e.printStackTrace();
        }

Otherwise it seems that for some users it still won’t work (I guess depending on their locale)

Read more comments on GitHub >

github_iconTop Results From Across the Web

gson failed to deserialize date time after changing from 24 ...
it seems failed to deserialize createdAt to date from shared preference if the user change their date format. here is the error.
Read more >
google-gson - Bountysource
When trying to deserialize this date ( Sep 26, 2016 18:13:24 ), after switching my device time preferences from 24 hour to 12...
Read more >
Viewing online file analysis results for 'dawntained_client.jar'
Tip: Click an analysed process below to view more details. Analysed 7 processes in total (System Resource Monitor). javaw.exe -jar "C:\dawntained_client.
Read more >
Index (client-java-api 2.0.0-beta1 API) - javadoc.io
Build full URL by concatenating base path, the given sub path and query parameters. ByteArrayAdapter() - Constructor for class io.kubernetes.client.
Read more >
Stripes... and Java Web Development Is Fun Again
based on a request-response cycle: when the user clicks something ... 24. The web.xml Configuration. Finally, as with any standard Java web application, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found