Getting Time in Milliseconds using currentTimeMillis() Method in Java
Before java 8 the compiler used to take it from the java.util.* package. But later since the use of date and time became important in every software and android application, java developed date class after java 8.Date and time class via java.utill package has been duplicated now. Java uses date and time class after the release of version java 8 to store the date and time. Now java usually stores Date in a typical fashion such that the number of milliseconds passed since 1 Jan 1970 in a long data type. Since it stores the milliseconds the accuracy of the exact time increases.
Syntax: Getting milliseconds
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Note: This return the number of milliseconds passed since 1970 as 00:00 1 January 1970 is considered as epoch time. Similarly, we can find out years, months, hours and rest of data from milliseconds.(System.currentTimeMillis()) / 1000) Returns Seconds passed (System.currentTimeMillis()) / 1000 / 60) Returns Minutes passed (System.currentTimeMillis()) / 1000 / 60 / 60); Returns Hours passed (System.currentTimeMillis()) / 1000 / 60 / 60 / 24); Returns days passed (System.currentTimeMillis()) / 1000 / 60 / 60 / 24 / 365); Returns Years passed
Milliseconds : 1616407215646 Seconds : 1616407215 Minutes : 26940120 Hours : 449002 Days : 18708 Years : 51
Output explanation: All the above outputs are obtained from the epoch time been set which is 1 January 1970. We can easily find seconds now if we divide the millisecond by 1000 we will get the number of seconds passed since 1 Jan 1970.