I'm using GSON to convert JSONArray to Java objects. I need to convert both long date and simple date format "yyyy-MM-dd'T'HH:mm'Z'" to Java objects. I'm able to convert alone long or simple date but couldn't able to convert both together.
used below code snippet for long conversion :
GsonBuilder builder = new GsonBuilder();
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
return new Date(json.getAsJsonPrimitive()
.getAsLong());
}
});
Gson gson = builder.create();
Type userListType = new TypeToken<ArrayList<CurrentLocation>>() {
}.getType();
gson.fromJson(data, userListType);
Sample data I need to convert :
[
{
"instance": {
"location": {
"lat": 31.522291,
"lon": -96.532816,
"timestamp": 1587693720000
},
"timeAtLocation": "2020-04-23T04:59Z"
}
},
{
"instance": {
"location": {
"lat": 31.522291,
"lon": -96.532816,
"timestamp": 1737693720000
},
"timeAtLocation": "2020-0-23T04:59Z"
}
}
]