TimeFormater.java
1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.wd.player.playerutil;
/*
* Copyright (C) 2010-2018 Alibaba Group Holding Limited.
*/
/**
* 时间格式化工具类
*
* @author lvjinhui
*/
public class TimeFormater {
/**
* 格式化毫秒数为 xx:xx:xx这样的时间格式。
*
* @param ms 毫秒数
* @return 格式化后的字符串
*/
public static String formatMs(long ms) {
int seconds = (int) (ms / 1000);
int finalSec = seconds % 60;
int finalMin = seconds / 60 % 60;
int finalHour = seconds / 3600;
StringBuilder msBuilder = new StringBuilder("");
if (finalHour > 9) {
msBuilder.append(finalHour).append(":");
} else if (finalHour > 0) {
msBuilder.append("0").append(finalHour).append(":");
}
if (finalMin > 9) {
msBuilder.append(finalMin).append(":");
} else if (finalMin > 0) {
msBuilder.append("0").append(finalMin).append(":");
} else {
msBuilder.append("00").append(":");
}
if (finalSec > 9) {
msBuilder.append(finalSec);
} else if (finalSec > 0) {
msBuilder.append("0").append(finalSec);
} else {
msBuilder.append("00");
}
return msBuilder.toString();
}
}