Java Fundamentals Hardening the DateFormat of the 92:dateformat class to achieve the conversion of dates and strings

Source: Internet
Author: User
Tags dateformat string format

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.