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中
<div> <table class="easyui-datagrid" id="userList" title="会员列表" data-options="singleSelect:false,collapsible:true,pagination:true,url:‘/rest/user/list‘,method:‘get‘,pageSize:5,toolbar:toolbar,pageList:[2,5,10]"> <thead> <tr> <th data-options="field:‘ck‘,checkbox:true"></th> <th data-options="field:‘id‘,width:60">ID</th> <th data-options="field:‘userName‘,width:200">用户名</th> <th data-options="field:‘name‘,width:100">姓名</th> <th data-options="field:‘age‘,width:100">年龄</th> <th data-options="field:‘sex‘,width:80,align:‘right‘,formatter:formatSet">性别</th> <th data-options="field:‘birthday‘,width:80,align:‘right‘,formatter:formatBirthday">出生日期</th> <th data-options="field:‘created‘,width:130,align:‘center‘,formatter:formatDate">创建日期</th> <th data-options="field:‘updated‘,width:130,align:‘center‘,formatter:formatDate">更新日期</th> </tr> </thead> </table> </div>
formatter:后面是我们自定义的函数名,不是内置的
<script type="text/javascript"> function formatDate(val, row) { var now = new Date(val); return now.format("yyyy-MM-dd hh:mm:ss"); } function formatBirthday(val, row) { var now = new Date(val); return now.format("yyyy-MM-dd"); } function formatSet(val, row) { if (val == 1) { return "男"; } else if (val == 2) { return "女"; } else { return "未知"; } } function getSelectionsIds() { var userList = $("#userList"); var sels = userList.datagrid("getSelections"); var ids = []; for (var i in sels) { ids.push(sels[i].id); } ids = ids.join(","); return ids; } var toolbar = [{ text: ‘新增‘, iconCls: ‘icon-add‘, handler: function () { $(‘#userAdd‘).window(‘open‘); } }, { text: ‘编辑‘, iconCls: ‘icon-edit‘, handler: function () { $.messager.alert(‘提示‘, ‘该功能由学员自己实现!‘); } }, { text: ‘删除‘, iconCls: ‘icon-cancel‘, handler: function () { var ids = getSelectionsIds(); if (ids.length == 0) { $.messager.alert(‘提示‘, ‘未选中用户!‘); return; } $.messager.confirm(‘确认‘, ‘确定删除ID为 ‘ + ids + ‘ 的会员吗?‘, function (r) { if (r) { $.post("/user/delete", {‘ids‘: ids}, function (data) { if (data.status == 200) { $.messager.alert(‘提示‘, ‘删除会员成功!‘, undefined, function () { $("#userList").datagrid("reload"); }); } }); } }); } }, ‘-‘, { text: ‘导出‘, iconCls: ‘icon-remove‘, handler: function () { var optins = $("#userList").datagrid("getPager").data("pagination").options; var page = optins.pageNumber; var rows = optins.pageSize; $("<form>").attr({ "action": "/user/export/excel", "method": "POST" }).append("<input type=‘text‘ name=‘page‘ value=‘" + page + "‘/>") .append("<input type=‘text‘ name=‘rows‘ value=‘" + rows + "‘/>").submit(); } }]; </script>
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:00 Format 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" );里面字符串头尾不能有空格