Java获取当前秒数

package com.qs.thread;

import java.time.LocalDateTime;
import java.util.Timer;
import java.util.TimerTask;

public class TraditionalTimerTest5 {

    public static void main(String[] args) {
        new Timer().schedule(new MyTimerTask(), 2000);

        while (true) {
            System.out.println(LocalDateTime.now().getSecond());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static class MyTimerTask extends TimerTask {
        public void run() {
            System.out.println("bombing!");
            new Timer().schedule(new MyTimerTask(), 2000);
        }
    }

}

你可能感兴趣的:(Java)