gson无法正常将时间戳转化成date
gson将时间戳转化成date时,报错
Failed to parse date ["1551950239757']: Invalid time zone indicator '3'
解决办法
添加一个long转date的解析器
@Test
public void fun1(){
GsonBuilder builder = new GsonBuilder();
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});
Gson gson = builder.create();
String str = "{\"name\":\"yjt\",\"date\":\"1552012460277\"}";
Person person = gson.fromJson(str,Person.class);
log.info("{}", person);
}
文章来自stackoverflow
Gson时间戳转换Date
本文介绍了解决Gson在将时间戳转换为Date类型时遇到的问题,通过自定义解析器实现正确转换,避免了因时间区域指示符导致的错误。
6781

被折叠的 条评论
为什么被折叠?



