Java Utililty Methods Nano Second Convert

List of utility methods to do Nano Second Convert

Description

The list of methods to do Nano Second Convert are organized into topic(s).

Method

longnanosecondsToSeconds(long nanoseconds)
nanoseconds To Seconds
return (long) (nanoseconds / NANOS_PER_SECOND);
StringnanosecondsToString(long ns)
Format a long representing nanoseconds into a string of the form "[seconds].[nanoseconds]" with the appropriate zero-padding.
ns = Math.abs(ns);
String time = Long.toString(ns);
int length = time.length();
if (time.length() > 9) {
    time = time.substring(0, length - 9) + "." + time.substring(length - 9); 
    return time;
for (int i = length; i < 9; i++) {
...
doublenanoSecondToSecond(double nano, int decimal)
Convert from nano second to second
double precision = Math.pow(10, decimal);
double second = nano / 1_000_000_000d;
return Math.round(second * precision) / precision;
StringnanoSecToString(final long nS)
Returns the given time in nanoseconds formatted as Sec.mSec uSec nSec
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);
...
intnanosForFractionalValue(int value, int width)
nanos For Fractional Value
switch (width) {
case 0:
    return 0;
case 1:
    return value * 10000000;
case 2:
    return value * 100000;
case 3:
...
longnanoTime()
Equivalent to System.nanoTime(), but based off the 1/1/1970 epoch like currentTimeMillis().
return System.nanoTime() + nanoTimeOffset;
longnanoTime()
nano Time
return System.nanoTime();
booleannanoTimeLessThan(long leftNanos, long rightNanos)
Compare two nanosecond times.
return ((leftNanos - rightNanos) < 0L);
doublenanoTimeToSeconds(long nanoTime)
nano Time To Seconds
return nanoTime / 1000000000.0;
floatnanoToMilli(double nanoseconds)
nano To Milli
return (float) nanoseconds / 1000 / 1000;