Date after() method in Java

The java.util.Date.after() method is used to check whether the current instance of the date is after the specified date.

Syntax:

dateObject.after(Date specifiedDate)

Parameter: It takes only one parameter specifiedDate of data type Date. This is the date which is to be checked in comparison to the instance of the date calling the function.



Return Value: The return type of this function is boolean. It returns true if current instance of the date is strictly larger than the specifiedDate. Otherwise it returns false.

Exceptions: If the specifiedDate is null, this method will throw NullPointerException when called upon.

Below programs illustrate after() method in Date class:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate
// after() function of Date class
  
import java.util.Date;
import java.util.Calendar;
public class GfG {
    // main method
    public static void main(String[] args)
    {
  
        // creating a Calendar object
        Calendar c = Calendar.getInstance();
  
        // set Month
        // MONTH starts with 0 i.e. ( 0 - Jan)
        c.set(Calendar.MONTH, 11);
  
        // set Date
        c.set(Calendar.DATE, 05);
  
        // set Year
        c.set(Calendar.YEAR, 1996);
  
        // creating a date object with specifed time.
        Date dateOne = c.getTime();
  
        // creating a date of object
        // storing the current date
        Date currentDate = new Date();
  
        System.out.print("Is currentDate after date one : ");
  
        // if currentDate is after dateOne
        System.out.println(currentDate.after(dateOne));
    }
}

chevron_right


Output:

Is currentDate after date one : true

Program 2: To demonstrate java.lang.NullPointerException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate
// after() function of Date class
  
import java.util.Date;
  
public class GfG {
    // main method
    public static void main(String[] args)
    {
  
        // creating a date of object
        // storing the current date
        Date currentDate = new Date();
  
        // speciefiedDate is assigned to null.
        Date specifiedDate = null;
  
        System.out.println("Passing null as parameter : ");
        try {
            // throws NullPointerException
            System.out.println(currentDate.after(specifiedDate));
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

chevron_right


Output:

Passing null as parameter : 
Exception: java.lang.NullPointerException


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.