Home » Java programming language

Java Calendar compareTo() Method with Example

Calendar Class compareTo() method: Here, we are going to learn about the compareTo() method of Calendar Class with its syntax and example.
Submitted by Preeti Jain, on January 23, 2020

Calendar Class compareTo() method

  • compareTo() method is available in java.util package.
  • compareTo() method is used to compare two Calendar objects or in other words, we can say this method is used to compare the time of this Calendar object and the given Calendar object.
  • compareTo() method is a non-static method, it is accessible with the class object and if we try to access the method with the class name then we will get an error.
  • compareTo() method may throw an exception at the time of comparing two Calendar objects.
    • NullPointerException: This exception may throw when the given parameter is null exists.
    • IllegalArgumentException: This exception may throw when the time of the given Calendar object is illegal.

Syntax:

    public int compareTo(Calendar obj2);

Parameter(s):

  • Calendar obj – represents the Calendar object to be compared with this Calendar object.

Return value:

The return type of this method is int, it returns the following values based on the below given cases,

  • It returns 0 if this Calendar time value is the same as the given Calendar time.
  • It returns the value < 0 if the time denoted by this Calendar is before the time denoted by the given Calendar parameter.
  • It returns the value > 0 if this Calendar time is after the time denoted by the given Calendar parameter.

Example:

// Java Program to demonstrate the example of
// int compareTo(Object) method of Calendar

import java.util.*;

public class CompareOfCalendar {
    public static void main(String[] args) {
        // Instantiating two Calendar object
        Calendar ca1 = Calendar.getInstance();
        Calendar ca2 = Calendar.getInstance();

        // By using add() method to add the 10 years
        // in ca2 to the current ca1
        ca2.add(Calendar.YEAR, 10);

        // Display ca1 and ca2
        System.out.println("ca1: " + ca1.getTime());
        System.out.println("ca2: " + ca2.getTime());

        // By using compareTo(Object) method is to
        // compare two calendar ca1 and ca2
        int comp = ca1.compareTo(ca2);

        // Display compared result
        System.out.println("ca1.compareTo(ca2): " + comp);
    }
}

Output

ca1: Thu Jan 23 11:51:26 GMT 2020
ca2: Wed Jan 23 11:51:26 GMT 2030
ca1.compareTo(ca2): -1






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.