1. 程式人生 > >java定時任務實現

java定時任務實現

今天寫了一個定時任務,廢話不說上程式碼:

package test.timetask;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * java定時任務
 * 
 * @author qin
 *
 */
public class TimeTask {
	Timer timer = new Timer();

	/**
	 * 在規定的時間內執行一次任務
	 * 
	 * @param task
	 *            任務
	 * @param executionTime
	 *            執行時間
	 * @throws Exception
	 */
	public void showTime(TimerTask task, String executionTime) throws Exception {
		try {
			Calendar calendar = Calendar.getInstance();
			// 獲取年
			int year = calendar.get(Calendar.YEAR);
			// 獲取月,
			int month = calendar.get(Calendar.MONTH);
			// 獲取日
			int day = calendar.get(Calendar.DAY_OF_MONTH);
			// 設定執行時間:年 月 日 時 分 秒
			String[] runTime = executionTime.split(",");
			int when = 00;
			int points = 00;
			int seconds = 00;
			if (Integer.parseInt(runTime[0]) != 0) {
				year = Integer.parseInt(runTime[0]);
			}
			if (Integer.parseInt(runTime[1]) != 0) {
				month = Integer.parseInt(runTime[1]);
			}
			if (Integer.parseInt(runTime[2]) != 0) {
				day = Integer.parseInt(runTime[2]);
			}
			if (Integer.parseInt(runTime[3]) != 0) {
				when = Integer.parseInt(runTime[3]);
			}
			if (Integer.parseInt(runTime[4]) != 0) {
				points = Integer.parseInt(runTime[4]);
			}
			if (Integer.parseInt(runTime[5]) != 0) {
				seconds = Integer.parseInt(runTime[5]);
			}
			calendar.set(year, month, day, when, points, seconds);
			Date date = calendar.getTime();
			System.out.println(date);
			// 設定為每天的date時刻執行,只執行一次。
			timer.schedule(task, date);
		} catch (Exception e) {
			System.out.println(e);
			throw new Exception(
					"定時任務出現錯誤+package test.timetask.TimeTask.showTime()");
		}

	}

	/**
	 * 
	 * @param task
	 *            任務
	 * @param executionTime
	 *            執行時間
	 * @param second
	 *            每隔多少秒執行一次任務
	 * @throws Exception
	 */
	public void repeatTimeTask(TimerTask task, String executionTime, int second)
			throws Exception {
		try {
			Calendar calendar = Calendar.getInstance();
			// 獲取年
			int year = calendar.get(Calendar.YEAR);
			// 獲取月,
			int month = calendar.get(Calendar.MONTH);
			// 獲取日
			int day = calendar.get(Calendar.DAY_OF_MONTH);
			// 設定執行時間:年 月 日 時 分 秒
			System.out.println("請輸入時間:(時分秒,以逗號隔開。例如:0,0,0,23,00,00)");
			String[] runTime = executionTime.split(",");
			int when = 00;
			int points = 00;
			int seconds = 00;
			if (Integer.parseInt(runTime[0]) != 0) {
				year = Integer.parseInt(runTime[0]);
			}
			if (Integer.parseInt(runTime[1]) != 0) {
				month = Integer.parseInt(runTime[1]);
			}
			if (Integer.parseInt(runTime[2]) != 0) {
				day = Integer.parseInt(runTime[2]);
			}
			if (Integer.parseInt(runTime[3]) != 0) {
				when = Integer.parseInt(runTime[3]);
			}
			if (Integer.parseInt(runTime[4]) != 0) {
				points = Integer.parseInt(runTime[4]);
			}
			if (Integer.parseInt(runTime[5]) != 0) {
				seconds = Integer.parseInt(runTime[5]);
			}
			calendar.set(year, month, day, when, points, seconds);
			Date date = calendar.getTime();
			System.out.println(date);
			// 如果需要重複執行 則使用以下方法。
			if (second == 0) {
				second = 1;
			}
			long period = second * 1000;
			timer.schedule(task, date, period);
		} catch (Exception e) {
			System.out.println(e);
			throw new Exception(
					"定時任務出現錯誤+package test.timetask.TimeTask.repeatTimeTask()");
		}

	}

}

下面是測試類:
package test.timetask;

import java.util.Date;
import java.util.TimerTask;

public class test {
	public static void main(String[] args) throws Exception {
		String executionTime = "0,0,0,17,43,00";
		TimerTask task = new TimerTask() {
			int count = 0;
			public void run() {
				count++;
				System.out.println("時間:"+ new Date()+"執行了"+count+"次~");
				
			}
		};
		TimeTask timeTask = new TimeTask();
		timeTask.showTime(task, executionTime);
		timeTask.repeatTimeTask(task, executionTime, 3);
	}

}


作為示例,如果有什麼地方不清楚,debug走一遍就查不多了。最近還想寫帶你東西,真實懶得寫。