How to implement isWeekday() and isWeekend()?
I would like to a开发者_如何学Pythonsk. I've a date format like 2011-06-05 00:00:00 I want to construct a method in java which will validate this date as week day or weekend.
the methods are something like this.
public boolean isWeekday(Date dt){
//process here
return true;
}
and
public boolean isWeekend(Date dt){
//process here
return true;
}
What should i code to validate the date given?
Thanks..
Calendar cal = new GregorianCalendar();
cal.setTime(dt);
int day = cal.get(Calendar.DAY_OF_WEEK);
return day == Calendar.SUNDAY || day == Calendar.SATURDAY;
Calendar cal = new GregorianCalendar(dt.getYear(), dt.getMonth(), dt.getDay());
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); // 6=Friday
For weekdays, dayOfWeek equals 2 (Monday), 3 (Tuesday), 4 (Wednesday), 5 (Thursday), 6 (Friday)
For weekends, dayOfWeek equal 7 (Saturday), 1 (Sunday)
精彩评论