您的位置:首页 > 编程语言 > Java开发

java定时任务实现

2016-07-08 14:54 399 查看
今天写了一个定时任务,废话不说上代码:

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走一遍就查不多了。最近还想写带你东西,真实懒得写。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java定时任务 java