Appreciate the advantages of Java.util.Canlendar

Source: Internet
Author: User
Tags date instance method set time variable
The Calendar class introduced from JDK1.1 is a different 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 idea of a variable field. Each class element is a domain, and these fields are represented as static variables in the Calendar class. These variable fields, you can get or set the field values by Get/set class methods.

Get the default calendar instance, set time for it
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 modified by the parameter values that are positive or negative. The only difference is that the Add method can overflow into higher-order variable fields. For example, if you step backwards three days from September 3, you will get:

Calendar cal = Calendar.getinstance ();
Cal.add (calendar.date,-3);
The value is: Week June or August 23:43:19 EDT 2002
However, use the Roll method to roll back three days:

Calendar cal = Calendar.getinstance ();
Cal.roll (calendar.date,-3);
Value: Monday last 23:43:47 EDT 2002
This is why the Add method is usually used primarily.

There is also a functional method--isleapyear (judging whether it is a leap year) method hidden in the most common calendar subclass.

Calendar cal = Calendar.getinstance ();
Booleanleapyear = ((GregorianCalendar) cal). Isleapyear (2002);
This value is False

Although it is an instance method, the behavior of the Isleapyear method behaves like a static method, requiring the provision of the year's parameter pass value to the calendar.

The Java.util.Calendar class looks more like a complex version of the data class by taking over the function of date modification. But it also provides additional functionality, not to mention its internationalization support, which makes it worthwhile to have the difficulty curve of learning.


Related Article

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.