按时间间隔执行

import java.util.Timer;
import java.util.TimerTask;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;

public  class FetchMail extends TimerTask {

  /**
  * Construct and use a TimerTask and Timer.
  */

  /**
  * Implements TimerTask's abstract run method.
  */
  public void run(){
    //toy implementation
    time=time+1;
    System.out.println(time);
  }

  // PRIVATE
  private static int time=1;
  //expressed in milliseconds
  //private final static long fONCE_PER_DAY = 1000*60*60*24;
  private final static long fONCE_PER_DAY = 1000*2;
  private final static int SECOND = 3;
  private final static int fONE_DAY = 1;
  private final static int fFOUR_AM = 4;
  private final static int fZERO_MINUTES = 0;

  private static Date getTomorrowMorning4am(){
    Calendar tomorrow = new GregorianCalendar();
    //tomorrow.add(Calendar.DATE, fONE_DAY);
    tomorrow.add(Calendar.SECOND,fONE_DAY);
    Calendar result = new GregorianCalendar(
      tomorrow.get(Calendar.YEAR),
      tomorrow.get(Calendar.MONTH),
      tomorrow.get(Calendar.DATE),
      fFOUR_AM,
      fZERO_MINUTES
    );
    System.out.println("11");
    return tomorrow.getTime();
  }

 /**
  * Method main
  *
  *
  * @param args
  *
  */
 public static void main(String[] args) {
  // TODO: Add your code here
      TimerTask fetchMail  = new FetchMail();
    Calendar tomorrow = new GregorianCalendar();
    //tomorrow.add(Calendar.DATE, fONE_DAY);
    tomorrow.add(Calendar.SECOND, SECOND);
    System.out.println(tomorrow.getTime());
    Calendar tomorrow1 = new GregorianCalendar();
    //tomorrow.add(Calendar.DATE, fONE_DAY);
    tomorrow1.add(Calendar.SECOND, SECOND+2);
   
    System.out.println(tomorrow1.getTime());
    //perform the task once a day at 4 a.m., starting tomorrow morning
    //(other styles are possible as well)
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(fetchMail, getTomorrowMorning4am(), fONCE_PER_DAY);
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值