Full introduction to time processing in J2EE

Source: Internet
Author: User
Time processing is quite common in program development. Here is a simple description of time processing.
I. Expression of time
Time can be expressed in two ways:
1. Represented by the difference in milliseconds between midnight and the present GMT on January 1, January 1, 1970
This method is suitable for comparing the difference between two times.
2. Expression in the form of objects
II. Related time processing
The time processing involves three classes in j2_m2:
1. System class
Long time = System. currentTimeMillis ();
You can use this method to obtain the current time. The expression of time is the first one mentioned above.
2. Date class
Date date = new Date ();
Obtain the current time, expressed in the form of an object.
3. Calendar class
Calendar calendar = Calendar. getInstance ();
III. Specific time processing operations
1. Conversion of the above three expressions:
A) convert the time obtained by the System class to the Date object
Date date = new Date (System. currentTimeMillis ());
B) convert an object of the Date type to an object of the Calendar type.
Calendar calendar = Calendar. getInstance ();
Date date = new Date ();
Calendar. setTime (date );
2. Use Calendar to complete some date operations:
Calendar is the most commonly used and powerful class in time processing. It can be used to obtain the date, day of week, and other information of a certain time.
Date obtained:
Calendar calendar = Calendar. getInstance ();
......
Int day = calendar. get (Calendar. DATE );
The operation for obtaining the date, year, and week is similar to this operation.
It should be noted that in the Calendar AR, the number of the month is 1 different from that of the actual month, that is, January is represented by the number 0, and February is represented by the number 1 ,...... December 11, December is represented by the number 11.

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.