Java Long Number Readable Format humanReadableTime(final long duration)

Here you can find the source of humanReadableTime(final long duration)

Description

human Readable Time

License

Open Source License

Declaration

public static String humanReadableTime(final long duration) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Cloud Bees, Inc./*from  w  w  w .j  a  v  a2s .c  o m*/
 * All rights reserved. 
 * This program is made available under the terms of the 
 * Eclipse Public License v1.0 which accompanies this distribution, 
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Cloud Bees, Inc. - initial API and implementation 
 *******************************************************************************/

public class Main {
    public static String humanReadableTime(final long duration) {
        String unit = "";
        long mins = duration / (60L * 1000);
        long hr = mins / 60;
        long yrs = hr / 24 / 365;

        if (mins <= 0) {
            long secs = duration / 1000;
            unit = secs + " sec";
            if (secs > 1) {
                unit = unit + "s";
            }
        } else if (mins < 60) {
            unit = mins + " min";
            if (mins > 1) {
                unit = unit + "s";
            }
        } else if (mins < 60 * 24) {
            //long newmins = mins - (hr * 60);
            unit = hr + " hr" + (hr > 1 ? "s" : "");/* + " " + newmins + " min" + (newmins > 1 ? "s" : "");*/
        } else if (yrs < 1) {
            long days = hr / 24L;
            unit = days + " day" + (days > 1 ? "s" : "")/* + ", " + hr + " hr" + (hr > 1 ? "s" : "")*/;
        } else {
            unit = yrs + " year" + (yrs > 1 ? "s" : "");
        }

        return unit;
    }
}

Related

  1. humanReadableDuration(long ms)
  2. humanReadableInt(long number)
  3. humanReadableNumber(long total)
  4. humanReadableSize(long byteCount)
  5. humanReadableSize(long bytes)
  6. humanTimeDiff(long timeDiff)
  7. readable(long bytes)
  8. stringForBytes(long bytes)
  9. stringify(long byteNumber)