正确的日期格式用于GsonBuilder日期格式 [英] Correct Date format to use for GsonBuilder Date Format

查看:432
本文介绍了正确的日期格式用于GsonBuilder日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户给我发送了一个日期为 2019-11-22T16:16:31.0065786 + 00:00的日期。我收到以下错误:

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:无法解析的日期:
2019-11-22T16:16 :31.0065786 + 00:00

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

我使用的日期格式为:

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

请让我知道使用哪种格式。

Please let me know which format to use.

推荐答案

此格式可以由 DateTimeFormatter 的DateTimeFormatter.ISO_ZONED_DATE_TIME 实例。它是 Java Time 软件包的一部分,该软件包与 1.8 版本一起发布。您应该使用 ZonedDateTime 来存储这样的值,但是我们也可以将其转换为过时的 Date 类。

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 +
                '}';
    }
}

以上代码打印:

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

中将 ZonedDateTime 更改为 Date 时DateValue 类,它将根据您的时区打印此日期。

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

另请参见:

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

这篇关于正确的日期格式用于GsonBuilder日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆