【问题标题】:How to check given timestamp is weekend? [closed]如何检查给定的时间戳是周末? [关闭]
【发布时间】:2013-05-14 15:40:24
【问题描述】:

谁能帮我用Java完成这个功能?谢谢

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

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

Check A Date String If Weekend In Java

【问题讨论】:

标签: java date


【解决方案1】:

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

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

【讨论】:

  • 很好的答案,谢谢!
【解决方案2】:

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

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;

【讨论】:

    【解决方案3】:

    就像日期计算很烦人一样

    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;
    

    【讨论】:

      猜你喜欢
      • 2021-10-15
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      相关资源
      最近更新 更多