question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GSON default date serializer is locale-specific

See original GitHub issue
This has caused problems for Caliper, which was relying on the default date 
serializer:
http://code.google.com/p/caliper/issues/detail?id=113

Work-around the problem by registering a date type adapter like the following:

  private static class DateTypeAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
    private final DateFormat dateFormat;

    private DateTypeAdapter() {
      dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz", Locale.US);
      dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    @Override public synchronized JsonElement serialize(Date date, Type type,
        JsonSerializationContext jsonSerializationContext) {
      return new JsonPrimitive(dateFormat.format(date));
    }

    @Override public synchronized Date deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) {
      try {
        return dateFormat.parse(jsonElement.getAsString());
      } catch (ParseException e) {
        throw new JsonParseException(e);
      }
    }
  }

Original issue reported on code.google.com by limpbizkit on 30 Jan 2011 at 1:01

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:15

github_iconTop GitHub Comments

5reactions
markkolichcommented, Sep 10, 2017
4reactions
Paxacommented, Oct 5, 2016

Worked as:

Gson gson = new GsonBuilder()
        .setPrettyPrinting()
        .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")
        .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
        .create();
Read more comments on GitHub >

github_iconTop Results From Across the Web

GSON default date serializer is locale-specific - Stack Overflow
The problem is that the server is returning the date as "Nov 1, 2019". In this format you don't have the time and...
Read more >
Gson (Gson 2.2.4 API) - javadoc.io
Gson provides default serialization and deserialization for Enums, Map , URL , URI , Locale , Date , BigDecimal , and BigInteger classes....
Read more >
com.google.gson.GsonBuilder.setDateFormat java code ...
Configures Gson to to serialize Date objects according to the style value provided. You can call this method or #setDateFormat(String) multiple times, but...
Read more >
How to format a date using the Gson library in Java?
A Gson is a JSON library for Java, which is created by Google. ... setDateFormat() method configures Gson to serialize Date objects ...
Read more >
Deserialize and Serialize DateTime with GSON | by Hao
And we want to serialize it into a format, like “yyyy-MM-dd”. Response contains two different types of Date format. GsonBuilder's setDateFormat.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found