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

GSON default date serializer is locale-specific #281

Closed
GoogleCodeExporter opened this issue Mar 19, 2015 · 15 comments
Closed

GSON default date serializer is locale-specific #281

GoogleCodeExporter opened this issue Mar 19, 2015 · 15 comments

Comments

@GoogleCodeExporter
Copy link

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

@GoogleCodeExporter
Copy link
Author

Fixed by r721.

Original comment by limpbizkit on 10 Feb 2011 at 1:07

  • Changed state: Fixed

@GoogleCodeExporter
Copy link
Author

It will be great if we can configure GsonBuilder to force used of ISO-8601 
format with UTC timezone, for serialization and deserialization.

Thanks

Original comment by boillodm...@gmail.com on 14 Jun 2011 at 10:03

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

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

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

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

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

Original comment by boillodm...@gmail.com on 14 Jun 2011 at 12:36

@GoogleCodeExporter
Copy link
Author

+1 ! And it would be nice to be able to specify the DateFormat and not just 
only the SDF pattern on the GsonBuilder

Original comment by lorber.s...@gmail.com on 15 Mar 2013 at 2:46

@GoogleCodeExporter
Copy link
Author

Which version of gson has this fix ?

Original comment by misramay...@gmail.com on 3 Jul 2014 at 9:48

@einmalfel
Copy link

still experiencing this issue in gson 2.3.1

@Burtan
Copy link

Burtan commented Jan 24, 2016

This issue still exists in Gson 2.4

@code77se
Copy link

Is the intention that the UtcDateTypeAdapter introduced in 2.4 should be manually registered as a type adapter?

@inder123
Copy link
Collaborator

@code77se Yes. Register UtcDateTypeAdapter manually.

@Paxa
Copy link

Paxa commented Oct 5, 2016

Still an issue in gson 2.7

import com.google.gson.typeadapters.UtcDateTypeAdapter;

Error:

error: package com.google.gson.typeadapters does not exist
import com.google.gson.typeadapters.UtcDateTypeAdapter;

@Paxa
Copy link

Paxa commented 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();

@Ashok-Varma
Copy link

@Paxa
this only adds hour difference. but for countries like India you will miss the minutes part.
Instead of X use Z to get the minutes part as well

Gson gson = new GsonBuilder()
        .setPrettyPrinting()
        .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
        .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
        .create();```

@marcusfey
Copy link

This will be slightly off topic still I hope you can help me. For my applications I get strings like "2017-10-13T18:10+03:00", "2017-10-13T14:15+02:00", etc. which have to be converted to a java.util.Date property without loosing time zone information.

I'm using new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssX").create()
already which leads to correct date/time but converted to my time zone and the original time zone property of the Date being lost.

How can I convert the String to Date while maintaining the time zone information?

Thanks!

@markkolich
Copy link

In case anyone was wondering where UtcDateTypeAdapter.java ended up, it's part of gson-extras:

https://github.com/google/gson/blob/d9cc7bc60b492664fe9f328d1bab9f9ae873f4d4/extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java

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

No branches or pull requests

9 participants