Java Long Number to Time getElapsedTimeSpecificationDescription(long sizeInBytes)

Here you can find the source of getElapsedTimeSpecificationDescription(long sizeInBytes)

Description

Returns a short string description of the size, in bytes, specified.

License

Open Source License

Parameter

Parameter Description
sizeInBytes the number of bytes whose size if to be described.

Return

a short-hand string description of the size in bytes.

Declaration

public static String getElapsedTimeSpecificationDescription(long sizeInBytes) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    public static long SECONDS_IN_MS = 1000;
    public static long MINUTES_IN_MS = 60 * SECONDS_IN_MS;
    public static long HOURS_IN_MS = 60 * MINUTES_IN_MS;
    public static long DAYS_IN_MS = 24 * HOURS_IN_MS;
    public static long WEEKS_IN_MS = 7 * DAYS_IN_MS;
    public static long YEARS_IN_MS = 52 * WEEKS_IN_MS;

    /**//from w w  w . j a v a2s .  c  om
     * Returns a short string description of the elapsed time, in bytes, specified.
     *
     * @param timeInMS the number of bytes whose size if to be described.
     * @param formatSpec the <code>DecimalFormal</code> format specification.
     * @return a short-hand string description of the size in bytes.
     * @see java.text.DateFormat
     */
    public static String getElapsedTimeSpecificationDescription(long timeInMS, String formatSpec) {
        StringBuffer sBuf = new StringBuffer();
        NumberFormat df = new DecimalFormat(formatSpec);
        long years = timeInMS / YEARS_IN_MS;
        if (years > 0) {
            sBuf.append(years).append(years > 1 ? " years" : " year");
        }

        long weeks = timeInMS % YEARS_IN_MS / WEEKS_IN_MS;
        if (weeks > 0) {
            sBuf.append(years > 0 ? ", " : " ").append(weeks).append(weeks > 1 ? " weeks" : " week");
        }

        long days = (timeInMS - (years * YEARS_IN_MS) - (weeks * WEEKS_IN_MS)) / DAYS_IN_MS;
        if (days > 0) {
            sBuf.append(years > 0 || weeks > 0 ? ", " : " ").append(days).append(days > 1 ? " days" : " day");
        }

        long hours = (timeInMS - (years * YEARS_IN_MS) - (weeks * WEEKS_IN_MS) - (days * DAYS_IN_MS)) / HOURS_IN_MS;
        if (hours > 0) {
            sBuf.append(years > 0 || weeks > 0 || days > 0 ? ", " : " ").append(hours)
                    .append(hours > 1 ? " hours" : " hour");
        }

        long minutes = (timeInMS - (years * YEARS_IN_MS) - (weeks * WEEKS_IN_MS) - (days * DAYS_IN_MS)
                - (hours * HOURS_IN_MS)) / MINUTES_IN_MS;
        if (minutes > 0) {
            sBuf.append(years > 0 || weeks > 0 || days > 0 || hours > 0 ? ", " : " ").append(minutes)
                    .append(minutes > 1 ? " minutes" : " minute");
        }

        long secs = (timeInMS - (years * YEARS_IN_MS) - (weeks * WEEKS_IN_MS) - (days * DAYS_IN_MS)
                - (hours * HOURS_IN_MS) - (minutes * MINUTES_IN_MS)) / SECONDS_IN_MS;
        if (secs > 0) {
            sBuf.append(years > 0 || weeks > 0 || days > 0 || hours > 0 || minutes > 0 ? ", " : " ").append(secs)
                    .append(secs > 1 ? " secs" : " sec");
        }

        long ms = timeInMS - (years * YEARS_IN_MS) - (weeks * WEEKS_IN_MS) - (days * DAYS_IN_MS)
                - (hours * HOURS_IN_MS) - (minutes * MINUTES_IN_MS) - (secs * SECONDS_IN_MS);
        if (ms > 0) {
            sBuf.append(years > 0 || weeks > 0 || days > 0 || hours > 0 || minutes > 0 || secs > 0 ? ", " : " ")
                    .append(ms).append(" ms");
        }

        return sBuf.toString();
    }

    /**
     * Returns a short string description of the size, in bytes, specified.
     *
     * @param sizeInBytes the number of bytes whose size if to be described.
     * @return a short-hand string description of the size in bytes.
     */
    public static String getElapsedTimeSpecificationDescription(long sizeInBytes) {
        return getElapsedTimeSpecificationDescription(sizeInBytes, "#.##");
    }
}

Related

  1. getDelay(long delay, long interval)
  2. getDT(long mills)
  3. getDuration(long time)
  4. getElapsedTime(long start, long end)
  5. getElapsedTime(long startTime, long finishTime)
  6. getExpires(long maxAge)
  7. getHMSFromMills(long millSec)
  8. getOldCookieDate(long time)
  9. getReadableDate(long longTime)