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.
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.