Java Date Time - Java Calendar.before(Object when)








Syntax

Calendar.before(Object when) has the following syntax.

public boolean before(Object when)

Example

In the following code shows how to use Calendar.before(Object when) method.

//w  ww. j ava 2s .c o  m
import java.util.Calendar;

public class Main {

   public static void main(String[] args) {

      Calendar cal = Calendar.getInstance();
      Calendar past = Calendar.getInstance();

      System.out.println("Current date: " + cal.getTime());

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

      // check if calendar date is before current date
      System.out.println(cal.before(past));
   }
}

The code above generates the following result.