[Common Java class libraries] _ date operation class (date, calendar AR) Notes

Source: Internet
Author: User

[Common Java class libraries] _ date operation class (date, calendar AR) Notes

Objectives of this chapter:

Understanding how to use the date class
You can use the calendar class to obtain the complete date:

The date class is a commonly used class, but its operation date format does not meet the individual's needs. If you want to further obtain the time you need, you can use the calendar class.

3.1, date class
The date class is defined in the Java. util package. The date class itself is very simple and you can directly output its instantiated object.
Standard Time Format: Fri Jan 11 14:13:47 CST 2013

Instance:

Import Java. util. date; public class datedemo01 {public static void main (string ARGs []) {date = new date (); // directly outputs the instantiated object system. out. println ("current time:" + date );}}

If you want to display the time in the desired format, you can use the calendar class.

3.2 calendar class
This class can be used to directly accurate the date to milliseconds.
It can be returned by different parameters of the date (year, month, day)

Public abstract class calendar extends object {}

The calendar class is an abstract class. Since it is an abstract class, it cannot be directly used. At this time, we need to use the concept of object polymorphism to instantiate this class object through the upward transformation relationship.
Obtain a complete date using the calendar class and use its subclass.
Instance:

Import Java. util. *; public class datedemo02 {public static void main (string ARGs []) {calendar = new gregoriancalendar (); // instantiate the calendar Class Object System. out. println ("year:" + calendar. get (calendar. year); system. out. println ("month" + calendar. get (calendar. month); system. out. println ("day_of_month:" + calendar. get (calendar. day_of_month :)); system. out. println ("hour_of_day:" + calendar. get (calendar. hour_of_day); system. out. println ("minute:" + calendar. get (calendar. minute); system. out. println ("Second:" + calendar. get (calendar. second); system. out. println ("millsecond:" + calendar. get (calendar. millsecond ));}}

You can easily obtain a complete date using this class. However, you must pay special attention to the addition of 1.
If you follow this method, it will be very troublesome. It is best to perform some related formatting operations on the date.

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.