Java Timestamp to date

Source: Internet
Author: User
Tags timestamp to date

1. Definition of time stamp

Timestamp (timestamp), usually a sequence of characters that uniquely identifies a moment in time, referring to the total number of seconds GMT January 01, 1970 00:00 00 seconds (Beijing time January 01, 1970 08:00 00 seconds) up to now.

Digital timestamp technology is a variant application of digital signature technology. In e-Commerce transaction files, time is a very important information. In a written contract, the date and signature of the document are important to prevent the document from being forged and tampered with. Digital Timestamp Services (dts:digital time stamp service) is one of the online e-commerce security services, which can provide the security of date and time information of electronic documents.

A timestamp (Time-stamp) is an encrypted document that forms a credential that consists of three parts:

(1) Summary of documents requiring time stamping (digest);

(2) The date and time that DTS received the file;

(3) The digital signature of DTS.

In general, the process of time stamping is that the user first encrypts the file that needs to be timestamp into a hash code, and then sends the digest to Dts,dts after it has joined the date and time information of the received file digest and then encrypts (digitally signed) the file and then sends it back to the user.

The time of the written signature is written by the signer himself, and the digital timestamp is not, it is added by the Certification Unit DTS, based on the time that DTS received the file.

Sometimes back to us in the background is a timestamp, we need to convert to show .

Such as

In the Easy-ui

<Div>    <Tableclass= "Easyui-datagrid"ID= "UserList"title= "Member list"data-options= "Singleselect:false,collapsible:true,pagination:true,url: '/rest/user/list ', method: ' Get ', Pagesize:5, toolbar:toolbar,pagelist:[2,5,10] ">        <thead>        <TR>            <thdata-options= "field: ' CK ', checkbox:true"></th>            <thdata-options= "field: ' ID ', width:60">Id</th>            <thdata-options= "field: ' UserName ', width:200">User name</th>            <thdata-options= "field: ' Name ', width:100">Name</th>            <thdata-options= "field: ' Age ', width:100">Age</th>            <thdata-options= "field: ' Sex ', width:80,align: ' Right ',formatter:formatset">Gender</th>            <thdata-options= "field: ' Birthday ', width:80,align: ' Right ',formatter:formatbirthday">Date of birth</th>            <thdata-options= "field: ' Created ', Width:130,align: ' Center ',formatter:formatdate">Date Created</th>            <thdata-options= "field: ' Updated ', width:130,align: ' Center ',formatter:formatdate">Update Date</th>        </TR>        </thead>    </Table></Div>
Formatter: The following is our custom function name, not the built-in

<script type= "Text/javascript" >function FormatDate (val, row) {var now = new Date (val);    Return Now.format ("Yyyy-mm-dd hh:mm:ss"); }    functionFormatbirthday (val, row) {varnow =NewDate (Val); returnNow.format ("Yyyy-mm-dd"); }    functionFormatset (val, row) {if(val = = 1) {            returnMale; } Else if(val = = 2) {            returnFemale; } Else {            returnUnknown; }    }    functionGetselectionsids () {varUserList = $ ("#userList"); varSels = Userlist.datagrid ("Getselections"); varids = [];  for(varIinchsels)        {Ids.push (sels[i].id); } IDs= Ids.join (","); returnIDs; }    vartoolbar =[{text:New, Iconcls:' Icon-add ', Handler:function () {            $(' #userAdd '). Window (' Open '); }}, {text:Edit, Iconcls:' Icon-edit ', Handler:function() {$.messager.alert (' Hint ', ' This function is implemented by the trainees themselves! '); }}, {text:Delete, Iconcls:' Icon-cancel ', Handler:function () {            varids =Getselectionsids (); if(Ids.length = = 0) {$.messager.alert (' Prompt ', ' no user selected! '); return; } $.messager.confirm (' Confirm ', ' OK Delete ID for ' + IDs + ' member? ‘,function(r) {if(R) {$.post ("/user/delete", {' IDs ': IDs},function(data) {if(Data.status = = 200) {$.messager.alert (' Hint ', ' Remove member success! ', Undefined,function () {                                $("#userList"). DataGrid ("Reload"));                        });                }                    });        }            }); }    }, ‘-‘, {text:Export, Iconcls:' Icon-remove ', Handler:function () {            varOptins = $ ("#userList"). DataGrid ("Getpager"). Data ("pagination"). Options; varpage =Optins.pagenumber; varrows =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");}

The Date object above is JS's built-in object, but the date object does not have the Format Method!!!

You may encounter this problem later, we need to use some methods of JS built-in objects, but we found that these built-in objects do not have these methods! , how? We can expand it ourselves!

Date.prototype.format =function(format) {varn \  {     "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(varKincho) {if(NewRegExp ("(" + K + ")"). Test (format)) {format= Format.replace (regexp.$1, Regexp.$1.length==1 o[k]: ("XX" + o[k]). substr ("" +O[k]) (length)); }     }     returnformat;};

Time stamp Processing

2. Timestamp converted to date (or String)

 Public classTest { Public Static voidMain (string[] args)throwsparseexception {simpledateformat format=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Long Time=NewLong (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); }}

Operation Result:

Format to string:1984-08-08 00:00:0000:00:00 CST 1984

3, date (or String) converted to timestamp

 Public class Test {    publicstaticvoidthrows  parseexception {        =  New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss" );        Stringtime = "2016-11-06 11:45:55";         = Format.parse (time);        System.out.print ("Format to Timestamp:" + date.gettime ());}    }

Operation Result:

Format to times:1478403955000

4. Attention

Define SimpleDateFormat when new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); There are no spaces in the string

Java timestamp to date (go)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.