본문 바로가기
Java

[Java] 초단위 -> 문자열 시분초로 변환(secToTime) 구현

by jn4624 2021. 10. 15.
반응형
public static String secToTime(int time) {
    int hours = (int) Math.floor(times / 3600);
    int minutes = (int) Math.floor((times - (hours * 3600)) / 60);
    int seconds = (int) Math.floor((times - (hours * 3600) - (minutes * 60)));
    
    String hoursStr = String.valueOf(hours);
    String minutesStr = String.valueOf(minutes);
    String secondsStr = String.valueOf(seconds);
        
    if (hours   < 10) {hoursStr = "0" + hours;}
    if (minutes < 10) {minutesStr = "0" + minutes;}
    if (seconds < 10) {secondsStr = "0" + seconds;}
        
    if(time <= 0) {
        return "00:00:00";
    } else {        
        return hoursStr+':'+minutesStr+':'+secondsStr;
    }
}
반응형