2013-05-14 81 views
-3

任何人都可以帮助我在Java中完成这个功能吗?谢谢如何检查给定的时间戳是周末?

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

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

Check A Date String If Weekend In Java

+1

查找日历和日期的类。我认为从日期开始的日历已过时 – Coffee 2013-05-14 15:41:42

+0

请参阅日历类4的线索 – Coffee 2013-05-14 15:42:17

+2

可以使用DateFormatter(IIRC)将该字符串解析为日历。一个'日历'可以告诉你星期几。 – 2013-05-14 15:42:44

回答

5

Calendar#get(DAY_OF_WEEK)它返回值周日,周一,...

你可以只用有条件检查Calendar.SATURDAY or Calendar.SUNDAY

+1

很好的答案,谢谢! – Coffee 2013-05-14 15:44:51

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; 
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;