谁能帮我用Java完成这个功能?谢谢
// e.g. "20130218001203638"
boolean isWeekend(String date)
{
... ...
}
找到一个给出我想要的确切答案的帖子。
谁能帮我用Java完成这个功能?谢谢
// e.g. "20130218001203638"
boolean isWeekend(String date)
{
... ...
}
找到一个给出我想要的确切答案的帖子。
Calendar#get(DAY_OF_WEEK)
它返回值 SUNDAY, MONDAY, ...
你可以有条件地检查什么Calendar.SATURDAY or Calendar.SUNDAY
。
这样的事情应该会有所帮助:
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;
就像日期计算很烦人一样
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;