Java LocalDate compare with compareTo() method

Description

Java LocalDate compare with compareTo() method

import java.time.LocalDate;
import java.time.Month;

public class Main {

  public static void main(String[] args) {
    LocalDate ldt1 = LocalDate.of(2020, Month.NOVEMBER, 11);
    LocalDate ldt2 = LocalDate.now();
    int comparison = ldt1.compareTo(ldt2);
    if (comparison > 0) {
      System.out.println(ldt1 + " is after " + ldt2);
    } else if (comparison < 0) {
      System.out.println(ldt1 + " is before " + ldt2);
    } else {/*from  ww  w .j av  a2s  .  co  m*/
      System.out.println(ldt1 + " is equal to " + ldt2);
    }

    if (ldt1.isAfter(ldt2)) {
      System.out.println(ldt1 + " is after " + ldt2);
    } else if (ldt1.isBefore(ldt2)) {
      System.out.println(ldt1 + " is before " + ldt2);
    } else if (ldt1.isEqual(ldt2)) {
      System.out.println(ldt1 + " is equal to " + ldt2);
    }

  }
}



PreviousNext

Related