定时指定时间执行
TimerTask clockinTask2030 = new TimerTask() {
@Override
public void run() {
Platform.runLater(new Runnable() {
@Override
public void run() {
showWindowAlert("大哥看下手机打卡了,晚上20点30分打卡了,不看就要走流程了");
}
});
}
};
//设置执行时间
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);//每天
//定制每天的08:30:00执行,
calendar.set(year, month, day, 8, 27, 00);
Date date = calendar.getTime();
//创建定时器
Timer clockinTimer = new Timer();
//设置的定时器 period(每隔多长时间运行几次) 单位毫秒
clockinTimer.schedule(clockinTask0830, date);
//定制每天的18:00:00执行,
calendar.set(year, month, day, 17, 57, 00);
Date date1800 = calendar.getTime();
clockinTimer.schedule(clockinTask1800, date1800);
//20:30:00执行,
calendar.set(year, month, day, 20, 27, 00);
Date date2030 = calendar.getTime();
clockinTimer.schedule(clockinTask2030, date2030);
logger.info("start clockin message");
calendar.set(year, month, day, 8, 27, 00)设置无效,
到现在原因不明,运行程序会立即执行,将calendar.set(year, month, day, 8, 27, 00)修改为
这里不设置年月日,就表示今天的
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 87);
calendar.set(Calendar.SECOND, 00);
calendar.set(Calendar.MONTH, 11)需要注意
calendar.set(Calendar.MONTH, 11)设定是12月
calendar.set(Calendar.MONTH, 12)设定的是1月
可以如下方法查看是否正确
calendar.set(Calendar.MONTH, 11)
System.out.println(calendar.getTime().toString());
输出
Tue Dec 26 12:05:50 CST 2023