java timer 实现在指定时间执行方法
最近在弄一个定时发送的项目,需要定时执行方法,于是用到了Timer,但是我想要的功能网络上的资源比较少,所以自己研究了一下。
下面是我写的一些代码
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
public class TimerTest {
/*
* 定时执行方法
*/
public void Timing(String sssString , int[] dateTime){
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.set(dateTime[0], dateTime[1]-1, dateTime[2], dateTime[3], dateTime[4], dateTime[5]);//第二的参数要主要,月是从0开始的,所以要-1.
Date date = calendar.getTime();//网络上很多都是用Date date = new Date(year, month, date, hrs, min, sec)
// System.out.println(date);
try {
timer.schedule(new NowTask( timer,sssString), date);//可以传递参数进去定时任务类
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 实现时间字符串转化为int数组,便于传递参数
*/
public int[] String2Int(String timeString){
String year = timeString.substring(0, 4);
String month = timeString.substring(5, 7);
String date = timeString.substring(8, 10);
String hour =timeString.substring(11, 13);
String min = timeString.substring(14, 16);
String sec = timeString.substring(17,19);
int[] time = new int [6];
String[] timeStrings = new String[]{year,month,date,hour,min,sec};
for (int i = 0; i < timeStrings.length; i++) {
time[i]=Integer.valueOf(timeStrings[i]);
}
return time;
}
public static void main(String[] args) {
TimerTest timerTest = new TimerTest();
String timeString ="2012/04/11 16:46:00";
timerTest.Timing("成功了", timerTest.String2Int(timeString));
}
}
import java.util.Date;
import java.util.Timer;
public class TimerTest {
/*
* 定时执行方法
*/
public void Timing(String sssString , int[] dateTime){
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.set(dateTime[0], dateTime[1]-1, dateTime[2], dateTime[3], dateTime[4], dateTime[5]);//第二的参数要主要,月是从0开始的,所以要-1.
Date date = calendar.getTime();//网络上很多都是用Date date = new Date(year, month, date, hrs, min, sec)
// System.out.println(date);
try {
timer.schedule(new NowTask( timer,sssString), date);//可以传递参数进去定时任务类
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 实现时间字符串转化为int数组,便于传递参数
*/
public int[] String2Int(String timeString){
String year = timeString.substring(0, 4);
String month = timeString.substring(5, 7);
String date = timeString.substring(8, 10);
String hour =timeString.substring(11, 13);
String min = timeString.substring(14, 16);
String sec = timeString.substring(17,19);
int[] time = new int [6];
String[] timeStrings = new String[]{year,month,date,hour,min,sec};
for (int i = 0; i < timeStrings.length; i++) {
time[i]=Integer.valueOf(timeStrings[i]);
}
return time;
}
public static void main(String[] args) {
TimerTest timerTest = new TimerTest();
String timeString ="2012/04/11 16:46:00";
timerTest.Timing("成功了", timerTest.String2Int(timeString));
}
}
然后是NowTask类
import java.util.Timer;
import java.util.TimerTask;
public class NowTask extends TimerTask{
private Timer timer;
private String string;
/*
* 构造器
*/
public NowTask(){
}
public NowTask(String string){
this.string = string;
}
public NowTask(Timer timer){
this.timer =timer;
}
public NowTask(Timer timer , String string){
this.timer = timer;
this.string =string;
}
//属性的get、set方法
public Timer getTimer() {
return timer;
}
public void setTimer(Timer timer) {
this.timer = timer;
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
/*
* (non-Javadoc)
* @see java.util.TimerTask#run()
* 执行方法
*/
@Override
public void run(){
//这里写需要定时执行的方法
System.out.println(string);
timer.cancel();//传递timer进来就是为了在方法执行完后退出,必须退出
System.out.println("结束");
}
}
import java.util.TimerTask;
public class NowTask extends TimerTask{
private Timer timer;
private String string;
/*
* 构造器
*/
public NowTask(){
}
public NowTask(String string){
this.string = string;
}
public NowTask(Timer timer){
this.timer =timer;
}
public NowTask(Timer timer , String string){
this.timer = timer;
this.string =string;
}
//属性的get、set方法
public Timer getTimer() {
return timer;
}
public void setTimer(Timer timer) {
this.timer = timer;
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
/*
* (non-Javadoc)
* @see java.util.TimerTask#run()
* 执行方法
*/
@Override
public void run(){
//这里写需要定时执行的方法
System.out.println(string);
timer.cancel();//传递timer进来就是为了在方法执行完后退出,必须退出
System.out.println("结束");
}
}
分享一下,大家支持一下。