Advantages of java. util. Canlendar

Source: Internet
Author: User
Learn more about java. util. Canlendar-general Linux technology-Linux programming and kernel information. The Calendar class introduced from JDK1.1 is another type of Date Processing class. Imagine it is a typical calendar hanging on a wall, with many dates and pages to read.

The basis of the Calendar class is the concept of variable fields. Each class element is a field, and these fields are static variables in the Calendar class. These variable fields can be obtained or set through the get/set class method.

// Obtain the default Calendar instance and set the time
Calendarcal = Calendar. getInstance ();
Intyear = cal. get (Calendar. YEAR );
Cal. set (Calendar. MONTH, Calendar. NOVEMBER );
The add and roll methods of the Calendar class provide the ability to convert between dates. Each method is modified by a parameter variable and a parameter value, which can be a positive or negative value. The difference is that the add method can overflow to the higher-order variable domain. For example, if you go backwards for three days from January 1, September 3, you will get:

Calendar cal = Calendar. getInstance ();
Cal. add (Calendar. DATE,-3 );
// The value is Saturday August 31 23:43:19 EDT 2002.
However, the roll method is used to roll back for three days:

Calendar cal = Calendar. getInstance ();
Cal. roll (Calendar. DATE,-3 );
// Value: Monday, January 1, September 30 23:43:47 EDT 2002
This is why the add method is generally used.

The isLeapYear (whether it is a leap year) method is also a functional method hidden in the most common subclass of Calendar.

Calendar cal = Calendar. getInstance ();
BooleanleapYear = (GregorianCalendar) cal). isLeapYear (2002 );
// The value is false.


Although it is an instance method, the isLeapYear method behavior is like a static method. You need to provide the year parameter to pass the value to the calendar.

By taking over the date modification function, the java. util. Calendar class looks more like a complex version of the Data class. However, it also provides additional functions, not to mention its international support, making it worth learning.
From-zdnet china

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.