用两种日期格式转换为Json的Gson无法正常工作

时间:2016-11-11 11:08:18

标签: java json date datetime gson

我有两种来自服务器Json的日期格式。 yyyy-MM-dd'T'HH:mm:ss yyyy-MM-dd 是我的日期格式。

所以我尝试使用以下代码将此Json转换为Gson

GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(Date.class, new DateDeserializer());
Gson gson = gsonBuilder.create();

这些是我的日期格式,

private static final String[] DATE_FORMATS = new String[] {
            "yyyy-MM-dd'T'HH:mm:ss",
            "yyyy-MM-dd"
};

private class DateDeserializer implements JsonDeserializer<Date> {

     @Override
     public Date deserialize(JsonElement jsonElement, Type typeOF,
                                JsonDeserializationContext context) throws JsonParseException {
          for (String format : DATE_FORMATS) {
                try {
                  return new SimpleDateFormat(format, Locale.US).parse(jsonElement.getAsString());
                } catch (ParseException e) {
                }
          }
          throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString()
                    + "\". Supported formats: " + Arrays.toString(DATE_FORMATS));
      }       

}

更新

这是我的Json对象,

{
  "idpatient": 56,
  "diabetesType": null,
  "language": null,
  "customId": "CUS790",
  "diabetesOther": null,
  "firstName": "Niro",
  "lastName": "Wije",
  "email": "sahirml@gmail.com",
  "dob": "1989-10-11",
  "parentEmail": null,
  "gender": "male",
  "diagnosedDate": "2016-11-11",
  "height": 0,
  "weight": 0,
  "heightUnit": null,
  "weightUnit": null,
  "theme": "Lite",
  "userName": "weera",
  "password": "asdf9999",
  "dateCreated": 1478880594000,
  "lastUpdated": 1478880594000
}

然后我尝试使用它将Json对象转换为Gson对象。我得到了以下错误日志。

11-11 16:11:17.735 28072-28072/my.api.example W/System.err: com.google.gson.JsonParseException: Unparseable date: "1478880594000". Supported formats: [yyyy-MM-dd'T'HH:mm:ss, yyyy-MM-dd]
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at my.api.example.activities.LoginActivity$DateDeserializer.deserialize(LoginActivity.java:180)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at my.api.example.activities.LoginActivity$DateDeserializer.deserialize(LoginActivity.java:168)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at com.google.gson.Gson.fromJson(Gson.java:810)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at com.google.gson.Gson.fromJson(Gson.java:775)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at retrofit.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:36)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at retrofit.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:24)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at retrofit.OkHttpCall.parseResponse(OkHttpCall.java:148)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at retrofit.OkHttpCall.access$100(OkHttpCall.java:29)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at retrofit.OkHttpCall$1.onResponse(OkHttpCall.java:94)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:168)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-11 16:11:17.755 28072-28072/my.api.example W/System.err:     at java.lang.Thread.run(Thread.java:841)
11-11 16:11:17.755 28072-28072/my.api.example D/LOGIN_ACTIVITY:  ERROR_MESSAGE Unparseable date: "1478880594000". Supported formats: [yyyy-MM-dd'T'HH:mm:ss, yyyy-MM-dd]

这意味着此方法不适用于我的日期格式。 我怎么能处理这个例外?

有任何想法吗?

3 个答案:

答案 0 :(得分:1)

根据错误,您共享服务器正在返回日期 毫秒,而不是您期望的格式。

com.google.gson.JsonParseException:无法解析的日期:&#34; 1478880594000&#34;。支持的格式:[yyyy-MM-dd&#39; T&#39; HH:mm:ss,yyyy-MM-dd]。

首先,您需要转换为日期,然后根据您的要求进行格式化。

答案 1 :(得分:1)

更新此行

return new SimpleDateFormat(format, Locale.US).format(new Date(jsonElement));
希望这会奏效。

答案 2 :(得分:0)

从日志中,我可以看到您没有传递java.util.Date,而是传递时间戳(长),并且您正在为java.util.Date类注册类型适配器。