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 the date class after java 8. Date and time class via java.util 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 milliseconds the accuracy of the exact time increases.
The package view of the method is as follows:
--> java.lang Package
--> System Class
--> currentTimeMillis() Method
Syntax: Getting milliseconds
System.current TimeMillis();
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
E xample 1: Here all outputs are obtained from the epoch time 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.
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
System.out.println("Milliseconds : "
+ System.currentTimeMillis());
System.out.println("Seconds : "
+ (System.currentTimeMillis())
/ 1000);
System.out.println("Minutes : "
+ (System.currentTimeMillis())
/ 1000 / 60);
System.out.println("Hours : "
+ (System.currentTimeMillis())
/ 1000 / 60 / 60);
System.out.println("Days : "
+ (System.currentTimeMillis())
/ 1000 / 60 / 60 / 24);
System.out.println("Years : "
+ (System.currentTimeMillis())
/ 1000 / 60 / 60 / 24
/ 365);
}
}
Output
Milliseconds : 1655351172200
Seconds : 1655351172
Minutes : 27589186
Hours : 459819
Days : 19159
Years : 52
Example 2:
Java
class GFG{
public static void main(String args[]){
long starting, ending;
System.out.println("Timing a loop from 0 to 100,000,000");
starting= System.currentTimeMillis();
for(long i=0;i<100000000L;i++);
ending=System.currentTimeMillis();
System.out.println("Elapsed time: "+(ending-starting));
}
}
Output
Timing a loop from 0 to 100,000,000
Elapsed time: 83
Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule. What We Offer:
Comprehensive Course
Expert Guidance for Efficient Learning
Hands-on Experience with Real-world Projects
Proven Track Record with 100,000+ Successful Geeks
Last Updated :
22 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...