Java Millisecond Convert convertMillisToHumanReadableForm(long milliseconds)

Here you can find the source of convertMillisToHumanReadableForm(long milliseconds)

Description

convert Millis To Human Readable Form

License

Open Source License

Declaration

public static String convertMillisToHumanReadableForm(long milliseconds) 

Method Source Code

//package com.java2s;

public class Main {
    public static final long MILLISECONDS_IN_A_DAY = 86400000;
    public static final long MILLISECONDS_IN_AN_HOUR = 3600000;
    public static final long MILLISECONDS_IN_A_MINUTE = 60000;

    public static String convertMillisToHumanReadableForm(long milliseconds) {

        if (milliseconds < 0) {
            return "CANNOT_CONVERT";
        }/*from  ww w  .  j a  va2s. c o m*/

        if (milliseconds > 86400000) {
            return getNumberOfDays(milliseconds);
        } else {
            return getNumberOfHours(milliseconds);
        }

    }

    public static String getNumberOfDays(long milliseconds) {

        long days = milliseconds / MILLISECONDS_IN_A_DAY;

        long remainder = milliseconds % MILLISECONDS_IN_A_DAY;

        return days + " days, " + getNumberOfHours(remainder);
    }

    public static String getNumberOfHours(long milliseconds) {
        long hours = milliseconds / MILLISECONDS_IN_AN_HOUR;

        long remainder = milliseconds % MILLISECONDS_IN_AN_HOUR;

        return hours + " hours, " + getNumberOfMinutes(remainder);
    }

    public static String getNumberOfMinutes(long milliseconds) {
        long minutes = milliseconds / MILLISECONDS_IN_A_MINUTE;

        long remainder = milliseconds % MILLISECONDS_IN_A_MINUTE;

        return minutes + " minutes, " + (remainder / 1000) + " seconds";
    }
}

Related

  1. convertMillisToString(long diff)
  2. convertMillisToTicks(long milliseconds)
  3. convertMillisToTime(String val)
  4. convertNanosecondToMillisecondString(final long nanos)