Date after(Date) in Java

after(Date): This method is available in java.util.Date class of Java.

Syntax:

boolean java.util.Date.after(Date when)

This method takes one argument of type Date as its parameter. This method tests if this date is after the specified date.

Parameters: One parameter is required for this method.

when: a date.

Returns: true if and only if the instant represented by this Date object is strictly later than the instant represented by when; false otherwise.

Throws:

NullPointerException - if when is null.

Approach 1: When no exception

Java

import java.util.Date;

public class Dateafter {
    public static void main(String[] args) {

        Date date = new Date();

        Date when = new Date();

        System.out.println(date.after(when));

    }
}

Output:

false


Approach 2: NullPointerException

Java

import java.util.Date;

public class Dateafter {
    public static void main(String[] args) {

        Date date = new Date();

        Date when = null;

        System.out.println(date.after(when));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "date" is null at java.base/java.util.Date.getMillisOf(Date.java:957) at java.base/java.util.Date.after(Date.java:930)


No comments:

Post a Comment