首页 > 编程语言 >Java将毫秒(时间戳)转时分秒格式

Java将毫秒(时间戳)转时分秒格式

时间:2023-02-24 16:02:07浏览次数:40  
标签:60 Java String long hours 毫秒 minuteString minutes 时分秒

/**
     * 把时间戳转换为:时分秒
     * @param millisecond
     * @return
     */
    public static String getTimeString(final long millisecond) {
        if (millisecond < 1000) {
            return "0" + "秒";
        }
        long second = millisecond / 1000;
        long seconds = second % 60;
        long minutes = second / 60;
        long hours = 0;
        if (minutes >= 60) {
            hours = minutes / 60;
            minutes = minutes % 60;
        }
        String timeString = "";
        String secondString = "";
        String minuteString = "";
        String hourString = "";
        if (seconds < 10) {
            secondString = "0" + seconds + "秒";
        } else {
            secondString = seconds + "秒";
        }
        if (minutes < 10 && hours < 1) {
            minuteString = minutes + "分";
        } else if (minutes < 10){
            minuteString =  "0" + minutes + "分";
        } else {
            minuteString = minutes + "分";
        }
        if (hours < 10) {
            hourString = hours + "时";
        } else {
            hourString = hours + "" + "时";
        }
        if (hours != 0) {
            timeString = hourString + minuteString + secondString;
        } else {
            timeString = minuteString + secondString;
        }
        return timeString;
    }

 

标签:60,Java,String,long,hours,毫秒,minuteString,minutes,时分秒
From: https://www.cnblogs.com/coderxiaobai/p/17151791.html

相关文章