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

Fix DateFormat time zone is not restored. #2548

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

/**
* Adapter for Date. Although this class appears stateless, it is not. DateFormat captures its time
Expand Down Expand Up @@ -84,10 +85,13 @@ private Date deserializeToDate(JsonReader in) throws IOException {
String s = in.nextString();
synchronized (dateFormats) {
for (DateFormat dateFormat : dateFormats) {
TimeZone originalTimeZone = dateFormat.getTimeZone();
try {
return dateFormat.parse(s);
} catch (ParseException ignored) {
// OK: try the next format
} finally {
dateFormat.setTimeZone(originalTimeZone);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.gson.functional;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import com.google.common.base.Splitter;
Expand All @@ -39,8 +40,10 @@
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import org.junit.Test;

/** Functional tests for the {@link JsonAdapter} annotation on classes. */
Expand Down Expand Up @@ -794,6 +797,26 @@ public void testAdapterCreatedByJdkUnsafe() {
assertThat(json).isEqualTo("false");
}

@Test
public void testGsonDateFormat() {
// Set the default timezone to UTC
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
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);

// Serialize the deserialized date object again
String jsonAfterDeserialization = gson.toJson(deserializedDate);
// 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);
}

@JsonAdapter(CreatedByJdkUnsafe.Serializer.class)
private static class CreatedByJdkUnsafe {
static class Serializer implements JsonSerializer<CreatedByJdkUnsafe> {
Expand Down
Loading