Compare two Date objects using compareTo method - Java Date Time

Java examples for Date Time:Legacy Date

Description

Compare two Date objects using compareTo method

Demo Code

 
import java.util.Date;
 
public class Main {
 
  public static void main(String[] args) {
    Date d1 = new Date();
    try{/*w  w w  .  j  av a  2s . c om*/
      Thread.sleep(10);
    }catch(Exception e){
    }
    Date d2 = new Date();
   
    System.out.println("First Date : " + d1);
    System.out.println("Second Date : " + d2);
   
    int results = d1.compareTo(d2);
   
    if(results > 0)
      System.out.println("First Date is after second");
    else if (results < 0)
      System.out.println("First Date is before second");
    else
      System.out.println("Both dates are equal");
  }
}

Result


Related Tutorials