Java Long Number to Time getTime(long nanos)

Here you can find the source of getTime(long nanos)

Description

Transforms a number of nano-seconds into a string representing from milliseconds to minutes.

License

Apache License

Parameter

Parameter Description
nanos a parameter

Declaration

public static String getTime(long nanos) 

Method Source Code

//package com.java2s;
/**//from   w ww .  j  a  v a 2  s  . co m
 *  Copyright 2015 SPeCS.
 * 
 *  Licensed 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.
 *  under the License.
 */

import java.text.NumberFormat;

public class Main {
    /**
     * Transforms a number of nano-seconds into a string representing from
     * milliseconds to minutes.
     * 
     * @param nanos
     * @return
     */
    public static String getTime(long nanos) {
        NumberFormat doubleFormat = NumberFormat.getNumberInstance();
        doubleFormat.setMaximumFractionDigits(2);

        // Check millis
        double millis = (double) nanos / 1000000;

        if (millis < 1000) {
            return doubleFormat.format(millis) + "ms";
        }

        double secs = millis / 1000;
        if (secs < 60) {
            return doubleFormat.format(secs) + "s";
        }

        double mins = secs / 60.0;

        return doubleFormat.format(mins) + " minutes";
    }
}

Related

  1. getSimpleDate(long time)
  2. getSMillon(long time)
  3. getSpeedString(long castTime, long bytes)
  4. getStrDateForLong(long time)
  5. getTextDate(long timeInMs)
  6. getTime(Long time)
  7. getTime(long time, boolean getTimeOnly, boolean fileSystemSafe)
  8. getTimeFromLong(long diff)
  9. getTimeFromLong(long diff)