我将来必须调用一个方法,所以我找到了一些示例链接有 链接 1 链接 2
但我必须只运行一次。在日期和时间:11-03-2014 10:15:20 (dd-MM-yyyy HH:MM:SS)
我怎么办??
该课程java.util.Timer
正是您所需要的:
首先,设置要计划的任务:
TimerTask task = new TimerTask() {
void run() {
//do the task
}
};
二、调度任务:
Date futureDate = ...///whenever you want
Timer timer = new Timer();
timer.schedule(task, futureDate);
计算现在和目标日期之间的延迟,并schedule()
以此延迟作为参数调用:
Date targetDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").parse(dateAsString);
long delayInMillis = targetDate - System.currentTimeMillis();
scheduler.schedule(task, delayInMillis, TimeUnit.MILLISECONDS);