Java TimeUnit Usage formatElapsedTime(final long seconds)

Here you can find the source of formatElapsedTime(final long seconds)

Description

Formats elapsed time in seconds to form days:hours:minutes:seconds.

License

Open Source License

Parameter

Parameter Description
seconds Elapsed time in seconds.

Return

Formated time as string d:hh:mm:ss

Declaration

public static String formatElapsedTime(final long seconds) 

Method Source Code


//package com.java2s;
/*//from   ww w . j av  a 2s . com
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

import com.google.common.base.Preconditions;
import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Formats elapsed time in seconds to form days:hours:minutes:seconds.
     *
     * @param seconds Elapsed time in seconds.
     * @return Formated time as string d:hh:mm:ss
     */
    public static String formatElapsedTime(final long seconds) {
        Preconditions.checkArgument(seconds >= 0);
        return String.format("%1d:%02d:%02d:%02d", TimeUnit.SECONDS.toDays(seconds),
                TimeUnit.SECONDS.toHours(seconds) - TimeUnit.DAYS.toHours(TimeUnit.SECONDS.toDays(seconds)),
                TimeUnit.SECONDS.toMinutes(seconds) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(seconds)),
                seconds - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(seconds)));
    }
}

Related

  1. formatDuration(long millis)
  2. formatDuration(long time)
  3. formatDurationSeconds(long seconds)
  4. formatDurationTill(long start)
  5. formatElapsedSecs(long secs)
  6. formatElapsedTime(long elapsedTimeMs)
  7. formatElapsedTime(long millis)
  8. formatInterval(final long l)
  9. formatMillis(final long millis)