Java Time Format formatTimeUnits(double nanos)

Here you can find the source of formatTimeUnits(double nanos)

Description

Format time in reasonable units.

License

Apache License

Parameter

Parameter Description
nanos Time duration in nanoseconds.

Return

Reasonable representation of the given name with unit appended.

Declaration

public static String formatTimeUnits(double nanos) 

Method Source Code

//package com.java2s;
/*/*ww w .  j a  v  a 2s.c  o m*/
 * Copyright 2015 Charles University in Prague
 * Copyright 2015 Vojtech Horky
 * 
 * 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.
 */

public class Main {
    /** Format time in reasonable units.
     *  
     * @param nanos Time duration in nanoseconds.
     * @return Reasonable representation of the given name with unit appended.
     */
    public static String formatTimeUnits(double nanos) {
        if (nanos < 1000) {
            return String.format("%.0fns", nanos);
        }
        double micros = nanos / 1000;
        if (micros < 1000) {
            return String.format("%.0fus", micros);
        }
        double millis = micros / 1000;
        if (millis < 1000) {
            return String.format("%.0fms", millis);
        }
        double sec = millis / 1000;
        millis = millis - sec * 1000;
        return String.format("%.0fs %.fms", sec, millis);
    }
}

Related

  1. formatTimeString(long millisecond)
  2. formatTimeString(String time)
  3. formatTimeTakenNs(long startTimeNs, String message)
  4. formatTimeToString(long milisec)
  5. formatTimeToString(long seconds)
  6. formatTimeZoneID(String id)
  7. formatToTimeStr(String str)
  8. formatUptime(long uptime)
  9. formatVideoRecordingTime(long t)