w3resource

Java Date.before() Method

public boolean before(Date dt)

The before() method is used to check if a given date is before another given date.

Package: java.util

Java Platform: Java SE 8

Syntax:

before(Date dt)

Parameters:

Name Description
dt a date

Return Value:
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.

Return Value Type: boolean

Throws:
NullPointerException - if when is null.

Pictorial Presentation of Jave Date before() method:

Java Date.before() Method

Example: Java Date.before() Method

The following example fills a Date before(Date when) .


import java.util.*;
public class Main {
   public static void main(String[] args) {

      // create 2 dates
      Date dt1 = new Date(2017, 3, 31);
      Date dt2 = new Date(2017, 5, 14);

      // Check if dt1 is  before dt2
      boolean result = dt1.before(dt2);
      System.out.println("Date1 is before date2: " + result);

      // Check if dt2 is  after dt1
      result = dt2.before(dt1);
      System.out.println("Date2 is before date1: " + result);
   }
}
 

Output:

Date1 is before date2: true
Date2 is before date1: false

Example of Throws: before(Date when) Method

NullPointerException - if when is null.

Java Code Editor:

Previous:after Method
Next:clone Method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-tutorial/util/date/java_date_before.php