/* * ATLauncher - https://github.com/ATLauncher/ATLauncher * Copyright (C) 2013 ATLauncher * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package co.forsaken.projectindigo.utils.mojangtokens; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.JsonSyntaxException; 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; public class DateTypeAdapter implements JsonDeserializer<Date>, JsonSerializer<Date> { private final DateFormat enUsFormat = DateFormat.getDateTimeInstance(2, 2, Locale.US); private final DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); @Override public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { if (!(json instanceof JsonPrimitive)) { throw new JsonParseException("The date " + json + " is not a string!"); } if (type != Date.class) { throw new IllegalArgumentException(getClass() + " cannot deserialize to " + type); } String value = json.getAsString(); synchronized (enUsFormat) { try { return enUsFormat.parse(value); } catch (ParseException e) { try { return iso8601Format.parse(value); } catch (ParseException e2) { try { String tmp = value.replace("Z", "+00:00"); return iso8601Format.parse(tmp.substring(0, 22) + tmp.substring(23)); } catch (ParseException e3) { throw new JsonSyntaxException("Invalid date " + value, e3); } } } } } @Override public JsonElement serialize(Date value, Type type, JsonSerializationContext context) { synchronized (enUsFormat) { String ret = this.iso8601Format.format(value); return new JsonPrimitive(ret.substring(0, 22) + ":" + ret.substring(22)); } } }