?? stopwatch.java
字號:
package stopwatch;//------------------------------------------------------------------------------public class StopWatch { static public int AN_HOUR = 60 * 60 * 1000; static public int A_MINUTE = 60 * 1000; ;private long startTime = -1; private long stopTime = -1;//------------------------------------------------------------------------------ /** * 啟動秒表 */ public void start() { this.startTime =System.currentTimeMillis(); }//------------------------------------------------------------------------------ /** * 停止秒表 */ public void stop() { this.stopTime =System.currentTimeMillis(); }//------------------------------------------------------------------------------ /** * 重置秒表 */ public void reset() { this.startTime = -1; this.stopTime = -1; }//------------------------------------------------------------------------------ /** * 分割時間 */ public void split() { this.stopTime =System.currentTimeMillis(); }//------------------------------------------------------------------------------ /** * 移除分割 */ public void unsplit() { this.stopTime = -1; }//------------------------------------------------------------------------------ /** * 獲得秒表的時間,這個時間或者是啟動時和最后一個分割時刻的時間差, * 或者是啟動時和停止時的時間差,或者是啟動時和這個方法被調用時的差 */ public long getTime() { if(stopTime != -1) {return(System.currentTimeMillis() - this.startTime);} else {return this.stopTime - this.startTime;} }//------------------------------------------------------------------------------ public String toString() { return getTimeString(); }//------------------------------------------------------------------------------ /** * 取得String類型的時間差 * 形式為小時,分鐘,秒和毫秒 ;*/ public String getTimeString() { int hours, minutes, seconds,milliseconds; long time = getTime(); hours = (int) (time / AN_HOUR); time = time - (hours *AN_HOUR); minutes = (int) (time / A_MINUTE); time = time - (minutes *A_MINUTE); seconds = (int) (time / 1000); time = time - (seconds * 1000); milliseconds = (int) time; return hours + "h:" +minutes + "m:" + seconds + "s:" + milliseconds; }//------------------------------------------------------------------------------}//end of class//------------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -