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();
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM