
cn.leancloud.gson.GsonUTCDateAdapter Maven / Gradle / Ivy
package cn.leancloud.gson;
import cn.leancloud.utils.StringUtil;
import com.google.gson.*;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class GsonUTCDateAdapter implements JsonSerializer, JsonDeserializer {
private final DateFormat dateFormat;
public GsonUTCDateAdapter() {
dateFormat = new SimpleDateFormat(StringUtil.dateFormat, Locale.getDefault());
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
}
@Override
public synchronized JsonElement serialize(Date date, Type type,
JsonSerializationContext jsonSerializationContext) {
return new JsonPrimitive(dateFormat.format(date));
}
@Override
public synchronized Date deserialize(JsonElement jsonElement,Type type,
JsonDeserializationContext jsonDeserializationContext) {
try {
return dateFormat.parse(jsonElement.getAsString());
} catch (ParseException e) {
throw new JsonParseException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy