Scenario: There is a string that needs to be converted to date, for example, "Fri Jun 08 15:22:40 CST 2012 ".
Solution: Use the parse () method of simpledateformate
ExampleCode:
Import Java. text. dateformat; import Java. text. parseexception; import Java. text. simpledateformat; import Java. util. date; import Java. util. locale; import Java. util. timezone; public class string2date {public static void main (string [] ARGs) {string STR = "Fri Jun 08 15:22:40 CST 2012"; Date mydate = new date (); simpledateformat SDF = new simpledateformat ("eee Mmm dd hh: mm: ss z yyyy", locale. english); try {mydate = SDF. parse (STR);} catch (parseexception e) {e. printstacktrace ();} system. out. println ("mydate:" + mydate. tostring ());}}
For the construction method of the first parameter in simpledateformate, see the JDK documentation, which contains a list and many examples.
Link: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html