0

I got wrong value when using Gson in some Android devices.

Below is json JSON {'Time':'1900-01-01T11:00:00.000+08:00'}

Below is my code for testing

new GsonBuilder().create().fromJson("{'Time':'1904-01-01T11:00:00.000+08:00'}", Info.class);

The result is in correct: Fri Jan 01 10:36:42 GMT+08:00 1904

new GsonBuilder().create().fromJson("{'Time':'1905-01-01T11:00:00.000+08:00'}", Info.class);

The result is correct: Sun Jan 01 11:00:00 GMT+08:00 1905

The android version is 10 (API 29), MIUI 12

May I know anybody has this issue

Eagle
  • 451
  • 2
  • 5
  • 14
  • This is definitely not a Gson bug, but the specifics of how date format works. I'm not sure but this all looks pretty close to https://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result . By the way, you don't need `Info` for the MWE: `gson.from("''", Date.class)` would work just fine. What if you also try to play around with `GsonBuilder.setDateFormat`? – terrorrussia-keeps-killing Dec 31 '20 at 07:11
  • Same code in other android device is correct. I don't know what problem exist. – Eagle Dec 31 '20 at 07:20
  • To be honest, I have no idea which one is correct (I didn't read the linked question in detail), but at my desktop machine (with Debian-derived Linux) I'm also getting "strange" results. For example, `1924-05-01T11:00:00.000+08:00` is `Thu May 01 05:02:04 EET 1924` but a day later `1924-05-02T11:00:00.000+08:00` is `Fri May 02 05:00:00 EET 1924`. My system timezone is not +08:00. Also if the date/time format omits the timezone (`yyyy-MM-dd'T'HH:mm:ss.sss`), it "starts" "working fine", showing accurate midnight time. This is the most I can tell now. – terrorrussia-keeps-killing Dec 31 '20 at 07:24
  • Can you verify that `Date.getTime()` has the same value though? `Date.toString()` is highly misleading, see https://stackoverflow.com/a/4199245, it displays the **instant** it represents as local time. As to why the time does not match exactly, it might be necessary to have a look at the historical time data. Maybe there were time or time zone adjustments around that time? – Marcono1234 Jan 02 '21 at 16:12

0 Answers0