snippetjavaMinor
Convert length of time to appropriate unit
Viewed 0 times
convertlengthtimeappropriateunit
Problem
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):
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?
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 > 60)
{
decayTime /= 60;
readable = decayTime + " hours";
if(decayTime > 24)
{
decayTime /= 24;
readable = decayTime + " days";
if(decayTime > 365)
{
decayTime /= 365;
readable = decayTime + " years";
if(decayTime > 1000000)
{
decayTime /= 1000000;
readable = decayTime + "mn years";
if(decayTime > 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?
Solution
Here's one approach, using
Just create one of these objects, and call the
Note that it's not quite right for negative arguments to
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 timeUnits = new TreeMap<>();
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 unitBelow = timeUnits.floorEntry(milliseconds);
return milliseconds / unitBelow.getKey() + unitBelow.getValue();
}
}Code Snippets
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
public class TimeFormatter {
private NavigableMap<Long,String> timeUnits = new TreeMap<>();
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<Long,String> unitBelow = timeUnits.floorEntry(milliseconds);
return milliseconds / unitBelow.getKey() + unitBelow.getValue();
}
}Context
StackExchange Code Review Q#139970, answer score: 3
Revisions (0)
No revisions yet.