Calendar.equals()

Date

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

date obj : Required Calendar date object, represented.
Returns value is boolean, true is returned if both dates are equal , false otherwise.

Examples with output
Let us create two dates , then compare them by using equals().
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.equals(my_cal_d2));//-1
}
}

Using clone()

We will create one more Calendar object by using clone().
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 
my_cal_d1.set(Calendar.YEAR,2025); 
Calendar my_cal_d2 = (Calendar) (Calendar) my_cal_d1.clone();  

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

System.out.println(my_cal_d1.equals(my_cal_d2));//true
}
}
Output
Tue Mar 04 09:42:23 IST 2025
Tue Mar 04 09:42:23 IST 2025
true
In above code we have two objects and equals() returns true as my_cal_d2 is clone of my_cal_d1.
Let us try two same date, month and year and compare them by using equal().
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();// created calendar
my_cal_d1.set(2020,2,25,10,10,20);
my_cal_d2.set(2020,2,25,10,10,20);
  

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

System.out.println(my_cal_d1.equals(my_cal_d2));//true
}
}
The output is here
Wed Mar 25 10:10:20 IST 2020
Wed Mar 25 10:10:20 IST 2020
false
Why the output of date checking by equals() is false here ? ( note that we used same Year, month , date, hour , minute and second in both objects.

The reason is when we set the date object , it adds the milliseconds part to it and these values are different based on the compilation time.

In the previous example above , we used clone(), so both the calendar objects are same and we got true as output of equals().

How to check if two dates are equal ?

We are checking Year, Month , and date part of two Calendar dates. We are not checking the time part here.

Note that two dates are equal if their Years ( YEAR ) are equal and if their DAY of the year ( DAY_OF_YEAR ) are equal. We used one if condition to check these status and displayed a message saying about the equality of two dates.
public static void main(String[] args) {

Calendar my_cal_d1 = Calendar.getInstance();// created calendar 
Calendar my_cal_d2 = Calendar.getInstance();// created calendar
my_cal_d1.set(2020,2,25,10,10,20);
my_cal_d2.set(2020,2,25,10,11,20);
  
System.out.println( my_cal_d1.getTime());
System.out.println( my_cal_d2.getTime());

if(my_cal_d1.get(Calendar.YEAR) == my_cal_d2.get(Calendar.YEAR) &&
		my_cal_d1.get(Calendar.DAY_OF_YEAR) == my_cal_d2.get(Calendar.DAY_OF_YEAR)  ) {
	
	System.out.println("Both dates are equal");
}else {
	System.out.println("Both dates are NOT equal");
}

System.out.println(my_cal_d1.equals(my_cal_d2));//true
}
}
Output is here
Wed Mar 25 10:10:20 IST 2020
Wed Mar 25 10:11:20 IST 2020
Both dates are equal
false
Here we are not checking the time part so the difference in Minutes field of both the dates are not affecting the if condition check.

Date & time tutorials



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-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer