Compare two Java Date objects using before method example - Java Date Time

Java examples for Date Time:Legacy Date

Introduction

use boolean before(Date anotherDate) method of Date Class to check whether a date is before the specified date

Demo Code

 
import java.util.Date;
 
public class Main {
 
  public static void main(String[] args) {
    Date d1 = new Date();
    try{//from   ww w .j a v a 2  s  .c o  m
      Thread.sleep(10);
    }catch(Exception e){
    }
    Date d2 = new Date();
   
    System.out.println("First Date : " + d1);
    System.out.println("Second Date : " + d2);
    System.out.println("Is first date before second ? : " + d1.before(d2));    
 
  }
}
 

Result


Related Tutorials