使用 Timer.schedule 在特定时间制作 java 程序 运行 不工作
Using Timer.schedule to make java program run at a specific time Not working
我正在尝试使用 Timer.schedule() 在特定时间自动执行任务 运行。但是,它不起作用。
我的代码:
- 我先尝试设置了一个定时任务
- 然后我尝试使用当前时间创建一个 Date 对象
- 然后我使用Timer.schedule(timertask, date) 告诉计算机什么时候做任务。
但是,定时器任务不会在指定日期开始。
请帮忙,谢谢。
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Main {
public static void main(String[] args){
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
System.out.println("Starts");
BookingPage page = new BookingPage();
page.logIn();
page.selectCourseAndTime();
page.finishSelectTime();
}
};
Date date = generateDate();
timer.schedule(task, date);
}
public static Date generateDate() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDateTime now = LocalDateTime.now();
String[] arr = dtf.format(now).split("/");
int[] currentTime = new int[arr.length];
for (int i = 0; i < currentTime.length; i++) {
currentTime[i] = Integer.parseInt(arr[i]);
}
Calendar calendar = Calendar.getInstance();
calendar.set(currentTime[0], currentTime[1], currentTime[2], 15, 24, 0);
Date date = calendar.getTime();
return date;
}
}
我认为这解决了问题
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class MyTimerTask extends TimerTask {
@Override
public void run() {
System.out.println("Timer task started at:"+new Date());
completeTask();
System.out.println("Timer task finished at:"+new Date());
}
private void completeTask() {
try {
//assuming it takes 20 secs to complete the task
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String args[]){
TimerTask timerTask = new MyTimerTask();
//running timer task as daemon thread
Timer timer = new Timer(true);
timer.scheduleAtFixedRate(timerTask, 0, 10*1000);
System.out.println("TimerTask started");
//cancel after sometime
try {
Thread.sleep(120000);
} catch (InterruptedException e) {
e.printStackTrace();
}
timer.cancel();
System.out.println("TimerTask cancelled");
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
做calendar.set时(年月日时分秒)
month 需要比当前月份小一个。例如,五月是 4。
问题已解决!
我正在尝试使用 Timer.schedule() 在特定时间自动执行任务 运行。但是,它不起作用。 我的代码:
- 我先尝试设置了一个定时任务
- 然后我尝试使用当前时间创建一个 Date 对象
- 然后我使用Timer.schedule(timertask, date) 告诉计算机什么时候做任务。 但是,定时器任务不会在指定日期开始。 请帮忙,谢谢。
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Main {
public static void main(String[] args){
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
System.out.println("Starts");
BookingPage page = new BookingPage();
page.logIn();
page.selectCourseAndTime();
page.finishSelectTime();
}
};
Date date = generateDate();
timer.schedule(task, date);
}
public static Date generateDate() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDateTime now = LocalDateTime.now();
String[] arr = dtf.format(now).split("/");
int[] currentTime = new int[arr.length];
for (int i = 0; i < currentTime.length; i++) {
currentTime[i] = Integer.parseInt(arr[i]);
}
Calendar calendar = Calendar.getInstance();
calendar.set(currentTime[0], currentTime[1], currentTime[2], 15, 24, 0);
Date date = calendar.getTime();
return date;
}
}
我认为这解决了问题
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class MyTimerTask extends TimerTask {
@Override
public void run() {
System.out.println("Timer task started at:"+new Date());
completeTask();
System.out.println("Timer task finished at:"+new Date());
}
private void completeTask() {
try {
//assuming it takes 20 secs to complete the task
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String args[]){
TimerTask timerTask = new MyTimerTask();
//running timer task as daemon thread
Timer timer = new Timer(true);
timer.scheduleAtFixedRate(timerTask, 0, 10*1000);
System.out.println("TimerTask started");
//cancel after sometime
try {
Thread.sleep(120000);
} catch (InterruptedException e) {
e.printStackTrace();
}
timer.cancel();
System.out.println("TimerTask cancelled");
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
做calendar.set时(年月日时分秒) month 需要比当前月份小一个。例如,五月是 4。 问题已解决!