使用 google gson 转换Timestamp或Date类型为JSON字符串.
http://blog.csdn.net/z69183787/article/details/13016289
创建类型适配类:
    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);
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); 
使用 google gson 转换Timestamp或Date类型为JSON字符串.的更多相关文章
- [转]使用 google gson 转换Timestamp或Date类型为JSON字符串.
		
创建类型适配类: import java.lang.reflect.Type; import java.sql.Timestamp; import java.text.DateFormat; 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框架,写个博客系统,在后台管理的日志管理中,遇到了后台查询的日期格式的结果返回到页面变成了日期的时间戳了.然后摸索了三种方法来解决.页面的显示问题如下图. 问题页面回顾: 本案 ...
 - 工具请求接口参数为string类型的JSON字符串时需要加转义字符模拟测试
		
例如postMan传String类型的json字符串请后台接口时,需要\转义
 - 【JavaScript】JS将Java的Timestamp转为Date类型
		
遇到一个小需求,由于要填充日期插件里的数据,前台要把java后台传来的Date类型的数据转成YYYY-MM-DD格式的时间数据.通过json传输,Java的Date类型的数据自动转成了时间戳,例如 “ ...
 - GSON转换日期数据为特定的JSON数据
		
通过JSON传递数据的时候经常需要传递日期,Java中可以通过GSON将日期转换为特定格式的JSON数据. 1.普通的GSON转换日期 public void query(HttpServletReq ...
 - 将String类型的json字符串转换成java对象
		
1,import com.fasterxml.jackson.databind.ObjectMapper; ObjectMapper mapper = new ObjectMapper(); Mycl ...
 - 关于Gson无法将匿名类转化为json字符串的问题
		
在使用gson过程中,一般会将数据存在一个对象模型中,使用gson将模型转换成json字符串用于数据交互. 代码形如: ArrayList<String> list = new Array ...
 - coding++:SpringBoot 处理前台字符串日期自动转换成后台date类型的三种办法
		
第(1)种: 使用@DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)注解在实体字段上. 这种方式的优点是:可以灵活的定义接收的类型 缺点很明显:不能全局统 ...
 
随机推荐
- Android之WebView学习
			
WebView常用方法 WebSettings 在使用WebView前我们都要进行相关的配置,常见的操作如下: WebSettings settings = mWebView.getSettings( ...
 - jquery学习笔记----元素筛选
			
1.eq() 筛选指定索引号的元素2.first() 筛选出第一个匹配的元素3.last() 筛选出最后一个匹配的元素4.hasClass() 检查匹配的元素是否含有指定的类5.filter() 筛 ...
 - 【转载】 Python动态生成变量
			
用Python循环创建多个变量, 如创建 a1= .a2= .a3= .a4= .a5= 或 self.a1= .self.a2= . self.a3= 一. 可以通 ...
 - phpMailer在thinkPHP框架中邮件发送
			
资源下载地址:http://pan.baidu.com/s/1c0kAoeO 提取码:ry5v 关键代码:application/Common/Common/funciton.php <?php ...
 - CG&CAD resource
			
Computational Geometry The Geometry Center (UIUC) Computational Geometry Pages (UIUC) Geometry in Ac ...
 - 软考之PV操作(同步)
			
这几天,陆续有那么三两个同学跟我讨论了一下关于软考上的PV操作的题,吾虽不才,但还是把同学们讲通了,在此,特分享一下自己的思路和想法,愿对大家有点帮助! 下面,我们就通过自己做过的试卷上两道题来分析: ...
 - 多个$(document).ready()函数的执行顺序问题,(未解决)
			
今天遇到了一个问题: jQuery获取不了动态添加的元素,我使用的是append添加的.寻求了帮助,得到解决方案: 在文件开头写上这样一段代码来获取,写在$(document).ready()里面. ...
 - AOP静态代理解析2-代码织入
			
当我们完成了所有的AspectJ的准备工作后便可以进行织入分析了,首先还是从LoadTimeWeaverAwareProcessor开始. LoadTimeWeaverAwareProcessor实现 ...
 - 数据库查询Database中的表
			
public class UserDA { SqlConnection conn; SqlCommand cmd; public UserDA(Use uuu) { conn =new SqlConn ...
 - Swift3.0语言教程获得一个公共的前缀
			
Swift3.0语言教程获得一个公共的前缀 Swift3.0语言教程获得一个公共的前缀,当在一个程序中有多个字符串时,我们需要判断是否有两个字符串有公共的前缀时,是很困难的.在NSString中的co ...