Java.util.Calendar.before() Method


The java.util.Calendar.before() method returns whether this Calendar's time is before the time represented by the specified Object (when).


Following is the declaration for java.util.Calendar.before() method

public boolean before(Object when)


  • when − the Object of time that is about to be compared.

Return Value

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




The following example shows the usage of java.util.Calendar.before() method.

package com.tutorialspoint;

import java.util.Calendar;

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

      // create calendar objects.
      Calendar cal = Calendar.getInstance();
      Calendar past = Calendar.getInstance();

      // print the current date
      System.out.println("Current date: " + cal.getTime());

      // change year in past calendar
      past.set(Calendar.YEAR, 2006);
      System.out.println("Year is " + past.get(Calendar.YEAR));

      // check if calendar date is before current date
      System.out.println("Before current date:" + cal.before(past));

Let us compile and run the above program, this will produce the following result −

Current date: Sat May 05 15:59:37 EEST 2012
Year is 2006
Before current date:false
Kickstart Your Career

Get certified by completing the course

Get Started