1 20 package JFlex; 21 22 30 public class Timer { 31 32 33 private long startTime, stopTime; 34 35 36 private boolean running; 37 38 39 42 public Timer() { 43 startTime = System.currentTimeMillis(); 44 running = true; 45 } 46 47 48 52 public void start() { 53 startTime = System.currentTimeMillis(); 54 running = true; 55 } 56 57 58 61 public void stop() { 62 stopTime = System.currentTimeMillis(); 63 running = false; 64 } 65 66 67 72 public long diff() { 73 if (running) 74 return System.currentTimeMillis()-startTime; 75 else 76 return stopTime-startTime; 77 } 78 79 80 87 public String toString() { 88 long diff = diff(); 89 90 long millis = diff%1000; 91 long secs = (diff/1000)%60; 92 long mins = (diff/(1000*60))%60; 93 long hs = (diff/(1000*3600))%24; 94 long days = diff/(1000*3600*24); 95 96 if (days > 0) 97 return days+"d "+hs+"h "+mins+"m "+secs+"s "+millis+"ms"; 98 99 if (hs > 0) 100 return hs+"h "+mins+"m "+secs+"s "+millis+"ms"; 101 102 if (mins > 0) 103 return mins+"m "+secs+"s "+millis+"ms"; 104 105 if (secs > 0) 106 return secs+"s "+millis+"ms"; 107 108 return millis+"ms"; 109 } 110 } 111 | Popular Tags |