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

DateFormat time zone is not restored after parsing, affecting subsequent serialization #2547

Closed
Marcono1234 opened this issue Nov 19, 2023 · 2 comments · Fixed by #2549
Closed
Labels

Comments

@Marcono1234
Copy link
Collaborator

Gson version

2.10.1

Java / Android version

Java 17

Description

When deserializing a java.util.Date (and probably any of its subtypes) using a pattern with time zone, subsequent serialization using the same pattern is affected by it and uses the same time zone which was provided during deserialization. This can lead to undesired and unexpected behavior (e.g. one user can influence the date format of another user).

The underlying issue is that DateFormat.parse / SimpleDateFormat.parse can change the time zone of the instance, but does not restore it afterwards, see JDK-6609675. So before any parse call it is necessary to obtain the time zone from the date format, then call parse and in a finally block restore the time zone again.
Based on the documentation it seems only the time zone is affected by this.

Expected behavior

A call to Gson.fromJson should not affect subsequent Gson.toJson calls.

Actual behavior

A call to Gson.fromJson can affect the output of subsequent Gson.toJson calls in case a date format which includes the time zone in the pattern is used.

Reproduction steps

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm z").create();
Date date = new Date(0);

// As expected: 1970-01-01 00:00 UTC
System.out.println(gson.toJson(date));

gson.fromJson("\"1970-01-01 00:00 PST\"", Date.class);

// Unexpected: 1969-12-31 16:00 PST
System.out.println(gson.toJson(date));
@chrustkiran
Copy link

shall I check this issue?

@Marcono1234
Copy link
Collaborator Author

Do you mean whether you can submit a pull request fixing this bug?

No sorry, someone else is already working on a fix for this in #2549. There is probably no point in two people concurrently trying to fix this. But thank you anyway for the offer!

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

Successfully merging a pull request may close this issue.

2 participants