Java knowledge Summary (4) -- Date Processing

Source: Internet
Author: User

1. format the date (simpledateformat );

A. Generate a simpledateformat Class Object and specify the format. For example:

Simpledateformat SDF = new simpledateformate ("mm DD, YYYY ");

B. Call the simpledateformat Class Object format (...) method for formatting. For example:

Date = new date ();

String dateformat = SDF. Format (date );

2. Obtain the year, month, day, hour, minute, and second of the time respectively. Example: (obtain the year method)

Date = new date ();

Calender. settime (date );

Calender calender = calender. getinstance ();

Int year = calender. Get (calender. year );

3. Convert string time to time objects.For example:

String S = "04:54:20 ";

Dateformat df = newsimpledateformat ("yyyy-mm-dd hh: mm: SS ");

Try {

Dated = DF. parse (s );

System. Out. println (d );

} Catch (parseexception e ){

E. printstacktrace ();

}

Note: The time format in the string must be the same as that in the simpledateformat class object before conversion. Otherwise, an exception (Java. Text. parseexception) is thrown ).

4. Main classes used to solve time problems:

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.