
If you are using Java 8 and above then you should definitely try out using java.time API for dealing with date and time. In order to find how much time has elapsed between an event A (start) and even B (end), you should make use of Instant and the Duration class to get the best results,
Example:First we create an object that holds instant in time before we start,
Instant eventA = Instant.now() //Capture the instant in time before we start
Now we have our logic that we want to test how much time it takes to execute (most of us will like to test algorithms like sorting)
for (int i=0;i<10;i++) {
Thread.sleep(1000);
}
First we create an object that holds instant in time before we start,
Instant eventB = Instant.now() //Instant in time after completion our logic execution.
Now let's calculate the difference between these two events,
Duration timeElapsed = Duration.between(eventA,eventB);
Sample outputs of multiple runs,
PT10.037126S
PT10.038047S
PT10.036216S
PT10.031141S
PT10.032412S
PT10.032311S
package org.code2care;
import java.time.Duration;
import java.time.Instant;
/**
* Java Programs by Code2care.org
*/
public class Java8AddDaysToDate {
public static void main(String[] args) throws InterruptedException {
Instant eventA = Instant.now();
for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
}
Instant eventB = Instant.now();
Duration timeElapsed = Duration.between(eventA, eventB);
System.out.println(timeElapsed);
}
}
More Posts related to Java,
- How to install Java 11 on Mac
- Get Client IP address from HTTP Response in Java
- SharePoint Open in the client application document opens in browser
- How to verify if java is installed on the computer and get version detail
- Java - Check if array contains the value
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- How to run Java Unit Test cases with Apache Maven?
- What Java version is used for Minecraft 1.18
- List of jar files for Jax-ws (SOAP) based Java Web Services
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- Convert Instant timestamp into LocalDateTime Java Code Example
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Create simple struts2 project using maven commands
- Java - PatternSyntaxException
- List of Online Java compiler with console
- Minecraft Java Edition
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- Java XML-RPC 3.1.x based web service example
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Java 8 foreach loop code examples
- Java -Day of the week using Java 8 DayOfWeek Enum
- Java 8 - Convert List to Map Examples
- Java: TimeZone List with GMT/UTC Offset
- list of jars required for hibernate 4.x.x
More Posts:
- Android is starting optimizing... app 1 of 1 - Android
- Youtube spacebar not working in search on macOS Safari - MacOS
- [Android Studio] Hardcoded string Button, should use @string resource - Android-Studio
- Detect if Cookies are enabled using JavaScript - JavaScript
- connection.url property value in hibernate.cfg.xml for mysql - Java
- How to get SharePoint List Item URL using PowerShell - SharePoint
- Microsoft Teams: Sign-in is blocked error - Teams
- Align html element at the center of page vertically and horizontally - Html