Last active
March 6, 2017 05:36
-
-
Save parthdave93/aa974b217b270fe6e3facfff2e7438da to your computer and use it in GitHub Desktop.
Parse dates on Api response level
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DateDeserializer implements JsonDeserializer<MyDate> { | |
//if not epoch time from server response | |
public static SimpleDateFormat serverFormatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'"); | |
public static SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); | |
@Override | |
public MyDate deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { | |
String date = element.getAsString(); | |
//if epoch sent then create date object directly new Date(time*1000) | |
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));//the timezones you want | |
serverFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); | |
try { | |
//skip this step if you get epochtime from server | |
Date serverDate = serverFormatter.parse(date); | |
return new MyDate(formatter.format(serverDate), serverDate); | |
} catch (ParseException e) { | |
System.err.println("Failed to parse Date due to:" + e); | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static GsonConverterFactory getGsonFactory() { | |
//Here you can create your own deserializers | |
//so what it will do is when it finds the dob from json response which is ideally string or long and calls this serializer to provide mydate instance | |
//and then deserialize method called with the json response value which can be 140005666... something like that epoch or date | |
// and deserializer will response the object from which you can get date formatted value | |
Gson gson = new GsonBuilder().setLenient().registerTypeAdapter(MyDate.class, new DateDeserializer()).create(); | |
return GsonConverterFactory.create(gson); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyDate implements Serializable { | |
private int mId; | |
private String formattedDate; | |
//still if you do want original date | |
private Date date; | |
.... | |
public MyDate(String value, Date date) { | |
this.val = value; | |
this.date = date; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReponseModel implements Serializable { | |
@SerializedName("dob") | |
private MyDate dob; | |
.... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Retrofit retrofit = new Retrofit.Builder().baseUrl(apiUrlProvider())//LOCAL_BASE_URL | |
.addConverterFactory(getGsonFactory()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).client(client).build(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment