Java Long Number Readable Format humanReadableDuration(long ms)

Here you can find the source of humanReadableDuration(long ms)

Description

Convert milliseconds to a human readable duration

License

Open Source License

Declaration

public static String humanReadableDuration(long ms) 

Method Source Code

//package com.java2s;
/*/*ww w  .  ja v a2 s  .c  o m*/
   Copyright 2010 Inexas. All rights reserved.
    
   Licensed under the Inexas Software License V1.0. You may not use this file 
   except in compliance with the License. You may obtain a copy of the License
   at http://www.inexas.com/ISL-V1.0.
    
   Unless required by applicable law or agreed to in writing, software distributed 
   under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
   CONDITIONS OF ANY KIND, either express or implied. See the License for the 
   specific language governing permissions and limitations under the License.
*/

public class Main {
    /**
     * Convert milliseconds to a human readable duration
     */
    public static String humanReadableDuration(long ms) {
        final StringBuilder sb = new StringBuilder();
        if (ms < 1999) { // 0.1999 ms: 1 second
            sb.append("1 second");
        } else if (ms < 60 * 1000) { // 2-59 secs: x seconds
            sb.append(ms / 1000);
            sb.append(" seconds");
        } else if (ms < 60 * 60 * 1000) { // 1-59 mins: x minutes
            final long minutes = ms / (60 * 1000);
            sb.append(minutes);
            sb.append(minutes > 1 ? " minutes" : " minute");
        } else if (ms < 24 * 60 * 60 * 1000) { // 1-23 hours: x hours
            final long hours = ms / (60 * 60 * 1000);
            sb.append(hours);
            sb.append(hours > 1 ? " hours" : " hour");
        } else { // 1.* days = x days
            final long days = ms / (24 * 60 * 60 * 1000);
            sb.append(days);
            sb.append(days > 1 ? " days" : " day");
        }
        return sb.toString();
    }
}

Related

  1. humanReadableByteCount(long bytes, boolean si)
  2. humanReadableBytes(long bytes)
  3. humanReadableBytes(long bytes)
  4. humanReadableDateDiff(long start, long end)
  5. humanReadableDuration(long length)
  6. humanReadableInt(long number)
  7. humanReadableNumber(long total)
  8. humanReadableSize(long byteCount)
  9. humanReadableSize(long bytes)