Gson dateformat to parse/output unix-timestamps
Creating a custom DateTypeAdapter
was the way to go.
MyDateTypeAdapter
public class MyDateTypeAdapter extends TypeAdapter<Date> {
@Override
public void write(JsonWriter out, Date value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
out.value(value.getTime() / 1000);
}
@Override
public Date read(JsonReader in) throws IOException {
if (in == null) {
return null;
}
return new Date(in.nextLong() * 1000);
}
}
Don't forget to register it!
MyDateTypeAdapter myAdapter = new MyDateTypeAdapter();
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, myAdapter).create();