Sunday, May 5, 2024
HomeJavaThe way to convert Milliseconds to Minutes and Seconds in Java? TimeUnit...

The way to convert Milliseconds to Minutes and Seconds in Java? TimeUnit Instance Tutorial


I used to transform milliseconds to seconds in Java by merely dividing it by 1000, after which into minutes by additional dividing it by 60, after which hours by even additional dividing by 60 and so forth. This works however its not probably the most elegant option to convert millisecond into hours, minutes, seconds or different time unit. JDK comes with a pleasant utility class known as TimeUnit, which as its title counsel, will let you convert one time unit into different. So in case you have milliseconds e.g. execution time of a technique and also you wish to convert into minutes and seconds to enhance readability, you should use TimeUnit.toSeconds() and TimeUnit.toMinutes() strategies. 
It even include sleep() methodology which can be utilized instead of Thread.sleep() methodology, as its extra readable as a result of it tells you precisely what number of seconds or minutes thread goes to sleep. 

On this tutorial, you’ll study learn how to convert milliseconds to seconds, minutes and different time items in java. btw, the calculation is just not correct, I imply given it return lengthy the fractional a part of seconds or minutes can be misplaced. 

10 examples of TimeUnit in Java

Let’s have a look at a few examples of changing between one time unit to different like millisecond to second, minutes to hours, and seconds to minutes in Java utilizing java.util.concurrent.TimeUnit class in Java 

milliseconds to seconds

lengthy seconds = TimeUnit.MILLISECONDS.toSeconds(50000);

milliseconds to minutes

lengthy minutes = TimeUnit.MILLISECONDS.toMinutes(300000); 

milliseconds to hours

lengthy hours   = TimeUnit.MILLISECONDS.toHours(300000); 

milliseconds to days

lengthy days    = TimeUnit.MILLISECONDS.toDays(300000);  

microseconds to milliseconds

lengthy milliseconds = TimeUnit.MICROSECONDS.toMillis(length);
lengthy seconds      = TimeUnit.MICROSECONDS.toSeconds(length);   

microsends to minutes

lengthy minutes      = TimeUnit.MICROSECONDS.toMinutes(length);    // 10 minutes

microsends to hours

int hours        = TimeUnit.MICROSECONDS.toHours(length);      

Java Program to transform milliseconds to seconds and minutes utilizing TimeUnit

Right here is full Java Program to cowl millisecond values to second, minute and hour in Java utilizing TimeUnit class. Backside line is it’s best to use TimeUnit  to transform Milliseconds to minutes and seconds

bundle dto;

import java.util.concurrent.TimeUnit;


public class TimeUnitDemo {

   public static void foremost(String args[]){
       
       lengthy milliseconds = 500*1000;
       
       lengthy seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds);    
       lengthy minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds);    
       lengthy hours   = TimeUnit.MILLISECONDS.toHours(milliseconds);      
       lengthy days    = TimeUnit.MILLISECONDS.toDays(milliseconds);       
       
       
       System.out.println("milliseconds : " + milliseconds);
       System.out.println("transformed to seconds : " + seconds);
       System.out.println("transformed to minutes: " + minutes);
       System.out.println("transformed to hours: " + hours);
       System.out.println("transformed to days: " + days);
       
       lengthy length = 600*1000*1000;
       milliseconds = TimeUnit.MICROSECONDS.toMillis(length);     
       seconds      = TimeUnit.MICROSECONDS.toSeconds(length);    
       minutes      = TimeUnit.MICROSECONDS.toMinutes(length);    
       hours        = TimeUnit.MICROSECONDS.toHours(length);      
       days         = TimeUnit.MICROSECONDS.toDays(length);       
       
       System.out.println("micro seconds : " + length);
       System.out.println("transformed to milliseconds : " + milliseconds);
       System.out.println("transformed to seconds : " + seconds);
       System.out.println("transformed to minutes: " + minutes);
       System.out.println("transformed to hours: " + hours);
       System.out.println("transformed to days: " + days);
   }

}

Output :

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

Vital issues to study TimeUnit

Listed here are a few importnat factors about TimeUnit class in Java which I believe each Java developer ought to know and keep in mind:

1) TimeUnit utility is from java.util.concurrent bundle

2) TimeUnit was launched in JDK 1.5, so its not accessible to decrease JRE variations.

3) TimeUnit helps many items e.g. nano seconds, micro seconds, milliseconds, seconds, minutes, hours and days.

4) You need to use TimeUnit class to transform from any supported unit to different utilizing static strategies.

5) TimeUnit is an enum and never a category or interface.

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

That is all about learn how to convert millisecond to seconds, minutes and hours in Java. That is probably the most elegant option to convert one time unit into one other. You needn’t preserve conversion issue. It is also much less error inclined because it scale back typing error. You may also use TimeUnit for timed wait, timed be part of and various of sleep methodology.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments