TimeUtils.java
1.19 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
package com.people.basemusic.utils;
/**
* Time:2024/1/5
* Author:ypf
* Description:时间工具类
*/
public class TimeUtils {
// 毫秒数转化为"00:00"时间格式
public static String calculateTime(long _time) {
int time = (int) _time;
int minute;
int second;
if (time >= 60) {
minute = time / 60;
second = time % 60;
return (minute < 10 ? "0" + minute : "" + minute) + (second < 10 ? ":0" + second : ":" + second);
} else {
second = time;
if (second < 10) {
return "00:0" + second;
}
return "00:" + second;
}
}
/**
* HH:mm:ss格式,小于1小时展示mm:ss格式
* @param totalSeconds 已/ 1000
* @return
*/
public static String convertMillisecondsToTime(long totalSeconds) {
long hours = totalSeconds / 3600;
long minutes = (totalSeconds % 3600) / 60;
long seconds = totalSeconds % 60;
if (hours > 0) {
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
} else {
return String.format("%02d:%02d", minutes, seconds);
}
}
}