首页javalegacy_date_timeJava Data Type - 如何使用计时器任务每隔五秒钟输出时间

Java Data Type - 如何使用计时器任务每隔五秒钟输出时间

我们想知道如何使用计时器任务每隔五秒钟输出时间。
import java.util.Timer;
import java.util.TimerTask;

public class Main {

  public String CurrentDate() {
    java.util.Date dt = new java.util.Date();
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
    String currentTime = sdf.format(dt);
    return currentTime;
  }

  public static void main(String[] args) {
    class SayHello extends TimerTask {
      Main thisObj = new Main();

      public void run() {
        String todaysdate = thisObj.CurrentDate();
        System.out.println(todaysdate);
      }
    }
    Timer timer = new Timer();
    timer.schedule(new SayHello(), 0, 5000);
  }

}