java.util.Calendar.after() method

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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output :

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

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output :

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


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.