今天解析json数据的时候出错了。 Invalid time zone indicator '6' (at offset 0) 也配置了Gson的setDateFormat格式 ,然而并没有用 最后还是加了registerTypeAdapter,这下通用了
private static final Gson gson = new GsonBuilder()
//.setDateFormat("yyyy-MM-dd 'T' HH:mm:ss 'Z'")
//.setDateFormat("yyyy-MM-dd HH:mm:ss")
.registerTypeAdapter(Date.class,new DateDeserializer())
.create();
private static class DateDeserializer implements JsonDeserializer<Date> {
private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json != null) {
final String jsonString = json.getAsString();
try {
return dateFormat.parse(jsonString);
} catch (ParseException e) {
e.printStackTrace();
}
final long jsonLong= json.getAsLong();
try {
return new Date(jsonLong);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}