java timer 指定某时间点执行
package com.northeasttycoon.service;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* describe : 指定某一时间点执行任务操作
* @author northeasttycoon
*/
public class ServiceBusiness implements IServiceBusiness {
final private static Log log = LogFactory.getLog(ServiceBusiness.class);
public ServiceBusiness() {
}
public void testTimer() throws Exception {
// 时间类
Calendar startDate = Calendar.getInstance();
//设置开始执行的时间为 某年-某月-某月 00:00:00
startDate.set(startDate.get(Calendar.YEAR), startDate.get(Calendar.MONTH), startDate.get(Calendar.DATE), 15, 10, 0);
// 1小时的毫秒设定
long timeInterval = 60 * 60 * 1000;
// 定时器实例
Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
// 定时器主要执行的代码块
System.out.println("定时器主要执行的代码!"+CommonUtil.GetNowDateTime());
}
// 设定的定时器在15:10分开始执行,每隔 1小时执行一次.
}, startDate.getTime(), timeInterval ); //timeInterval 是一天的毫秒数,也是执行间隔
};
public static void main(String[] args) throws Exception {
ServiceBusiness business = new ServiceBusiness();
business.testTimer();
}