Javaのを使用して特定の時間()でタスクを実行したい。私のコードは動作しますが、うまく見えません。私は2つの文字列を比較しています。私はgetTimeInMills()を使って日付を比較しようとしましたが、数字は同じではありません。ここで特定の日付を待っています。カレンダーJavaコードの最適化
は私のコードです:
import java.util.Calendar;
class Voter {
Calendar current,deadline;
boolean keepGoing = true;
public static void main(String[] args) {
Voter strzal = new Voter();
strzal.shoot();
}
public void shoot() {
deadline = Calendar.getInstance();
deadline.set(2011,6,21,11,20,00);
while (keepGoing) {
// wait 1 second
try {
Thread.sleep(1000);
System.out.print(".");
} catch (InterruptedException ex) { ex.printStackTrace(); }
current = Calendar.getInstance();
if (deadline.getTime().toString().equals(current.getTime().toString())) {
System.out.println("Got it!");
keepGoing = false;
} // end of if
} // end of while
} // end of shoot() method
}
私は締め切りに等しくなるように、現在の時刻が(例では、私は2011年7月21日11に設定を待って、ループ内で実行しています:午前10時)。
コードを改善するにはどうすればよいですか?私は、例えば、compareTo()やgetTimeInMills()メソッドをカレンダークラスから使いたいのですが、期限を正しく設定することはできません。
ありがとうございます。
+1してくれてありがとう – mKorbel