JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

How to calculate execution / elapsed time Java 8?


This page shows examples on how to calculate execution or elapsed time to perform some task using Java 8 new date and time API. We are calculating execution / elapsed time in both milli seconds and seconds.

Instant.now().toEpochMilli() method gives current time in milli seconds.

Instant.now().getEpochSecond() method gives current time in seconds.

CalcElapsedTimeEx
package com.java2novice.datetime;

import java.time.Instant;
import java.util.concurrent.TimeUnit;

public class CalcElapsedTimeEx {

	public static void main(String a[]) {

		// calculate elapsed time in milli seconds
		Long startTime = Instant.now().toEpochMilli();
		try {
			// do your business logic here
			doYourBusinessLogic();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Long endTime = Instant.now().toEpochMilli();
		Long elapsedTime = endTime - startTime;
		System.out.println("Elapsed time in milli seconds: "+elapsedTime);

		// calculate elapsed time in seconds
		Long stTimeInSec = Instant.now().getEpochSecond();
		try {
			// do your business logic here
			doYourBusinessLogic();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Long endTimeInSec = Instant.now().getEpochSecond();

		elapsedTime = endTimeInSec - stTimeInSec;
		System.out.println("Elapsed time in seconds: "+elapsedTime);
	}

	private static void doYourBusinessLogic() throws InterruptedException {

		TimeUnit.SECONDS.sleep(3);
	}
}

Output:
Elapsed time in milli seconds: 3004
Elapsed time in seconds: 3
<< Previous Program | Next Program >>

Java-8 Date and Time API Examples

  1. Java 8 LocalDateTime example.
  2. Java 8 ZonedDateTime example.
  3. Java 8 LocalDate example.
  4. Java 8 LocalTime example.
  5. Create date time with custom values in Java 8.
  6. Java 8 Instant (Timestamp) example.
  7. Java 8 Period example.
  8. Java 8 Duration example.
  9. Java 8 Date parsing and formatting example.
  10. How to compare two dates in Java 8?
  11. How to get current date and time Java 8?
  12. How to calculate execution / elapsed time Java 8?
  13. How to convert string to Date Java 8?
  14. How to add/substract days to Date in Java 8?
Knowledge Centre
Exception Vs Error
An error is an irrecoverable condition occurring at runtime like out of memory error. These kind of jvm errors cannot be handled at runtime. Exceptions are because of condition failures, which can be handled easily at runtime.
Famous Quotations
It is easier to fight for one’s principles than to live up to them.
-- Alfred Adler

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.