Java.lang.System.currentTimeMillis() Method with Examples
Last Updated :
22 Jun, 2022
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.currentTimeMillis();
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
Example 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
// Java Program to illustrate TimeMillis() Method
// Importing input output classes
import java.io.*;
// Main Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Note: Whenever the program is run
// outputs will vary as it is time is real index
// Print an display commands using
// curentTimeMillis() method
// 1. Printing the Milliseconds passed at particular
// moment
System.out.println("Milliseconds : "
+ System.currentTimeMillis());
// 2. Printing the Seconds passed at particular
// moment
System.out.println("Seconds : "
+ (System.currentTimeMillis())
/ 1000);
// 3. Printing the Minutes passed at particular
// moment
System.out.println("Minutes : "
+ (System.currentTimeMillis())
/ 1000 / 60);
// 4. Printing the Hours passed at particular moment
System.out.println("Hours : "
+ (System.currentTimeMillis())
/ 1000 / 60 / 60);
// 5. Printing the Days passed at particular moment
System.out.println("Days : "
+ (System.currentTimeMillis())
/ 1000 / 60 / 60 / 24);
// 6. Printing the Years passed at particular moment
System.out.println("Years : "
+ (System.currentTimeMillis())
/ 1000 / 60 / 60 / 24
/ 365);
}
}
OutputMilliseconds : 1655351172200
Seconds : 1655351172
Minutes : 27589186
Hours : 459819
Days : 19159
Years : 52
Example 2:
Java
// Timing program execution
class GFG{
public static void main(String args[]){
long starting, ending;
System.out.println("Timing a loop from 0 to 100,000,000");
// time a for loop from 0 to 100,000,000
starting= System.currentTimeMillis(); // get the starting time
for(long i=0;i<100000000L;i++);
ending=System.currentTimeMillis(); // get ending time
System.out.println("Elapsed time: "+(ending-starting));
}
}
OutputTiming a loop from 0 to 100,000,000
Elapsed time: 83