0
while (r.next()) {
    String rn = r.getString(3);
    String sqldate = r.getString(2); // database reservation date
}

Getting date from database

DateFormat RD = new SimpleDateFormat("yyyy-MM-dd");
     java.sql.Date converteddate = new java.sql.Date(RD.parse(sqldate).getTime());
     Calendar cal = Calendar.getInstance();
     Calendar cal1 = Calendar.getInstance();
     cal.setTime(converteddate);
     cal.add(Calendar.DATE, 7);
     if(cal<=cal1){ }// i need to this opration i dont know how

I need to check cal is <= Cal1. Any ideas?

Lion
  • 17,515
  • 21
  • 76
  • 104
user1750832
  • 75
  • 1
  • 10

2 Answers2

9

Calendar being Comparable, you can use the Calendar#compareTo() method:

compareTo returns the value 0 if the time represented by the argument is equal to the time represented by this Calendar; a value less than 0 if the time of this Calendar is before the time represented by the argument; and a value greater than 0 if the time of this Calendar is after the time represented by the argument

In your case, to determine if cal is before or equal to cal1, you would use:

if (cal.compareTo(cal1) <= 0) {}
assylias
  • 297,541
  • 71
  • 621
  • 741
  • 1
    can you please explain me what 0 do? – user1750832 Oct 18 '12 at 08:36
  • 1
    Have you clicked on the link I have provided? *"compareTo returns the value 0 if the time represented by the argument is equal to the time represented by this Calendar; a value less than 0 if the time of this Calendar is before the time represented by the argument; and a value greater than 0 if the time of this Calendar is after the time represented by the argument."* – assylias Oct 18 '12 at 08:37
  • thank you i have one more problem, how to check today is btween cal1 cal2 ? – user1750832 Oct 18 '12 at 08:53
  • Get [today's date](http://stackoverflow.com/questions/5046771/how-to-get-todays-date) in calToday and check that cal1 <= calToday and calToday <= cal2. – assylias Oct 18 '12 at 08:59
2

How about using the simple method:

if (cal.before(cal1)) { ... }

This reads: "If a date is before another date, you should do the following..".

I think it makes it easier to read if you write "before" than "compareTo", even though the operation is the same. Also read the documentation for Java Calendar.

EDIT: I've updated the answer a bit to include what's been commented below so the answer is more complete. Thanks to assylias for pointing it out, but if you want to compare if a date is before or equal to another date, you should use:

if (!cal.after(cal1)) { ... }

This reads: "If a date is not after another date, you should do the following..". Also note that "not after" is the same as "before or equal to".

Heskja
  • 797
  • 6
  • 19
  • 2
    Note that before is equivalent to `cal.compareTo(cal1) < 0`, not `cal.compareTo(cal1) <= 0`, which would be equivalent to `!cal.after(cal1)`. – assylias Oct 18 '12 at 08:35
  • 1
    @assylias Thanks for pointing it out. I've updated the answer to include that point, so it's a more complete answer. – Heskja Oct 18 '12 at 08:41
  • how to check today is btween cal1 cal2 ? – user1750832 Oct 18 '12 at 08:51
  • `if (today.after(cal1) && today.before(cal2)) { ... } ` if you want to know if today is between cal1 and cal2. – Heskja Oct 18 '12 at 09:01