Android 时钟(由秒转变为时分秒)

【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; }

 

    推荐阅读