How to execute function automatically at specified time in java

后端 未结 2 2009
小蘑菇
小蘑菇 2021-01-26 17:57

I have got a requirement in which i need to execute a function at specified time set by the user.The function contains the code to generate a pdf file from database and save it

2条回答
  •  迷失自我
    2021-01-26 18:28

    Okay, so I looked a bit into the Timer class and found your approach to be almost correct. You'll need to edit your code a little bit to get the wanted functionality.

    Calendar calendar = Calendar.getInstance();
    date.set(Calendar.DAY_OF_WEEK, getUserInputDay()); //
    calendar.set(Calendar.HOUR_OF_DAY, getUserInputHour());
    calendar.set(Calendar.MINUTE, getUserInputMinute());
    calendar.set(Calendar.SECOND, getUserInputSeconds());
    Date time = calendar.getTime();
    
    timer = new Timer();
    timer.schedule(new ReportGenerator(), time);
    

    This will start a timer that launches the "ReportGenerator" at the stated time. You'll have to figure out yourself how to get the input from the user (should be fairly simple!)

提交回复
热议问题