Android Development Note "Java.util.Calendar.compareTo ()"

Source: Internet
Author: User

The Java.util.Calendar.compareTo () method compares the time value (the millisecond offset) between the Calendar object and the Anothercalendar object.

Statement

The following is a declaration of the Java.util.Calendar.compareTo () method

 Public int compareTo (Calendar Anothercalendar)
Parameters
    • Anothercalendar--The Calendar object to compare.

return value

If the time represented by the parameter is equal to the time method represented by this calendar object, it returns 0, or if the time of this calendar is a value that is less than 0 before the time represented by the parameter, or if the time represented by the calendar returns a value greater than 0.

Abnormal
    • NullPointerException-If the specified calendar is null.

    • IllegalArgumentException-If you cannot get the time value of the calendar object specified

Example

The following example shows the use of the Java.util.calendar.compareTo () method.

 PackageCom.yiibai;ImportJava.util.*; Public classCalendardemo { Public Static voidMain (string[] args) {//create the calendar at the different datesCalendar Cal1 =NewGregorianCalendar (2015, 8, 15); Calendar Cal2=NewGregorianCalendar (2008, 1, 02); //Compare the time values represented by the calendar objects.      inti =Cal1.compareto (CAL2); //return positive value if equals else return negative valueSYSTEM.OUT.PRINTLN ("The result is:" +i); //Compare again but with the calendars swapped      intj =Cal2.compareto (CAL); //return positive value if equals else return negative valueSYSTEM.OUT.PRINTLN ("The result is:" +j); }}

Let's compile and run the above program, which will produce the following results:

The result is:1 Theresult is:-1

Android Development Note "Java.util.Calendar.compareTo ()"

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.