Time Unit Conversion in Java

Ever needed a utility which converts from one unit of time to another?

Well with Java 5 and higher, you won’t have to create your own conversion routine any longer.

Say for example you wanted to specify a value in seconds but the method you need to call expects a value in milliseconds (which is a common thing in Java).

Then this would be how you can convert the value with the java.util.concurrent.TimeUnit utility.

long oneMinute = TimeUnit.SECONDS.toMillis(60);

Java 5’s TimeUnit supports conversion to and from nano, micro, milli and seconds while Java 6’s TimeUnit also supports minutes, hours and days.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: