Package COM. apple. sqm. common; import Java. SQL. timestamp; import Java. text. simpledateformat; import Java. util. calendar; import Java. util. date; import Java. util. timezone;/*** @ author junbos **/public class datehelper {Private Static final string default_pattern = "yyyymmddhhmmss "; /*** @ Param time * @ Param pattern * @ return */public static string timestamp2str (timestamp time, string pattern) {If (Ti Me = NULL) {Throw new illegalargumentexception ("timestamp is null");} If (pattern! = NULL &&! "". Equals (pattern) {If (! "Yyyymmddhhmmss". Equals (pattern )&&! "Yyyy-mm-dd hh: mm: SS". Equals (pattern )&&! "Yyyy-mm-dd". Equals (pattern )&&! "Mm/DD/YYYY ". equals (pattern) {Throw new illegalargumentexception ("Date Format [" + pattern + "] is invalid") ;}} else {pattern = default_pattern;} calendar Cal = calendar. getinstance (timezone. getdefault (); Cal. settime (time); simpledateformat SDF = new simpledateformat (pattern); Return SDF. format (Cal. gettime ();} public static timestamp str2timestamp (string timestr, string pattern) {timestamp result = NULL; If (timestr = NULL) {Throw new illegalargumentexception ("timestamp is null");} If (pattern! = NULL &&! "". Equals (pattern) {If (! "Yyyymmddhhmmss". Equals (pattern )&&! "Yyyy-mm-dd hh: mm: SS". Equals (pattern )&&! "Mm/DD/YYYY hh: mm: SS". Equals (pattern )&&! "Yyyy-mm-dd". Equals (pattern )&&! "Mm/DD/YYYY ". equals (pattern) {Throw new illegalargumentexception ("Date Format [" + pattern + "] is invalid") ;}} else {pattern = default_pattern;} date d = NULL; simpledateformat SDF = new simpledateformat (pattern); try {d = SDF. parse (timestr); Result = new timestamp (D. gettime ();} catch (exception e) {e. printstacktrace ();} return result;} public static void main (string [] ARGs) {system. out. println (str2timestamp ("13:10:00", "yyyy-mm-dd hh: mm: SS"); system. out. println (timestamp2str (New timestamp (system. currenttimemillis (), "yyyy-mm-dd hh: mm: SS"); system. out. println (str2timestamp ("01/25/2008", "mm/DD/YYYY "));}}