Java Utility Library

Java Calendar - before() Method



The java.util.Calendar.before() method is used to test whether this Calendar represents a time before the time represented by the specified Object.

Syntax

public boolean before(Object when)

Parameters

when Specify the Object to be compared.

Return Value

Returns true if the time of this Calendar is before the time represented by when; false otherwise.

Exception

NA.

Example:

In the example below, the java.util.Calendar.before() method is used to check whether the time of the given Calendar is before the time represented by the specified Object.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating Calendars
    Calendar Cal1 = new GregorianCalendar(98, 10, 25);
    Calendar Cal2 = new GregorianCalendar(99, 10, 25);
    Calendar Cal3 = new GregorianCalendar(97, 10, 25);

    //checking Cal1 is before Cal2
    System.out.println("Cal1 is before Cal2: " + Cal1.before(Cal2));

    //checking Cal1 is before Cal3
    System.out.println("Cal1 is before Cal3: " + Cal1.before(Cal3));
  }
}

The output of the above code will be:

Cal1 is before Cal2: true
Cal1 is before Cal3: false

❮ Java.util - Calendar