Java.util.Calendar.After() Method


Description

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

Declaration

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

public boolean after(Object when)

Parameters

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

Return Value

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

Exception

NA

Example

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

package com.tutorialspoint;

import java.util.*;

public class CalendarDemo {
   public static void main(String[] args) {
   
      // create calendar objects.
      Calendar cal = Calendar.getInstance();
      Calendar future = Calendar.getInstance();

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

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

      // check if calendar date is after current date
      Date time = future.getTime();
      
      if (future.after(cal)) {
         System.out.println("Date " + time + " is after current date.");
      }
   }
}

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

Current date: Thu Apr 26 18:58:58 EEST 2012
Future calendar's year is 2015
Date Sun Apr 26 18:58:58 EEST 2015 is after current date.
java_util_calendar.htm
Advertisements