Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Cron Expression using Quartz .NET

Is it possible using the Quartz .NET assembly to generate a cron expression? I saw that the CronScheduleBuilder class has a private member cronExpression which is essentially what I am looking for. Is there any other way to get the cron expression itself?

like image 468
Ian R. O'Brien Avatar asked Nov 26 '12 14:11

Ian R. O'Brien


People also ask

Does Quartz use cron?

Using Cron TriggersQuartz supports Cron-like expressions for specifying timers in a handy format.

What is Quartz cron expression?

A Cron Expressions quartz. Trigger. A cron expression is a string consisting of six or seven subexpressions (fields) that describe individual details of the schedule. These fields, separated by white space, can contain any of the allowed values with various combinations of the allowed characters for that field.

What is cron generator?

CronMaker is a simple application which helps you to build cron expressions. CronMaker uses Quartz open source scheduler. Generated expressions are based on Quartz cron format. For your feedback send email to [email protected]. Generate cron expression.

What is question mark in cron?

A question mark ( ? ) is allowed in the day-of-month and day-of-week fields. It is used to specify “no specific value,” which is useful when you need to specify something in one of these two fields, but not in the other.


2 Answers

Possible using ICronTrigger.CronExpressionString

CronScheduleBuilder csb = CronScheduleBuilder
    .WeeklyOnDayAndHourAndMinute(DayOfWeek.Monday, 12, 0);

ICronTrigger trigger = (ICronTrigger)TriggerBuilder
    .Create()
    .WithSchedule(csb)
    .Build();

string cronExpression = trigger.CronExpressionString;
like image 144
Ian R. O'Brien Avatar answered Sep 18 '22 18:09

Ian R. O'Brien


Using Ian answer, I have created a small extension method. Hopefully it will be useful for someone else...

public static class QuartzExtensionMethods
{
    public static string ToCronString(this CronScheduleBuilder cronSchedule)
    {
        ICronTrigger trigger = (ICronTrigger)TriggerBuilder
        .Create()
        .WithSchedule(cronSchedule)
        .Build();

        return trigger.CronExpressionString;
    }
}
like image 21
Thomas Avatar answered Sep 18 '22 18:09

Thomas