简体   繁体   English

将来如何在特定日期用Java运行任务

[英]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.

相关问题 Java Timer-如何在将来的特定时间安排任务 - java timer - how to scheduletask at a particular time in the future Java JNI和Future Task - Java JNI and Future Task 您如何在Java中比较日期,上一个日期和将来的日期? - How do you compare date, previous date and future date in Java? 如何在用户请求的特定日期和时间运行一组特定的Java代码 - How to run a specific set of java code at a particular date and time requested by user 如何在Java中将日历设置为特定日期和时间 - How to set Calender to Particular date and time in java 如何在特定时间段内使用 ScheduledExecutorService 运行任务? - How to run a task DURING PARTICULAR PERIOD OF TIME with ScheduledExecutorService? 如何在周四晚上7点之后使用ScheduledExecutorsService运行特定任务? - How to run a particular task after 7 PM on Thursday using ScheduledExecutorsService? 如何使用 ScheduledExecutorService 每天在特定时间运行某些任务? - How to run certain task every day at a particular time using ScheduledExecutorService? 如何在Java中运行soapUi的特定测试步骤 - How to run particular Test step of soapUi in java 如何确定Java之间的现在和任意未来日期之间的时间 - How to determine the time between now and an arbitrary future date in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM