根据时间等参数转换为Cron表达式的Helper
在网上找了好久只看到有大佬写解析Cron表达式的代码但未看到有将年、月、日、时、分、秒、周等参数转换为Cron表达式并校验合法性的代码
希望大佬们指教一下哪里能看到相关代码,不胜感激,java、C#等面向对象语言均可 这种功能直接找 AI 就行了啊
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CronExpressionGenerator {
public static String generateCronExpression(int year, int month, int dayOfMonth, int hour, int minute, int second, int dayOfWeek) {
// Cron表达式支持的日期范围是1970年1月1日以后
if (year < 1970) {
throw new IllegalArgumentException("Year must be greater than or equal to 1970");
}
// 校验星期几的范围
if (dayOfWeek < 1 || dayOfWeek > 7) {
throw new IllegalArgumentException("DayOfWeek must be between 1 and 7");
}
// 将给定的时间信息转换为Date对象
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, dayOfMonth, hour, minute, second);
Date date = calendar.getTime();
// 使用SimpleDateFormat格式化日期,得到CRON表达式中的年月日时分秒部分
SimpleDateFormat dateFormat = new SimpleDateFormat("ss mm HH dd MM ? yyyy");
String cronExpression = dateFormat.format(date);
// 将星期几部分替换为给定的dayOfWeek
cronExpression = cronExpression.replace("?", String.valueOf(dayOfWeek));
return cronExpression;
}
public static void main(String[] args) {
// 示例:生成2023年11月15日10点30分每秒执行一次的CRON表达式
String cronExpression = generateCronExpression(2023, 11, 15, 10, 30, 0, 3); // 星期三
System.out.println(cronExpression); // 输出:0 30 10 15 11 ? 2023
}
}
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
public class CronExpressionValidator {
private static final List<String> ALLOWED_SPECIAL_CHARS = Arrays.asList(",", "-", "*", "/", "?", "L", "W", "#");
private static final String CRON_PATTERN = "^(\\*|(\\d+|-\\d+\\/\\d+|\\d+\\-\\d+|\\d+L|\\d+W|\\d+#\\d+)){1,5} \\*(\\*|(\\d+|-\\d+\\/\\d+|\\d+\\-\\d+|\\d+L|\\d+W|\\d+#\\d+)){1,5} \\*(\\*|(\\d+|-\\d+\\/\\d+|\\d+\\-\\d+|\\d+L|\\d+W|\\d+#\\d+)){1,5} \\*|(\\d+|-\\d+\\/\\d+|\\d+\\-\\d+|\\d+L|\\d+W|\\d+#\\d+)){1,2} \\? (\\*|(\\d+|\\d{2}))$";
public static boolean isValidCronExpression(String cronExpression) {
// 空字符串或null直接返回false
if (cronExpression == null || cronExpression.isEmpty()) {
return false;
}
// 检查每个字段的特殊字符是否合法
String[] fields = cronExpression.split(" ");
if (fields.length != 6 && fields.length != 7) {
return false;
}
for (String field : fields) {
for (char c : field.toCharArray()) {
if (!Character.isDigit(c) && !ALLOWED_SPECIAL_CHARS.contains(String.valueOf(c))) {
return false;
}
}
}
// 使用正则表达式进行校验
return Pattern.matches(CRON_PATTERN, cronExpression);
}
public static void main(String[] args) {
String validCron = "0 0 12 * * ? *";
String invalidCron = "0 0 12 * * ?";
System.out.println(validCron + ": " + isValidCronExpression(validCron));
System.out.println(invalidCron + ": " + isValidCronExpression(invalidCron));
}
} Ashtareve 发表于 2024-11-19 22:55
这种功能直接找 AI 就行了啊
import java.text.SimpleDateFormat;
import java.u ...
AI写的不太完整且有很多纰漏,不过还是感谢大佬了 这种还真是 得AI智能 一下就写好了 AI写的就可以借鉴 用JAVA写的代码,你复制试一下
import java.util.regex.Pattern;
public class CronExpressionGenerator {
public static String generateCronExpression(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second, Integer weekday) {
StringBuilder cronParts = new StringBuilder();
if (second!= null) {
if (second < 0 || second > 59) {
throw new IllegalArgumentException("Second must be between 0 and 59");
}
cronParts.append(second).append(" ");
} else {
cronParts.append("* ");
}
if (minute!= null) {
if (minute < 0 || minute > 59) {
throw new IllegalArgumentException("Minute must be between 0 and 59");
}
cronParts.append(minute).append(" ");
} else {
cronParts.append("* ");
}
if (hour!= null) {
if (hour < 0 || hour > 23) {
throw new IllegalArgumentException("Hour must be between 0 and 23");
}
cronParts.append(hour).append(" ");
} else {
cronParts.append("* ");
}
if (day!= null) {
if (day < 1 || day > 31) {
throw new IllegalArgumentException("Day must be between 1 and 31");
}
cronParts.append(day).append(" ");
} else {
cronParts.append("* ");
}
if (month!= null) {
if (month < 1 || month > 12) {
throw new IllegalArgumentException("Month must be between 1 and 12");
}
cronParts.append(month).append(" ");
} else {
cronParts.append("* ");
}
if (year!= null) {
cronParts.append(year).append(" ");
} else {
cronParts.append("* ");
}
if (weekday!= null) {
if (weekday < 0 || weekday > 6) {
throw new IllegalArgumentException("Weekday must be between 0 and 6");
}
cronParts.append(weekday).append(" ");
} else {
cronParts.append("* ");
}
return cronParts.toString().trim();
}
public static boolean validateCronExpression(String cronExpression) {
Pattern pattern = Pattern.compile("^((\\*|(|)) (\\*|(|)) (\\*|(|)) (\\*|()) (\\*|()) (\\*|(+)))$");
return pattern.matcher(cronExpression).matches();
}
public static void main(String[] args) {
Integer year = 2024;
Integer month = 11;
Integer day = 20;
Integer hour = 14;
Integer minute = 30;
Integer second = 0;
Integer weekday = 3;
try {
String cronExpr = generateCronExpression(year, month, day, hour, minute, second, weekday);
System.out.println("Generated Cron Expression: " + cronExpr);
if (validateCronExpression(cronExpr)) {
System.out.println("Cron Expression is valid.");
} else {
System.out.println("Cron Expression is invalid.");
}
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}
}
} 去参考下quartz的代码,里面有操作的
页:
[1]