OffsetDateTime 的 Gson 反序列化问题
Gson deserializing issue for OffsetDateTime
我正在使用 gson 2.8
进行序列化和反序列化 我在我的 class 中有 OffsetDateTime
字段,当我给出值 "orderTime":"2018-05-02T14:23:00Z"
我得到它作为 "2018-05-02T14:23Z"
我期望 "2018-05-02T14:23:00Z"
如果我给出 "orderTime":"2018-05-02T14:23:01Z"
我得到的是预期的 "2018-05-02T14:23:00Z"
。无论如何要解决这个问题?
这是我的订单Class
import com.google.gson.annotations.SerializedName;
import java.time.OffsetDateTime;
public class Order {
@SerializedName("orderId")
private String orderId = null;
@SerializedName("orderType")
private String orderType = null;
@SerializedName("orderTime")
private OffsetDateTime orderTime = null;
....
}
Is there anyway to fix this issue?
没有任何修复。问题是到底有没有问题
2018-05-02T14:23:00Z
和2018-05-02T14:23Z
表示同一时间点。当秒为 0 时,它们在格式中是可选的。该格式称为 ISO 8601。因此您已获得预期的值。
您看到的字符串是由OffsetDateTime.toString()
生成的。无法修改此方法的工作方式(甚至无法重写,因为 OffsetDateTime
是最终的 class)。
虽然有一个修复程序可以看到另一个字符串。使用格式化程序:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssXXXXX");
System.out.println(orderTime.format(formatter));
这会打印:
2018-05-02T14:23:00Z
我正在使用 gson 2.8
进行序列化和反序列化 我在我的 class 中有 OffsetDateTime
字段,当我给出值 "orderTime":"2018-05-02T14:23:00Z"
我得到它作为 "2018-05-02T14:23Z"
我期望 "2018-05-02T14:23:00Z"
如果我给出 "orderTime":"2018-05-02T14:23:01Z"
我得到的是预期的 "2018-05-02T14:23:00Z"
。无论如何要解决这个问题?
这是我的订单Class
import com.google.gson.annotations.SerializedName;
import java.time.OffsetDateTime;
public class Order {
@SerializedName("orderId")
private String orderId = null;
@SerializedName("orderType")
private String orderType = null;
@SerializedName("orderTime")
private OffsetDateTime orderTime = null;
....
}
Is there anyway to fix this issue?
没有任何修复。问题是到底有没有问题
2018-05-02T14:23:00Z
和2018-05-02T14:23Z
表示同一时间点。当秒为 0 时,它们在格式中是可选的。该格式称为 ISO 8601。因此您已获得预期的值。
您看到的字符串是由OffsetDateTime.toString()
生成的。无法修改此方法的工作方式(甚至无法重写,因为 OffsetDateTime
是最终的 class)。
虽然有一个修复程序可以看到另一个字符串。使用格式化程序:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssXXXXX");
System.out.println(orderTime.format(formatter));
这会打印:
2018-05-02T14:23:00Z