TimeUtils.java 1.19 KB
package com.wd.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);
        }
    }

}