All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.droidlogix.dbflare.a2e.DbFlareDateDeserializer Maven / Gradle / Ivy

There is a newer version: 1.0.8
Show newest version
package com.droidlogix.dbflare.a2e;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import org.joda.time.DateTime;

import java.lang.reflect.Type;
import java.util.Date;

public class DbFlareDateDeserializer implements JsonDeserializer
{
	@Override
	public Object deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException
	{
		try
		{
			return new Date(jsonElement.getAsJsonPrimitive().getAsLong()); // Try to parse first if passed value is an epoch time
		}
		catch (Exception exception1)
		{
			try
			{
				return new DateTime(jsonElement.getAsString()).toDate(); // Parse as string following Joda DateTime
			}
			catch(Exception exception2)
			{
			}
		}
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy