package vn.fractal.male.maleapp.common;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Locale;
public class DateDeserializer implements JsonDeserializer<Date> {
private final String[] DATE_FORMATS = new String[]{
"yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z"
};
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
for (String format : DATE_FORMATS) {
try {
return new SimpleDateFormat(format, Locale.US).parse(json.getAsString());
} catch (ParseException e) {
}
}
throw new JsonParseException("Unparseable date: \"" + json.getAsString()
+ "\". Supported formats: " + Arrays.toString(DATE_FORMATS));
}
}