views:

64

answers:

3

I wanna see if someDate has any day in it. Am I checking it right?

Calendar cal = Calendar.getInstance();
cal.setTime(someDate); // someDate is a Date
int day = cal.get(Calendar.DAY_OF_MONTH);
if(day == 0){
  // code //
}
+3  A: 

I'm not sure what you mean by "has any day in it" - all Dates will have a day in them... :-)

Other than that, you probably want the following:

Calendar cal = Calendar.getInstance();
cal.setTime(someDate); // someDate is a Date
int day = cal.get(Calendar.DAY_OF_WEEK);
if(day == Calendar.SUNDAY){
  // code //
}

The big change is that you want to get the DAY_OF_WEEK field; what your example does is gets the day within the month (e.g. September 15th would return "15"). Secondly, comparing with Calendar.SUNDAY (or equivalent) is clearer and less error-prone that directly comparing with e.g. 0, even if the code is equivalent.

Andrzej Doyle
A: 

Every date object will have a day. The day of the month is never going to be 0 though, it will be in the range 1-31. Meaning that your check will always fail.

Kip
A: 

If I understand correctly you want Calendar.DAY_OF_WEEK.

Chris R