java凌晨12点_java - JAVA如果我在每天中午12点之后安排我的时间表,会发生什么? - SO中文参考 - www.soinside.com...
它實際上是Timer / TimerTask組合(link)的更通用的替代品
此外,Java 8提供了一些有用的工具來進行所需的時間計算。一個例子可能是:
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public void schedule(Runnable command) {
LocalDateTime currentTime = LocalDateTime.now();
LocalDateTime executionDate = LocalDateTime.of(currentTime.getYear(),
currentTime.getMonth(),
currentTime.getDayOfMonth(),
12, 0); // begin execution at 12:00 AM
long initialDelay;
if(currentTime.isAfter(executionDate)){
// take the next day, if we passed the execution date
initialDelay = currentTime.until(executionDate.plusDays(1), ChronoUnit.MILLIS);
} else {
initialDelay = currentTime.until(executionDate, ChronoUnit.MILLIS);
}
long delay = TimeUnit.HOURS.toMillis(24); // repeat after 24 hours
ScheduledFuture> x = scheduler.scheduleWithFixedDelay(command, initialDelay, delay , TimeUnit.MILLISECONDS);
}
總結
以上是生活随笔為你收集整理的java凌晨12点_java - JAVA如果我在每天中午12点之后安排我的时间表,会发生什么? - SO中文参考 - www.soinside.com...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: b150主板升级内存,这些你必须知道
- 下一篇: java中的topicFont_Font