1 import java.text.SimpleDateFormat; 2 import java.util.Date; 3 import java.util.TimerTask; 4 public class TimerDemo extends TimerTask{ 5 public void run(){ 6 SimpleDateFormat sdf=null; 7 sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); 8 System.out.println(sdf.format(new Date())); 9 } 10 }
1 import java.util.Timer; 2 public class TestTask { 3 public static void main(String[] args) { 4 Timer t=new Timer(); 5 TimerDemo td=new TimerDemo(); 6 t.schedule(td, 1000,2000); 7 } 8 }