0

i need to convert a String with this format

2014-06-12T11:05:37.425Z

To a date for parse with this class:

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

Im trying with this:

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

But give me this error;

 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

How can i do it this..?I dont know so much about gson library..

colymore
  • 11,776
  • 13
  • 48
  • 90
  • Use custom deserializer [Here's an example][1] [1]: http://stackoverflow.com/questions/16590377/custom-json-deserializer-using-gson – Sercan Ozdemir Jun 13 '14 at 11:40

1 Answers1

2

Your date string pattern is wrong. Try to use setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")

Alex
  • 3,382
  • 2
  • 32
  • 41