完整的Android程序,主要功能是實現(xiàn)秒表計時
需求:
默認為"00:00:00",點擊開始按鈕時清零后開始計時,出現(xiàn)如10:28:34。點擊停止的時候停止計時。
問題:
使用Calendar DateFormat的方法,不設(shè)置時區(qū)獲取到的小時是本地時區(qū)的(東八區(qū)的就是8),設(shè)置成GMT標準時區(qū)獲取到的時間是12小時(12:00:00),設(shè)置24小時制無效。
在開始時間加減各種小時都無效,而且計時只能到12小時就自動跳上去了,始終無法出現(xiàn)默認狀態(tài)00:00:00開始計時的效果。
嘗試各種時間設(shè)置方法無效后只能自己寫一個根據(jù)秒數(shù)轉(zhuǎn)換時間格式字符串的方法了,經(jīng)過測試是沒問題的,兩位數(shù)只能顯示99小時為最大,如需要更大小時數(shù)需要改改方法。
另外小時數(shù)也不能無限大,超過long數(shù)據(jù)類型長度會變成負數(shù)的,會出現(xiàn)異常的。
顯示效果:
測試類:
1 public class TestTime {
2 public static void main(String[] args) { 3 TestTime tt = new TestTime(); 4 tt.showTimeCount(99*3600000+75*1000); 5 } 6 7 //時間計數(shù)器,最多只能到99小時,如需要更大小時數(shù)需要改改方法 8 public String showTimeCount(long time) { 9 System.out.println("time="+time); 10 if(time >= 360000000){ 11 return "00:00:00"; 12 } 13 String timeCount = ""; 14 long hourc = time/3600000; 15 String hour = "0" + hourc; 16 System.out.println("hour="+hour); 17 hour = hour.substring(hour.length()-2, hour.length()); 18 System.out.println("hour2="+hour); 19 20 long minuec = (time-hourc*3600000)/(60000); 21 String minue = "0" + minuec; 22 System.out.println("minue="+minue); 23 minue = minue.substring(minue.length()-2, minue.length()); 24 System.out.println("minue2="+minue); 25 26 long secc = (time-hourc*3600000-minuec*60000)/1000; 27 String sec = "0" + secc; 28 System.out.println("sec="+sec); 29 sec = sec.substring(sec.length()-2, sec.length()); 30 System.out.println("sec2="+sec); 31 timeCount = hour + ":" + minue + ":" + sec; 32 System.out.println("timeCount="+timeCount); 33 return timeCount; 34 } 35 36 }
實際例子:
1 //時間計數(shù)器,最多只能到99小時,如需要更大小時數(shù)需要改改方法
2 public String showTimeCount(long time) { 3 if(time >= 360000000){ 4 return "00:00:00"; 5 } 6 String timeCount = ""; 7 long hourc = time/3600000; 8 String hour = "0" + hourc; 9 hour = hour.substring(hour.length()-2, hour.length()); 10 11 long minuec = (time-hourc*3600000)/(60000); 12 String minue = "0" + minuec; 13 minue = minue.substring(minue.length()-2, minue.length()); 14 15 long secc = (time-hourc*3600000-minuec*60000)/1000; 16 String sec = "0" + secc; 17 sec = sec.substring(sec.length()-2, sec.length()); 18 timeCount = hour + ":" + minue + ":" + sec; 19 return timeCount; 20 } 21 22 private Handler stepTimeHandler; 23 private Runnable mTicker; 24 long startTime = 0; 25 26 //開始按鈕 27 class startBtnListener implements OnClickListener { 28 @Override 29 public void onClick(View v) { 30 Button b = (Button)v; 31 String buttonText = b.getText().toString(); 32 if("Start".equalsIgnoreCase(buttonText)){ 33 b.setText("Stop"); 34 // 清零 開始計時 35 stepTimeTV.setText("00:00:00"); 36 stepTimeHandler = new Handler(); 37 startTime = System.currentTimeMillis(); 38 mTicker = new Runnable() { 39 public void run() { 40 String content = showTimeCount(System.currentTimeMillis() - startTime); 41 stepTimeTV.setText(content); 42 43 long now = SystemClock.uptimeMillis(); 44 long next = now + (1000 - now % 1000); 45 stepTimeHandler.postAtTime(mTicker, next); 46 } 47 }; 48 //啟動計時線程,定時更新 49 mTicker.run(); 50 }else{ 51 b.setText("Start"); 52 //停止計時 Remove any pending posts of Runnable r that are in the message queue. 53 stepTimeHandler.removeCallbacks(mTicker); 54 } 55 } 56 }
用時間格式化的方式測試代碼:
1 //開始按鈕 通過Calendar時間設(shè)置的方式,無法正常顯示小時為0
2 class startBtnListener implements OnClickListener { 3 @Override 4 public void onClick(View v) { 5 Button b = (Button)v; 6 String buttonText = b.getText().toString(); 7 if("Start".equalsIgnoreCase(buttonText)){ 8 b.setText("Stop"); 9 // 清零 開始計時 10 stepTimeTV.setText("00:00:00"); 11 if (mCalendar == null) { 12 mCalendar = Calendar.getInstance(); 13 TimeZone tz = TimeZone.getTimeZone("GMT");//GMT+8 14 mCalendar.setTimeZone(tz); 15 mCalendar.get(Calendar.HOUR_OF_DAY);//24小時制 16 } 17 stepTimeHandler = new Handler(); 18 //System.uptimeMillis() //記錄從機器啟動后到現(xiàn)在的毫秒數(shù),當系統(tǒng)進入深度睡眠時,此計時器將會停止 19 //System.currentTimeMillis() //返回自1970年1月1日到現(xiàn)在的毫秒數(shù),通常用來設(shè)置日期和時間 20 //System.elapsedRealtime() //返回從機器啟動后到現(xiàn)在的毫秒數(shù),包括系統(tǒng)深度睡眠的時間,api里沒有這個方法 21 //直接取得的是當?shù)貢r區(qū)時間,當?shù)貢r間跟時區(qū)有關(guān),設(shè)置GMT后始終多12小時 22 startTime = System.currentTimeMillis();//12*3600000 - 36*3600000減掉或者加上12小時都不行 3 mTicker = new Runnable() { 24 public void run() { 25 //這個減出來的日期是1970年的 時間格式不能出現(xiàn)00:00:00 12:00:00 26 long showTime = System.currentTimeMillis() - startTime; 27 Log.i(TAG,showTime+""); 28 mCalendar.setTimeInMillis(showTime + 13*3600000 + 1000); 29 String content = (String) DateFormat.format(mFormat, mCalendar); 30 stepTimeTV.setText(content); 31 32 long now = SystemClock.uptimeMillis(); 33 long next = now + (1000 - now % 1000); 34 stepTimeHandler.postAtTime(mTicker, next); 35 } 36 }; 37 //啟動計時線程,定時更新 38 mTicker.run(); 39 }else{ 40 b.setText("Start"); 41 //停止計時 Remove any pending posts of Runnable r that are in the message queue. 42 stepTimeHandler.removeCallbacks(mTicker); 43 } 44 } 45 } 46 47 private Handler stepTimeHandler; 48 Calendar mCalendar; 49 String mFormat = "yyyy-MM-dd hh:mm:ss";//yyyy-MM-dd 50 long startTime = 0; 51 private Runnable mTicker;