Java Date compare with compareTo() method

Description

Java Date compare with compareTo() method

import java.time.Instant;
import java.util.Date;
 
public class Main {
 
  public static void main(String[] args) {
    Date d1 = new Date();

    Date d2 = Date.from(Instant.EPOCH);
   //from w  ww. ja va2  s .c  om
    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");
  }
}



PreviousNext

Related