Java Milliseconds elapsedMillis(long milliseconds)

Here you can find the source of elapsedMillis(long milliseconds)

Description

Returns a string representation of the specified elapsed time in the format "H hours M minutes S seconds".

License

Open Source License

Parameter

Parameter Description
milliseconds the elapsed time in milliseconds

Return

a string representation of the specified elapsed time.

Declaration

public static String elapsedMillis(long milliseconds) 

Method Source Code

//package com.java2s;
/*//from w ww  .j av  a  2 s .  c  om
 * Copyright (C) 2014 Brian L. Browning
 *
 * This file is part of Beagle
 *
 * Beagle is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Beagle is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
    * Returns a string representation of the specified elapsed time
    * in the format "H hours M minutes S seconds".
    *
    * @param milliseconds the elapsed time in milliseconds
    *
    * @return a string representation of the specified elapsed time.
    */
    public static String elapsedMillis(long milliseconds) {
        return elapsedNanos(1000000 * milliseconds);
    }

    /**
    * Returns a string representation of the specified elapsed time
    * in the format "H hours M minutes S seconds".
    *
    * @param nanoseconds the elapsed time in nanoseconds
    *
    * @return a string representation of the specified elapsed time.
    */
    public static String elapsedNanos(long nanoseconds) {
        long seconds = Math.round(nanoseconds / 1000000000.0);
        StringBuilder sb = new StringBuilder(80);
        if (seconds >= 3600) {
            long hours = seconds / 3600;
            sb.append(hours);
            sb.append(hours == 1 ? " hour " : " hours ");
            seconds %= 3600;

        }
        if (seconds >= 60) {
            long minutes = seconds / 60;
            sb.append(minutes);
            sb.append(minutes == 1 ? " minute " : " minutes ");
            seconds %= 60;
        }
        sb.append(seconds);
        sb.append(seconds == 1 ? " second" : " seconds");
        return sb.toString();
    }
}

Related

  1. delayUntilNextSecond(int millis)
  2. diffInMillis(long instantOne, long instantTwo)
  3. diffMillisec(String lhs, String rhs)
  4. diffTimeInMillis(long start, long end)
  5. durationInMillis(long startNano)
  6. elapsedMillis(long startTime)
  7. elapsedMillis(long t0nanos, long t1nanos)
  8. ensureSleepMillis(long millis)
  9. expensiveMethodTakingMillis(final int millis)