Java Compare two Calendar Values
Tags: Java Calendar
In this Java core tutorial we learn how to compare two java.util.Calendar objects in Java programming language using compareTo(), equals(), before() and after() methods.
Table of contents
- Compare two Calendar objects using compareTo() method
- Compare two Calendar objects for Equality
- Compare if a Calendar before another Calendar
- Compare if a Calendar after another Calendar
Compare two Calendar objects using compareTo() method
In Java, to compare two Calendar objects for ordering we can use the Calendar.compareTo() method which return int value of -1, 0 or 1 if the Calendar object is before, equal or after the Calendar object it compare to.
The following Java example code to show the Calendar.compareTo() method return -1 when the Calendar object is before another Calendar object it compare to.
CompareCalendarExample1.java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class CompareCalendarExample1 {
public static void main(String... args) {
Calendar calendar1 = new GregorianCalendar(2022, Calendar.JULY, 10);
Calendar calendar2 = new GregorianCalendar(2022, Calendar.JULY, 11);
int result = calendar1.compareTo(calendar2);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println("calendar1: " + simpleDateFormat.format(calendar1.getTime()));
System.out.println("calendar2: " + simpleDateFormat.format(calendar2.getTime()));
System.out.println("calendar1 compareTo calendar2: " + result);
}
}
calendar1: 2022/07/10 00:00:00
calendar2: 2022/07/11 00:00:00
calendar1 compareTo calendar2: -1
The following Java example code to show the Calendar.compareTo() method return 0 when the Calendar object is equal another Calendar object it compare to.
CompareCalendarExample2.java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class CompareCalendarExample2 {
public static void main(String... args) {
Calendar calendar1 = new GregorianCalendar(2022, Calendar.JULY, 11);
Calendar calendar2 = new GregorianCalendar(2022, Calendar.JULY, 11);
int result = calendar1.compareTo(calendar2);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println("calendar1: " + simpleDateFormat.format(calendar1.getTime()));
System.out.println("calendar2: " + simpleDateFormat.format(calendar2.getTime()));
System.out.println("calendar1 compareTo calendar2: " + result);
}
}
calendar1: 2022/07/11 00:00:00
calendar2: 2022/07/11 00:00:00
calendar1 compareTo calendar2: 0
The following Java example code to show the Calendar.compareTo() method return 1 when the Calendar object is after another Calendar object it compare to.
CompareCalendarExample3.java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class CompareCalendarExample3 {
public static void main(String... args) {
Calendar calendar1 = new GregorianCalendar(2022, Calendar.JULY, 12);
Calendar calendar2 = new GregorianCalendar(2022, Calendar.JULY, 11);
int result = calendar1.compareTo(calendar2);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println("calendar1: " + simpleDateFormat.format(calendar1.getTime()));
System.out.println("calendar2: " + simpleDateFormat.format(calendar2.getTime()));
System.out.println("calendar1 compareTo calendar2: " + result);
}
}
calendar1: 2022/07/12 00:00:00
calendar2: 2022/07/11 00:00:00
calendar1 compareTo calendar2: 1
Compare two Calendar objects for Equality
In Java to check if two Calendar objects are equal or not we can use the Calendar.equals() method as following Java code.
CompareCalendarExample4.java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class CompareCalendarExample4 {
public static void main(String... args) {
Calendar calendar1 = new GregorianCalendar(2022, Calendar.JULY, 11);
Calendar calendar2 = new GregorianCalendar(2022, Calendar.JULY, 11);
Calendar calendar3 = new GregorianCalendar(2022, Calendar.JULY, 12);
boolean result1 = calendar1.equals(calendar2);
boolean result2 = calendar1.equals(calendar3);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println("calendar1: " + simpleDateFormat.format(calendar1.getTime()));
System.out.println("calendar2: " + simpleDateFormat.format(calendar2.getTime()));
System.out.println("calendar3: " + simpleDateFormat.format(calendar3.getTime()));
System.out.println("calendar1 is equal to calendar2: " + result1);
System.out.println("calendar1 is equal to calendar3: " + result2);
}
}
calendar1: 2022/07/11 00:00:00
calendar2: 2022/07/11 00:00:00
calendar3: 2022/07/12 00:00:00
calendar1 is equal to calendar2: true
calendar1 is equal to calendar3: false
Compare if a Calendar before another Calendar
In Java to check if a Calendar object is before another specified Calendar object we can use the Calendar.before() method as below.
CompareCalendarExample5.java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class CompareCalendarExample5 {
public static void main(String... args) {
Calendar calendar1 = new GregorianCalendar(2022, Calendar.JULY, 10);
Calendar calendar2 = new GregorianCalendar(2022, Calendar.JULY, 11);
Calendar calendar3 = new GregorianCalendar(2022, Calendar.JULY, 9);
boolean result1 = calendar1.before(calendar2);
boolean result2 = calendar1.before(calendar3);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println("calendar1: " + simpleDateFormat.format(calendar1.getTime()));
System.out.println("calendar2: " + simpleDateFormat.format(calendar2.getTime()));
System.out.println("calendar3: " + simpleDateFormat.format(calendar3.getTime()));
System.out.println("calendar1 is before calendar2: " + result1);
System.out.println("calendar1 is before calendar3: " + result2);
}
}
calendar1: 2022/07/10 00:00:00
calendar2: 2022/07/11 00:00:00
calendar3: 2022/07/09 00:00:00
calendar1 is before calendar2: true
calendar1 is before calendar3: false
Compare if a Calendar after another Calendar
In Java to check if a Calendar object is after another specified Calendar object we can use the Calendar.after() method as below.
CompareCalendarExample6.java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class CompareCalendarExample6 {
public static void main(String... args) {
Calendar calendar1 = new GregorianCalendar(2022, Calendar.JULY, 10);
Calendar calendar2 = new GregorianCalendar(2022, Calendar.JULY, 11);
Calendar calendar3 = new GregorianCalendar(2022, Calendar.JULY, 9);
boolean result1 = calendar1.after(calendar2);
boolean result2 = calendar1.after(calendar3);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println("calendar1: " + simpleDateFormat.format(calendar1.getTime()));
System.out.println("calendar2: " + simpleDateFormat.format(calendar2.getTime()));
System.out.println("calendar3: " + simpleDateFormat.format(calendar3.getTime()));
System.out.println("calendar1 is after calendar2: " + result1);
System.out.println("calendar1 is after calendar3: " + result2);
}
}
calendar1: 2022/07/10 00:00:00
calendar2: 2022/07/11 00:00:00
calendar3: 2022/07/09 00:00:00
calendar1 is after calendar2: false
calendar1 is after calendar3: true
Happy Coding 😊
Related Articles
Java Compare two LocalDate Values
Java Compare two LocalTime Values