侧边栏壁纸
博主头像
分享你我博主等级

行动起来,活在当下

  • 累计撰写 108 篇文章
  • 累计创建 13 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

C# Cron表达式生成

管理员
2020-12-12 / 0 评论 / 0 点赞 / 3 阅读 / 17989 字
      
           /// <summary>
        /// 时间格式转换成Quartz任务调度器Cron表达式
        /// </summary>
        /// <param name="time">时间值</param>
        /// <param name="format">格式ss(秒)|mm(分)|HH(小时)|HH:mm|HH:mm:ss</param>
        /// <returns></returns>
        public static string TimeToQuartzCron(string time, string format)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(time)) return "";
                if (format.Equals("ss"))
                {
                    var second = SecondConversion(Convert.ToInt32(time));
                    return CreateCron(second, "ss");
                }
                else if (format.Equals("mm"))
                {
                    var second = SecondConversion(Convert.ToInt32(Convert.ToInt32(time) * 60.0));
                    return CreateCron(second, "mm");
                }
                else if (format.Equals("HH"))
                {
                    var second = SecondConversion(Convert.ToInt32(Convert.ToInt32(time) * 3600.0));
                    return CreateCron(second, "HH");
                }
                else if (format.Equals("HH:mm"))
                {
                    if (!Regex.IsMatch(time, "^([0-2]+[0-3]|[0-1]+[0-9])\\:([0-5]+[0-9])*$")) throw new Exception("格式有问题");
                    string[] times = time.Split(':');
                    TimeSpan ts = new TimeSpan(Convert.ToInt32(times[0]), Convert.ToInt32(times[1]), 0);
                    return CreateCron(ts, "HH:mm");
                }
                else if (format.Equals("HH:mm:ss"))
                {
                    if (!Regex.IsMatch(time, "^([0-2]+[0-3]|[0-1]+[0-9])\\:([0-5]+[0-9])\\:([0-5]+[0-9])*$")) throw new Exception("格式有问题");
                    string[] times = time.Split(':');
                    TimeSpan ts = new TimeSpan(Convert.ToInt32(times[0]), Convert.ToInt32(times[1]), Convert.ToInt32(times[2]));
                    return CreateCron(ts, "HH:mm:ss");
                }
                else if (format.Equals("dd"))
                {
                }
                else if (format.Equals("MM"))
                {
                }
                return null;
            }
            catch (Exception ea)
            {
                throw ea;
            }
        }
        /// <summary>
        /// 秒转时分秒
        /// </summary>
        /// <param name="second"></param>
        /// <returns></returns>
        public static TimeSpan SecondConversion(int second)
        {
            int hour = second / 3600;
            int min = (second - hour * 3600) / 60;
            int sen = second - hour * 3600 - min * 60;
            TimeSpan ts = new TimeSpan(0, 0, second);
            return ts;
        }
        /// <summary>
        /// 生成表达式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        private static string CreateCron(TimeSpan date, string format)
        {
            string ss = "*", mm = "*", hh = "*", dd = "*", month = "*", week = "? ";
            if (Regex.IsMatch(date.ToString(), "^(00:00:)[0-5]+[0-9]*$"))
            {
                //主要针对零点做处理
                if (format.Equals("HH:mm"))
                {
                    hh = "0/"+date.ToString("%h");
                    mm = date.ToString("%m");
                }
                else if (format.Equals("HH:mm:ss"))
                {
                    //取小时、分、秒
                    hh = "0/"+date.ToString("%h");
                    ss = date.ToString("%s");
                    mm = date.ToString("%m");
                }
                else
                {
                    //取秒
                    ss = "0/"+date.ToString("%s");
                }
            }
            else if (Regex.IsMatch(date.ToString(), "^(00:)([0-5]+[1-9]|[1-5]+[0-9])\\:([0-5]+[0-9])*$"))
            {
                //主要针对零点做处理
                if (format.Equals("HH:mm"))
                {
                    hh = "0/"+date.ToString("%h");
                    mm = date.ToString("%m");
                }
                else if (format.Equals("HH:mm:ss"))
                {
                    //取小时、分、秒
                    hh = "0/"+date.ToString("%h");
                    ss = date.ToString("%s");
                    mm = date.ToString("%m");
                }
                else
                {
                    //取分和秒
                    mm = "0/"+date.ToString("%m");
                    ss = date.ToString("%s");
                }
            }
            else if (Regex.IsMatch(date.ToString(), "^([0-2]+[0-3]|[0-1]+[0-9])\\:([0-5]+[0-9])\\:([0-5]+[0-9])*$"))
            {
                //取小时、分、秒
                hh = "0/"+date.ToString("%h");
                ss = date.ToString("%s");
                mm = date.ToString("%m");
            }
            else if (Regex.IsMatch(date.ToString(), "^([1-9]|[0-2]+[0-9]|[3]+[0-1])\\.([0-2]+[0-3]|[0-1]+[0-9])\\:([0-5]+[0-9])\\:([0-5]+[0-9])*$"))
            {
                //取天
                dd = "0/"+date.ToString("%d");
                //取小时
                hh = date.ToString("%h");
                //取分
                mm = date.ToString("%m");
                //取秒
                ss = date.ToString("%s");
            }
            else
            {
                return null;
            }
            string cronValue = ss + " " + mm + " " + hh + " " + dd + " " + month + " " + week;
            return cronValue;
        }
0

评论区