gson转换Timestamp类型 "2012-11-3 10:49:15" --> "2012-11-03 10:49:15"
来源:互联网 发布:日语学校 知乎 编辑:程序博客网 时间:2024/06/05 03:36
问题:
解决过程:
- public
class TimestampTypeAdapter implementsJsonSerializer<Timestamp>, JsonDeserializer<Timestamp>{ -
private final DateFormat newformat = SimpleDateFormat( "yyyy-MM-ddHH:mm:ss" ); -
public JsonElement serialize(Timestamp ts, Type t, JsonSerializationContext jsc) { -
String dfString = format.format(new Date(ts.getTime())); -
return new JsonPrimitive(dfString); -
} -
public Timestamp throwsdeserialize(JsonElement json, Type t, JsonDeserializationConte xt jsc) JsonParseException { -
if (!(json instanceofJsonPrimitive)) { -
throw new JsonParseException( "Thedate );should be a string value" -
} -
-
try { -
Date date = format.parse(json.getAsString()); -
return new Timestamp(date.getTime()); -
} catch (ParseException e) { -
throw new JsonParseException(e); -
} -
} -
}
应用类型适配器
- GsonBuilder
gsonBuilder new= GsonBuilder(); - gsonBuilder.setDateFormat("yyyy-MM-dd
hh:mm:ss" ); - gsonBuilder.registerTypeAdapter(Timestamp.class,new
TimestampTypeAdapter()); - Gson
GSON = gsonBuilder.create(); - String
json = GSON.toJson(new Timestamp(( newDate()).getTime()));
- gson转换Timestamp类型 "2012-11-3 10:49:15" --> "2012-11-03 10:49:15"
- 用gson如何处理json转换Timestamp类型
- 使用 google gson 转换Timestamp或Date类型为JSON字符串.
- 使用 google gson 转换Timestamp或Date类型为JSON字符串.
- 使用 google gson 转换Timestamp或Date类型为JSON字符串
- String,date,timestamp 类型转换
- Oracle timestamp类型转换成date类型
- date类型,怎么转换为timestamp类型?
- 输出数字2016-10-31 15:39:58.188是Timestamp类型 ,sql拼装
- String 和Timestamp类型的相互转换
- Date、String和Timestamp类型转换
- oracle.sql.TIMESTAMP类型转换处理 .
- Date、String和Timestamp类型转换
- Date、String和Timestamp类型转换
- Date、String和Timestamp类型转换
- MySQL中的时间类型转换timestamp
- Gson的fromJson指定转换类型
- 【GSON】复杂集合类型转换为Json
- S3C2440驱动简析——串口驱动 .
- C++笔记1
- ARQ自动重传协议模拟
- 软件架构风格
- 快捷方式查看系统的配置信息 使用dxdiag
- gson转换Timestamp类型 "2012-11-3 10:49:15" --> "2012-11-03 10:49:15"
- DNS中的正向解析与反向解析
- redis的图形界面管理工具:phpredisadmin
- Android源码解析--Looper
- 一例 Run-Time Check Failure #2 - Stack around the variable 'ID' was corrupted.的解决
- Adaboost 原理 应用
- 简单感知器模型解决简单真值表问题
- Struts2单选按钮标签s:radio的使用及其设置默认值
- Java 推荐读物与源代码阅读