[英]How run task in future in java at a particular date
類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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.