Skip to content
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

Bug Parse Date 1966-11-01 #2539

Open
1 of 4 tasks
mariofelesdossantosjunior opened this issue Nov 10, 2023 · 5 comments
Open
1 of 4 tasks

Bug Parse Date 1966-11-01 #2539

mariofelesdossantosjunior opened this issue Nov 10, 2023 · 5 comments
Labels

Comments

@mariofelesdossantosjunior
Copy link

mariofelesdossantosjunior commented Nov 10, 2023

Gson version

Version: 2.10.1

Java / Android version

Android target sdk 33

Used tools

  • Maven; version:
  • Gradle; version:
  • ProGuard (attach the configuration file please); version:
  • ...

Description

Failure in parse date '1966-11-01'

Expected behavior

Failed parsing '1966-11-01' as Date; at path $.birthday
image

If it's a day's difference from success

image

@fzanutto
Copy link

This error seems to be happening since Gson 2.9.1.
Downgrading to 2.9.0 the error does not happen and the test succeeds

Stack trace:

Failed parsing '1966-11-01' as Date; at path $.birthday
com.google.gson.JsonSyntaxException: Failed parsing '1966-11-01' as Date; at path $.birthday
	at com.google.gson.internal.bind.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:165)
	at com.google.gson.internal.bind.DefaultDateTypeAdapter.read(DefaultDateTypeAdapter.java:148)
	at com.google.gson.internal.bind.DefaultDateTypeAdapter.read(DefaultDateTypeAdapter.java:48)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:161)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:266)
	at com.google.gson.Gson.fromJson(Gson.java:1058)
	at com.google.gson.Gson.fromJson(Gson.java:1016)
	at com.google.gson.Gson.fromJson(Gson.java:959)
	at com.google.gson.Gson.fromJson(Gson.java:927)

@Marcono1234
Copy link
Collaborator

The exception should have a cause ("Caused by: ..."), could you please include that part of the stack trace as well?

I assume #2134 might be responsible for this issue, but I could not reproduce it yet locally, for me no exception occurred.
For testing I used the following code, does that cause the exception for you as well1?

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
System.out.println(gson.fromJson("1966-11-01", Date.class));

Could you also please provide the output of this?

System.out.println(Locale.getDefault());
System.out.println(TimeZone.getDefault());

Maybe the default locale or timezone on your PC somehow affects the date parsing. Could be related to #1511, that your default timezone does not consider 1966-11-01 to be a valid date (?).

Footnotes

  1. In the future please also include the reproduction code as text in the issue, not just as screenshot. That makes it easier to test it.

@mariofelesdossantosjunior
Copy link
Author

mariofelesdossantosjunior commented Nov 13, 2023

Hello, The problem only occurs when running on a device, or via Roboletric in tests.

Execute:

System.out.println(Locale.getDefault());
System.out.println(TimeZone.getDefault());

Result:

pt_BR
sun.util.calendar.ZoneInfo[id="America/Sao_Paulo",offset=-10800000,dstSavings=0,useDaylight=false,transitions=93,lastRule=null]

@Marcono1234
Copy link
Collaborator

Marcono1234 commented Nov 13, 2023

Thanks! This seems to be caused indeed by your default timezone. If I add the following to the test code above

TimeZone.setDefault(TimeZone.getTimeZone("America/Sao_Paulo"));

It fails with:

Exception in thread "main" com.google.gson.JsonSyntaxException: Failed parsing '1966-11-01' as Date; at path $
	at com.google.gson.internal.bind.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:164)
	at com.google.gson.internal.bind.DefaultDateTypeAdapter.read(DefaultDateTypeAdapter.java:147)
	at com.google.gson.internal.bind.DefaultDateTypeAdapter.read(DefaultDateTypeAdapter.java:47)
	at com.google.gson.Gson.fromJson(Gson.java:1227)
	at com.google.gson.Gson.fromJson(Gson.java:1137)
	at com.google.gson.Gson.fromJson(Gson.java:1047)
	at com.google.gson.Gson.fromJson(Gson.java:982)
	at GsonTest4.main(GsonTest4.java:251)
Caused by: java.text.ParseException: Failed to parse date ["1966-11-01"]: HOUR_OF_DAY: 0 -> 1
	at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:279)
	at com.google.gson.internal.bind.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:162)
	... 7 more
Caused by: java.lang.IllegalArgumentException: HOUR_OF_DAY: 0 -> 1
	at java.base/java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2790)
	at java.base/java.util.Calendar.updateTime(Calendar.java:3411)
	at java.base/java.util.Calendar.getTimeInMillis(Calendar.java:1805)
	at java.base/java.util.Calendar.getTime(Calendar.java:1776)
	at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:160)
	... 8 more

Not sure if this can be fixed in any way other than reverting #2134 1, which would mean Gson would fail to detect dates which are invalid for all timezones, or fixing #1511, which might negatively affect other users.

As pointed out in the discussion in #2480, it might be better if you switched from java.util.Date to java.time.LocalDate for parsing your date in yyyy-MM-dd format (and write a custom TypeAdapter for LocalDate because Gson has currently no built-in one).

@eamonnmcmanus, what do you think?

Footnotes

  1. However, the question would be then if that malformed Date for 1966-11-01 with America/Sao_Paulo timezone behaves correctly, or if you will get incorrect results when trying to use it, since this does not represent a valid date apparently.

@ivanroldanannoson
Copy link

Getting same error for 1982-01-01 and 1933-01-01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants