gpt4 book ai didi

java - 如何每天下午 2 点运行 TimerTask?

转载 作者:IT老高 更新时间:2023-10-28 20:56:36 25 4
gpt4 key购买 nike

我想每天下午 2 点执行一项工作。我可以使用 java.util.Timer 的哪种方法来安排我的工作?

2 小时后,运行它将停止作业并重新安排到第二天下午 2 点。

最佳答案

Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 2);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);

// every night at 2am you run your task
Timer timer = new Timer();
timer.schedule(new YourTask(), today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS)); // period: 1 day

关于java - 如何每天下午 2 点运行 TimerTask?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9375882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com