2

My client sends me a date in "2019-11-22T16:16:31.0065786+00:00" format. I am getting the following error:

java.text.ParseException: Unparseable date: "2019-11-22T16:16:31.0065786+00:00"

The date format that I am using is:

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

Please let me know which format to use.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
Abdul
  • 21
  • 3
  • Possible duplicate of [GsonBuilder setDateFormat for "2011-10-26T20:29:59-07:00"](https://stackoverflow.com/questions/7910734/gsonbuilder-setdateformat-for-2011-10-26t202959-0700). See also: [Which Java Date format is this “YYYY-MM-DD 00:00:00+00:00”?](https://stackoverflow.com/questions/27313005/which-java-date-format-is-this-yyyy-mm-dd-0000000000) – Michał Ziober Nov 22 '19 at 20:26
  • Hi Michael, thanks for responding. I have 2 issues together here and your links provide solution to only one of them. The format I get is 7 digits of milli or nano seconds (0065786) followed by the time zone (+00:00). is there a date format to cover this format?. Thanks – Abdul Nov 25 '19 at 15:45

1 Answers1

1

This format can be handled by DateTimeFormatter.ISO_ZONED_DATE_TIME instance of DateTimeFormatter. It is a part of Java Time package which was released together with 1.8 version. You should use ZonedDateTime to store values like this but we can convert it also to obsolete Date class.

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;
import java.time.ZonedDateTime;
import java.util.Date;

public class GsonApp {

    public static void main(String[] args) {
        Gson gson = new GsonBuilder()
                .setPrettyPrinting()
                .registerTypeAdapter(Date.class, new DateJsonDeserializer())
                .registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeJsonDeserializer())
                .create();

        System.out.println(gson.fromJson("{\"value\":\"2019-11-22T16:16:31.0065786+00:00\"}", DateValue.class));
    }
}

class DateJsonDeserializer implements JsonDeserializer<Date> {
    @Override
    public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        ZonedDateTime zdt = ZonedDateTime.parse(json.getAsString());

        return Date.from(zdt.toInstant());
    }
}

class ZonedDateTimeJsonDeserializer implements JsonDeserializer<ZonedDateTime> {
    @Override
    public ZonedDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        return ZonedDateTime.parse(json.getAsString());
    }
}

class DateValue {
    private ZonedDateTime value;

    public ZonedDateTime getValue() {
        return value;
    }

    public void setValue(ZonedDateTime value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "DateValue{" +
                "value=" + value +
                '}';
    }
}

Above code prints:

DateValue{value=2019-11-22T16:16:31.006578600Z}

When you change ZonedDateTime to Date in DateValue class it will print this date with relation to your time zone.

See also:

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146