Java Data Type How to - Check if a date is greater than other








Question

We would like to know how to check if a date is greater than other.

Answer

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*from w  w w .  j  a v  a 2  s .c o m*/
public class Main {
  public static void main(String[] args) {
    try {

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      Date date1 = sdf.parse("2015-12-31");
      Date date2 = sdf.parse("2015-01-31");

      System.out.println(sdf.format(date1));
      System.out.println(sdf.format(date2));

      if (date1.after(date2)) {
        System.out.println("Date1 is after Date2");
      }

      if (date1.before(date2)) {
        System.out.println("Date1 is before Date2");
      }

      if (date1.equals(date2)) {
        System.out.println("Date1 is equal Date2");
      }

    } catch (ParseException ex) {
      ex.printStackTrace();
    }
  }
}

The code above generates the following result.