Convert length of time to appropriate unit I have a time, in seconds, which has the possibility to be very large. I wish to convert said time into the "appropriate" rounded, readable format.

I already have code which achieves this, however it's not very efficient (and contains a whole bunch of magic numbers):

String readable = decayTime + " minutes";
if(decayTime &gt; 60)
{
    decayTime /= 60;
    readable = decayTime + " hours";

    if(decayTime &gt; 24)
    {
        decayTime /= 24;
        readable = decayTime + " days";

        if(decayTime &gt; 365)
        {
            decayTime /= 365;
            readable = decayTime + " years";

            if(decayTime &gt; 1000000)
            {
                decayTime /= 1000000;
                readable = decayTime + "mn years";

                if(decayTime &gt; 1000)
                {
                    decayTime /= 1000;
                    readable = decayTime + "bn years"; 
                }
            }
        }
    }


Apart from switching out the magic numbers, I can't personally think how to make it better.

What would be a better approach to this, or is there something in-built which could help?
Here's one approach, using TreeMap.  It looks up your number of milliseconds in a pre-populated map, finds the appropriate entry and does the division.

Just create one of these objects, and call the format method as many times as you need to.

Note that it's not quite right for negative arguments to format - but you might want to put your own logic in around that, for example, to throw some kind of exception.

import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;

public class TimeFormatter {

    private NavigableMap&lt;Long,String&gt; timeUnits = new TreeMap&lt;&gt;();

    public TimeFormatter() {
        timeUnits.put(Long.MIN_VALUE, " is not a valid argument");
        timeUnits.put(1L, " milliseconds");
        timeUnits.put(1000L, " seconds");
        timeUnits.put(60 * 1000L, " minutes");
        timeUnits.put(60 * 60 * 1000L, " hours");
        timeUnits.put(24 * 60 * 60 * 1000L, " days");
        timeUnits.put(365 * 24 * 60 * 60 * 1000L, " years");
        timeUnits.put(1000000L * 365 * 24 * 60 * 60 * 1000L, " million years");
        timeUnits.put(1000000000L * 365 * 24 * 60 * 60 * 1000L, " billion years");
    }

    public String format(long milliseconds) {
        Map.Entry&lt;Long,String&gt; unitBelow = timeUnits.floorEntry(milliseconds);
        return milliseconds / unitBelow.getKey() + unitBelow.getValue();
    }
}