使用 google gson 转换Timestamp或Date类型为JSON字符串.

gson lib 包为1.4版本.

创建类型适配类:

Timestamp类型适配类代码 复制代码
  1. import java.lang.reflect.Type;   
  2. import java.sql.Timestamp;   
  3. import java.text.DateFormat;   
  4. import java.text.ParseException;   
  5. import java.text.SimpleDateFormat;   
  6. import java.util.Date;   
  7.   
  8. import com.google.gson.JsonDeserializationContext;   
  9. import com.google.gson.JsonDeserializer;   
  10. import com.google.gson.JsonElement;   
  11. import com.google.gson.JsonParseException;   
  12. import com.google.gson.JsonPrimitive;   
  13. import com.google.gson.JsonSerializationContext;   
  14. import com.google.gson.JsonSerializer;   
  15.   
  16. public class TimestampTypeAdapter implements JsonSerializer<Timestamp>, JsonDeserializer<Timestamp>{   
  17.     private final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  18.     public JsonElement serialize(Timestamp src, Type arg1, JsonSerializationContext arg2) {   
  19.         String dateFormatAsString = format.format(new Date(src.getTime()));   
  20.         return new JsonPrimitive(dateFormatAsString);   
  21.     }   
  22.   
  23.     public Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {   
  24.         if (!(json instanceof JsonPrimitive)) {   
  25.             throw new JsonParseException("The date should be a string value");   
  26.         }   
  27.   
  28.         try {   
  29.             Date date = format.parse(json.getAsString());   
  30.             return new Timestamp(date.getTime());   
  31.         } catch (ParseException e) {   
  32.             throw new JsonParseException(e);   
  33.         }   
  34.     }   
  35.   
  36. }  

  类型适配类

   应用类型适配器 写道

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 类型的时间转换第二种方式;

 

Java代码 复制代码
  1. Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();   
  2. String jsonString = gson.toJson(new Date(System.currentTimeMillis()),Date.class);   
  3. System.out.println(jsonString);  
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"

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java_时间戳与Date_相互转化 (2012-02-10 17:54:49) 转载▼ 标签: java 时间戳 date 转换 转化 杂谈 分类: java 1、时间戳的定义   时间戳是指文件属性里的创建、修改、访问时间。 数字时间戳技术是数字签名技术一种变种的应用。在电子商务交易文件中,时间是十分重要的信息。在书面合同中,文件签署的日期和签名一样均是十分重要的防止文件被伪造和篡改的关键性内容。数字时间戳服务(DTS:digital time stamp service)是网上电子商务安全服务项目之一,能提供电子文件的日期和时间信息的安全保护。 编辑本段组成部分   时间戳(time-stamp)是一个经加密后形成的凭证文档,它包括三个部分:    (1)需加时间戳的文件的摘要(digest);    (2)DTS收到文件的日期和时间;    (3)DTS的数字签名。    一般来说,时间戳产生的过程为:用户首先将需要加时间戳的文件用Hash编码加密形成摘要,然后将该摘要发送到DTS,DTS在加入了收到文件摘要的日期和时间信息后再对该文件加密(数字签名),然后送回用户。    书面签署文件的时间是由签署人自己写上的,而数字时间戳则不然,它是由认证单位DTS来加的,以DTS收到文件的时间为依据。 2、时间戳转化为Date(or String) //时间戳转化为Sting或Date SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); Long time=new Long(445555555); String d = format.format(time); Date date=format.parse(d); System.out.println("Format To String(Date):"+d); System.out.println("Format To Date:"+date); 运行结果: Format To String(Date):1970-01-06 11:45:55 Format To Date:Tue Jan 06 11:45:55 CST 1970 3、Date(or String)转化为时间戳 //Date或者String转化为时间戳 SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); String time="1970-01-06 11:45:55"; Date date = format.parse(time); System.out.print("Format To times:"+date.getTime()); 运行结果: Format To times:445555000 4、注意 定义SimpleDateFormat时new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );里面字符串头尾不能有空格,有空格那是用转换时对应的时间空格也要有空格(两者是对应的),比如: //Date或者String转化为时间戳 SimpleDateFormat format = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " ); String time="1970-01-06 11:45:55"; Date date = format.parse(time); System.out.print("Format To times:"+date.getTime()); 运行结果(报错): Exception in thread "main" java.text.ParseException: Unparseable date: "1970-01-06 11:45:55" 改正: //Date或者String转化为时间戳 SimpleDateFormat format = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " ); String time=" 1970-01-06 11:45:55 ";//注:改正后这里前后也加了空格 Date date = format.parse(time); System.out.print("Format To times:"+date.getTime()); 运行结果: Format To times:445555000

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值