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,158
  • 43
  • 107
  • 190

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
  • 64,776
  • 13
  • 121
  • 163
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,084
  • 14
  • 60
  • 83
Evgeniy Dorofeev
  • 129,181
  • 28
  • 195
  • 266
0

Use after and before methods to compare.

Nikolay Kuznetsov
  • 9,217
  • 12
  • 53
  • 97