我的计划程序有问题,我想在特定时间运行我的程序。但是,它工作正常,但停止工作。我无法指定问题。请在此处输入图像描述
我的代码
import java.util.*;
import java.text.*;
import java.util.TimerTask ;
public class ScheduleTask {
static DateFormat df = new SimpleDateFormat("HH:mm:ss");
static Date currentDate=new Date();
static Timer timer = new Timer();
public static void main(String[] args) throws ParseException {
System.out.println("Current Time: " + df.format( currentDate));
//Date and time at which you want to execute
Date date = df.parse("13:57:35");
timer.schedule(new MyTimeTask(), date);
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, 1);
date=c.getTime();
System.out.print(date);
}
private static class MyTimeTask extends TimerTask {
public void run() {
System.out.println("Running Task");
System.out.println("Current Time: " + df.format(currentDate));
timer.cancel();
}}
}
1条答案
按热度按时间0lvr5msh1#
您可以使用timertask执行此操作,下面是一个示例: