Java Calendar after() Method
Last Updated :
09 May, 2025
The after() method in the java.util.Calendar class is used when we want to check if one calendar date comes after the another one. This method returns true if the time represented by this Calendar is after the time represented by when Object. If it is not the case, false is returned.
This method is helpful when we compare two dates or times in Java. For example, to check if a schedules meeting has already passed or if a deadline is upcoming.
Syntax of Calendar after() Method
public boolean after(Object when)
- Parameter: when: This is the object we want to compare with.
- Returns: It returns true if the current Calendar time is after the when time, otherwise it returns false.
Examples of Java Calendar after() Method
Example 1: In this example, we will compare two calendar instanced created at the same time.
Java
// Java program to compare two
// Calendar instances using after()
import java.util.Calendar;
public class Geeks {
public static void main(String[] args) {
// Create the first Calendar instance
Calendar c1 = Calendar.getInstance();
System.out.println("Time 1: " + c1.getTime());
// Create the second Calendar instance
Calendar c2 = Calendar.getInstance();
System.out.println("Time 2: " + c2.getTime());
// Compare if cal1 is after cal2
System.out.println("Is Time 1 after Time 2? " + c1.after(c2));
}
}
OutputTime 1: Fri May 09 06:22:27 UTC 2025
Time 2: Fri May 09 06:22:27 UTC 2025
Is Time 1 after Time 2? false
Explanation: Here in this example, both times are nearly the same. So, the after() method returns false.
Example 2: In this example, we will manually set a past date and will see what happens.
Java
// Java program to check if the current date
// is after a specific past year
import java.util.Calendar;
public class Geeks {
public static void main(String[] args) {
// Create a Calendar instance for the current date
Calendar cur = Calendar.getInstance();
System.out.println("Current Date: " + cur.getTime());
// Create another Calendar and set it to a past year
Calendar past = Calendar.getInstance();
past.set(Calendar.YEAR, 2015);
// Compare if current date is after the year 2015
System.out.println("Is current date after 2015? " + cur.after(past));
}
}
OutputCurrent Date: Fri May 09 06:30:52 UTC 2025
Is current date after 2015? true
Explanation: Here in this example we can see the year 2025 is after 2015, so the after() method returns true.
Important Points:
- The after() method is very useful when we need to compare two dates in Java.
- This method can also check if a particular time has already passed.
- Always pass another Calendar object as the argument for correct comparison.