there is such a code

gson = new GsonBuilder() .setDateFormat(DateFormat.FULL, DateFormat.FULL).create(); 

tried to do and so

 gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create(); 

to put

setDateFormat("yyyy-MM-dd'T'HH:mm:ssX").create(); setDateFormat("yyyy-MM-dd'T'HH:mm:ssz").create(); setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();

also tried when deserializing json into an object

gson.fromJson(string, object.getClass());

always the same mistake

Exception in thread "Thread-3" com.google.gson.JsonSyntaxException: 1469504907000 at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:106) at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:83) at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:37) at com.google.gson.internal.bind.TreeTypeAdapter.read(TreeTypeAdapter.java:69) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220) at com.google.gson.Gson.fromJson(Gson.java:887) at com.google.gson.Gson.fromJson(Gson.java:852) at com.google.gson.Gson.fromJson(Gson.java:801) at com.google.gson.Gson.fromJson(Gson.java:773) at jsmarty.core.common.json.JsonHandler.deserialize(JsonHandler.java:79) at jsmarty.core.router.SdoRouteWorkerThread.run(SdoRouteWorkerThread.java:71) at java.lang.Thread.run(Thread.java:745) Caused by: java.text.ParseException: Failed to parse date ["1469504907000']: Invalid time zone indicator '0' at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274) at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:104) ... 12 more Caused by: java.lang.IndexOutOfBoundsException: Invalid time zone indicator '0' at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:245) ... 13 more

  • one
    Look at the answers here - maybe the fact is that you have a date in the form of timeStamp, not a date - YuriySPb
  • @YuriSPb date comes in timeStamp, in the date type object date. Shouldn't Gson transform this? - drugs_and_code
  • one
    Well, apparently can not. We need to help him. From the link above, I have seen two different examples - try them - YuriySPb
  • @YuriyPb I clicked on the link) and tried to solve my problem as described there but unfortunately the problem did not go away (the exact same error - drugs_and_code

1 answer 1

 public static class GsonUTCDateAdapter implements JsonSerializer<Date>,JsonDeserializer<Date> { private final DateFormat dateFormat; public GsonUTCDateAdapter() { dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); //This is the format I need dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); //This is the key line which converts the date to UTC which cannot be accessed with the default serializer } @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); } } } 

Then:

 Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new GsonUTCDateAdapter()).create(); Date now=new Date(); System.out.println(gson.toJson(now));