亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android計時器,時間計算器的實現(xiàn)方法

 更新時間:2013年03月12日 11:15:24   作者:  
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)異常的。

顯示效果:

 

 

測試類:

復(fù)制代碼 代碼如下:

public class TestTime {
     public static void main(String[] args) {
         TestTime tt = new TestTime();
         tt.showTimeCount(99*3600000+75*1000);
     }

     //時間計數(shù)器,最多只能到99小時,如需要更大小時數(shù)需要改改方法
     public String showTimeCount(long time) {
         System.out.println("time="+time);
         if(time >= 360000000){
             return "00:00:00";
         }
         String timeCount = "";
         long hourc = time/3600000;
         String hour = "0" + hourc;
         System.out.println("hour="+hour);
         hour = hour.substring(hour.length()-2, hour.length());
         System.out.println("hour2="+hour);

         long minuec = (time-hourc*3600000)/(60000);
         String minue = "0" + minuec;
         System.out.println("minue="+minue);
         minue = minue.substring(minue.length()-2, minue.length());
         System.out.println("minue2="+minue);

         long secc = (time-hourc*3600000-minuec*60000)/1000;
         String sec = "0" + secc;
         System.out.println("sec="+sec);
         sec = sec.substring(sec.length()-2, sec.length());
         System.out.println("sec2="+sec);
         timeCount = hour + ":" + minue + ":" + sec;
         System.out.println("timeCount="+timeCount);
         return timeCount;
     }

 }

實際例子:

復(fù)制代碼 代碼如下:

//時間計數(shù)器,最多只能到99小時,如需要更大小時數(shù)需要改改方法
     public String showTimeCount(long time) {
         if(time >= 360000000){
             return "00:00:00";
         }
         String timeCount = "";
         long hourc = time/3600000;
         String hour = "0" + hourc;
         hour = hour.substring(hour.length()-2, hour.length());

         long minuec = (time-hourc*3600000)/(60000);
         String minue = "0" + minuec;
         minue = minue.substring(minue.length()-2, minue.length());

         long secc = (time-hourc*3600000-minuec*60000)/1000;
         String sec = "0" + secc;
         sec = sec.substring(sec.length()-2, sec.length());
         timeCount = hour + ":" + minue + ":" + sec;
         return timeCount;
     }

     private Handler stepTimeHandler;
     private Runnable mTicker;
     long startTime = 0;

     //開始按鈕
     class startBtnListener implements OnClickListener {
         @Override
         public void onClick(View v) {
             Button b = (Button)v;
             String buttonText = b.getText().toString();
             if("Start".equalsIgnoreCase(buttonText)){
                 b.setText("Stop");
                 // 清零 開始計時
                 stepTimeTV.setText("00:00:00");
                 stepTimeHandler = new Handler();
                 startTime = System.currentTimeMillis();
                 mTicker = new Runnable() {
                     public void run() {
                         String content = showTimeCount(System.currentTimeMillis() - startTime);
                         stepTimeTV.setText(content);

                         long now = SystemClock.uptimeMillis();
                         long next = now + (1000 - now % 1000);
                         stepTimeHandler.postAtTime(mTicker, next);
                     }
                 };
                 //啟動計時線程,定時更新
                 mTicker.run();
             }else{
                 b.setText("Start");
                 //停止計時 Remove any pending posts of Runnable r that are in the message queue.
                 stepTimeHandler.removeCallbacks(mTicker);
             }
         }
     }

用時間格式化的方式測試代碼:

復(fù)制代碼 代碼如下:

//開始按鈕 通過Calendar時間設(shè)置的方式,無法正常顯示小時為0
     class startBtnListener implements OnClickListener {
         @Override
         public void onClick(View v) {
             Button b = (Button)v;
             String buttonText = b.getText().toString();
             if("Start".equalsIgnoreCase(buttonText)){
                 b.setText("Stop");
                 // 清零 開始計時
                 stepTimeTV.setText("00:00:00");
                 if (mCalendar == null) {
                     mCalendar = Calendar.getInstance();
                     TimeZone tz = TimeZone.getTimeZone("GMT");//GMT+8
                     mCalendar.setTimeZone(tz);
                     mCalendar.get(Calendar.HOUR_OF_DAY);//24小時制
                 }
                 stepTimeHandler = new Handler();
                 //System.uptimeMillis()         //記錄從機器啟動后到現(xiàn)在的毫秒數(shù),當(dāng)系統(tǒng)進入深度睡眠時,此計時器將會停止
                 //System.currentTimeMillis()   //返回自1970年1月1日到現(xiàn)在的毫秒數(shù),通常用來設(shè)置日期和時間
                 //System.elapsedRealtime()   //返回從機器啟動后到現(xiàn)在的毫秒數(shù),包括系統(tǒng)深度睡眠的時間,api里沒有這個方法
                 //直接取得的是當(dāng)?shù)貢r區(qū)時間,當(dāng)?shù)貢r間跟時區(qū)有關(guān),設(shè)置GMT后始終多12小時
                 startTime = System.currentTimeMillis();//12*3600000  - 36*3600000減掉或者加上12小時都不行
                 mTicker = new Runnable() {
                     public void run() {
                         //這個減出來的日期是1970年的  時間格式不能出現(xiàn)00:00:00 12:00:00
                         long showTime = System.currentTimeMillis() - startTime;
                         Log.i(TAG,showTime+"");
                         mCalendar.setTimeInMillis(showTime + 13*3600000 + 1000);
                         String content = (String) DateFormat.format(mFormat, mCalendar);
                         stepTimeTV.setText(content);

                         long now = SystemClock.uptimeMillis();
                         long next = now + (1000 - now % 1000);
                         stepTimeHandler.postAtTime(mTicker, next);
                     }
                 };
                 //啟動計時線程,定時更新
                 mTicker.run();
             }else{
                 b.setText("Start");
                 //停止計時 Remove any pending posts of Runnable r that are in the message queue.
                 stepTimeHandler.removeCallbacks(mTicker);
             }
         }
     }

     private Handler stepTimeHandler;
     Calendar mCalendar;
     String mFormat = "yyyy-MM-dd hh:mm:ss";//yyyy-MM-dd
     long startTime = 0;
     private Runnable mTicker;

相關(guān)文章

最新評論