Create Cron Expression using Quartz .NET

2019-05-07 01:54发布

问题:

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?

回答1:

Possible using ICronTrigger.CronExpressionString

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

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

string cronExpression = trigger.CronExpressionString;


回答2:

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

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

        return trigger.CronExpressionString;
    }
}