Java Time Elapsed elapsedNanos(long nanoseconds)

Here you can find the source of elapsedNanos(long nanoseconds)

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
nanoseconds the elapsed time in nanoseconds

Return

a string representation of the specified elapsed time

Declaration

public static String elapsedNanos(long nanoseconds) 

Method Source Code

//package com.java2s;
/*//w  ww  .j  a v a  2s .com
 * 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 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. elapsed(long duration, long required)
  2. elapsed(long from, long required)
  3. elapsed(long from, long to)
  4. elapsedMicros(String startTime, String endTime)
  5. elapsedNanos(long startNanoTime)
  6. elapsedTime(long endTime, long beginTime)
  7. elapsedTime(long milli)
  8. elapsedTime(long ms)