Java Fundamentals Hardening 93: Date Tool class authoring and test Cases

Source: Internet
Author: User
Tags string format

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

Related Article

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.