Java Formatter Usage msToHumanReadableDelta(long start)

Here you can find the source of msToHumanReadableDelta(long start)

Description

Write the given timestamp in a 'nice' format (e.g.h 3h4m instead of 188m)

License

Open Source License

Parameter

Parameter Description
start The start time of the period of interest.

Return

A human readable representation of the start time less the time at which the method was called.

Declaration

public static String msToHumanReadableDelta(long start) 

Method Source Code

//package com.java2s;
//License from project: MIT License 

import java.util.Formatter;

public class Main {
    /**//from w ww. ja  v  a  2  s. c  om
     * Write the given timestamp in a 'nice' format (e.g.h 3h4m instead of 188m) 
     * @param start
     *            The start time of the period of interest.
     * @return A human readable representation of the start time less the time at which the method was called.
     */
    public static String msToHumanReadableDelta(long start) {
        long stop = System.currentTimeMillis();
        return msToHumanReadable(stop - start);
    }

    /**
     * @param ms
     *            The number of milliseconds to parse nicely.
     * @return A string containing an easily human-readable representation of some number of milliseconds. Best results
     *         for a reasonable number of hours (aka < 24).
     */
    public static String msToHumanReadable(long ms) {

        if (ms < 1000) {
            return ms + " ms";
        } else if (ms >= 1000 && ms < 60000) {
            return new Formatter().format("%.2f", ms / 1000f) + " sec";
        } else if (ms >= 60000 && ms < 60000 * 60) {
            long min = ms / 1000 / 60;
            long sec = (ms - (min * 60000)) / 1000;

            return min + " min " + sec + " sec";
        } else if (ms >= 60000 * 60) {
            long hours = ms / 1000 / 60 / 60;
            long min = (ms - hours * 60000 * 60) / 1000 / 60;

            if (hours > 1)
                return hours + " hours " + min + " min";
            else
                return hours + " hour " + min + " min";
        } else {
            // This should never happen.
            return "";
        }

    }
}

Related

  1. getRoundedString(double value, int powerOf10)
  2. incrementAlpha(String suppliedPrefix, String lastNumber, int numberLength)
  3. listToString(List list, String separator)
  4. long2Mac(final long macAddress)
  5. millisToHumanTime(long milliseconds)
  6. numberWithLeadingZeroes(int n, int totalChars)
  7. showVxlanHeaderOutput()
  8. substitution(Formatter formatter, int flags, int width, int precision, StringBuilder obj)
  9. toHexString(final byte[] data)