get Duration in English - Java java.util

Java examples for java.util:Date Format

Description

get Duration in English

Demo Code

// Licensed under the Apache License, Version 2.0 (the "License");
//package com.java2s;

public class Main {
    public static String getDuration(long millis) {
        long seconds = millis / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;
        long days = hours / 24;
        StringBuilder sb = new StringBuilder();
        if (days >= 1) {
            sb.append(days).append(" day");
            if (days >= 1)
                sb.append('s');
            sb.append(", ");
        }/*from   ww  w  .  ja  v a2s .c  o m*/
        if (hours >= 1) {
            sb.append(hours % 24).append(" hour");
            if ((hours % 24) > 1)
                sb.append('s');
            sb.append(", ");
        }
        if (minutes >= 1) {
            sb.append(minutes % 60).append(" minute");
            if ((minutes % 60) > 1)
                sb.append('s');
            sb.append(", ");
        }
        sb.append(seconds % 60).append(" second");
        if ((seconds % 60) > 1)
            sb.append('s');
        return sb.toString();
    }
}

Related Tutorials