Gson“2014-06-12T11:05:37.425Z”到目前为止在Android上

时间:2014-06-13 11:33:21

标签: android gson

我需要转换这种格式的字符串

2014-06-12T11:05:37.425Z

到解析此类的日期:

package com.virteu.gg.domain;

import java.util.Date;

/**
 * Created by colymore on 13/6/14.
 */
public class Status {

  private String id;
  private State[] states;

  public Status() {
  }

  public Status(String id, State[] states) {
    this.id = id;
    this.states = states;
  }

  public State[] getStates() {
    return states;
  }

  public void setStates(State[] states) {
    this.states = states;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  class State {
    private Date date;
    private String state;

    State(Date date, String state) {
      this.date = date;
      this.state = state;
    }
  }
}

我正在尝试这个:

 Gson gson = new GsonBuilder()
        .setDateFormat("yyyy-MM-dd'T'HH:mm:ssz")
        .create();
        Status[] status = gson.fromJson(value.toString(), new TypeToken<Status[]>() {}.getType());

但是给我这个错误;

 06-13 13:30:34.959  28113-28113/? E/Report ::﹕ com.google.gson.JsonSyntaxException: 2014-06-12T11:05:37.425Z--------- Stack trace ---------
    com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:107)    com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:82)    com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:35)    com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)    com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)    com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72)    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)    com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)    com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72)    com.google.gson.Gson.fromJson(Gson.java:803)    com.google.gson.Gson.fromJson(Gson.java:768)    com.google.gson.Gson.fromJson(Gson.java:717)    com.virteu.gg.activities.LoginActivity$3.success(LoginActivity.java:115)    com.virteu.gg.services.api.ApiCallBase.apiSuccess(ApiCallBase.java:39)    com.virteu.gg.services.api.GetStatusApiCall$1.onSuccess(Ge

我怎么能这样做..?我不太了解gson库..

1 个答案:

答案 0 :(得分:2)

您的日期字符串模式错误。 尝试使用setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")