Java _ Timestamp and date _ mutual conversion functions

Source: Internet
Author: User

1. convert a date to a timestamp
Public long gettimestamp () throws parseexception {</P> <p> date date1 = new simpledateformat ("yyyy/mm/dd hh: mm: SS") <br/>. parse ("2009/12/11 00:00:00"); <br/> date date2 = new simpledateformat ("yyyy/mm/dd hh: mm: SS") <br/>. parse ("1970/01/01 08:00:00"); <br/> long l = date1.gettime ()-date2.gettime ()> 0? Date1.gettime () <br/>-date2.gettime (): date2.gettime ()-date1.gettime (); <br/> long Rand = (INT) (math. random () * 1000); </P> <p> return rand; <br/>}

Ii. Convert the timestamp to the date type

Public void getdate (string unixdate) {</P> <p> simpledateformat FM1 = new simpledateformat ("DD/MM/YYYY hh: mm: SS "); <br/> simpledateformat FM2 = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); <br/> long unixlong = 0; <br/> string date = ""; <br/> try {<br/> unixlong = long. parselong (unixdate) * 1000; <br/>} catch (exception ex) {<br/> system. out. println ("String Conversion long error. Please confirm that the data can be converted! "); <Br/>}< br/> try {<br/> date = fm1.format (unixlong); <br/> date = fm2.format (new date (date )); <br/>} catch (exception ex) {<br/> system. out. println ("String Conversion date error. Please confirm that the data can be converted! "); <Br/>}< br/> system. Out. println (date); <br/>}

The system obtains the timestamp:

System. currenttimemillis (); 

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.