如何检查给定的时间戳是周末?

时间:2013-05-14 15:40:25

标签: java date

有人可以帮我在Java中完成这个功能吗?感谢

// e.g. "20130218001203638"
boolean isWeekend(String date)
{
    ... ...
}

找一篇帖子给出我想要的确切答案。

Check A Date String If Weekend In Java

3 个答案:

答案 0 :(得分:5)

Calendar#get(DAY_OF_WEEK)它返回值SUNDAY,MONDAY,...

您可以通过Calendar.SATURDAY or Calendar.SUNDAY有条件地检查。

答案 1 :(得分:1)

这样的事情应该会有所帮助:

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;

答案 2 :(得分:0)

就像日期计算一样令人厌烦

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;