Calendar.compareTo()

Date

Calendar.compareTo(date obj) Check by comparing the calendar with the date represented.

date obj : Required Calendar date object, represented.
Returns value

-1 : Returns -1 if calendar value is less than the calender object in the argument.
0 : Returns 0 if calendar value is equal to the calender object in the argument.
1 : Returns 1 if calendar value is greater than the calender object in the argument.

Examples with output
Let us create two dates , then compare them by using compareTo().
package my_proj;
 
import java.util.Calendar;
 
public class my_first {
public static void main(String[] args) {

Calendar my_cal_d1 = Calendar.getInstance();// created calendar 
Calendar my_cal_d2 = Calendar.getInstance();

my_cal_d1.set(2020,2, 1,8,9,10); // Input date
my_cal_d2.set(2020,2, 2,8,9,10); // Input date

System.out.println( my_cal_d1.getTime());
System.out.println( my_cal_d2.getTime());

System.out.println(my_cal_d1.compareTo(my_cal_d2));//-1
}
}
Output is here
Sun Mar 01 08:09:10 IST 2020
Mon Mar 02 08:09:10 IST 2020
-1
In above code we have two objects and compareTo() returns -1 as my_cal_d1 is less than my_cal_d2.
By changing the values of my_cal_d1 and my_cal_d2 we will get 1 as output. Change these lines in above code.
my_cal_d2.set(2020,2, 1,8,9,10); // Input date
my_cal_d1.set(2020,2, 2,8,9,10); // Input date
The output will change like this.
Mon Mar 02 08:09:10 IST 2020
Sun Mar 01 08:09:10 IST 2020
1

Equal dates

Now we will keep both dates same. Check the output.
package my_proj;
 
import java.util.Calendar;
 
public class my_first {
public static void main(String[] args) {

Calendar my_cal_d1 = Calendar.getInstance();// created calendar 
Calendar my_cal_d2 = Calendar.getInstance();

my_cal_d2.set(2020,2, 1,8,9,10); // Input date
my_cal_d1.set(2020,2, 1,8,9,10); // Input date

System.out.println( my_cal_d1.getTime());
System.out.println( my_cal_d2.getTime());

System.out.println(my_cal_d1.compareTo(my_cal_d2));//-1
}
}
When both the dates are same ( inputs are same ) why the output of compareTo() is -1 ( not 0 ) ?
Here when two dates are created with equal date and time , still based on their execution time the milliseconds associated with each of them is different. So they are not equal as compareTo() checks the milliseconds also. Check the code below. We have used getTimeInMillis() to read the millseconds of our first variable my_cal_d1.
long my_millis=my_cal_d1.getTimeInMillis();
my_cal_d2.setTimeInMillis(my_millis);
The same value we have assigned to variable my_cal_d2 by using setTimeInMillis()
Now both the dates are equal so compareTo() will return 0.
package my_proj;
 
import java.util.Calendar;
 
public class my_first {
public static void main(String[] args) {

Calendar my_cal_d1 = Calendar.getInstance();// created calendar 
Calendar my_cal_d2 = Calendar.getInstance();

my_cal_d1.set(2020,2, 1,8,9,10); // Input date
my_cal_d2.set(2020,2, 1,8,9,10); // Input date

System.out.println( my_cal_d1.getTime());
System.out.println( my_cal_d2.getTime());

long my_millis=my_cal_d1.getTimeInMillis();
my_cal_d2.setTimeInMillis(my_millis);

System.out.println( my_cal_d1.getTimeInMillis());
System.out.println( my_cal_d2.getTimeInMillis());

System.out.println(my_cal_d1.compareTo(my_cal_d2));// 0
}
}
Output is here
Sun Mar 01 08:09:10 IST 2020
Sun Mar 01 08:09:10 IST 2020
1583030350891
1583030350891
0
Date & time tutorials


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer