gpt4 book ai didi

java - 如何在 "For Every 1.00 am of Friday"中为 cron 作业指定时间(以毫秒为单位)?

转载 作者:行者123 更新时间:2023-12-02 07:16:46 25 4
gpt4 key购买 nike

嗨,我正在尝试编写一个类(class)来安排在特定时间的工作。这是代码,

public void test(){
Timer timer = new Timer();
Calendar date = Calendar.getInstance();
date.set(Calendar.HOUR,0);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.SECOND, 2);
date.set(Calendar.MILLISECOND,0);

//schedule to run on 1 am every friday.
timer.schedule(new JobRunner(), date.getTime(),1*0*0*0);
}

请帮忙解决最后一行,我不知道如何在其中指定时间。

谢谢

最佳答案

我希望这会有所帮助。

    static final long period = 7*24*60*60*1000;//one week

public void test(){
Timer timer = new Timer();
Calendar date = Calendar.getInstance();
date.set(Calendar.HOUR_OF_DAY, 1);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND,0);
date.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
//in case you run the batch before date
if (date.before(Calendar.getInstance())) {
date.add(Calendar.DAY_OF_YEAR, 7);
}

long delay = date.getTimeInMillis() - new Date().getTime();
//schedule to run on 1 am every friday.
timer.schedule(new JobRunner(), delay, period);
}

问候!

关于java - 如何在 "For Every 1.00 am of Friday"中为 cron 作业指定时间(以毫秒为单位)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14837568/

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