Check if two calendar objects represent the same local time in Java
Description
The following code shows how to check if two calendar objects represent the same local time.
Example
/*from w w w. java2 s. c om*/
import java.util.Calendar;
public class Main {
public static void main(String[] argv) {
System.out.println(isSameLocalTime(Calendar.getInstance(),
Calendar.getInstance()));
}
public static boolean isSameLocalTime(Calendar cal1, Calendar cal2) {
if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("The date must not be null");
}
return (cal1.get(Calendar.MILLISECOND) == cal2.get(Calendar.MILLISECOND)
&& cal1.get(Calendar.SECOND) == cal2.get(Calendar.SECOND)
&& cal1.get(Calendar.MINUTE) == cal2.get(Calendar.MINUTE)
&& cal1.get(Calendar.HOUR) == cal2.get(Calendar.HOUR)
&& cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)
&& cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
&& cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && cal1.getClass() == cal2
.getClass());
}
}
The code above generates the following result.