java.util.Date.after()方法用于检查日期的当前实例是否在指定的日期之后。
语法如下:
dateObject.after(Date specifiedDate)
参数:仅需一个参数指定日期数据类型日期。与调用该函数的日期实例相比, 这是要检查的日期。
返回值:该函数的返回类型为boolean。它返回true如果日期的当前实例严格大于指定的日期。否则返回false.
例外情况:如果指定的日期为null, 则此方法将引发空指针异常当被要求时。
下面的程序说明Date类中的after()方法:
程序1:
// 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 specified 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));
}
}
输出如下:
Is currentDate after date one : true
程式2:演示java.lang.NullPointerException
// 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();
// specifiedDate 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);
}
}
}
输出如下:
Passing null as parameter :
Exception: java.lang.NullPointerException