Java.util.Calendar.before() Method

Advertisements


Description

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

Declaration

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

public boolean before(Object when)

Parameters

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

Return Value

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

Exception

  • NA

Example

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



Advertisements
Advertisements