Java中的Date after()方法

这个 JAVAutil。日期之后() 方法用于检查日期的当前实例是否在指定日期之后。

null

语法:

dateObject.after(Date specifiedDate)

参数: 它只需要一个参数 指定日期 日期的数据类型。这是要与调用函数的日期实例进行比较检查的日期。

返回值: 此函数的返回类型为 布尔值 .它回来了 符合事实的 如果日期的当前实例严格大于指定的日期。否则它就会回来 错误的 .

例外情况: 如果specifiedDate为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

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享