- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get elapsed time in Java
To get the elapsed time, first get the start time.
long start = System.nanoTime();
Now, we will get the end time after pausing for 5 seconds.
long end = System.nanoTime();
For the elapsed time, find the difference between end and start.
long elapsed = end - start;
Example
import java.util.concurrent.TimeUnit; public class Demo { public static void main(String[] args) throws InterruptedException { long start = System.nanoTime(); TimeUnit.SECONDS.sleep(5); long end = System.nanoTime(); long elapsed = end - start; System.out.println("Execution time (nanoseconds) = " + elapsed); } }
Output
Execution time (nanoseconds) = 5000820351
- Related Articles
- Get elapsed time in minutes in Java
- Get elapsed time in days in Java
- Measuring elapsed time in Java
- Compute elapsed time in seconds in Java
- Compute elapsed time in hours in Java
- How to measure elapsed time in Java?
- How to calculate elapsed/execution time in Java?
- Compute the elapsed time of an operation in Java
- How to measure elapsed time in nanoseconds with Java?
- Computer elapsed time of an operation in milliseconds in Java
- How to measure elapsed time in python?
- How to calculate Elapsed time in OpenCV using C++?
- Get current time information in Java
- Get the default Time Zone in Java
- Get time in milliseconds using Java Calendar

Advertisements