Java tutorial
/* * GeoPeril - A platform for the computation and web-mapping of hazard specific * geospatial data, as well as for serving functionality to handle, share, and * communicate threat specific information in a collaborative environment. * * Copyright (C) 2013 GFZ German Research Centre for Geosciences * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * Contributors: * Johannes Spazier (GFZ) - initial implementation * Sven Reissland (GFZ) - initial implementation * Martin Hammitzsch (GFZ) - initial implementation */ package Misc; 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; 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; public class GsonUTCdateAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> { private final DateFormat dateFormat; public GsonUTCdateAdapter() { dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); 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); } } }