完整的Android程序,主要功能是实现秒表计时
需求:
默认为"00:00:00",点击开始按钮时清零后开始计时,出现如10:28:34。点击停止的时候停止计时。
问题:
使用Calendar DateFormat的方法,不设置时区获取到的小时是本地时区的(东八区的就是8),设置成GMT标准时区获取到的时间是12小时(12:00:00),设置24小时制无效。
在开始时间加减各种小时都无效,而且计时只能到12小时就自动跳上去了,始终无法出现默认状态00:00:00开始计时的效果。
尝试各种时间设置方法无效后只能自己写一个根据秒数转换时间格式字符串的方法了,经过测试是没问题的,两位数只能显示99小时为最大,如需要更大小时数需要改改方法。
另外小时数也不能无限大,超过long数据类型长度会变成负数的,会出现异常的。
显示效果:
测试类:
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 //时间计数器,最多只能到99小时,如需要更大小时数需要改改方法 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 //时间计数器,最多只能到99小时,如需要更大小时数需要改改方法
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时间设置的方式,无法正常显示小时为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() //记录从机器启动后到现在的毫秒数,当系统进入深度睡眠时,此计时器将会停止 19 //System.currentTimeMillis() //返回自1970年1月1日到现在的毫秒数,通常用来设置日期和时间 20 //System.elapsedRealtime() //返回从机器启动后到现在的毫秒数,包括系统深度睡眠的时间,api里没有这个方法 21 //直接取得的是当地时区时间,当地时间跟时区有关,设置GMT后始终多12小时 22 startTime = System.currentTimeMillis();//12*3600000 - 36*3600000减掉或者加上12小时都不行 3 mTicker = new Runnable() { 24 public void run() { 25 //这个减出来的日期是1970年的 时间格式不能出现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;