MyTask.java: package com.timer; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimerTask; public class MyTask extends TimerTask{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public void run(){ System.out.println(sdf.format(new Date())); } } TestTimer.java: package com.timer; import java.util.Timer; public class TestTimer{ public static void main(String[] args){ MyTask myTask = new MyTask(); Timer timer = new Timer(); timer.schedule(myTask, 1000, 1000); System.out.println("main end"); } } 输出结果: main end 2009-12-31 19:25:58 2009-12-31 19:25:59 2009-12-31 19:26:00 2009-12-31 19:26:01 2009-12-31 19:26:02 2009-12-31 19:26:03