1. Definition of timestamp
Timestamp refers to the creation, modification, and access time in file attributes.
Digital timestamp technology is an application of a variant of digital signature technology. In e-commerce transaction documents, time is very important information. In written contracts, the date of the document signing is as important as the signature of the document to prevent the document from being forged and tampered with. Digital time stamp service (DTS: digital time stamp service) is one of the online e-commerce security services projects, which can provide security protection for date and time information of electronic files.
A time-stamp is an encrypted credential document that consists of three parts:
Generally speaking, the process of generating timestamps is: the user first encrypts the file that needs to be timestamped into a digest with Hash encoding, and then sends the digest to DTS. After adding the date and time information of the file digest received, the DTS encrypts the file (digital signature), and then sends it back to the user.
The time for signing the document in writing is written by the signer himself, while the number timestamp is not the case. It is added by the certification unit DTS and is based on the time when the document is received by DTS.
2. Convert timestamps to Date (or String)
//Convert timestamps to Sting or Date SimpleDateFormat format = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Long time=newLong(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:"+d); Running result: Format To String(Date):1970-01-06 11:45:55Format To Date:Tue Jan 06 11:45:55 CST 1970 3. Date (or String) is converted into a timestamp
//Date or String is converted into timestamp SimpleDateFormat format = newSimpleDateFormat("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()); Running results:
Format To times:445555000
4. Pay attention
When defining SimpleDateFormat, newSimpleDateFormat("yyyy-MM-dd HH:mm:ss"); there cannot be spaces at the beginning and end of the string. If there are spaces, the corresponding time spaces during conversion must also have spaces (the two are corresponding), such as:
//Date or String is converted into timestamp SimpleDateFormat format = newSimpleDateFormat(" 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()); Running result (Error):
Exception in thread "main"Java.text.ParseException: Unparseable date: "1970-01-06 11:45:55"
correct:
//Date or String is converted into timestamp SimpleDateFormat format = newSimpleDateFormat(" yyyy-MM-dd HH:mm:ss "); String time=" 1970-01-06 11:45:55 ";//Note: After correction, spaces are added here. Date date = format.parse(time); System.out.print("Format To times:"+date.getTime()); Running result: Format To times:445555000 1. GetTime() in the Date class in java obtains the timestamp. The timestamp generated in java is accurate to the millisecond level, while in unix is accurate to the second level, so the timestamp generated through java needs to be divided by 1000.
2. The following is the java code
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Baidu { /** * @param args */ public static void main(String[] args) { try { String time = "2011/07/29 14:50:11"; Date date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(time); long unixTimestamp = date.getTime()/1000; System.out.println(unixTimestamp); } catch (ParseException e) { e.printStackTrace(); } } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.