Open In App

Java Calendar before() Method

Last Updated : 09 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The before() method in the java.util.Calendar class used to check if one date or time is before the another one. The method returns true if the time represented by this Calendar is before the time represented by when Object. If it is not the case, false is returned.

Syntax of Calendar before() Method

public boolean before(Object when)

  • Parameter: when: The object we need to compare with.
  • Returns: This method returns true if the current date is before the when date, otherwise false.

Examples of Calendar before() Method

Example 1: In this example, we will check if a date is before another by adding a delay.

Java
// Java program to check if one Calendar time is 
// before another using a time delay
import java.util.Calendar;

public class Geeks {
    public static void main(String[] args) throws InterruptedException {

        // Create the first Calendar object with current time
        Calendar c1 = Calendar.getInstance();
        System.out.println("Time 1: " + c1.getTime());

        // Create the second Calendar object 
        // with a slightly later time
        Calendar c2 = Calendar.getInstance();
        System.out.println("Time 2: " + c2.getTime());

        // Check if Time 1 is before Time 2
        System.out.println("Is Time 1 before Time 2? " + c1.before(c2));
    }
}

Output
Time 1: Fri May 09 07:20:14 UTC 2025
Time 2: Fri May 09 07:20:15 UTC 2025
Is Time 1 before Time 2? true


Example 2: In this example, we will compare the current date with a manually set past date.

Java
// Java program to check if current date 
// is before a past year
import java.util.Calendar;

public class Geeks {
    public static void main(String[] args) {

        // Create a Calendar object with the current date
        Calendar cur = Calendar.getInstance();
        System.out.println("Current date: " + cur.getTime());

        // Create another Calendar and 
        // set it to an old year (2015)
        Calendar past = Calendar.getInstance();
        past.set(Calendar.YEAR, 2015);

        // Show the year we set
        System.out.println("Year set in second calendar: " + past.get(Calendar.YEAR));

        System.out.println("Is current date before 2015? " + cur.before(past));
    }
}

Output
Current date: Fri May 09 07:24:11 UTC 2025
Year set in second calendar: 2015
Is current date before 2015? false

Explanation: Here in this example, the current date is not before 2015, so, the before() method returns false.


Example 3: In this example, we will compare current date with a future date.

Java
// Java program to check if current date 
// is before a future year
import java.util.Calendar;

public class Geeks {
    public static void main(String[] args) {

        // Create a Calendar object with the current time
        Calendar cur = Calendar.getInstance();
        System.out.println("Current date: " + cur.getTime());

        // Create another Calendar and 
        // set it to a future year
        Calendar future = Calendar.getInstance();
        future.set(Calendar.YEAR, 2027);

        // Display the year set in the second calendar
        System.out.println("Year set in second calendar: " + future.get(Calendar.YEAR));

        // Check if current date is before 2027
        System.out.println("Is current date before 2027? " + cur.before(future));
    }
}

Output
Current date: Fri May 09 07:35:21 UTC 2025
Year set in second calendar: 2027
Is current date before 2027? true

Explanation: Here in this example, the year 2025 is before 2027. So, the before() method returns true.

Important Points:

  • We should use the before() method when we need to know if a date is earlier than the other date.
  • It is very helpful for tasks like scheduling, expiration checks etc.
  • Always compare with another Calendar object for correct results.

Practice Tags :

Similar Reads