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 ();