Helper function to convert Time in nanoseconds to a nice string - Java java.util

Java examples for java.util:Second

Description

Helper function to convert Time in nanoseconds to a nice string

Demo Code


//package com.java2s;

public class Main {
    /**/*from w  ww . j a  v a2 s. co m*/
     * Helper function to convert Time in nanoseconds to a nice string
     *
     * @param ns
     *            nanoseconds
     * @return String from nanoseconds
     */
    public static String timeToString(long ns) {
        long MICROSECOND = 1000;
        long MILISECOND = MICROSECOND * 1000;
        long SECOND = 1000 * MILISECOND;
        long MINUTE = 60 * SECOND;
        long HOUR = 60 * MINUTE;
        long DAY = 24 * HOUR;
        StringBuffer text = new StringBuffer("");
        if (ns > DAY) {
            text.append(ns / DAY).append(" days ");
            ns %= DAY;
        }
        if (ns > HOUR) {
            text.append(ns / HOUR).append(" hours ");
            ns %= HOUR;
        }
        if (ns > MINUTE) {
            text.append(ns / MINUTE).append(" minutes ");
            ns %= MINUTE;
        }
        if (ns > SECOND) {
            text.append(ns / SECOND).append(" seconds ");
            ns %= SECOND;
        }
        if (ns > MILISECOND) {
            text.append(ns / MILISECOND).append(" milliseconds ");
            ns %= MILISECOND;
        }
        if (ns > MICROSECOND) {
            text.append(ns / MICROSECOND).append(" microseconds ");
            ns %= MICROSECOND;
        }
        text.append(ns + " ns");
        return text.toString();
    }
}

Related Tutorials