com.google.gson.JsonSyntaxException 时间问题

作者:LoveEmperor-王子様

问题报错:

    com.google.gson.JsonSyntaxException: 2018-07-27T09:01:31.660Z
        at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:81)
        at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:66)
        at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:41)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
        at com.google.gson.Gson.fromJson(Gson.java:810)
        at com.google.gson.Gson.fromJson(Gson.java:775)
        at com.google.gson.Gson.fromJson(Gson.java:724)
        at com.google.gson.Gson.fromJson(Gson.java:696)

一般是接收时间格式问题:
用Gson解析时指定类型:

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

或:

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

你可能感兴趣的:(Java)