今天解析json數據的時候出錯了。 Invalid time zone indicator '6' (at offset 0) 也配置了Gson的setDateFormat格式 ,然而並無用 最後仍是加了registerTypeAdapter,這下通用了json
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; } }