/** * date overrides the ToString () method and uses a string to describe the time represented by the current date object. * such as: mon feb 17 15:36:55 cst 2014 * actually the string returned by the ToString () method of date has a very clear description of the time, * but for non-English regions, the string is not friendly enough, We prefer to represent time in a particular region. * For example, we are more accustomed to the following style: 2014-02-17 15:36:55 Monday * java provides us with a class, SimpleDateFormat, which can complete date and string * Format conversion between . * Simply put, SimpleDateFormat is the conversion of strings to date based on a particular date format. * For example: yyyy mm month DD Day--hh:mm:ss can match 2014 year January 06--13:22:41 * * Instantiate simpledateformat sdf = new First SimpleDateFormat (YYYY-MM-DD); * * then convert each other in two formats. * Date --- string * string timestr = sdf.format (date); * * String --- Date * Date date = String.parse (TIMESTR); * * */
Public classSimpleDateFormatDemo1 { Public Static voidMain (string[] args)throwsParseException {//Instantiate SimpleDateFormat FirstSimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd"); //instantiating a dateDate Date =NewDate (); //Date---StringString Timestr =Sdf.format (date); //output the converted stringSystem.out.println (TIMESTR);//2016-04-30//String---DateString time = "2016-12-8"; //parse the string into the date format according to the SDF definition formatDate =Sdf.parse (time); //Output converted dateSYSTEM.OUT.PRINTLN (date);//Thu Dec 00:00:00 gmt+08:00 }}
SimpleDateFormatDemo1 Conversion of date and string formats