1 PackageFengke.dateandsimpledateformat;2 3 Importjava.text.ParseException;4 ImportJava.text.SimpleDateFormat;5 Importjava.util.Date;6 /**7 * Date and use of SimpleDateFormat;8 * @authorFeng Ke9 * Content:Ten * creation of 1.date; One * 2.simpleDateFormat Usage: Format (); Parse (). A * - */ - Public classDateandsimpledateformat { the - StaticDate date=NewDate (); - - Public Static voidMain (string[] args) { +Date date2=NewDate (); -Date Date3 =NULL; + //static methods can only call static variables A //date format not used atSYSTEM.OUT.PRINTLN ("Not used date format:"); - System.out.println (date); - //Set Date format -SimpleDateFormat simpledateformat=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); - //formatting time, Format () method converts a date to text in the specified format -String date2_temp=Simpledateformat.format (date2); in //Use date format -SYSTEM.OUT.PRINTLN ("Use date format (date---" String): "); to System.out.println (date2_temp); + //convert text to a date using the parse () method -String time= "January 14, 2015 9:50:23"; the //Parse () converts the text date to date *SimpleDateFormat simpledateformat2=NewSimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"); $ Try {Panax Notoginsengdate3=Simpledateformat2.parse (time); -}Catch(ParseException e) { the //TODO auto-generated Catch block + e.printstacktrace (); A } theSYSTEM.OUT.PRINTLN ("Use date conversion format (String---" date): "); + System.out.println (date3); - $ $ } - -}
Use of Date and SimpleDateFormat