Can anyone help me to complete this function in Java? thanks
// e.g. "20130218001203638"
boolean isWeekend(String date)
{
... ...
}
Find a post giving the exact answer I want.
Can anyone help me to complete this function in Java? thanks
// e.g. "20130218001203638"
boolean isWeekend(String date)
{
... ...
}
Find a post giving the exact answer I want.
Calendar#get(DAY_OF_WEEK)
it return values SUNDAY, MONDAY, ...
what you can just conditionally check with Calendar.SATURDAY or Calendar.SUNDAY
.
Something like this should help:
boolean isWeekend = false;
Date date = new Date();
//assuming your date string is time in long format,
//if not then use SimpleDateFormat class
date.setTime(Long.parseLong("20130218001203638"));
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
isWeekend = true;
}
return isWeekend;
Just as date calculation is wearisome
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
Date d = df.parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
int wday = cal.get(Calendar.DAY_OF_WEEK);
return wday == Calendar.SATURDAY || wday == Calendar.SUNDAY;