Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Getting Time in Milliseconds using currentTimeMillis() Method in Java

  • Difficulty Level : Medium
  • Last Updated : 12 Apr, 2021

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.

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

Implementation:



Example

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);
    }
}
Output
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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!