How to call a method on specific time in java?

前端 未结 9 1850
Happy的楠姐
Happy的楠姐 2020-11-27 04:43

Is it possible to call a method in java at specific time? For example of I have a piece of code like this:

class Test{

    public static void main(String ar         


        
9条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 05:18

    It is possible. You can schedule a method at some time using Timer and TimerTask.

    For example:

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 10);
    calendar.set(Calendar.MINUTE, 30);
    calendar.set(Calendar.SECOND, 0);
    
    Date alarmTime = calendar.getTime();
    
    Timer _timer = new Timer();
    _timer.schedule(foo, alarmTime);
    

    Refer these links:

    • Timer
    • TimerTask

提交回复
热议问题