Java_ time stamp and Date_ mutual transformation of the implementation code _java

Source: Internet
Author: User

1, the definition of time stamp

Timestamp refers to the creation, modification, and access time in file attributes.

Digital timestamp technology is a kind of application of digital signature technology. Time is a very important information in the electronic commerce transaction file. In a written contract, the date and signature of the document are important to prevent the forgery and tampering of the documents. Digital timestamp service (dts:digital time stamp services) is one of the online e-commerce security services, which can provide the security protection of the date and time information of electronic files.

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

    1. Summary of documents requiring timestamp (digest);
    2. The date and time that DTS received the file;
    3. The digital signature of DTS.

In general, the timestamp process is that the user first encrypts a file that needs to be stamped with a hash code to form a summary, and then sends the digest to Dts,dts to encrypt (digitally sign) the file after adding the date and time information that received the summary of the file, and then send it back to the user.

The time of the written signature document is written by the signer himself, and the digital timestamp is not, it is added by DTS of the authentication unit, and the time of receiving the document is based on DTS.

2, timestamp converted to date (or String)

The timestamp is converted 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:" +date); 
  Run Result:
    format to String (Date): 1970-01-06 11:45:55
Format to date:tue the 11:45:55 CST 1970

3, date (or String) converted to timestamp

Date or string is converted to 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 ());

Run Result:

Format to times:445555000

4. Note

Define SimpleDateFormat Newsimpledateformat ("Yyyy-mm-dd HH:mm:ss"); inside the string can not have a space, there is a space that is the conversion of the corresponding time space to have a space (the two are corresponding), such as:

 Date or string is converted to 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 ()); 

Run result (Error):

Exception in thread "main" Java.text.ParseException:Unparseable Date: "1970-01-06 11:45:55"

Correct:

 Date or string is converted to timestamp
simpledateformat format = Newsimpledateformat ("Yyyy-mm-dd HH:mm:ss"); 
String time= "1970-01-06 11:45:55";//Note: Before and after the correction, a space is also added to 
date date = Format.parse (time); 
System.out.print ("Format to Times:" +date.gettime ()); 
 Run Result:
  Format to times:445555000

The gettime () in the date class in Java is getting the timestamp, the timestamp generated in Java is accurate to the millisecond level, and UNIX is accurate to the second level, so the timestamp generated through Java needs to be divided by 1000.

Two, 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 ti me = "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 the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.