备注:以下代码是从网上摘抄
方法体:
/// <summary> /// Corn表达式的运行时间 /// </summary> /// <param name="cron">表达式</param> /// <param name="times">计算次数</param> /// <param name="startTime">开始时间</param> /// <returns></returns> public static String GetCronSchdule(String cron, int times, DateTimeOffset startTime) { String timeSchdule = ""; if (!CronExpression.IsValidExpression(cron)) { return "Cron is Illegal!"; } try { ITrigger trigger1 = TriggerBuilder.Create() .WithCronSchedule(cron).StartAt(DateTime.Now) .Build(); DateTimeOffset? LastTime = startTime; Console.WriteLine(LastTime.ToString()); for (int i = 0; i < times; i++) { DateTimeOffset? s = trigger1.GetFireTimeAfter(LastTime); LastTime = s; Console.WriteLine(((DateTimeOffset)s).AddHours(8).ToString()); } } catch (Exception e) { timeSchdule = "unKnow Time!"; } return timeSchdule; }
调用:
GetCronSchdule("/5 * * ? * *", 10, DateTime.Now);
如果是简单表达式,代码如下:
ITrigger trigger1 = TriggerBuilder.Create() .WithIdentity("触发器名称", "触发器组") .StartAt(DateTime.Now.AddSeconds(180)) .EndAt(DateTime.Now.AddMinutes(10))//现在开始 .Build(); var ds = trigger1.GetFireTimeAfter(DateTime.Now); Console.WriteLine(((DateTimeOffset)ds).ToString());