实现Timer 接口 可以创建定时器 继承TimerTask类,可以创建计划任务。 使用定时器,绑定计划任务,就OK了。例如public class MyTimerTask extends TimerTask { public static int i=0; @Override public void run() { Date d = new Date(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); String dstr = df.format(d); i++; System.out.println("时间"+dstr+"\t执行"+i+"次"); }} public static void main(String[] args) { Timer myt = new Timer(); MyTimerTask task = new MyTimerTask(); Date runtime = new Date(); //myt.schedule(task, runtime); myt.schedule(task, runtime,1000); //每1秒中执行一次 }
2、让操作系统的定制任务在特定时间调用你的jar
继承TimerTask类,可以创建计划任务。
使用定时器,绑定计划任务,就OK了。例如public class MyTimerTask extends TimerTask {
public static int i=0;
@Override
public void run() {
Date d = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
String dstr = df.format(d);
i++;
System.out.println("时间"+dstr+"\t执行"+i+"次");
}}
public static void main(String[] args) {
Timer myt = new Timer();
MyTimerTask task = new MyTimerTask();
Date runtime = new Date();
//myt.schedule(task, runtime);
myt.schedule(task, runtime,1000); //每1秒中执行一次
}
你可以指定周几。
上面1000每秒执行一次,你可以指定更长的间隔。
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 1);
calendar.set(Calendar.MINUTE, 0);
long period = 24 * 60 * 60 * 1000;
time.scheduleAtFixedRate(new ClearDoc(), calendar.getTime(),period);
每天一点执行一次。
period是间隔时间。
new ClearDoc()这个方法是继承自TimerTask类,也就是你要执行的任务。。
calendar.getTime()这个方法是第一次执行的时间。
import java.text.SimpleDateFormat;/**
* Created by IntelliJ IDEA.
* User: xdhe_wdtx
* Date: 2010-6-24
* Time: 14:43:16
* To change this template use File | Settings | File Templates.
*/
public class MailMain { public static void main(String[] args){
try{
SimpleDateFormat currDate=new SimpleDateFormat("yyyy-MM-dd");
String currstr = currDate.format(new Date());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date fixedDate=sdf.parse(currstr+" 17:30:00");
System.out.println("启动时设置发送时间:"+fixedDate.toString());
System.out.println("启动时系统时间:"+new Date().toString());
Timer timer=new Timer();
MailTask mTask=new MailTask();
// timer.scheduleAtFixedRate(mTask,new Date(),24*60*60*1000);
timer.scheduleAtFixedRate(mTask,fixedDate,24*60*60*1000);
// TestTask tTask=new TestTask();
// timer.scheduleAtFixedRate(tTask,new Date(),10000);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
import org.apache.commons.mail.MultiPartEmail;import java.util.TimerTask;
import java.util.Date;
import java.io.*;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.text.SimpleDateFormat;/**
* Created by IntelliJ IDEA.
* User: xdhe_wdtx
* Date: 2010-6-24
* Time: 14:43:46
* To change this template use File | Settings | File Templates.
*/
public class MailTask extends TimerTask { public void run(){
try{
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
String currDate=sdf.format(new Date());
String path=System.getProperties().getProperty("user.dir")+System.getProperties().getProperty("file.separator")+"sendlogs"+System.getProperties().getProperty("file.separator");
String filename=currDate+"report.txt";
// System.out.println("filename:"+path+filename);
/***************************************获取统计信息***********************************************/
File file=new File(path+filename);
BufferedWriter bfw=new BufferedWriter(new FileWriter(file));
String url="jdbc:oracle:thin:@IP:1521:test";
String usr="***";
String pwd="***";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn= DriverManager.getConnection(url,usr,pwd);
PreparedStatement pst=null;
bfw.write("发送类别,总数,成功数,成功率");
bfw.newLine();
pst=conn.prepareStatement("select '早记录' as sname ,count(1) total,sum(decode(i_status,200,1,0)) success from t_send_his_006 t where d_savedate between sysdate -0.4 and sysdate -0.3 union all select '晚记录' as sname ,count(1) total,sum(decode(i_status,200,1,0)) success from t_send_his_006 t where d_savedate between sysdate -0.1 and sysdate ");
ResultSet rs=pst.executeQuery();
while(rs.next()){
int total=rs.getInt("total");
int suc=rs.getInt("success");
float rate=0.00f;
if(total!=0){
rate=(float)suc/(float)total*100;
}
bfw.write(rs.getString("sname")+":"+String.valueOf(total)+","+String.valueOf(suc)+","+String.valueOf(rate));
bfw.newLine();
}
rs.close();
pst.close();
bfw.close(); /***************************************获取统计信息***********************************************/
System.out.println(currDate+"统计数据已经成功!"); }catch(Exception ex){
ex.printStackTrace();
}finally{
}
}
}