Skip to content

Instantly share code, notes, and snippets.

@CMingTseng
Forked from jromero/CalendarGson.java
Created June 13, 2018 14:34
Show Gist options
  • Save CMingTseng/23ccecbf169ead985cdb00304d98e0f7 to your computer and use it in GitHub Desktop.
Save CMingTseng/23ccecbf169ead985cdb00304d98e0f7 to your computer and use it in GitHub Desktop.
DateDeserializer for Gson based on different SimpleDateFormats
package org.dallasmakerspace.kiosk.data.calendar;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.dallasmakerspace.kiosk.gson.DateDeserializer;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by javier.romero on 3/1/14.
*/
public class CalendarGson {
private static Gson sGson;
public static Gson getGson() {
if (sGson == null) {
sGson = new GsonBuilder()
.registerTypeAdapter(Event.EventDateTime.class,
new DateDeserializer<Event.EventDateTime>(
Event.EventDateTime.DATE_FORMAT, Event.EventDateTime.class))
.registerTypeAdapter(Event.StartEndDateTime.class,
new DateDeserializer<Event.StartEndDateTime>(
Event.StartEndDateTime.DATE_FORMAT, Event.StartEndDateTime.class))
.registerTypeAdapter(Event.SimpleDate.class,
new DateDeserializer<Event.SimpleDate>(
Event.SimpleDate.DATE_FORMAT, Event.SimpleDate.class))
.create();
}
return sGson;
}
private CalendarGson() {
}
}
package org.dallasmakerspace.kiosk.gson;
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.Date;
/**
* Created by javier.romero on 3/1/14.
*/
public class DateDeserializer<T extends Date> implements JsonDeserializer<T> {
private static final String TAG = DateDeserializer.class.getSimpleName();
private final SimpleDateFormat mSimpleDateFormat;
private final Class<T> mClazz;
public DateDeserializer(SimpleDateFormat simpleDateFormat, Class<T> clazz) {
mSimpleDateFormat = simpleDateFormat;
mClazz = clazz;
}
@Override
public T deserialize(JsonElement element, Type arg1, JsonDeserializationContext context) throws JsonParseException {
String dateString = element.getAsString();
try {
T date = mClazz.newInstance();
date.setTime(mSimpleDateFormat.parse(dateString).getTime());
return date;
} catch (InstantiationException e) {
throw new JsonParseException(e.getMessage(), e);
} catch (IllegalAccessException e) {
throw new JsonParseException(e.getMessage(), e);
} catch (ParseException e) {
throw new JsonParseException(e.getMessage(), e);
}
}
}
package org.dallasmakerspace.kiosk.data.calendar;
import com.google.gson.annotations.SerializedName;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by javier.romero on 3/1/14.
*/
public class Event {
@SerializedName("id")
private String mId;
@SerializedName("status")
private String mStatus;
@SerializedName("htmlLink")
private String mHtmlLink;
@SerializedName("created")
private EventDateTime mCreated;
//@SerializedName("updated")
private EventDateTime mUpdated;
@SerializedName("summary")
private String mSummary;
@SerializedName("description")
private String mDescription;
@SerializedName("location")
private String mLocation;
@SerializedName("start")
private ConditionalDateTime mStart;
@SerializedName("end")
private ConditionalDateTime mEnd;
public String getId() {
return mId;
}
public String getStatus() {
return mStatus;
}
public String getHtmlLink() {
return mHtmlLink;
}
public EventDateTime getCreated() {
return mCreated;
}
public EventDateTime getUpdated() {
return mUpdated;
}
public String getSummary() {
return mSummary;
}
public String getDescription() {
return mDescription;
}
public String getLocation() {
return mLocation;
}
public ConditionalDateTime getStart() {
return mStart;
}
public ConditionalDateTime getEnd() {
return mEnd;
}
public boolean isAllDayEvent() {
return mStart.isAllDayEvent() && mEnd.isAllDayEvent();
}
/**
*
*/
public static class ConditionalDateTime {
@SerializedName("dateTime")
private StartEndDateTime mDateTime;
@SerializedName("date")
private SimpleDate mDate;
public SimpleDate getDate() {
return mDate;
}
public StartEndDateTime getDateTime() {
return mDateTime;
}
/**
* If it is an all day event then only date is populated (not DateTime)
* @return
*/
public boolean isAllDayEvent() {
return mDate != null;
}
}
public static class EventDateTime extends Date {
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
}
public static class StartEndDateTime extends Date {
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
}
public static class SimpleDate extends java.util.Date {
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment