before(Date): This method is available in java.util.Date class of Java.
Syntax:
boolean java.util.Date.before(Date when)
This method takes one argument of type Date as its parameter. This method tests if this date is before the specified date.
Parameters: One parameter is required for this method.
when: a date.
Returns: true if and only if the instant of time represented by this Date object is strictly earlier 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 Datebefore {public static void main(String[] args) {Date date = new Date();Date when = new Date();System.out.println(date.before(when));}}
Output:
false
Approach 2: NullPointerException
Java
import java.util.Date;public class Datebefore {public static void main(String[] args) {Date date = new Date();Date when = null;System.out.println(date.before(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.before(Date.java:916)
No comments:
Post a Comment