[英]How run task in future in java at a particular date
I have have to call a method in future so i found some example Link are there Link 1 link 2 将来我必须调用一个方法,所以我找到了一些示例链接是否存在链接1 链接2
But I have to run it ONE TIME only. 但是我只能运行一次。 at Date and Time : 11-03-2014 10:15:20 (dd-MM-yyyy HH:MM:SS)
在日期和时间:2014年11月3日10:15:20(dd-MM-yyyy HH:MM:SS)
I how do it?? 我怎么办??
The class java.util.Timer
has exactly what you need: 类
java.util.Timer
正是您需要的:
First, set-up the task to be scheduled: 首先,设置要安排的任务:
TimerTask task = new TimerTask() {
void run() {
//do the task
}
};
Second, schedule the task: 其次,安排任务:
Date futureDate = ...///whenever you want
Timer timer = new Timer();
timer.schedule(task, futureDate);
Compute the delay between now and your target date, and call schedule()
with this delay as argument: 计算从现在到您的目标日期之间的延迟,并使用此延迟作为参数调用
schedule()
:
Date targetDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").parse(dateAsString);
long delayInMillis = targetDate - System.currentTimeMillis();
scheduler.schedule(task, delayInMillis, TimeUnit.MILLISECONDS);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.