源程序如下:
import java.util.*;public class hi {
public static void main(String args[]) 
throws java.io.IOException {
TimerTask task = new TimerTask() {
public void run() {
System.out.println("Hi");
}
};
Timer timer = new Timer();
timer.schedule(task, 0, 2000);//定时发送
System.out.println("Press ENTER to stop");
System.in.read(new byte[10]);
timer.cancel();
}
}请问以上程序如何修改为可以自定义起始时间呢?timer.schedule(task, 0, 2000)中我设置的时间0开始 不知道如何根据外部输入而确定时间的方法.
求教.

解决方案 »

  1.   

    * @param task   task to be scheduled.
         * @param firstTime First time at which task is to be executed.
         * @param period time in milliseconds between successive task executions.
         * @throws IllegalArgumentException if <tt>time.getTime()</tt> is negative.
         * @throws IllegalStateException if task was already scheduled or
         *         cancelled, timer was cancelled, or timer thread terminated.
         */
        public void schedule(TimerTask task, Date firstTime, long period) {
            if (period <= 0)
                throw new IllegalArgumentException("Non-positive period.");
            sched(task, firstTime.getTime(), -period);
        }
      

  2.   

    scheduleAtFixedRate
    public void scheduleAtFixedRate(TimerTask task,
                                    Date firstTime,
                                    long period)Schedules the specified task for repeated fixed-rate execution, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period. 
    In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate). Fixed-rate execution is appropriate for recurring activities that are sensitive to absolute time, such as ringing a chime every hour on the hour, or running scheduled maintenance every day at a particular time. It is also appropriate for for recurring activities where the total time to perform a fixed number of executions is important, such as a countdown timer that ticks once every second for ten seconds. Finally, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain synchronized with respect to one another. 
    Parameters:
    task - task to be scheduled.
    firstTime - First time at which task is to be executed.
    period - time in milliseconds between successive task executions. 
    Throws: 
    IllegalArgumentException - if time.getTime() is negative. 
    IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.
      

  3.   

    在CSDN搜索“闹钟”,可以找到类似的帖子。
      

  4.   

    可以设定开始时间的
    schedule有好多重载方法
      

  5.   

    up
    如何在前台通过JSP页面输入日期参数控制timer的执行呢?
      

  6.   

    public void schedule(TimerTask task,
                         Date firstTime,
                         long period)
      

  7.   

    用Quartz吧,可以让一个任务在特定的时间执行。
    spring有集成