1、时间戳的定义
时间戳(timestamp),通常是一个数字序列,唯一地标识某一刻的时间,指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的毫秒数。
数字时间戳技术是数字签名技术一种变种的应用。在电子商务交易文件中,时间是十分重要的信息。在书面合同中,文件签署的日期和签名一样均是十分重要的防止文件被伪造和篡改的关键性内容。数字时间戳服务(DTS:digital time stamp service)是网上电子商务安全服务项目之一,能提供电子文件的日期和时间信息的安全保护。
时间戳(time-stamp)是一个经加密后形成的凭证文档,它包括三个部分:
(1)需加时间戳的文件的摘要(digest);
(2)DTS收到文件的日期和时间;
(3)DTS的数字签名。
一般来说,时间戳产生的过程为:用户首先将需要加时间戳的文件用Hash编码加密形成摘要,然后将该摘要发送到DTS,DTS在加入了收到文件摘要的日期和时间信息后再对该文件加密(数字签名),然后送回用户。
书面签署文件的时间是由签署人自己写上的,而数字时间戳则不然,它是由认证单位DTS来加的,以DTS收到文件的时间为依据。
有时后台返回给我们的是时间戳,我们就需要转换一下才能展示。
如
在easy-ui中
ID 用户名 姓名 年龄 性别 出生日期 创建日期 更新日期
formatter:后面是我们自定义的函数名,不是内置的
function formatDate(val, row) { var now = new Date(val); return now.format("yyyy-MM-dd hh:mm:ss");}
上面的Date对象是js的内置对象,但是Date对象没有format方法!!!
以后可能会遇到这种问题,我们需要用到js内置对象的某些方法,但是我们发现这些内置对象没有这些方法!,咋办呢?我们自己来扩展!
Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond }; if(/(y+)/.test(format)){ format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); } for(var k in o) { if(new RegExp("("+ k +")").test(format)){ format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); } } return format; };
时间戳处理
2、时间戳转化为Date(or String)
public class Test { public static void main(String[] args) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Long time = new Long(460742400000L); String toString = format.format(time); Date toDate = format.parse(toString); System.out.println("Format To String:" + toString); System.out.println("Format To Date:" + toDate); }}
运行结果:
Format To String:1984-08-08 00:00:00Format To Date:Wed Aug 08 00:00:00 CST 1984
3、Date(or String)转化为时间戳
public class Test { public static void main(String[] args) throws ParseException { SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); String time="2016-11-06 11:45:55"; Date date = format.parse(time); System.out.print("Format To timestamp:" + date.getTime()); }}
运行结果:
Format To times:1478403955000
4、注意
定义SimpleDateFormat时new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );里面字符串头尾不能有空格