【问题标题】:Getting Week days [duplicate]获取工作日 [重复]
【发布时间】:2012-12-17 18:28:39
【问题描述】:

我创建了一个每天在特定时间运行的程序,

但我希望这个程序在某些特定的日子(周末)停止,

我已经使用下面的代码来设置当前时间,

public int GetDateNow()
{
    Calendar currentdate = Calendar.getInstance();
    DateFormat dateformat = new SimpleDateFormat("HHmm");
    String datenow = dateformat.format(currentdate.getTime());
    int DN=Integer.parseInt(datenow);
    return DN;
}    

以及主类中的以下代码

while(true)
{
    Thread.sleep(1*1000);
    if(gt.GetDateNow()==0000)
    {
        //perform action
    }
}

【问题讨论】:

标签: java


【解决方案1】:

使用Calendar

calendarInstance.get(Calendar.DAY_OF_WEEK);

并将其与周末进行比较(Calendar.SATURDAY, Calendar.SUNDAY 或取决于国家/地区)

【讨论】:

  • +1 另外,除了比较之外,请记住从 1 到 7 的日期范围是 SUNDAY == 1SATURDAY == 7
  • 我试过你的代码但还是不行,我不知道在(主类中的if语句)中放什么
  • 你尝试了什么,但没有成功,你能用代码更新你的帖子
【解决方案2】:

Spring Framework 提供基于 cron 表达式的任务调度,您只需在 context.xml 中配置您的任务,例如

<task:scheduled ref="task" method="execute" cron="* 15 9-17 * * MON-FRI" />

【讨论】:

    【解决方案3】:

    好的,谢谢大家,我使用代码后工作正常

    calendarInstance.get(Calendar.DAY_OF_WEEK);
    

    一切顺利,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多