http://www.verejava.com/?id=17159702655333
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Test1 {
private Timer timer;
public Test1() {
timer = new Timer();
//每隔 1 秒 显示当前时间
timer.schedule(new MyTask(), new Date(), 1000);
}
class MyTask extends TimerTask {
@Override
public void run() {
//打印当前时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(new Date()));
}
}
public static void main(String[] args) {
new Test1();
}
}