Java 定时器

package com.sayes.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.Timer;
import java.util.TimerTask;


public class ProTimer {
	
	
	public void StartTimer(String date,final String Close){
		//"2013-01-17 16:25:00"
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	     try {
	    	 if(date.length()<=10){
	    		 date=date+" 00:00:00";
	    	 }
	         Date datetime = sdf .parse(date);
	         final Timer  timer = new Timer();
	         System.out.println("==定时间器已经启动==");
	         timer.scheduleAtFixedRate(new TimerTask() {
	     
	             public void run() {
	            	 
	                System.out.println("执行了");
	                
	                if(Close.equals("close")){	 
                      timer.cancel();//使用这个方法退出任务
	                }
	             }
	          
	         }, datetime ,24*60*60*1000);
	     } catch (ParseException e) {
	        e.printStackTrace();
	     }
		
	}
	
	public static void main(String[] args) {
	      
	     try {
	    	 ProTimer st=new ProTimer(); 
	    	 st.StartTimer("2013-01-17 16:38:00","close");
	     } catch (Exception e) {
	        e.printStackTrace();
	     }

	}
	
	

}

你可能感兴趣的:(java)