Correct Date format to use for GsonBuilder Date Format

心不动则不痛 提交于 2020-06-27 16:21:23

问题


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.


回答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:

  • java.util.Date format SSSSSS: if not microseconds what are the last 3 digits?
  • Java Date toString contains a timezone… Why?



回答2:


The given pattern is "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX". Please check https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Task {
    public static void main(String[] args) { 
        String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = simpleDateFormat.parse("2019-11-22T16:16:31.0065786+00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(date);  
    }
}


来源:https://stackoverflow.com/questions/58999880/correct-date-format-to-use-for-gsonbuilder-date-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!