Elye Elye - 1 year ago 91
Android Question

How to define a duration elegantly in Java 7

To define a duration, sometimes we just write below

long thirtyMinutes = 30 * 60 * 1000; // In Miliseconds

As of Java 8 with the java.time framework, we could make it more elegant using

long thirtyMinutes = Duration.ofMinutes(30).toMillis();

But as I'm still in Java 7 (Android), I can't use java.time.Duration. So is there anyway I could have it define as elegant as Java 8, instead of just ruing the multiplication equation above?

Answer Source

Without a backported library- java.util.concurrent.TimeUnit

//convert 4 hours to milliseconds    
long ms = TimeUnit.Hours.toMillis(4);  

Similar for minutes, days, etc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download