自定义DateAdapter
public class DateAdapter implements JsonDeserializer<Date> { private final DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); public Date deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { try { return df.parse(arg0.getAsString()); } catch (ParseException e) { e.printStackTrace(); } return null; } }
然后在代码中创建转换器即可:
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new DateAdapter()).create();
时间: 2024-11-01 14:47:09