Unlikely Teacher

1. Share Everything* [Programming Gotchas, Technology News, Insights on Living and Everything in Between]

Time Unit Conversion in Java

March 28th, 2008 · No Comments · 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.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • StumbleUpon
  • Tumblr
  • Twitter

Tags: ·

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment