1 public class TestTimer { 2 public static void main (String [] args) { 3 // (1) create an object of Timer 4 Timer t = new Timer (); 5 // (2) call the schedule () method to execute the task 6 // Create an object of the task class 7 TimerTask task = new Clock (); 8 // The task to be executed, the second parameter is the execution start time of the task, and the third parameter is how often to execute 9 t .schedule (task, new Date (System.currentTimeMillis () + 1000), 1000 ); 10 } 11 } 12 class Clock extends TimerTask{ 13 long time=1000;//1秒 14 @Override 15 public void run() { 16 Date date=new Date(time); 17 System.out.println(date.toLocaleString()); 18 time+=1000; 19 } 20 }