java.util.Calendar before(Object when)

Description :

This java tutorial shows how to use the before(Object when) method of Calendar class of java.util package. This method returns whether this Calendar represents a time before the time represented by the specified Object.

Method Syntax :

public String getCalendarType()

Parameter Input :


DataType Parameter Description
Object when the Object to be compared


Method Returns :

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

Compatibility Version :

Requires Java 1.1 and up

Exception :


Java Code Example :

This java example source code demonstrates the use of before(Object when)  method of Calendar class.


import java.util.Calendar; import java.util.Scanner;

/* * This example source code demonstrates the use of * before(Object when) method of Calendar class */

public class CalendarBeforeExample {

public static void main(String

[] args) throws InterruptedException {

Calendar cal1 = Calendar.getInstance(); System.out.println("Time 1:" + cal1.getTime()); // sleep for 5 seconds Thread.sleep(5000); Calendar cal2 = Calendar.getInstance(); System.out.println("Time 2:" + cal2.getTime()); System.out.println(cal1.before(cal2)); // creating an object which is not a calendar Object c = new Scanner(""); // this will always return false System.out.println(cal1.before(c));



Sample Output :

Running the before(Object when) method example source code of Calendar class will give you the following output:

Time 1:Sat Feb 07 23:42:16 CST 2015
Time 2:Sat Feb 07 23:42:22 CST 2015

Exception Scenario :


Similar Method :

  • N/A

Suggested Reading List :

References :