java 定时任务 TimerTask Timer
package com.test.tasket; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import java.util.Timer; import java.util.TimerTask; public class TestTasket extends TimerTask{ private int count = 0; private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); /** * @param args */ public static void main(String[] args) { TestTasket tt = new TestTasket(); tt.testTime(); } public void testTime() { try { TestTasket tasket = new TestTasket(); Timer timer = new Timer(); String timeNow = "2011-10-31 11:25:00"; Date timeDate = sdf.parse(timeNow); timer.schedule(tasket,timeDate, 1000); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { System.out.println("时间:" + sdf.format(new Date()) + "已经执行任务"+count+"次"); count ++ ; }