2

I have two dates like

Date dateOfEnq = sdf.parse(ea.getDateOfEnquiry());
Date validDate = DateUtil.getAfterMonth(dateOfEnq, range);

Now i am doing

if (dateOfEnq <= validDate) {
    ount++;
}

but it gives me error that

The operator <= is undefined for the argument type(s) java.util.Date, java.util.Date.

how can i compare these two dates ?

Thanks

Basit
  • 8,426
  • 46
  • 116
  • 196

3 Answers3

3

dateOfEnq.before(anotherDate); (or) dateOfEnq.after(anotherDate); etc., to compate two dates.

Read javadoc for more information.

Note: It is better to use latest API like Joda when you want to use dates.

kosa
  • 65,990
  • 13
  • 130
  • 167
2

java.util.Date implements Comparable interface. That means it has compareTo method which you can use to compare 2 dates

 int res = date1.compareTo(date2);
  • res = 0 if date1 is equal to date2;
  • res < 0 if date1 is before date2;
  • res > 0 if date1 is after date2
Jla
  • 11,304
  • 14
  • 61
  • 84
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

Use after and before methods to compare.

Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101