![java package tutorial](/java/lang/images/java.jpg)
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java System currentTimeMillis() Method
Description
The Java System currentTimeMillis() method returns the current time in milliseconds.The unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger.
For example, many operating systems measure time in units of tens of milliseconds.
Declaration
Following is the declaration for java.lang.System.currentTimeMillis() method
public static long currentTimeMillis()
Parameters
NA
Return Value
This method returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC(coordinated universal time).
Exception
NA
Example: Getting Current Time in Millseconds
The following example shows the usage of Java System currentTimeMillis() method. In this example, we're printing the current time in milliseconds using System.currentTimeMillis() method.
package com.tutorialspoint; public class SystemDemo { public static void main(String[] args) { // returns the current time in milliseconds System.out.print("Current Time in milliseconds = "); System.out.println(System.currentTimeMillis()); } }
Output
Let us compile and run the above program, this will produce the following result −
Current Time in milliseconds = 1719557974290
Example: Using Current Time in Millseconds to Check Execution Time of a Code
The following example shows the usage of Java System currentTimeMillis() method. In this example, we've noted down the start time of a code snippet by getting the current time in millis and then code is executed in a for loop with a sleep() statement. Once loop is completed, we've again noted down the end time by getting the current time in milliseconds.
package com.tutorialspoint; public class SystemDemo { public static void main(String[] args) throws InterruptedException { // start time of the code snippet long startTime = System.currentTimeMillis(); int sum = 0; // a time consuming task for (int i = 0; i < 10; i++) { // sleep for 100 ms Thread.sleep(100); sum += i; } // end time of the code snippet long endTime = System.currentTimeMillis(); System.out.println("Program took " + (endTime - startTime) + " ms") ; } }
Output
Let us compile and run the above program, this will produce the following result −
Program took 1097 ms