java生成cron表达式
2019-07-31 11:01 话猫 阅读(6530) 评论(0) 编辑 收藏 举报bean类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | package com.cst.klocwork.service.cron; public class TaskScheduleModel { /** * 所选作业类型: * 1 -> 每天 * 2 -> 每月 * 3 -> 每周 * 4 ->间隔(每隔2个小时,每隔30分钟) */ Integer jobType; /**一周的哪几天*/ Integer[] dayOfWeeks; /**一个月的哪几天*/ Integer[] dayOfMonths; /**秒 */ Integer second; /**分 */ Integer minute; /**时 */ Integer hour; public Integer getJobType() { return jobType; } public void setJobType(Integer jobType) { this .jobType = jobType; } public Integer[] getDayOfWeeks() { return dayOfWeeks; } public void setDayOfWeeks(Integer[] dayOfWeeks) { this .dayOfWeeks = dayOfWeeks; } public Integer[] getDayOfMonths() { return dayOfMonths; } public void setDayOfMonths(Integer[] dayOfMonths) { this .dayOfMonths = dayOfMonths; } public Integer getSecond() { return second; } public void setSecond(Integer second) { this .second = second; } public Integer getMinute() { return minute; } public void setMinute(Integer minute) { this .minute = minute; } public Integer getHour() { return hour; } public void setHour(Integer hour) { this .hour = hour; } } |
util类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | /** * @ClassName: CronUtil * @Description: Cron表达式工具类 * 目前支持三种常用的cron表达式 * 1.每天的某个时间点执行 例:12 12 12 * * ?表示每天12时12分12秒执行 * 2.每周的哪几天执行 例:12 12 12 ? * 1,2,3表示每周的周1周2周3 ,12时12分12秒执行 * 3.每月的哪几天执行 例:12 12 12 1,21,13 * ?表示每月的1号21号13号 12时12分12秒执行 * @author * @date * */ public class CronUtil { /** * *方法摘要:构建Cron表达式 *@param taskScheduleModel *@return String */ public static String createCronExpression(TaskScheduleModel taskScheduleModel){ StringBuffer cronExp = new StringBuffer( "" ); if ( null == taskScheduleModel.getJobType()) { System.out.println( "执行周期未配置" ); //执行周期未配置 } if ( null != taskScheduleModel.getSecond() && null != taskScheduleModel.getMinute() && null != taskScheduleModel.getHour()) { //秒 cronExp.append(taskScheduleModel.getSecond()).append( " " ); //分 cronExp.append(taskScheduleModel.getMinute()).append( " " ); //小时 cronExp.append(taskScheduleModel.getHour()).append( " " ); //每天 if (taskScheduleModel.getJobType().intValue() == 1 ){ cronExp.append( "* " ); //日 cronExp.append( "* " ); //月 cronExp.append( "?" ); //周 } //按每周 else if (taskScheduleModel.getJobType().intValue() == 3 ){ //一个月中第几天 cronExp.append( "? " ); //月份 cronExp.append( "* " ); //周 Integer[] weeks = taskScheduleModel.getDayOfWeeks(); for ( int i = 0 ; i < weeks.length; i++){ if (i == 0 ){ cronExp.append(weeks[i]); } else { cronExp.append( "," ).append(weeks[i]); } } } //按每月 else if (taskScheduleModel.getJobType().intValue() == 2 ){ //一个月中的哪几天 Integer[] days = taskScheduleModel.getDayOfMonths(); for ( int i = 0 ; i < days.length; i++){ if (i == 0 ){ cronExp.append(days[i]); } else { cronExp.append( "," ).append(days[i]); } } //月份 cronExp.append( " * " ); //周 cronExp.append( "?" ); } } else { System.out.println( "时或分或秒参数未配置" ); //时或分或秒参数未配置 } return cronExp.toString(); } /** * *方法摘要:生成计划的详细描述 *@param taskScheduleModel *@return String */ public static String createDescription(TaskScheduleModel taskScheduleModel){ StringBuffer description = new StringBuffer( "" ); //计划执行开始时间 // Date startTime = taskScheduleModel.getScheduleStartTime(); if ( null != taskScheduleModel.getSecond() && null != taskScheduleModel.getMinute() && null != taskScheduleModel.getHour()) { //按每天 if (taskScheduleModel.getJobType().intValue() == 1 ){ description.append( "每天" ); description.append(taskScheduleModel.getHour()).append( "时" ); description.append(taskScheduleModel.getMinute()).append( "分" ); description.append(taskScheduleModel.getSecond()).append( "秒" ); description.append( "执行" ); } //按每周 else if (taskScheduleModel.getJobType().intValue() == 3 ){ if (taskScheduleModel.getDayOfWeeks() != null && taskScheduleModel.getDayOfWeeks().length > 0 ) { String days = "" ; for ( int i : taskScheduleModel.getDayOfWeeks()) { days += "周" + i; } description.append( "每周的" ).append(days).append( " " ); } if ( null != taskScheduleModel.getSecond() && null != taskScheduleModel.getMinute() && null != taskScheduleModel.getHour()) { description.append( "," ); description.append(taskScheduleModel.getHour()).append( "时" ); description.append(taskScheduleModel.getMinute()).append( "分" ); description.append(taskScheduleModel.getSecond()).append( "秒" ); } description.append( "执行" ); } //按每月 else if (taskScheduleModel.getJobType().intValue() == 2 ){ //选择月份 if (taskScheduleModel.getDayOfMonths() != null && taskScheduleModel.getDayOfMonths().length > 0 ) { String days = "" ; for ( int i : taskScheduleModel.getDayOfMonths()) { days += i + "号" ; } description.append( "每月的" ).append(days).append( " " ); } description.append(taskScheduleModel.getHour()).append( "时" ); description.append(taskScheduleModel.getMinute()).append( "分" ); description.append(taskScheduleModel.getSecond()).append( "秒" ); description.append( "执行" ); } } return description.toString(); } //参考例子 public static void main(String[] args) { //执行时间:每天的12时12分12秒 start TaskScheduleModel taskScheduleModel = new TaskScheduleModel(); taskScheduleModel.setJobType( 1 ); //按每天 Integer hour = 12 ; //时 Integer minute = 12 ; //分 Integer second = 12 ; //秒 taskScheduleModel.setHour(hour); taskScheduleModel.setMinute(minute); taskScheduleModel.setSecond(second); String cropExp = createCronExpression(taskScheduleModel); System.out.println(cropExp + ":" + createDescription(taskScheduleModel)); //执行时间:每天的12时12分12秒 end taskScheduleModel.setJobType( 3 ); //每周的哪几天执行 Integer[] dayOfWeeks = new Integer[ 3 ]; dayOfWeeks[ 0 ] = 1 ; dayOfWeeks[ 1 ] = 2 ; dayOfWeeks[ 2 ] = 3 ; taskScheduleModel.setDayOfWeeks(dayOfWeeks); cropExp = createCronExpression(taskScheduleModel); System.out.println(cropExp + ":" + createDescription(taskScheduleModel)); taskScheduleModel.setJobType( 2 ); //每月的哪几天执行 Integer[] dayOfMonths = new Integer[ 3 ]; dayOfMonths[ 0 ] = 1 ; dayOfMonths[ 1 ] = 21 ; dayOfMonths[ 2 ] = 13 ; taskScheduleModel.setDayOfMonths(dayOfMonths); cropExp = createCronExpression(taskScheduleModel); System.out.println(cropExp + ":" + createDescription(taskScheduleModel)); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了