[转]使用 google gson 转换Timestamp或Date类型为JSON字符串.
创建类型适配类:
import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer; public class TimestampTypeAdapter implements JsonSerializer<Timestamp>, JsonDeserializer<Timestamp>{
private final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public JsonElement serialize(Timestamp src, Type arg1, JsonSerializationContext arg2) {
String dateFormatAsString = format.format(new Date(src.getTime()));
return new JsonPrimitive(dateFormatAsString);
} public Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (!(json instanceof JsonPrimitive)) {
throw new JsonParseException("The date should be a string value");
} try {
Date date = format.parse(json.getAsString());
return new Timestamp(date.getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
} }
类型适配类
应用类型适配器 写道
Gson gson = new GsonBuilder().registerTypeAdapter(Timestamp.class,new TimestampTypeAdapter()).setDateFormat("yyyy-MM-dd HH:mm:ss").create();
String jsonString = gson.toJson(resourceInfo,ResourceGeoInfo.class);
输出结果
{"positionTime":"2010-01-07 10:57:27"}
Date 类型的时间转换第二种方式;
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
String jsonString = gson.toJson(new Date(System.currentTimeMillis()),Date.class);
System.out.println(jsonString);
输出结果:
"2010-01-07 12:24:34"
[转]使用 google gson 转换Timestamp或Date类型为JSON字符串.的更多相关文章
- 使用 google gson 转换Timestamp或Date类型为JSON字符串.
http://blog.csdn.net/z69183787/article/details/13016289 创建类型适配类: import java.lang.reflect.Type; impo ...
- 使用 google gson 转换Timestamp为JSON字符串
package com.test.base; import java.lang.reflect.Type; import java.sql.Timestamp; import java.text.Da ...
- 后台date类型转换为json字符串时,返回前台页面的是long类型的时间戳问题解决
学习springboot框架,写个博客系统,在后台管理的日志管理中,遇到了后台查询的日期格式的结果返回到页面变成了日期的时间戳了.然后摸索了三种方法来解决.页面的显示问题如下图. 问题页面回顾: 本案 ...
- GSON转换日期数据为特定的JSON数据
通过JSON传递数据的时候经常需要传递日期,Java中可以通过GSON将日期转换为特定格式的JSON数据. 1.普通的GSON转换日期 public void query(HttpServletReq ...
- 【JavaScript】JS将Java的Timestamp转为Date类型
遇到一个小需求,由于要填充日期插件里的数据,前台要把java后台传来的Date类型的数据转成YYYY-MM-DD格式的时间数据.通过json传输,Java的Date类型的数据自动转成了时间戳,例如 “ ...
- 关于Gson无法将匿名类转化为json字符串的问题
在使用gson过程中,一般会将数据存在一个对象模型中,使用gson将模型转换成json字符串用于数据交互. 代码形如: ArrayList<String> list = new Array ...
- coding++:SpringBoot 处理前台字符串日期自动转换成后台date类型的三种办法
第(1)种: 使用@DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)注解在实体字段上. 这种方式的优点是:可以灵活的定义接收的类型 缺点很明显:不能全局统 ...
- springmvc/springboot处理前台字符串日期自动转换成后台date类型的三种办法
参考https://blog.csdn.net/eumenides_/article/details/79033505 补充一个:Formatter也可以实现.
- 将String类型的json字符串转换成java对象
1,import com.fasterxml.jackson.databind.ObjectMapper; ObjectMapper mapper = new ObjectMapper(); Mycl ...
随机推荐
- iOS开发之手势识别汇总
iOS开发之手势识别汇总 iOS开发中手势识别有六种: 轻击手势(TapGestureRecognizer), 轻扫手势 (SwipeGestureRecognizer), 长按手势(LongPres ...
- 数据结构--用Objective-C简单实现的数据结构:栈
前言:最近在学习数据结构,这里用Objective-C简单实现了一下栈.用Objective-C确实好容易,因为我使用了Cocoa框架提供了NSMutableArray作为存储元素的集合,操作集合元素 ...
- [转]从JVM角度看线程安全与垃圾收集
线程安全 Java内存模型中,程序(进程)拥有一块内存空间,可以被所有的线程共享,即MainMemory(主内存):而每个线程又有一块独立的内存空间,即WorkingMemory(工作内存).普通情况 ...
- Computer Network and Internet(1)
计算机网路相关的教材很少,TCP/IP,HTTP 协议非常多,很难找到一个合适的材料去学习. <计算机网络>自上而下方法是这个方面的经典之作. 1.what is internet? 1. ...
- 面试题整理:SQL(一)
1.横纵表转换 A表 Name Course Grade Alex English 80 Alex Chinese 70 Alex Japanese 85 Bob English 75 Bob Chi ...
- Asp.net MVC的Model Binder工作流程以及扩展方法(1) - Custom Model Binder
在Asp.net MVC中, Model Binder是生命周期中的一个非常重要的部分.搞清楚Model Binder的流程,能够帮助理解Model Binder的背后发生了什么.同时该系列文章会列举 ...
- mysql-3 检索数据(1)
SELECT 语句 SELECT检索表数据,必须至少给出两条信息--------想选择什么,以及从什么地方选择. 检索一个列 SELECT prod_name FROM products; 上述语句利 ...
- spring注入静态成员变量提示invalid setter method
果然还是不够细心啊,被坑一晚上.. 一个极其简单的小程序,但是需要通过xml文件配置注入一个值,唯一的特别是要注入的属性是类中的静态成员变量.. 如下,然后自动生成get和set方法..坑就从此开始了 ...
- aircack-ng抓握手包
1.关闭影响进程 airmon-ng check kill 将要进入监听模式的无线网卡断开它已连接的AP 2.查看无线网卡的名字 ifconfig ,例如 wlan0 3.进入监听模式: airmon ...
- [Notes] Learn Python2.7 From Python Tutorial
I have planed to learn Python for many times. I have started to learn Python for many times . Howeve ...