引入quartz
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</dependency>
public void test(@RequestParam("datetime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime datetime) {
/*
每五分钟执行一次
每小时执行一次
每天执行一次
每月执行一次
*/
CronExpression cronMinute = new CronExpression("0 */5 * * * ?");
CronExpression cronHour = new CronExpression("0 0 */1 * * ?");
CronExpression cronDay = new CronExpression("0 0 0 */1 * ?");
CronExpression cronMonth = new CronExpression("0 0 0 1 */1 ?");
Date date = Date.from(datetime.atZone(ZoneId.systemDefault()).toInstant());
if (cronMinute.isSatisfiedBy(date)) {
System.out.println("分钟");
}
if (cronHour.isSatisfiedBy(date)) {
System.out.println("小时");
}
if (cronDay.isSatisfiedBy(date)) {
System.out.println("日");
}
if (cronMonth.isSatisfiedBy(date)) {
System.out.println("月");
}
return null;
}