1. dateutil. Java, the code is as follows:
1 Packagecn.itcast_04;2 3 Importjava.text.ParseException;4 ImportJava.text.SimpleDateFormat;5 Importjava.util.Date;6 7 /**8 * This is the tool class for converting dates and strings to each other9 * Ten * @authorWind One */ A Public classDateutil { - PrivateDateutil () { - } the - /** - * The function of this method is to turn the date into a string - * + * @paramD - * Converted Date Object + * @paramformat A * Pass over the format to be converted at * @returnformatted string - */ - Public Staticstring datetostring (Date D, string format) { - //SimpleDateFormat sdf = new SimpleDateFormat (format); - //return Sdf.format (d); - return NewSimpleDateFormat (format). Format (d); in } - to /** + * The function of this method is to parse a string into a Date object - * the * @params * * Parsed string $ * @paramformatPanax Notoginseng * Pass over the format to be converted - * @returnResolved Date Object the * @throwsparseexception + */ A Public StaticDate stringtodate (string s, string format) the throwsParseException { + return NewSimpleDateFormat (format). Parse (s); - } $}
2. Dateutildemo. Java, as follows:
1 Packagecn.itcast_04;2 3 Importjava.text.ParseException;4 Importjava.util.Date;5 6 /*7 * Test of Tool class8 */9 Public classDateutildemo {Ten Public Static voidMain (string[] args)throwsParseException { OneDate d =NewDate (); A //YYYY-MM-DD HH:mm:ss -String s = dateutil.datetostring (d, "yyyy mm month DD Day HH:MM:SS"); - System.out.println (s); the -String s2 = dateutil.datetostring (d, "yyyy mm month DD Day"); - System.out.println (S2); - +String s3 = dateutil.datetostring (D, "HH:mm:ss"); - System.out.println (S3); + AString str = "2014-10-14"; atDate dd = dateutil.stringtodate (str, "YYYY-MM-DD"); - System.out.println (DD); - } -}
The results of the test run are as follows:
Java Fundamentals Hardening 93: Date Tool class authoring and test Cases