Java - How to compare Time and Date

How to compare Time and Date in Java.

  1. In Java8, LocalDateTime, LocalDate, LocalTime, ZonedDateTime are provided.

You can compare date/time in this API.

  1. You can compare date/time with Date and Calendar used before Java8.

Java 8: Date/Time API Comparison

LocalDateTime, LocalDate, LocalTime, ZonedDateTime provide the following methods:

  1. isBefore() : Returns true if it is past than the argument
  2. isAfter() : Returns true if it is more future than the argument
  3. isEqual() : Returns true when the time is the same as the argument
public void compareDate1() throws ParseException {

    LocalDateTime date1 = LocalDateTime.parse("2019-07-12T10:11:50.000");
    LocalDateTime date2 = LocalDateTime.parse("2019-11-12T15:20:31.000");

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

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

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

result

2019-07-12T10:11:50
2019-11-12T15:20:31
Date1 is before Date2

Compare Dates by Date

The Date class provides the following methods:

  1. before() : Returns true if it is past than the argument
  2. after() : Returns true if it is more future than the argument
  3. equal() : Returns true when the time is the same as the argument
public void compareDate2() throws ParseException {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date1 = sdf.parse("2019-07-12");
    Date date2 = sdf.parse("2019-11-01");
    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");
    }
}

result

2019-07-12
2019-11-01
Date1 is before Date2

Compare Dates from Date (compareTo)

The Date class provides a compareTo() method, which allows you to compare dates.

  1. compareTo() > 0 : future than argument
  2. compareTo() < 0 : past the argument
  3. compareTo() == 0 : time equal to argument
public void compareDate3() throws ParseException {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date1 = sdf.parse("2019-07-12");
    Date date2 = sdf.parse("2019-11-01");
    System.out.println(sdf.format(date1));
    System.out.println(sdf.format(date2));

    if (date1.compareTo(date2) > 0) {
        System.out.println("Date1 is after Date2");
    } else if(date1.compareTo(date2) < 0){
        System.out.println("Date1 is before Date2");
    } else {
        System.out.println("Date1 is equal to Date2");
    }
}

result

2019-07-12
2019-11-01
Date1 is before Date2

Compare Dates in Calendar

The Calendar class provides the following methods:

  1. before() : Returns true if it is past than the argument
  2. after() : Returns true if it is more future than the argument
  3. equal() : Returns true when the time is the same as the argument
public void compareDate4() throws ParseException {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date1 = sdf.parse("2019-07-12");
    Date date2 = sdf.parse("2019-11-01");
    System.out.println(sdf.format(date1));
    System.out.println(sdf.format(date2));

    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);
    cal2.setTime(date2);

    if(cal1.after(cal2)){
        System.out.println("Date1 is after Date2");
    }

    if(cal1.before(cal2)){
        System.out.println("Date1 is before Date2");
    }

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

result

2019-07-12
2019-11-01
Date1 is before Date2

Reference

codechachaCopyright ©2019 codechacha