【问题标题】:Cron expression with start and end time带有开始和结束时间的 Cron 表达式
【发布时间】:2016-10-16 08:57:56
【问题描述】:

我正在尝试每天编写一个具有特定开始时间和结束时间的 cron 表达式。 IE。 每天 10:15 到 17:35 的每一分钟

一种可能的解决方案是编写 3 个不同的 cron 表达式,如下所示:

0 15-59 10 * * *
0 * 11-17 * * *
0 0-35 17 * * *

有没有什么方法可以用一个 cron 表达式来写这个?

【问题讨论】:

  • 也许从 10 点到 17 点每分钟从 cron 运行一次,只检查脚本中的分钟数?
  • @Marki555:这根本不能解决问题,因为我需要将 cron 表达式传递给另一个将使用它来执行某些工作的服务器。
  • 注意:每一行有一个星号太多。 (或更好:应该省略前导 0
  • @wildplasser:我使用的格式是 - second minute hour dayOfMonth Month DayOfWeek 0 15-59 10 * * * 并排除了另外一个 * 年份,因为它不是必需的。文档参考:quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/…如果我做错了什么,请纠正我。
  • 石英!= cron。 man 5 crontab

标签: cron


【解决方案1】:

Quartz 调度程序 c# 在几小时之间运行作业

使用玉米面膜:

        ITrigger trigger_1 = TriggerBuilder.Create()
            .ForJob("YOUR_JOB")
            .WithIdentity("trigger_1")
            .StartAt(startTime)
            .WithCronSchedule("0 0/1 8-13 ? *   SUN,MON,TUE,WED,SAT *")
            .Build();

使用 Quartz 时间表:

trigger_1 = TriggerBuilder
            .Create()
            .ForJob("YOUR_JOB")
            .WithIdentity("trigger_1")
            .StartAt(startTime)
            .WithDailyTimeIntervalSchedule(c => c
                .OnEveryDay()
                .WithIntervalInMinutes(1).WithRepeatCount(1)
                .StartingDailyAt(new TimeOfDay(08, 30))
                .EndingDailyAt(new TimeOfDay(12, 30))
            )
            .Build();

【讨论】:

    【解决方案2】:
       HashMap<String, String> weekday = new HashMap<>();
        // String strDays ="[Monday, Friday]";
        String strCalDay = "";
    
        String startDate[] = fromDate.split("-");
        String endDate[] = toDate.split("-");
    
        weekday.put("MONDAY", "1");
        weekday.put("TUESDAY", "2");
        weekday.put("WEDNESDAY", "3");
        weekday.put("THURSDAY", "4");
        weekday.put("FRIDAY", "5");
        weekday.put("SATURDAY", "6");
        weekday.put("SUNDAY", "7");
    
        strDays = strDays.replace("[", "").replace("]", "");
        String strDay[] = strDays.trim().toUpperCase().split(",");
        if (strDay != null && strDay.length > 0) {
            for (int i = 0; i < strDay.length; i++) {
                strCalDay = strCalDay + "," + weekday.get(strDay[i].trim().replace("\"", ""));
            }
            // System.out.println(" No of days :: "+ strCalDay);
            strCalDay = strCalDay.replaceFirst(",", "");
            //System.out.println(" No of days :: "+ strCalDay);
        }
    
    
        Calendar startCal = Calendar.getInstance(TimeZone.getDefault());
        Calendar endCal = Calendar.getInstance(TimeZone.getDefault());
        startCal.set(Integer.parseInt(startDate[0].toString()),
                Integer.parseInt(startDate[1].toString()),
                Integer.parseInt((startDate[2].toString())));
    
        endCal.set(Integer.parseInt(endDate[0].toString()),
                Integer.parseInt(endDate[1].toString()),
                Integer.parseInt((endDate[2].toString())));
    
        int yearsInBetween = endCal.get(Calendar.YEAR) - startCal.get(Calendar.YEAR);
        int monthsDiff = endCal.get(Calendar.MONTH) - startCal.get(Calendar.MONTH);
        long ageInMonths = yearsInBetween * 12 + monthsDiff;
        System.out.println("  ageInMonths: " + ageInMonths);
    
    
        if (startCal.get(Calendar.MONTH) != endCal.get(Calendar.MONTH)) {
            if (ageInMonths == 3) {
                Calendar middleMonth1 = (Calendar) startCal.clone();
                middleMonth1.add(Calendar.MONTH, 1);
                Calendar middleMonth2 = (Calendar) startCal.clone();
                middleMonth2.add(Calendar.MONTH, 2);
    
                int getFirstMMonth = middleMonth1.get(Calendar.MONTH);
                int getSecMMonth = middleMonth2.get(Calendar.MONTH);
                if (getFirstMMonth == 0) {
                    getFirstMMonth = 12;
                    System.out.println("  getFirstMMonth : " + getFirstMMonth);
                }
                if (getSecMMonth == 0) {
                    getSecMMonth = 12;
                    // System.out.println("  getSecMMonth : " +getSecMMonth );
                }
                exp = strTimeMin + " " + strTime + " " + startDate[2] + "-" + "31,1-31,1-31,1-" + endDate[2] + " " +
                        startDate[1] + "," + getFirstMMonth + "," + getSecMMonth + "," + endDate[1] + " *";
    
                //  System.out.println("  expression for   month3 : " + exp);
            }
            if (ageInMonths == 2) {
                Calendar middleMonth1 = (Calendar) startCal.clone();
                middleMonth1.add(Calendar.MONTH, 1);
                //System.out.println("  get middleDate month : " + middleMonth1.get(Calendar.MONTH));
                int getMiddleMonth = middleMonth1.get(Calendar.MONTH);
                if (getMiddleMonth == 0) {
                    getMiddleMonth = 12;
                    //  System.out.println("  getMiddleMonth : " +getMiddleMonth );
                }
                //  System.out.println("  Outside getMiddleMonth : " +getMiddleMonth );
    
    
                endCal.set(Calendar.MONTH, 11);
                //System.out.println("  get middleDate month 001: " + endCal.get(Calendar.MONTH));
    
                exp = strTimeMin + " " + strTime + " " + startDate[2] + "-" + "31,1-31,1-" +
                        endDate[2] + " " + startDate[1] + "," +
                        getMiddleMonth + "," + endDate[1] + " *";
                // System.out.println("  get end  month2 : " + exp);
            } else if (ageInMonths == 1) {
    
                exp = strTimeMin + " " + strTime + " " + startDate[2] + "-31,1-" +
                        endDate[2] + " " + startDate[1] + "," + endDate[1] + " *";
                //System.out.println("  expression for  one month : " + exp);
            }
    
        } else {
    
            exp = strTimeMin + " " + strTime + " " + startDate[2] + "-" + endDate[2] + " " + endDate[1] + " *";
        }
    
        System.out.println("  expression for  before parameter : " + exp);
    
        //Run with only Start Date
        if (strRepeat != null && strRepeat.equalsIgnoreCase("No")) {
            exp = strTimeMin + " " + strTime + " " + startDate[2] + " " + startDate[1] + " *";
        } else if (strRepeat != null && strRepeat.equalsIgnoreCase("Daily")) {
            exp = exp;
        } else if (strRepeat != null && strRepeat.equalsIgnoreCase("Weekdays")) {
    
    
            // System.out.println("  expression for  weekdays before  : " + exp);
            exp = exp.replace("*", strCalDay);
            // System.out.println("  expression for  weekdays after  : " + exp);
    
    
        } else if (strRepeat != null && strRepeat.equalsIgnoreCase("Weekends")) {
    
            // System.out.println("  expression for  weekend  before  : " + exp);
            exp = exp.replace("*", "6,7");
            // System.out.println("  expression for  weekend after  : " + exp);
        }
        System.out.println(" cron expression ::: " + exp);
    

    【讨论】:

    • 以上代码将根据我的项目要求在最长 3 个月的开始和结束日期执行 cron 表达式。
    【解决方案3】:

    没有其他方法可以使用单个 crone 表达式来实现,只能为特定的 startDate 和 endDate 指定多个 crone 表达式。 不过,第二个 crone 表达式略有修改(突出显示)

    0 15-59 10 * * *(上午 10:15 至上午 10:59 之间的每分钟)

    0 * 11-16 * * *(每分钟,上午 11:00 至下午 04:59 之间)

    0 0-35 17 * * *(05:00 PM 和 05:35 PM 之间的每分钟)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多