Android秒數轉時間
阿新 • • 發佈:2019-02-16
最近在做一個播放器,讀取介面上面要顯示歌曲時長
MediaPlayer可以讀取出總毫秒數,需要手動轉換。
下面是轉換程式碼。
/*
* 秒數轉時間
* */
private String secondsToTime(int seconds) {
StringBuilder time = new StringBuilder();
int min;int hour;
//將毫秒轉換成秒
seconds = seconds / 1000;
if (seconds > 60) {
min = seconds / 60 ;
seconds = seconds % 60;
}
if (minute > 60) {
hour = min / 60;
min = min % 60;
}
//拼接
if (hour< 10)
time.append("0");
time.append(hour);
time.append(":");
if (min < 10)
time.append ("0");
time.append(min);
time.append(":");
if (seconds < 10)
time.append("0");
time.append(seconds);
return time.toString();
}