2012/03/13

A Stitch in Time,currentTimeMillis,TimerTask ,getWhen,UpdateTimeTask


A Stitch in Time

class UpdateTimeTask extends TimerTask {
   public void run() {
       long millis = System.currentTimeMillis() - startTime;
       int seconds = (int) (millis / 1000);
       int minutes = seconds / 60;
       seconds     = seconds % 60;

       timeLabel.setText(String.format("%d:%02d", minutes, seconds));
   }
}
And in the event listener to start this update, the following Timer() instance is used:
if(startTime == 0L) {
   startTime = evt.getWhen();
   timer = new Timer();
   timer.schedule(new UpdateTimeTask(), 100, 200);
}

No comments:

Post a Comment