Skip to content
Related Articles

Related Articles

Improve Article

java.util.Calendar.after() method

  • Difficulty Level : Medium
  • Last Updated : 25 Apr, 2018
Geek Week

java.util.Calendar.after() is a method in Calendar class of java.util package. The method returns true if the time represented by this Calendar is after the time represented by when Object. If it is not the case, false is returned.

Syntax :

public boolean after(Object when)

Where, when is the Object 
that is to be compared.

Below are some examples to understand the implementation of the Calendar.after() function in a better way :

Example 1 :




// Java code show the usage of
// after() method of Calendar class
import java.util.*;
  
class GFG {
  
// Driver code
public static void main(String[] args) 
            throws InterruptedException {
                  
    // creating calendar object
    Calendar cal_obj1 = Calendar.getInstance();
                  
    // printing current date
    System.out.println("Time 1 : " + cal_obj1.getTime());
      
    // creating Calendar object     
    Calendar cal_obj2 = Calendar.getInstance();
      
    // printing current date
    System.out.println("Time 2 : " + cal_obj2.getTime());
                  
    // checking if 1st date is after 2nd date
    // and printing the result
    System.out.println(cal_obj1.after(cal_obj2));
    }
}

Output :



Time 1 : Thu Mar 01 09:26:04 UTC 2018
Time 2 : Thu Mar 01 09:26:04 UTC 2018
false

Example 2 :




// Java code to show the usage of
// after() method of Calendar class
import java.util.*;
  
class GFG {
      
    // Driver code
    public static void main(String[] args)
    {
  
        // creating calendar objects
        Calendar cal_obj1 = Calendar.getInstance();
        Calendar cal_obj2 = Calendar.getInstance();
  
        // displaying the current date
        System.out.println("Current date is : " + cal_obj1.getTime());
  
        // changing year in cal_obj2 calendar
        cal_obj2.set(Calendar.YEAR, 2010);
  
        // check if calendar date is after current date
        System.out.println("Result : " + cal_obj1.after(cal_obj2));
    }
}

Output :

Current date is : Thu Mar 01 09:27:19 UTC 2018
Result : true

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :