I’m trying to find out if a date is between two specific dates or not. I’m using the Calendar library to represent the dates. My code:
public static void double getnumber(Calendar cal){
Calendar cred1 = Calendar.getInstance();
Calendar cred2 = Calendar.getInstance();
cred1.set(Calendar.JANUARY, 21, 2012);
cred2.set(Calendar.FEBRUARY, 8, 2012);
if(cal.after(cred1) && cal.before(cred2)){
System.out.println("case 1");
}
else{
System.out.println("case 2");
}
}
The date I’m testing is this:
Calendar test = Calendar.getInstance();
test.set(Calendar.JANUARY, 25, 2012);
getnumber(test);
My problem is that when I test that date, the application always print “Case 2” when it’s supposed to print “Case 1”. Any ideas why? Any help is much appreciated, as I’ve tried everything but nothing seems to work, thanks.
The javadoc of Calendar.set(int, int, int) says:
You set month – day – year : which is not the right order