Java Milliseconds getDurationFromMillis(final long millis)

Here you can find the source of getDurationFromMillis(final long millis)

Description

Given a number of milliseconds, calculates a duration string.

License

Open Source License

Parameter

Parameter Description
millis How long the duration should represent.

Return

The duration string.

Declaration

public static String getDurationFromMillis(final long millis) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from   w w w .  j  a  v  a  2 s. c o  m
     * Given a number of milliseconds, calculates a duration string.
     *
     * @param millis How long the duration should represent.
     * @return The duration string.
     */
    public static String getDurationFromMillis(final long millis) {
        final long seconds = millis / 1000;
        final long minutes = seconds / 60;
        if (minutes == 0) {
            return seconds + "s ago";
        }
        final long hours = minutes / 60;
        if (hours == 0) {
            return minutes + "m ago";
        }
        final long days = hours / 24;
        if (days == 0) {
            return hours + "h ago";
        }
        return days + "d ago";
    }
}

Related

  1. expensiveMethodTakingMillis(final int millis)
  2. filetimeToMillis(long filetime)
  3. formatDateMillis(long millis)
  4. FormatDateTime(Calendar p_date, String p_seperator, boolean p_showMilliseconds)
  5. fractionOfDayToMilliseconds(float fFractionOfDay)
  6. getDurationSecFromMillisDouble(long millis)
  7. getElapsedTimeInMilliseconds(Date startDate, Date endDate)
  8. getExpirationInMilliSeconds(long currentTime, int expirationTime)
  9. getExpirationMillisAbs(int recordExpirationTimestamp)