android计时器,时间计算器的实现方法

时间:2022-02-27 14:02:03

需求:默认为"00:00:00",点击开始按钮时清零后开始计时,出现如10:28:34。点击停止的时候停止计时。
问题:使用Calendar DateFormat的方法,不设置时区获取到的小时是本地时区的(东八区的就是8),设置成GMT标准时区获取到的时间是12小时(12:00:00),设置24小时制无效。
在开始时间加减各种小时都无效,而且计时只能到12小时就自动跳上去了,始终无法出现默认状态00:00:00开始计时的效果。
尝试各种时间设置方法无效后只能自己写一个根据秒数转换时间格式字符串的方法了,经过测试是没问题的,两位数只能显示99小时为最大,如需要更大小时数需要改改方法。
另外小时数也不能无限大,超过long数据类型长度会变成负数的,会出现异常的。

显示效果:

 

android计时器,时间计算器的实现方法

 

android计时器,时间计算器的实现方法

测试类:

 

复制代码 代码如下:

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

     //时间计数器,最多只能到99小时,如需要更大小时数需要改改方法
     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;
     }

 }

 

实际例子:

 

复制代码 代码如下:

//时间计数器,最多只能到99小时,如需要更大小时数需要改改方法
     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);
             }
         }
     }

 

用时间格式化的方式测试代码:

 

复制代码 代码如下:

//开始按钮 通过Calendar时间设置的方式,无法正常显示小时为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()         //记录从机器启动后到现在的毫秒数,当系统进入深度睡眠时,此计时器将会停止
                 //System.currentTimeMillis()   //返回自1970年1月1日到现在的毫秒数,通常用来设置日期和时间
                 //System.elapsedRealtime()   //返回从机器启动后到现在的毫秒数,包括系统深度睡眠的时间,api里没有这个方法
                 //直接取得的是当地时区时间,当地时间跟时区有关,设置GMT后始终多12小时
                 startTime = System.currentTimeMillis();//12*3600000  - 36*3600000减掉或者加上12小时都不行
                 mTicker = new Runnable() {
                     public void run() {
                         //这个减出来的日期是1970年的  时间格式不能出现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;