Java Utility Library

Java Date - before() Method



The java.util.Date.before() method is used to test if this date is before the specified date or not. The method returns true if this Date object is strictly earlier than the specified Date object, else returns false.

Syntax

public boolean before(Date when)

Parameters

when Specify a date.

Return Value

Returns true if this Date object is strictly earlier than the when Date object; false otherwise.

Exception

Throws NullPointerException, if when is null.

Example:

In the example below, the java.util.Date.before() method is used to check whether the given Date is before the specified date or not.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating Dates
    Date Dt1 = new Date();
    Date Dt2 = new Date();
    Date Dt3 = new Date();

    //checking Dt1 is before Dt2
    System.out.println("Dt1 is before Dt2: " + Dt1.before(Dt2));

    //checking Dt1 is before Dt3
    System.out.println("Dt1 is before Dt3: " + Dt1.before(Dt3));
  }
}

The output of the above code will be:

Dt1 is before Dt2: true
Dt1 is before Dt3: false

❮ Java.util - Date