On the code:
import Java.text.simpledateformat;import java.util.Date; Public classDateutil {/** * Timestamp converted into a date format string * @param seconds exact to the second string * @param formatstr * @return*/ Public Staticstring timestamp2date (string seconds,string format) {if(Seconds = =NULL|| Seconds.isempty () | | Seconds.equals ("NULL")){ return ""; } if(Format = =NULL||Format.isempty ()) {Format="YYYY-MM-DD HH:mm:ss"; } SimpleDateFormat SDF=NewSimpleDateFormat (format); returnSdf.format (NewDate (Long.valueof (seconds+" the"))); } /** * Date format string converted to Timestamp * @param date string dates * @param format such as: Yyyy-mm-dd HH:MM:SS * @return*/ Public Staticstring Date2timestamp (string date_str,string format) {Try{SimpleDateFormat SDF=NewSimpleDateFormat (format); returnString.valueof (Sdf.parse (DATE_STR). GetTime ()/ +); } Catch(Exception e) {e.printstacktrace (); } return ""; } /** * Get current timestamp (accurate to seconds) * @return*/ Public StaticString TimeStamp () {LongTime =System.currenttimemillis (); String T= String.valueof (time/ +); returnT; } Public Static voidMain (string[] args) {String TimeStamp=TimeStamp (); System. out. println ("timestamp="+timestamp);//Run output: timestamp=1470278082System. out. println (System.currenttimemillis ());//Run output: 1470278082980//The function of this method is to return the current computer time, the expression format of the time is the current computer time and GMT time (GMT) January 1, 1970 0:0 0 seconds difference in the number of millisecondsString Date= Timestamp2date (TimeStamp,"YYYY-MM-DD HH:mm:ss"); System. out. println ("date="+date);//Run output: date=2016-08-04 10:34:42String TIMESTAMP2= Date2timestamp (date,"YYYY-MM-DD HH:mm:ss"); System. out. println (TIMESTAMP2);//Run output: 1470278082 } }
The interaction of the Java timestamp and the date format string