Java Date class and SimpleDateFormat class

Source: Internet
Author: User
Tags string format

1  Public classDemo6_date {2 3     /*4 * A: class date indicates a specific moment, accurate to milliseconds5 * B: Construction method6 * Public date () No parameter represents the current time7 * Public date (long date) when parameters are passed in to obtain a specific time8 * C: Member Method9 * Public long getTime () Gets the millisecond value of the time, similar to System.currenttimemillis ()Ten * public void settime (long time) One      *   A      */ -      Public Static voidMain (string[] args) { -         //demo1 (); theDate d =NewDate (); -System.out.println (D.gettime ());//1529382803822 -System.out.println (System.currenttimemillis ());//1529382803822 -          +     } -  +      Public Static voiddemo1 () { ADate D1 =NewDate (); atSystem.out.println (D1);//Tue June 12:29:27 CST 2018 -Date D2 =NewDate (0); -System.out.println (D2);//Thu Jan 08:00:00 CST 1970 constructs an incoming parameter of 0 o'clock, returns the January 1, 1970, the reason is 8 points, because our Beijing East eight district -     } -}
1  Public classDemo7_simpledateformat {2 3     /*4 * Its parent class is DateFormat is an abstract class5 * B: Construction method6 * Public SimpleDateFormat ()7 * Public SimpleDateFormat (String pattern) pattern-describes patterns for date and time formats8 * C: Member Method9 * Public final string format (date date) formats the given date as a datetime stringTen * Public Date Parse (string source) parses the string literal, generating a Date object One      */  A      Public Static voidMain (string[] args)throwsParseException { -   -         //demo1 (); the         //Demo2 (); -String s = "June 06, 2017 06:06:06"; -SimpleDateFormat SDF =NewSimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"); -Date d =Sdf.parse (s); +System.out.println (d);//Tue June 06:06:06 CST -     } +  A      Public Static voidDemo2 () { atDate d =NewDate ();//Get current Time -SimpleDateFormat SDF =NewSimpleDateFormat ("yyyy mm month DD Day HH:mm:ss");//Create date format class object -System.out.println (Sdf.format (d));//format a string date June 06, 2017 13:24:30 -     } -  -      Public Static voiddemo1 () { inDate d =NewDate ();//Get current Time -SimpleDateFormat SDF =NewSimpleDateFormat ();//Create date format class object toSystem.out.println (Sdf.format (d));//format a string date 17-6-6 pm 1:17 +     } -  the}

1 /*2 * Get the current time how many days you were born3 * 1. Put the current date and your birthday in string type first4 * 2. Format the two string as a Date object5 * 3. Get the millisecond difference of two objects6 * 4. The total number of days divided by the conversion millisecond difference (1000*60*60*24)7      */8      Public Static voidMain (string[] args)throwsParseException {9 TenString today = "June 06, 2017"; OneString birthday = "June 06, 1998"; ASimpleDateFormat SDF =NewSimpleDateFormat ("yyyy mm month DD Day"); -Date D1 =Sdf.parse (today); -Date D2 =sdf.parse (birthday); theSystem.out.println (D1.gettime ()-d2.gettime ()); -         LongTime = D1.gettime ()-d2.gettime (); -System.out.println (time/1000/60/60/24); -}

Java Date class and SimpleDateFormat class

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.