首页 > 代码库 > Android 时钟(由秒转变为时分秒)
Android 时钟(由秒转变为时分秒)
int second = 0;
Handler handler = new Handler(); handler.post(runnable);Runnable runnable = new Runnable() { @Override public void run() { second++; patrol_time_txt.setText(Util.getTime(second)); handler.postDelayed(runnable, 1000); } };
//根据秒数转化为时分秒 00:00:00
public static String getTime(int second) { if (second < 10) { return "00:0" + second; } if (second < 60) { return "00:" + second; } if (second < 3600) { int minute = second / 60; second = second - minute * 60; if (minute < 10) { if (second < 10) { return "0" + minute + ":0" + second; } return "0" + minute + ":" + second; } if (second < 10) { return minute + ":0" + second; } return minute + ":" + second; } int hour = second / 3600; int minute = (second - hour * 3600) / 60; second = second - hour * 3600 - minute * 60; if (hour < 10) { if (minute < 10) { if (second < 10) { return "0" + hour + ":0" + minute + ":0" + second; } return "0" + hour + ":0" + minute + ":" + second; } if (second < 10) { return "0" + hour + minute + ":0" + second; } return "0" + hour + minute + ":" + second; } if (minute < 10) { if (second < 10) { return hour + ":0" + minute + ":0" + second; } return hour + ":0" + minute + ":" + second; } if (second < 10) { return hour + minute + ":0" + second; } return hour + minute + ":" + second; }
Android 时钟(由秒转变为时分秒)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。