2016-07-08 1 views
1

分単位でスケジュールを設定するJavaシステムを構成したいが、毎回そのアクションを停止し、分。Javaでスケジュールを設定し、分単位で実行する(継続的に実行する)

public static void main(String[] args) throws ParseException { 
    // TODO code application logic here 
    Timer timer = new Timer(); 
    TimerTask tt = new TimerTask() { 
     @Override 
     public void run() { 

      Calendar cal = Calendar.getInstance(); //this is the method you should use, not the Date(), because it is deprecated. 

      int min = cal.get(Calendar.MINUTE);//get the hour number of the day, from 0 to 23 
      try { 

       if (min == 17) {      
        System.out.println("doing the scheduled task at " + min); 
        this.cancel(); 
       } 

       if (min == 18) {       
        System.out.println("doing the scheduled task at " + min); 
        this.cancel(); 

       } 
      } catch (Exception e) { 
       System.out.println("Error Occurs " + e.getMessage()); 
      } 
     } 
    }; 

    timer.schedule(tt, 1000, 1000);// delay the task 1 second, and then run task every one second 

} 

親切に支援:(

+0

私のためにうまく動作しますか、エラーが表示されますか? – zombie

+0

正確に何をしたいのですか?...それはコンソールに17を印刷しています!! – sparsh610

+0

@ゾンビのエラーはありませんが、コードを実行していない時間が18分になったとき – LuidonVon

答えて

0
if (min == 17) {      
    System.out.println("doing the scheduled task at " + min); 
    this.cancel(); 
} 

this.cancelを()犯人はある

Here is the description 

パブリックブール()

このタイマータスクを取り消しキャンセルもしタスクが1回限りの実行が予定されており、まだ実行されていないか、またはまだスケジュールされていない場合、実行されることはありません。繰り返し実行するようにスケジュールされているタスクは、決して再び実行されません。

希望がこれを助けます

関連する問題