欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-12 19:18:58
需求:默认为"00:00:00",点击开始按钮时清零后开始计时,出现如10:28:34。点击停止的时候停止计时。问题:使用calendar dateformat的方法,不设...

需求:默认为"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;

上一篇:

下一篇: