Convert seconds to Hour:Minute:Second

前端 未结 27 2145
说谎
说谎 2020-11-22 07:56

I need to convert seconds to \"Hour:Minute:Second\".

For example: \"685\" converted to \"00:11:25\"

How can I achieve this?

27条回答
  •  情歌与酒
    2020-11-22 08:36

    In java you can use this way.

       private String getHmaa(long seconds) {
        String string;
        int hours = (int) seconds / 3600;
        int remainder = (int) seconds - hours * 3600;
        int mins = remainder / 60;
        //remainder = remainder - mins * 60;
        //int secs = remainder;
    
        if (hours < 12 && hours > 0) {
            if (mins < 10) {
                string = String.valueOf((hours < 10 ? "0" + hours : hours) + ":" + (mins > 0 ? "0" + mins : "0") + " AM");
            } else {
                string = String.valueOf((hours < 10 ? "0" + hours : hours) + ":" + (mins > 0 ? mins : "0") + " AM");
            }
        } else if (hours >= 12) {
            if (mins < 10) {
                string = String.valueOf(((hours - 12) < 10 ? "0" + (hours - 12) : ((hours - 12) == 12 ? "0" : (hours - 12))) + ":" + (mins > 0 ? "0" + mins : "0") + ((hours - 12) == 12 ? " AM" : " PM"));
            } else {
                string = String.valueOf(((hours - 12) < 10 ? "0" + (hours - 12) : ((hours - 12) == 12 ? "0" : (hours - 12))) + ":" + (mins > 0 ? mins : "0") + ((hours - 12) == 12 ? " AM" : " PM"));
            }
        } else {
            if (mins < 10) {
                string = String.valueOf("0" + ":" + (mins > 0 ? "0" + mins : "0") + " AM");
            } else {
                string = String.valueOf("0" + ":" + (mins > 0 ? mins : "0") + " AM");
            }
        }
        return string;
    }
    

提交回复
热议问题