DateFormat
time zone is not restored after parsing, affecting subsequent serialization
#2547
Labels
DateFormat
time zone is not restored after parsing, affecting subsequent serialization
#2547
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 anyparse
call it is necessary to obtain the time zone from the date format, then callparse
and in afinally
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 subsequentGson.toJson
calls.Actual behavior
A call to
Gson.fromJson
can affect the output of subsequentGson.toJson
calls in case a date format which includes the time zone in the pattern is used.Reproduction steps
The text was updated successfully, but these errors were encountered: