1. DateFormat class overview:
DateFormat is an abstract class of date/time formatting subclasses that formats and resolves dates or times in a language-independent manner. is an abstract class, so use its subclasses SimpleDateFormat
2. SimpleDateFormat Construction Method:
1 Public SimpleDateFormat () 2 Public SimpleDateFormat (String pattern)
3. Member Methods
1 Public Final String Format (date date) 2 Public Date Parse (String source)
4. code example:
1 Packagecn.itcast_03;2 3 Importjava.text.ParseException;4 ImportJava.text.SimpleDateFormat;5 Importjava.util.Date;6 7 /*8 * Date--String (formatted)9 * Public Final String format (date date)Ten * One * String--Date (parse) A * Public Date Parse (String source) - * - * Dateforamt: the date and string can be formatted and parsed, but because it is an abstract class, the specific subclass SimpleDateFormat is used. the * - * Construction method of SimpleDateFormat: - * SimpleDateFormat (): Default mode - * SimpleDateFormat (String pattern): given pattern + * How to write this pattern string? - * By looking at the API, we find the corresponding pattern + * Year y A * Month M at * Day D - * Hours H - * min M - * seconds S - * - * December 12, 2014 12:12:12 in */ - Public classDateformatdemo { to Public Static voidMain (string[] args)throwsParseException { + //Date--String - //Create Date Object theDate d =NewDate (); * //creating a formatted object $ //SimpleDateFormat sdf = new SimpleDateFormat ();Panax Notoginseng //given pattern -SimpleDateFormat SDF =NewSimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"); the //Public Final String format (date date) +String s =Sdf.format (d); A System.out.println (s); the + - //String--Date $String str = "2008-08-08 12:12:12"; $ //when parsing a string into a date, be aware that the format must match the given string format -SimpleDateFormat SDF2 =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); -Date DD =sdf2.parse (str); the System.out.println (DD); - }Wuyi}
The results of the operation are as follows:
Java Fundamentals Hardening the DateFormat of the 92:dateformat class to achieve the conversion of dates and strings