Java Nano Second Convert nanoSecToString(final long nS)

Here you can find the source of nanoSecToString(final long nS)

Description

Returns the given time in nanoseconds formatted as Sec.mSec uSec nSec

License

Apache License

Parameter

Parameter Description
nS the given nanoseconds

Return

the given time in nanoseconds formatted as Sec.mSec uSec nSec

Declaration

public static String nanoSecToString(final long nS) 

Method Source Code

//package com.java2s;
/*//from  w  w  w .  j a v a 2  s. c  o m
 * Copyright 2015-16, Yahoo! Inc.
 * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
 */

public class Main {
    /**
     * Returns the given time in nanoseconds formatted as Sec.mSec uSec nSec
     * @param nS the given nanoseconds
     * @return the given time in nanoseconds formatted as Sec.mSec uSec nSec
     */
    public static String nanoSecToString(final long nS) {
        final long rem_nS = (long) (nS % 1000.0);
        final long rem_uS = (long) ((nS / 1000.0) % 1000.0);
        final long rem_mS = (long) ((nS / 1000000.0) % 1000.0);
        final long sec = (long) (nS / 1000000000.0);
        final String nSstr = zeroPad(Long.toString(rem_nS), 3);
        final String uSstr = zeroPad(Long.toString(rem_uS), 3);
        final String mSstr = zeroPad(Long.toString(rem_mS), 3);
        return String.format("%d.%3s %3s %3s", sec, mSstr, uSstr, nSstr);
    }

    /**
     * Prepend the given string with zeros. If the given string is equal or greater than the given
     * field length, it will be returned without modification.
     * @param s the given string
     * @param fieldLength desired total field length including the given string
     * @return the given string prepended with zeros.
     */
    public static final String zeroPad(final String s, final int fieldLength) {
        return characterPad(s, fieldLength, '0', false);
    }

    /**
     * Prepend or postpend the given string with the given character to fill the given field length.
     * If the given string is equal or greater than the given field length, it will be returned
     * without modification.
     * @param s the given string
     * @param fieldLength the desired field length
     * @param padChar the desired pad character
     * @param postpend if true append the pacCharacters to the end of the string.
     * @return prepended or postpended given string with the given character to fill the given field
     * length.
     */
    public static final String characterPad(final String s, final int fieldLength, final char padChar,
            final boolean postpend) {
        final char[] chArr = s.toCharArray();
        final int sLen = chArr.length;
        if (sLen < fieldLength) {
            final char[] out = new char[fieldLength];
            final int blanks = fieldLength - sLen;

            if (postpend) {
                for (int i = 0; i < sLen; i++) {
                    out[i] = chArr[i];
                }
                for (int i = sLen; i < fieldLength; i++) {
                    out[i] = padChar;
                }
            } else { //prepend
                for (int i = 0; i < blanks; i++) {
                    out[i] = padChar;
                }
                for (int i = blanks; i < fieldLength; i++) {
                    out[i] = chArr[i - blanks];
                }
            }

            return String.valueOf(out);
        }
        return s;
    }
}

Related

  1. nanoSeconds(long millis)
  2. nanoSecondsToSeconds(final long nanoSeconds)
  3. nanosecondsToSeconds(Long nanoseconds)
  4. nanosecondsToString(long ns)
  5. nanoSecondToSecond(double nano, int decimal)
  6. nanosForFractionalValue(int value, int width)
  7. nanoTime()
  8. nanoTime()
  9. nanoTimeLessThan(long leftNanos, long rightNanos)