/// <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; }
版权归属:
管理员
许可协议:
本文使用《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议授权
评论区