Java Millisecond Format formatMillisecondsToConventional(long time)

Here you can find the source of formatMillisecondsToConventional(long time)

Description

Convert milliseconds value to a human-readable duration

License

Open Source License

Parameter

Parameter Description
time a parameter

Return

Human readable string version of passed time

Declaration

public static String formatMillisecondsToConventional(long time) 

Method Source Code

//package com.java2s;
/*/*  w  ww .j a  v  a  2s .c  o m*/
 * ArchiveUtils
 *
 * $Header: /cvsroot/archive-crawler/ArchiveOpenCrawler/src/java/org/archive/util/ArchiveUtils.java,v 1.38 2007/01/23 00:29:48 gojomo Exp $
 *
 * Created on Jul 7, 2003
 *
 * Copyright (C) 2003 Internet Archive.
 *
 * This file is part of the Heritrix web crawler (crawler.archive.org).
 *
 * Heritrix is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * any later version.
 *
 * Heritrix is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with Heritrix; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

public class Main {
    /** milliseconds in an hour */
    private static final int HOUR_IN_MS = 60 * 60 * 1000;
    /** milliseconds in a day */
    private static final int DAY_IN_MS = 24 * HOUR_IN_MS;

    /**
     * Convert milliseconds value to a human-readable duration
     * @param time
     * @return Human readable string version of passed <code>time</code>
     */
    public static String formatMillisecondsToConventional(long time) {
        return formatMillisecondsToConventional(time, true);
    }

    /**
     * Convert milliseconds value to a human-readable duration
     * @param time
     * @param toMs whether to print to the ms
     * @return Human readable string version of passed <code>time</code>
     */
    public static String formatMillisecondsToConventional(long time, boolean toMs) {
        StringBuffer sb = new StringBuffer();
        if (time < 0) {
            sb.append("-");
        }
        long absTime = Math.abs(time);
        if (!toMs && absTime < 1000) {
            return "0s";
        }
        if (absTime > DAY_IN_MS) {
            // days
            sb.append(absTime / DAY_IN_MS + "d");
            absTime = absTime % DAY_IN_MS;
        }
        if (absTime > HOUR_IN_MS) {
            //got hours.
            sb.append(absTime / HOUR_IN_MS + "h");
            absTime = absTime % HOUR_IN_MS;
        }
        if (absTime > 60000) {
            sb.append(absTime / 60000 + "m");
            absTime = absTime % 60000;
        }
        if (absTime > 1000) {
            sb.append(absTime / 1000 + "s");
            absTime = absTime % 1000;
        }
        if (toMs) {
            sb.append(absTime + "ms");
        }
        return sb.toString();
    }
}

Related

  1. formatMilliseconds (final long ms)
  2. formatMilliseconds(Double milliseconds)
  3. formatMilliseconds(long milliseconds)
  4. formatMilliSecondsHumanReadable(long x)
  5. formatMillisecondsToConventional(long duration, int unitCount)
  6. formattedMillis(long millis)
  7. formatTimeInMillis(long millis)
  8. formatTimeMillis(long millis)
  9. formatTimeMillis(long timeMillis)