- How to convert milliseconds to Date in Java — Tutorial example
- Java program to convert millisecond to Date in Java
- 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
- Prerequisites
- Conversion Logic
- Testing the Program
- Output
- Java milliseconds in one day
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
How to convert milliseconds to Date in Java — Tutorial example
Do you want to convert milliseconds to Date in Java? Actually java.util.Date is internally specified in milliseconds from epoch. So any date is the number of milliseconds passed since January 1, 1970, 00:00:00 GMT and Date provides constructor which can be used to create Date from milliseconds. Knowing the fact that Date is internally maintained in milliseconds allows you to store date in form of milliseconds in Server or in your Class because that can be effectively expressed with a long value.
In fact, many experienced Java programmers store Date as long values while writing Immutable class which requires Date, one of the reasons for that is Date being mutable and long value of Date can be very handy?
By the ways this is next in Date related article, we have already discussed How to convert String to Date and How to get a current month, year, and day of the week from Date in Java. If you haven’t read them already, you may find them useful. In this Java tutorial, we will see an example of converting milliseconds into Dates in Java.
Java program to convert millisecond to Date in Java
There are many different ways to convert milliseconds into Date in Java. One can use java.util.Date(long Millis) constructor or java.util.Calendar.setTimeInMillis() method. In this article, we will see examples of both methods to create Date from A millisecond in Java.
By the way here we are using SimpleDateFormat to format Date in Java which is not thread-safe and should not be shared between multiple threads.
import java.text.DateFormat ;
import java.text.SimpleDateFormat ;
import java.util.Calendar ;
import java.util.Date ;
/**
* Java program to convert Millisecond to Date in Java. Java API provides utility
* method to get millisecond from Date and convert Millisecond to Date in Java.
* @author http://javarevisited.blogspot.com
*/
public class MillisToDate
public static void main ( String args [])
//Converting milliseconds to Date using java.util.Date
//current time in milliseconds
long currentDateTime = System. currentTimeMillis () ;
//creating Date from millisecond
Date currentDate = new Date ( currentDateTime ) ;
//printing value of Date
System. out . println ( «current Date: » + currentDate ) ;
DateFormat df = new SimpleDateFormat ( «dd:MM:yy:HH:mm:ss» ) ;
//formatted value of current Date
System. out . println ( «Milliseconds to Date: » + df. format ( currentDate )) ;
//Converting milliseconds to Date using Calendar
Calendar cal = Calendar. getInstance () ;
cal. setTimeInMillis ( currentDateTime ) ;
System. out . println ( «Milliseconds to Date using Calendar:»
//copying one Date’s value into another Date in Java
Date now = new Date () ;
Date copiedDate = new Date ( now. getTime ()) ;
System. out . println ( «original Date: » + df. format ( now )) ;
System. out . println ( «copied Date: » + df. format ( copiedDate )) ;
>
Output:
current Date: Wed Feb 29 01: 58 : 46 VET 2012
Milliseconds to Date: 29 :02: 12 :01: 58 : 46
Milliseconds to Date using Calendar: 29 :02: 12 :01: 58 : 46
original Date: 29 :02: 12 :01: 58 : 46
copied Date: 29 :02: 12 :01: 58 : 46
Another useful usage of keeping Date in a millisecond is, It’s easy to convert between java.util.Date and java.sql.Date. SQL doesn’t provide Date in form of java.util.Date you often need to convert SQL Date to util Date but keep the value of Date as long millisecond value allows you to create both java.sql.Date and java.util.Date . One more benefit of keeping the date in long millisecond value is, it’s easy to copy the value of one Date into another in Java.
That’s all on how to convert milliseconds to Date in Java. We have seen two approaches one is using Date class while the other is using Calendar class. I personally prefer java.util.Date way. let me know if you come across any other way of converting milliseconds into Date in Java.
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
This example shows you hot to convert milliseconds into days, hours, minutes, seconds in Java. Sometimes you need to convert the milliseconds into days or hours or minutes or seconds.
Typically 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.
Prerequisites
Conversion Logic
The following Java source code will give you the exact thing what you are looking for.
I am not using any modulo (%) or division (/) operators to convert milliseconds into days, hours, minutes, seconds but using Java’s built-in functions to make it happen.
In the below source code I first find out the no of days then hours, then minutes and so on. If it does not find any integer value for any unit of time then it gives 0.
package com.roytuts.time; import java.util.concurrent.TimeUnit; public class MillisToDayHrMinSec < public static void main(String[] args) < final long milliseconds = 5478965412358l; final long dy = TimeUnit.MILLISECONDS.toDays(milliseconds); final long hr = TimeUnit.MILLISECONDS.toHours(milliseconds) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(milliseconds)); final long min = TimeUnit.MILLISECONDS.toMinutes(milliseconds) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliseconds)); final long sec = 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(String.format("%d Days %d Hours %d Minutes %d Seconds %d Milliseconds", dy, hr, min, sec, ms)); >>
Testing the Program
When you run the above program you will see below output in the console:
Output
63413 Days 22 Hours 50 Minutes 12 Seconds 358 Milliseconds
That’s all. Hope you got idea how to convert milliseconds into days, hours, minutes, seconds. If you want you may also calculate other units like months, years etc.
Java milliseconds in one day
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter