Java TimeUnit Usage secondsToHMS(long seconds)

Here you can find the source of secondsToHMS(long seconds)

Description

Show time in seconds as hours, minutes and seconds (hh:mm:ss)

License

Apache License

Parameter

Parameter Description
seconds (elapsed) time in seconds

Return

human readable time string "hh:mm:ss"

Declaration

public static String secondsToHMS(long seconds) 

Method Source Code

//package com.java2s;
/**//  w  w w  .  j av  a 2  s . c o m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Show time in seconds as hours, minutes and seconds (hh:mm:ss)
     * 
     * @param seconds
     *          (elapsed) time in seconds
     * @return human readable time string "hh:mm:ss"
     */
    public static String secondsToHMS(long seconds) {
        long hours = TimeUnit.SECONDS.toHours(seconds);
        long minutes = TimeUnit.SECONDS.toMinutes(seconds)
                % TimeUnit.HOURS.toMinutes(1);
        seconds = TimeUnit.SECONDS.toSeconds(seconds)
                % TimeUnit.MINUTES.toSeconds(1);
        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }
}

Related

  1. second(long second)
  2. seconds(long timeInMillis)
  3. secondsFromNow(long seconds)
  4. secondsPassed(int startTime)
  5. secondsToETA(long secondsTotal)
  6. secondsToMillis(int seconds)
  7. secondsToYearsDaysHoursMinutesSeconds(double seconds)
  8. selectDurationUnitForDisplay(long durationInNanos)
  9. sleep()