-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Fix DateFormat time zone is not restored and add Test. #2549
Changes from 5 commits
46942b6
6ad06f9
fb8c463
fb850a5
7928fe4
3da89da
db4ff5e
7b8e91a
1687fb1
23bbbca
3b7c7d0
a8a13e7
49f0caf
edc10e1
72cc078
d3767a8
972900a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -30,6 +30,7 @@ | |||||||||||||||||||
import java.text.ParseException; | ||||||||||||||||||||
import java.text.SimpleDateFormat; | ||||||||||||||||||||
import java.util.Date; | ||||||||||||||||||||
import java.util.TimeZone; | ||||||||||||||||||||
|
||||||||||||||||||||
/** | ||||||||||||||||||||
* Adapter for java.sql.Time. Although this class appears stateless, it is not. DateFormat captures | ||||||||||||||||||||
|
@@ -62,7 +63,9 @@ public Time read(JsonReader in) throws IOException { | |||||||||||||||||||
String s = in.nextString(); | ||||||||||||||||||||
try { | ||||||||||||||||||||
synchronized (this) { | ||||||||||||||||||||
TimeZone originalTimeZone = format.getTimeZone(); // Save the original time zone | ||||||||||||||||||||
Date date = format.parse(s); | ||||||||||||||||||||
format.setTimeZone(originalTimeZone); // Restore the original time zone | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use
Suggested change
(you might to have to reformat the code above) |
||||||||||||||||||||
return new Time(date.getTime()); | ||||||||||||||||||||
} | ||||||||||||||||||||
} catch (ParseException e) { | ||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -18,9 +18,11 @@ | |||||||
|
||||||||
import static com.google.common.truth.Truth.assertThat; | ||||||||
import static com.google.common.truth.Truth.assertWithMessage; | ||||||||
import static org.junit.Assert.assertEquals; | ||||||||
import static org.junit.Assert.fail; | ||||||||
|
||||||||
import com.google.gson.Gson; | ||||||||
import com.google.gson.GsonBuilder; | ||||||||
import com.google.gson.TypeAdapter; | ||||||||
import com.google.gson.TypeAdapterFactory; | ||||||||
import com.google.gson.internal.bind.DefaultDateTypeAdapter.DateType; | ||||||||
|
@@ -223,6 +225,27 @@ public void testUnexpectedToken() throws Exception { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
@Test | ||||||||
public void testGsonDateFormat() { | ||||||||
// Set the default timezone to UTC | ||||||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC")); | ||||||||
eamonnmcmanus marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm z").create(); | ||||||||
Date originalDate = new Date(0); | ||||||||
|
||||||||
// Serialize the date object | ||||||||
String json = gson.toJson(originalDate); | ||||||||
assertEquals("\"1970-01-01 00:00 UTC\"", json); | ||||||||
|
||||||||
// Deserialize a date string with the PST timezone | ||||||||
Date deserializedDate = gson.fromJson("\"1970-01-01 00:00 PST\"", Date.class); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add an assertion here that the date was parsed correctly, I assume it would be this:
Suggested change
|
||||||||
|
||||||||
// Serialize the deserialized date object again | ||||||||
String jsonAfterDeserialization = gson.toJson(deserializedDate); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this should rather call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hyt |
||||||||
// The expectation is that the date, after deserialization, when serialized again should still | ||||||||
// be in the UTC timezone | ||||||||
assertEquals("\"1970-01-01 08:00 UTC\"", jsonAfterDeserialization); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use Truth instead of the JUnit methods here; that will leads to better failure messages and makes the assertion easier to read:
Suggested change
|
||||||||
} | ||||||||
|
||||||||
private static TypeAdapter<Date> dateAdapter(TypeAdapterFactory adapterFactory) { | ||||||||
TypeAdapter<Date> adapter = adapterFactory.create(new Gson(), TypeToken.get(Date.class)); | ||||||||
assertThat(adapter).isNotNull(); | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use
try-finally
here as well, to be safe (in case time zone was already changed despite an exception being thrown):(you might to have to reformat the code above)