com.urbanairship.connect.client.model.responses.DateTimeAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of connect-client Show documentation
Show all versions of connect-client Show documentation
The UA Connect Java client library
/*
Copyright 2015 Urban Airship and Contributors
*/
package com.urbanairship.connect.client.model.responses;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import java.lang.reflect.Type;
public class DateTimeAdapter implements JsonDeserializer, JsonSerializer {
@Override
public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
return new JsonPrimitive(fmt.print(src));
}
@Override
public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
DateTimeFormatter fmt = ISODateTimeFormat.dateTimeParser().withZoneUTC();
try {
return fmt.parseDateTime(json.getAsString());
} catch (Exception e) {
return new DateTime(json.getAsString()).withZone(DateTimeZone.UTC);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy