- Java – Get time in milliseconds using Date, Calendar and ZonedDateTime
- 1. Getting current time in Milliseconds
- 2. Get Time in Milliseconds for the Given date and time
- References:
- Related Posts:
- Top Related Articles:
- About the Author
- Java Convert Number of Hours to Milliseconds
- How to convert number of hours to milliseconds in Java
- Java Program to Convert Hour to Millisecond and Millisecond to Hour
- Java Program to Convert Hour to Millisecond and Millisecond to Hour
- Method-1: Java Program to Convert Hour to Millisecond and Millisecond to Hour By Using Static Input Value
- Method-2: Java Program to Convert Hour to Millisecond and Millisecond to Hour By Using User Input Value
- Method-3: Java Program to Convert Hour to Millisecond and Millisecond to Hour By Using User Defined Method
- How to convert milliseconds into hours and days in Java?
- Method 1: Simple Conversion
- Method 2: Using Java’s built-in TimeUnit class
- Method 3: Using Joda-Time library
- Convert Milliseconds into Days, Hours, Minutes, Seconds in Java
Java – Get time in milliseconds using Date, Calendar and ZonedDateTime
In this tutorial we will see how to get current time or given time in milliseconds in Java.
There are three ways to get time in milliseconds in java.
1) Using public long getTime() method of Date class.
2) Using public long getTimeInMillis() method of Calendar class
3) Java 8 – ZonedDateTime.now().toInstant().toEpochMilli() returns current time in milliseconds.
1. Getting current time in Milliseconds
In this example, we are getting the current time and then using the methods getTime() and getTimeInMillis(), which returns the Date time and Calendar time in milliseconds respectively. There is a simple way of doing the same in Java 8 using ZonedDateTime, I have shown that as a third way of getting time in millis in the Program.
import java.util.Calendar; import java.util.Date; import java.time.ZonedDateTime; public class Example < public static void main(String[] args) < //Getting the current date Date date = new Date(); //This method returns the time in millis long timeMilli = date.getTime(); System.out.println("Time in milliseconds using Date class: " + timeMilli); //creating Calendar instance Calendar calendar = Calendar.getInstance(); //Returns current time in millis long timeMilli2 = calendar.getTimeInMillis(); System.out.println("Time in milliseconds using Calendar: " + timeMilli2); //Java 8 - toEpochMilli() method of ZonedDateTime System.out.println("Getting time in milliseconds in Java 8: " + ZonedDateTime.now().toInstant().toEpochMilli()); >>
Time in milliseconds using Date class: 1508484583259 Time in milliseconds using Calendar: 1508484583267 Getting time in milliseconds in Java 8: 1508484583331
2. Get Time in Milliseconds for the Given date and time
In this example, we have given a date and time and we are displaying the given time in Milliseconds.
import java.util.Calendar; import java.util.Date; import java.text.SimpleDateFormat; import java.text.ParseException; public class Example < public static void main(String[] args) < //Specifying the pattern of input date and time SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss"); String dateString = "22-03-2017 11:18:32"; try< //formatting the dateString to convert it into a Date Date date = sdf.parse(dateString); System.out.println("Given Time in milliseconds : "+date.getTime()); Calendar calendar = Calendar.getInstance(); //Setting the Calendar date and time to the given date and time calendar.setTime(date); System.out.println("Given Time in milliseconds : "+calendar.getTimeInMillis()); >catch(ParseException e) < e.printStackTrace(); >> >
Given Time in milliseconds : 1490161712000 Given Time in milliseconds : 1490161712000
References:
Related Posts:
Top Related Articles:
About the Author
I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.
Java Convert Number of Hours to Milliseconds
In this Java core tutorial, we learn how to convert a number of hours to milliseconds using the java.time.Duration class in Java programming language.
How to convert number of hours to milliseconds in Java
In Java, with a given number of hours we can use the Duration.toMillis() method to convert it to milliseconds value as the example Java code below.
import java.time.Duration; public class ConvertHoursToMillisecondsExample public static void main(String. args) long numberOfHours = 4; // Convert Number of Hours to Milliseconds long numberOfMilliseconds = Duration.ofHours(numberOfHours).toMillis(); System.out.println("Number of hours: " + numberOfHours); System.out.println("Number of milliseconds: " + numberOfMilliseconds); > >
Number of hours: 4 Number of milliseconds: 14400000
Java Program to Convert Hour to Millisecond and Millisecond to Hour
In this article we will see how to convert Hour to Millisecond and Millisecond to Hour by using Java programming language.
Java Program to Convert Hour to Millisecond and Millisecond to Hour
Before jumping into the program let’s know the relationship between Hour and Millisecond and how we can convert Hour to Millisecond and vice versa.
Hour and Millisecond are used as unit of time.
1 Hour = 3.6e+6 Millisecond 1 Millisecond = 2.7778e-7 Hour
Formula to convert Millisecond to Hour.
Formula to convert Hour to Millisecond.
Let’s see different ways to convert Hour to Millisecond and Millisecond to Hour.
Method-1: Java Program to Convert Hour to Millisecond and Millisecond to Hour By Using Static Input Value
- Declare Hour and Millisecond value.
- Then convert Hour to Millisecond and Millisecond to Hour by using the formula.
- Print result.
import java.util.*; public class Main < public static void main(String args[]) < //Scanner class object created Scanner sc=new Scanner(System.in); //value of hour declared double hour = 1; //value of millisecond declared double millisecond = 1; //converting hour to millisecond double ms = hour * 3.6e+6; //converting millisecond to hour double hr = millisecond / 3.6e+6; //printing result System.out.println("Value of "+hour+" hour in millisecond: "+ ms); System.out.println("Value of "+millisecond+" millisecond in hour: "+ hr); >>
Output: Value of 1.0 hour in millisecond: 3600000.0 Value of 1.0 millisecond in hour: 2.7777777777777776E-7
Method-2: Java Program to Convert Hour to Millisecond and Millisecond to Hour By Using User Input Value
- Take user input of Hour and Millisecond value.
- Then convert Hour to Millisecond and Millisecond to Hour by using the formula.
- Print result.
import java.util.*; public class Main < public static void main(String args[]) < //Scanner class object created Scanner sc=new Scanner(System.in); //Taking the value input of double variable hour System.out.println("Enter value of hour: "); double hour = sc.nextDouble(); //Taking the value input of double variable millisecond System.out.println("Enter value of millisecond: "); double millisecond = sc.nextDouble(); //converting hour to millisecond double ms = hour * 3.6e+6; //converting millisecond to hour double hr = millisecond / 3.6e+6; //printing result System.out.println("Value of "+hour+" hour in millisecond: "+ ms); System.out.println("Value of "+millisecond+" millisecond in hour: "+ hr); >>
Output: Enter value of hour: 3 Enter value of millisecond: 900000 Value of 3.0 hour in millisecond: 1.08E7 Value of 900000.0 millisecond in hour: 0.25
Method-3: Java Program to Convert Hour to Millisecond and Millisecond to Hour By Using User Defined Method
- Take user input of Hour and Millisecond value.
- Call a user defined method by passing Hour and Millisecond value as parameter.
- Inside method convert Hour to Millisecond and Millisecond to Hour by using the formula.
- Print result.
import java.util.*; public class Main < public static void main(String args[]) < //Scanner class object created Scanner sc=new Scanner(System.in); //Taking the value input of double variable hour System.out.println("Enter value of hour: "); double hour = sc.nextDouble(); //Taking the value input of double variable millisecond System.out.println("Enter value of millisecond: "); double millisecond = sc.nextDouble(); //calling user defined method convert() convert(hour, millisecond); >//convert() method to convert second to hour and vice versa public static void convert(double hour, double millisecond) < //converting hour to millisecond double ms = hour * 3.6e+6; //converting millisecond to hour double hr = millisecond / 3.6e+6; //printing result System.out.println("Value of "+hour+" hour in millisecond: "+ ms); System.out.println("Value of "+millisecond+" millisecond in hour: "+ hr); >>
Output: Enter value of hour: 5 Enter value of millisecond: 1000000 Value of 5.0 hour in millisecond: 1.8E7 Value of 1000000.0 millisecond in hour: 0.2777777777777778
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Related Java Programs:
How to convert milliseconds into hours and days in Java?
When working with time values in Java, it is common to use the millisecond representation of time, since it provides a convenient way to represent a specific point in time as a numerical value. However, sometimes it is necessary to display the time in a more human-readable format, such as hours or days. This can be achieved by converting the number of milliseconds into hours or days, taking into account the conversion factors for each unit of time.
Method 1: Simple Conversion
To convert milliseconds into hours and days using simple conversion in Java, you can follow these steps:
- First, get the milliseconds value that you want to convert.
- Then, convert the milliseconds into seconds by dividing it by 1000.
- Next, convert the seconds into minutes by dividing it by 60.
- After that, convert the minutes into hours by dividing it by 60.
- Finally, convert the hours into days by dividing it by 24.
Here is the Java code that implements the above steps:
long milliseconds = 86400000; // 1 day in milliseconds long seconds = milliseconds / 1000; long minutes = seconds / 60; long hours = minutes / 60; long days = hours / 24; System.out.println("Milliseconds: " + milliseconds); System.out.println("Seconds: " + seconds); System.out.println("Minutes: " + minutes); System.out.println("Hours: " + hours); System.out.println("Days: " + days);
Milliseconds: 86400000 Seconds: 86400 Minutes: 1440 Hours: 24 Days: 1
In the above code, we have initialized the milliseconds variable with the value of 1 day in milliseconds. Then, we have divided it by 1000 to get the seconds value, by 60 to get the minutes value, by 60 again to get the hours value, and by 24 to get the days value. Finally, we have printed all the converted values using the System.out.println() method.
You can use the above code as a reference to convert any milliseconds value into hours and days using simple conversion in Java.
Method 2: Using Java’s built-in TimeUnit class
To convert milliseconds into hours and days using Java’s built-in TimeUnit class, you can follow these steps:
- First, you need to create a long variable to store the milliseconds value that you want to convert. Let’s call it «milliseconds».
long milliseconds = 86400000; // 1 day in milliseconds
- Next, you can use the TimeUnit class to convert the milliseconds value into hours and days. The TimeUnit class provides several methods for this purpose, such as «convert», «toDays», and «toHours».
long hours = TimeUnit.MILLISECONDS.toHours(milliseconds); long days = TimeUnit.MILLISECONDS.toDays(milliseconds);
In this example, we are using the «toHours» and «toDays» methods to convert the «milliseconds» value into hours and days, respectively.
System.out.println("Milliseconds: " + milliseconds); System.out.println("Hours: " + hours); System.out.println("Days: " + days);
Here’s the complete code example:
import java.util.concurrent.TimeUnit; public class MillisecondsToHoursAndDays public static void main(String[] args) long milliseconds = 86400000; // 1 day in milliseconds long hours = TimeUnit.MILLISECONDS.toHours(milliseconds); long days = TimeUnit.MILLISECONDS.toDays(milliseconds); System.out.println("Milliseconds: " + milliseconds); System.out.println("Hours: " + hours); System.out.println("Days: " + days); > >
Milliseconds: 86400000 Hours: 24 Days: 1
In this example, we have successfully converted the given milliseconds value into hours and days using Java’s built-in TimeUnit class.
Method 3: Using Joda-Time library
To convert milliseconds into hours and days using Joda-Time library, you can follow the steps below:
- First, you need to add the Joda-Time library to your project. You can do this by adding the following dependency to your project’s pom.xml file:
dependency> groupId>joda-timegroupId> artifactId>joda-timeartifactId> version>2.10.10version> dependency>
- Next, you can create a Duration object using the milliseconds value. This can be done using the Duration.millis() method as shown below:
long milliseconds = 86400000; // 1 day in milliseconds Duration duration = Duration.millis(milliseconds);
- To convert the duration into hours, you can use the Duration.getStandardHours() method as shown below:
long hours = duration.getStandardHours();
- Similarly, to convert the duration into days, you can use the Duration.getStandardDays() method as shown below:
long days = duration.getStandardDays();
import org.joda.time.Duration; public class MillisecondsConverter public static void main(String[] args) long milliseconds = 86400000; // 1 day in milliseconds Duration duration = Duration.millis(milliseconds); long hours = duration.getStandardHours(); long days = duration.getStandardDays(); System.out.println("Milliseconds: " + milliseconds); System.out.println("Hours: " + hours); System.out.println("Days: " + days); > >
Milliseconds: 86400000 Hours: 24 Days: 1
That’s it! You have successfully converted milliseconds into hours and days using Joda-Time library.
Convert Milliseconds into Days, Hours, Minutes, Seconds in Java
Basically when you want to check the exact time taken to execute a program then you may need to calculate the time. So in this case you get the start time in milliseconds and end time in milliseconds and you want to check how much time that program took to execute. Therefore you would like to convert the milliseconds into minutes and seconds to make it more readable format because millisecond unit may not be so understandable quickly.
Lets see the below example, it will provide you better idea to convert the milliseconds into days or hours or minutes or seconds.
Formula to convert Milliseconds into Days, Hours, Minutes, Seconds in Java:
seconds = MilliSeconds / 1000;
minutes = seconds / 60;
hours = minutes / 60;
days = hours / 24;
import java.util.concurrent.TimeUnit; public class MillisToDayHrMinSec public static void main(String[] args) final long milliseconds = 5478965412358l; final long day = TimeUnit.MILLISECONDS.toDays(milliseconds); final long hours = TimeUnit.MILLISECONDS.toHours(milliseconds) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(milliseconds)); final long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliseconds)); final long seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds)); final long ms = TimeUnit.MILLISECONDS.toMillis(milliseconds) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(milliseconds)); System.out.println("milliseconds :-" + milliseconds); System.out.println(String.format("%d Days %d Hours %d Minutes %d Seconds %d Milliseconds", day, hours, minutes, seconds, ms)); > >
Output :
———————
milliseconds :-5478965412358
63413 Days 22 Hours 50 Minutes 12 Seconds 358 Milliseconds
Hope you like this simple example for time conversion, where we are converting milliseconds into days or hours or minutes or seconds. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.