
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java System.nanoTime vs System.currentTimeMillis
What are the Time Operations in Java?
There are two-time operations provided by the Java environment. For time-related operations, users can use these operations.
-
System.nanoTime()
-
System.currentTimeMillis()
System.nanoTime() mainly known as an expensive call, is used to get a more specific value of time. And, System.currentTimeMillis() most authentic possible passed time, helps to get the time value based on the operating system. The first function returns the time value in Nano Seconds (can give a negative value sometimes) and on the other hand second function in milliseconds. So it's obvious, that System.nanoTime() is one step ahead and more acceptable for its accuracy.
What are System.nanoTime() and System.currentTimeMillis()
-
The System.nanoTime() method in Java Environment helps to find the difference at two pointers.
-
In simple words, it helps to get a time reading before the process starts and another after executing the method.
-
It is not recommended every time to profile a code because the time may differ based on the OS of a system. So, we can experience some inaccuracy.
-
The System.currentTimeMillis(), is a thread-safe method that will not return ambiguous results.
-
This function returns the date, time, and year as time.
-
Where the reference is fixed, there we can get accurate output. But when a user makes some changes in system time, it will give wrong results at that moment.
Comparison of System.nanoTime() and System.currentTimeMillis()
Here is a general example of the two specific Java time operations -
package javalangtimeslot; import java.lang.*; public class javatimeslot { public static void main(String[] args) { System.out.print("Time in nanoseconds we can see here = "); System.out.println(System.nanoTime()); System.out.print("Time in milliseconds we can see here = "); System.out.println(System.currentTimeMillis()); } }
Output
Time in nanoseconds we can see here = 4083437933186033 Time in milliseconds we can see here = 1680778649732
Java Program using System.currentTimeMillis() Method
The System.currentTimeMillis() method returns the current time in milli second unit. This method tells us how much time a part of a code takes to execute.
Syntax for System.currentTimeMillis()
System.currentTimeMillis(); --> java.language Package --> Declare System Class --> currentTimeMillis() Method
Steps to implement System.currentTimeMillis() in Java
-
Load libraries.
-
Declare the main() method by using TimeMillis() Method.
-
Display the milliseconds, minutes, hours, and days.
-
Print output.
Example 1
This example shows how to get the current time in milliseconds and convert it to seconds, minutes, hours, days, and years ?
import java.io.*; public class timefuncjava { public static void main(String[] args){ System.out.println("Milliseconds Are Here : " + System.currentTimeMillis()); System.out.println("Seconds Are Here: " + (System.currentTimeMillis())/ 2000); System.out.println("Minutes Are Here: " + (System.currentTimeMillis())/ 2000 / 120); System.out.println("Hours Are Here: " + (System.currentTimeMillis())/ 2000 / 120 / 120); System.out.println("Days Are: " + (System.currentTimeMillis())/ 2000 / 120 / 120 / 48); System.out.println("Years Total: " + (System.currentTimeMillis()) / 2000 / 120 / 120 / 48 / 365); System.out.println("Milliseconds Are Here : " + System.currentTimeMillis()); System.out.println("Seconds Are Here: " + (System.currentTimeMillis())/ 2000); System.out.println("Minutes Are Here: " + (System.currentTimeMillis())/ 2000 / 120); System.out.println("Hours Are Here: " + (System.currentTimeMillis())/ 2000 / 120 / 120); System.out.println("Days Are: " + (System.currentTimeMillis())/ 2000 / 120 / 120 / 48); System.out.println("Years Total: " + (System.currentTimeMillis()) / 2000 / 120 / 120 / 48 / 365); } }
Output
Milliseconds Are Here : 1680778875055 Seconds Are Here: 840389437 Minutes Are Here: 7003245 Hours Are Here: 58360 Days Are: 1215 Years Total: 3 Milliseconds Are Here : 1680778875062 Seconds Are Here: 840389437 Minutes Are Here: 7003245 Hours Are Here: 58360 Days Are: 1215 Years Total: 3
Here in this building code, we have used the TimeMillis to get milliseconds, seconds, minutes, hours, days, and years.
Example 2
This example shows how to measure the time taken to run a loop using System.currentTimeMillis() ?
public class timeprogjava{ public static void main(String args[]){ long starting, ending; System.out.println("Timing a loop from 0 to 100,000,000,000"); starting= System.currentTimeMillis(); for(long k=0;k<100000000000L;k++); ending=System.currentTimeMillis(); System.out.println("Elapsed time Is: "+(ending-starting)); } }
Output
Timing a loop from 0 to 100,000,000,000
Java Program using System.nanoTime() Method
System.nanoTime() Method is a Java Environment, which is a high-definition time pool in the nanosecond unit. This method always returns the current value.
Syntax for System.nanoTime()
public static long nanoTime() long process startTime = System.nanoTime(); long process estimatedTime = System.nanoTime() - startTime;
Steps to implement System.nano.Time() in Java
-
Load libraries.
-
Declare main() method by using System.nanoTime().
-
Display the table of 2.
-
The loop will produce a multiple of 2 (2x Multiple).
-
Print output from the given input.
Example 1
Using nano Time() -difference, we can calculate the consumed time by an empty loop to run this for the next 1000 times. The time output we will get before and after the loop ?
public class javacodetime { public static void main(String args[]) { long nanoTime2 = System.nanoTime(); for(int k=0; k < 10000; k++) { } long nanoTime5 = System.nanoTime(); long runTimeInNanoSeconds = nanoTime5 - nanoTime2; System.out.println("Time taken to execute the process by for loop : " + runTimeInNanoSeconds + " nano seconds"); } }
Output
Time taken to execute the process by for loop : 132509 nano seconds
Example 2
This example will help us to get the current time by using the System.nanoTime() method in nanoseconds ?
public class javatimecode { public static void main(String args[]) { long nanoTime = System.nanoTime(); System.out.println("Current time as in Nano Seconds : "+nanoTime); } }
Output
Current time as in Nano Seconds : 4084089973012410
Conclusion
In conclusion, the System.nano Time() method is required to use when the problem is too heavy. For a fast performance like HD games, the nano time operation is the best option to avail. But for the accurate output, we can use System.currentTimeMillis().
In this article, we have learned how to get the time by using the two-time method. We also built some Java code by using the logic.