1 25 26 34 public class ElapsedTimer { 35 private long startTime; 37 private long stopTime; 38 39 42 public ElapsedTimer() { 43 reset(); 44 } 45 46 49 public void stop() { 50 stopTime = System.currentTimeMillis(); 51 } 52 53 56 public final void reset() { 57 startTime = System.currentTimeMillis(); 58 } 59 60 public long getElapsedMilliseconds() { 61 long st = (stopTime == 0) ? System.currentTimeMillis() : stopTime; 62 return st - startTime; 63 } 64 65 public int getElapsedSeconds() { 66 return (int)(getElapsedMilliseconds() / 1000L); 67 } 68 69 public String toString() { 70 long ms = getElapsedMilliseconds(); 71 int sec = (int)(ms / 1000L); 72 int frac = (int)(ms % 1000L); 73 StringBuffer sb = new StringBuffer (); 74 sb.append(ms / 1000L); 75 sb.append('.'); 76 sb.append(ms % 1000L); 77 sb.append(" seconds"); 78 return sb.toString(); 79 } 80 } 81 | Popular Tags |