Saturday, April 22, 2023

How to convert Milliseconds to Minutes and Seconds in Java? TimeUnit Example Tutorial

I used to convert milliseconds to seconds in Java by simply dividing it by 1000, and then into minutes by further dividing it by 60, and then hours by even further dividing by 60 and so on. This works but its not the most elegant way to convert millisecond into hours, minutes, seconds or other time unit. JDK comes with a nice utility class called TimeUnit, which as its name suggest, allow you to convert one time unit into other. So if you have milliseconds e.g. execution time of a method and you want to convert into minutes and seconds to improve readability, you can use TimeUnit.toSeconds() and TimeUnit.toMinutes() methods. 

It even come with sleep() method which can be used in place of Thread.sleep() method, as its more readable because it tells you exactly how many seconds or minutes thread is going to sleep. 

In this tutorial, you will learn how to convert milliseconds to seconds, minutes and other time units in java. btw, the calculation is not accurate, I mean given it return long the fractional part of seconds or minutes will be lost. 


10 Examples of TimeUnit in Java

Let's see a couple of examples of converting between one time unit to other like millisecond to second, minutes to hours, and seconds to minutes in Java using java.util.concurrent.TimeUnit class in Java 

milliseconds to seconds

long seconds = TimeUnit.MILLISECONDS.toSeconds(50000); // 5 seconds

milliseconds to minutes

long minutes = TimeUnit.MILLISECONDS.toMinutes(300000); // 5 minutes

milliseconds to hours

long hours   = TimeUnit.MILLISECONDS.toHours(300000); // 0 hours

milliseconds to days

long days    = TimeUnit.MILLISECONDS.toDays(300000);  // 0 days


microseconds to milliseconds

long milliseconds = TimeUnit.MICROSECONDS.toMillis(duration); // 600,000 milliseconds

long seconds      = TimeUnit.MICROSECONDS.toSeconds(duration);    // 600 seconds


microsends to minutes
long minutes      = TimeUnit.MICROSECONDS.toMinutes(duration);    // 10 minutes

microsends to hours
int hours        = TimeUnit.MICROSECONDS.toHours(duration);      // 0 hours



Java Program to convert milliseconds to seconds and minutes using TimeUnit

Here is complete Java Program to cover millisecond values to second, minute and hour in Java using TimeUnit class. Bottom line is you should use TimeUnit  to convert Milliseconds to minutes and seconds

package dto;

import java.util.concurrent.TimeUnit;

/**
 * Java Program to demonstrate how to use TimeUnit class to 
 * convert millisecond and microsecond to seconds, minutes
 * hours and days in Java.
 * 
 * @author WINDOWS 11
 *
 */
public class TimeUnitDemo {

   public static void main(String args[]){
       
       long milliseconds = 500*1000;
       
       long seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds);    // 500 seconds
       long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds);    // 8 minutes
       long hours   = TimeUnit.MILLISECONDS.toHours(milliseconds);      // 0 hours
       long days    = TimeUnit.MILLISECONDS.toDays(milliseconds);       // 0 days
       
       
       System.out.println("milliseconds : " + milliseconds);
       System.out.println("converted to seconds : " + seconds);
       System.out.println("converted to minutes: " + minutes);
       System.out.println("converted to hours: " + hours);
       System.out.println("converted to days: " + days);
       
       long duration = 600*1000*1000;
       milliseconds = TimeUnit.MICROSECONDS.toMillis(duration);     // 600,000 milliseconds
       seconds      = TimeUnit.MICROSECONDS.toSeconds(duration);    // 600 seconds
       minutes      = TimeUnit.MICROSECONDS.toMinutes(duration);    // 10 minutes
       hours        = TimeUnit.MICROSECONDS.toHours(duration);      // 0 hours
       days         = TimeUnit.MICROSECONDS.toDays(duration);       // 0 days
       
       System.out.println("micro seconds : " + duration);
       System.out.println("converted to milliseconds : " + milliseconds);
       System.out.println("converted to seconds : " + seconds);
       System.out.println("converted to minutes: " + minutes);
       System.out.println("converted to hours: " + hours);
       System.out.println("converted to days: " + days);
   }

}

Output :
milliseconds : 500000
converted to seconds : 500
converted to minutes: 8
converted to hours: 0
converted to days: 0
micro seconds : 600000000
converted to milliseconds : 600000
converted to seconds : 600
converted to minutes: 10
converted to hours: 0
converted to days: 0

Important things to learn about TimeUnit

Here are a couple of importnat points about TimeUnit class in Java which I think every Java developer should know and remember:

1) TimeUnit utility is from java.util.concurrent package
2) TimeUnit was introduced in JDK 1.5, so its not available to lower JRE versions.
3) TimeUnit supports many units e.g. nano seconds, micro seconds, milliseconds, seconds, minutes, hours and days.
4) You can use TimeUnit class to convert from any supported unit to other using static methods.
5) TimeUnit is an enum and not a class or interface.

How to convert Milliseconds to Minutes and Seconds in Java? TimeUnit Example Tutorial


That's all about how to convert millisecond to seconds, minutes and hours in Java. This is the most elegant way to convert one time unit into another. You don't need to maintain conversion factor. It's also less error prone as it reduce typing error. You can also use TimeUnit for timed wait, timed join and alternative of sleep method.

No comments:

Post a Comment