The TimeUnit Class

Categories: Java

When dealing with time-related types in Java, the “joda” library is excellent. However for simple conversions of time units to/from milliseconds, somebody recently pointed out to me a very useful class that is already in the Java standard libraries: java.util.concurrent.TimeUnit.

long millisPerWeek = TimeUnit.DAY.toMillis(7);

long diffMillis = endTimestamp - startTimestamp;
long diffDays = TimeUnit.MILLISECONDS.toDays(diffMillis);


Seems like TimeUnit isn’t widely known yet; others have also been surprised to stumble across it.