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走一遍就查不多了。最近还想写带你东西,真实懒得写。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值