Java解析Cron表达式
Java解析Cron表达式
Java解析Cron表达式
1、需求
2、实现
2.1、Java代码
2.2、前端代码
1、需求
例子:表中Cron表达式字段:
页面显示:
2、实现
/****
* todo: 获取cron表达式的值
* @return
*/
@Action
public String getCronExpValue() {
HttpServletRequest request = ActionContext.getActionContext().getHttpServletRequest();
String taskId = request.getParameter("taskId");
StringBuilder getCronExpSql = new StringBuilder();
getCronExpSql.append(" SELECT ");
getCronExpSql.append(" id,frequency,frequencycron,frequencydesc ");
getCronExpSql.append(" FROM ");
getCronExpSql.append(" dc_collectiontask ");
getCronExpSql.append(" WHERE ");
getCronExpSql.append(" id = ?");
List> maps = jdbcTemplate.queryForList(getCronExpSql.toString(), taskId);
JSONObject resObj = new JSONObject();
for (Map map : maps) {
int frequency = Integer.parseInt(map.get("frequency").toString());
String frequencycron = map.get("frequencycron").toString();
String frequencydesc = map.get("frequencydesc").toString();
String[] frequencySplit = frequencycron.split(" ");
if (frequency == 1) {
if (frequencydesc.contains("月")) {
//0 2 6 7 * ?
//每月7号6点2分
resObj.put("frequencyType", 5);
//天
resObj.put("day", frequencySplit[3]);
//小时
resObj.put("hour", frequencySplit[2]);
//分钟
resObj.put("minute", frequencySplit[1]);
} else if (frequencydesc.contains("星")) {
resObj.put("frequencyType", 4);
//0 7 3 ? * THU
//每个星期四3点7分
//小时
resObj.put("hour", frequencySplit[2]);
//分钟
resObj.put("minute", frequencySplit[1]);
//周
resObj.put("week", frequencySplit[5]);
} else if (frequencydesc.contains("天")) {
resObj.put("frequencyType", 3);
//0 5 5 1/5 * ?
//小时
resObj.put("hour", frequencySplit[2]);
//分钟
resObj.put("minute", frequencySplit[1]);
//隔天
resObj.put("day", frequencySplit[3].split("/")[1]);
} else if (frequencydesc.contains("时")) {
resObj.put("frequencyType", 1);
//小时
resObj.put("hour", frequencySplit[2].split("/")[1]);
//0 0 0/4 * * ?
} else if (frequencydesc.contains("分")) {
//分钟
resObj.put("frequencyType", 2);
//0 0/15 * * * ?
resObj.put("minute", frequencySplit[1].split("/")[1]);
}
}
}
return resObj.toString();
}
$.ajax({
url: '/dataasset/collection/DataCollection/getCronExpValue?theme=none',
data: {taskId: taskId},
type: 'post',
async: false,
success: function (result) {
var res = JSON.parse(result);
var frequencyType = res.frequencyType;
if (frequencyType == "1") {//时
$("input[name='editTimeTypeSele'][value='1']").attr("checked", true);
$("#editHourDate").val(res.hour);
} else if (frequencyType == "2") {// 分
$("input[name='editTimeTypeSele'][value='2']").attr("checked", true);
$("#editMinuteDate").val(res.minute);
} else if (frequencyType == "3") {// 天
$("input[name='editTimeTypeSele'][value='3']").attr("checked", true);
$("#editDayDate").val(res.day);
$("#editDayhourDate").val(res.hour);
$("#editDayminuteDate").val(res.minute);
} else if (frequencyType == "4") {// 周
$("input[name='editTimeTypeSele'][value='4']").attr("checked", true);
$("#editWeekDate").val(res.week);
$("#editDayhourDate").val(res.hour);
$("#editDayminuteDate").val(res.minute);
} else if (frequencyType == "5") {// 月
$("input[name='editTimeTypeSele'][value='5']").attr("checked", true);
$("#editMonthDate").val(res.day);
$("#editDayhourDate").val(res.hour);
$("#editDayminuteDate").val(res.minute);
}
}
});
Java解析Cron表达式相关教程