我需要执行具有可自定义系统日期的计划任务,例如,我应该能够将计划程序日期设置为 2019-01-15,在这种情况下,计划程序必须执行该日期计划的所有任务,了解当前日期是 2019-02-12。目前,我只看到了使用 Spring 或 Quartz 的时区解决方案。 谢谢!.
最佳答案
使用TaskScheduler
模块:
private TaskScheduler scheduler;
Runnable exampleRunnable = new Runnable(){
@Override
public void run() {
System.out.println("Executing task");
}
};
@Async
public void executeTaskT() {
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
scheduler = new ConcurrentTaskScheduler(localExecutor);
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date scheduledDate = dateFormat.parse("12-02-2019", dateFormat);
scheduler.schedule(exampleRunnable, scheduledDate);
}
executeTaskT(); //call it somewhere after the spring application has been configured
有关更多信息,请参阅: Spring scheduling task - run only once
关于java - 将自定义系统日期设置为计划程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54652565/