time - Waiting for specific date. Calendar Java Code Optimization -


i want perform task using java at specific, given hour. code works, doesn't good. i'm comparing 2 strings. tried compare dates using gettimeinmills() numbers aren't same.

here's code:

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 

}

i'm running in loop, waiting current time equal deadline (in example set 21st july 2011 11:10 am).

how can improve code? i'd use example compareto() or gettimeinmills() method calendar class, can't set deadline properly.

thanks in advance.

using scheduledexecutorservice better suggestion, knowledge

calendar deadline = calendar.getinstance(); deadline.set(2011,6,21,11,20,00); long endtime = deadline.gettime().gettime();  // later long current = system.currenttimemillis(); if (current >= endtime) {     system.out.println("got it!");     break; } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -