Here you can find the source of jsonToBeanDateSerializer(String jsonStr, Class
public static <T> T jsonToBeanDateSerializer(String jsonStr, Class<T> cl, final String pattern)
//package com.java2s; //License from project: Apache License import com.google.gson.Gson; import com.google.gson.GsonBuilder; 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.SimpleDateFormat; import java.util.Date; public class Main { private static Gson gson = null; public static <T> T jsonToBeanDateSerializer(String jsonStr, Class<T> cl, final String pattern) { Object obj = null;/*from w w w . jav a2 s .co m*/ gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { @Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { SimpleDateFormat format = new SimpleDateFormat(pattern); String dateString = json.getAsString(); try { return format.parse(dateString); } catch (Exception e) { } return null; } }).setDateFormat(pattern).create(); if (gson != null) { obj = gson.fromJson(jsonStr, cl); } return (T) obj; } }