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,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- [Working] Steps to Install Windows 10/11 on M1/M2 Mac using a Free VM - MacOS
- Check CPU Usage using Mac Terminal Command - MacOS
- URL Encode cURL HTTP GET\POST Data with Examples - cURL
- Python: Traverse List Backwards - Python
- Fix [Fatal Error] :2:6: The processing instruction target matching [xX][mM][lL] is not allowed. - Java
- zsh: command not found: cls macOS Big Sur - MacOS
- Install Java 17 (LTS) JDK on ARM based M1/M2 Mac Natively - MacOS
- Java StringJoiner vs StringBuilder - Java