zoukankan      html  css  js  c++  java
  • C# 将datatime类型转为Cron

      /// <summary>
            /// datetime修改为corn表达式用于自动预测
            /// </summary>
            /// <param name="time"></param>
            /// <returns></returns>
            public string DatetimeToCron(string time,string Week)
            {
                var cronValue = "";
                string weekCron = "? * *";
                try
                {
                    switch (Week)
                    {
                        case "星期一":
                            weekCron = "? * 2";
                            break;
                        case "星期二":
                            weekCron = "? * 3";
                            break;
                        case "星期三":
                            weekCron = "? * 4";
                            break;
                        case "星期四":
                            weekCron = "? * 5";
                            break;
                        case "星期五":
                            weekCron = "? * 6";
                            break;
                        case "星期六":
                            weekCron = "? * 7";
                            break;
                        case "星期日":
                            weekCron = "? * 1";
                            break;
    
                    }
                    if (string.IsNullOrWhiteSpace(time)) return "";
                    string error = "传入的时间值[" + time + "]格式有误!";
                    int ss = 0, mi = 0, hh = 0;
                    if (time.Length < 5) throw new Exception(error);
                    if (time.Substring(2, 1) != ":") throw new Exception(error);
    
                    if (!int.TryParse(time.Substring(0, 2), out hh))
                        throw new Exception(error);
                    if (!int.TryParse(time.Substring(3, 2), out mi))
                        throw new Exception(error);
                    if (time.Length > 5)
                    {
                        if (time.Substring(5, 1) != ":") throw new Exception(error);
                        if (!int.TryParse(time.Substring(6), out ss))
                            throw new Exception(error);
                    }
                    if (ss > 59) throw new Exception(error);
                    if (mi > 59) throw new Exception(error);
                    if (hh > 23) throw new Exception(error);
                    cronValue = ss + " " + mi + " " + hh + " " + weekCron;
    
                }
                catch (Exception e)
                {
    
                    LogHelper.WriteLog("datetime修改为corn表达式用于自动预测", e);
                }
    
                return cronValue;
    
            }
  • 相关阅读:
    浏览器渲染HTML页面步骤
    JavaScript中必记英语单词及含义
    JavaScript中的线程与进程
    成绩转换 题解
    计算球的体积 题解 #define
    计算两点间的距离 题解
    ASCII码排序 题解
    python学习——协程
    python学习——进程
    python学习——锁
  • 原文地址:https://www.cnblogs.com/provedl/p/13892368.html
Copyright © 2011-2022 走看看