How to call a method on specific time in java?

前端 未结 9 1889
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:25

    Timer timer = new Timer();
    Calendar calendar = Calendar.getInstance(); // gets a calendar using the default time zone and locale.
    calendar.add(Calendar.SECOND, 5);
    Date date = calendar.getTime();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            test();
        }
    }, date);
    

提交回复
热议问题