Date after() method in Java
Last Updated :
07 Nov, 2019
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:
import java.util.Date;
import java.util.Calendar;
public class GfG {
public static void main(String[] args)
{
Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, 11 );
c.set(Calendar.DATE, 05 );
c.set(Calendar.YEAR, 1996 );
Date dateOne = c.getTime();
Date currentDate = new Date();
System.out.print( "Is currentDate after date one : " );
System.out.println(currentDate.after(dateOne));
}
}
|
Output:
Is currentDate after date one : true
Program 2: To demonstrate java.lang.NullPointerException
import java.util.Date;
public class GfG {
public static void main(String[] args)
{
Date currentDate = new Date();
Date specifiedDate = null ;
System.out.println( "Passing null as parameter : " );
try {
System.out.println(currentDate.after(specifiedDate));
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
Passing null as parameter :
Exception: java.lang.NullPointerException
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...