一、直接上代码
public class TimeUtils {
/**
* 转换毫秒格式 HH:mm:ss
* 1s = 1000ms
* 1m = 60s
* 1h = 60m
* 1d = 24h
* @param ms 时间戳 毫秒
*/
public static String formatDuring(long ms){
// 时
long hours = ((ms + 28800000) % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
// 分
long minutes = (ms % (1000 * 60 * 60)) / (1000 * 60);
// 秒
long seconds = (ms % (1000 * 60)) / 1000;
String h = hours + "";
if (hours < 10) {
h = "0" + h;
}
String m = minutes + "";
if (minutes < 10) {
m = "0" + m;
}
String s = seconds + "";
if (seconds < 10) {
s = "0" + s;
}
return h + ":" + m + ":" + s;
}
}
二、注意事项
1、由于时区的原因,转换出的“时”会有所不同,我们所在的时区为东八区,需要在转换“时”的时间戳加上28800000。