Java 让scheduleAtFixedRate接受小时而不是毫秒?
我如何让scheduleAtFixedRate以小时而不是毫秒接受参数,如下所示?程序应定期运行1小时,即11:25:00:00、12:25:00:00等Java 让scheduleAtFixedRate接受小时而不是毫秒?,java,timer,timertask,Java,Timer,Timertask,我如何让scheduleAtFixedRate以小时而不是毫秒接受参数,如下所示?程序应定期运行1小时,即11:25:00:00、12:25:00:00等 public class Kronos { public static void main(String[] args) throws ParseException { DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:s
public class Kronos {
public static void main(String[] args) throws ParseException {
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = dateFormatter.parse("2017-02-09 11:25:00");
long hours = 1;
long msPerHour = 3600000;
long period = hours * msPerHour;
Timer timer = new Timer();
TimerTask taskNew = new TimerTask() {
@Override
public void run() {
System.out.println("ALL WORK AND NO PLAY MAKES JACK A DULL BOY");
}
};
taskNew.scheduledExecutionTime();
timer.scheduleAtFixedRate(taskNew, d, period);
}
}
最初我考虑了类似于
TimeFormat.HOURS
的东西,但我不知道如何实现它…TimeUnit.HOURS.toMillis(HOURS)
是一种将小时转换为毫秒的简洁方法。所讨论的方法似乎只能接受毫秒。TimeUnit.HOURS.toMillis(HOURS)
是一种将小时转换为毫秒的简明方法。所讨论的方法似乎只能接受毫秒以外的任何值。你说的“在小时内接受参数”是什么意思?您的代码符合您的要求…您可以使用TimeUnit.HOURS.toMillis(HOURS)
使用TimeUnit.HOURS.toMillis(numberOfHours)
。您所说的“以小时为单位接受参数”是什么意思?您的代码符合您的要求…您可以使用TimeUnit.HOURS.toMillis(HOURS)
使用TimeUnit.HOURS.toMillis(numberOfHours)
。