【问题标题】:Using Timer.schedule to make java program run at a specific time Not working使用 Timer.schedule 使 java 程序在特定时间运行不工作
【发布时间】:2020-11-14 23:35:42
【问题描述】:

我正在尝试使用 Timer.schedule() 使任务在特定时间自动运行。但是,它不起作用。 我的代码:

  1. 我尝试先设置定时器任务
  2. 然后我尝试使用当前时间创建一个 Date 对象
  3. 然后我使用 Timer.schedule(timertask, date) 告诉计算机何时执行任务。 但是,timertask 不会在指定的日期开始。 请帮忙,谢谢。
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;
    }
}

【问题讨论】:

    标签: java timertask


    【解决方案1】:

    我认为这解决了问题

    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();
        }
    }
    

    【讨论】:

      【解决方案2】:

      在做calendar.set(年、月、日、时、分、秒)时 月份需要比当前月份小一。例如,五月是 4。 问题解决了!

      【讨论】:

        猜你喜欢
        • 2011-09-06
        • 1970-01-01
        • 2012-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-06
        • 2011-03-01
        • 1970-01-01
        相关资源
        最近更新 更多