
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);
}
}
- Add two numbers using Java Generics
- Convert Java List to Json String using Jackson
- Convert Java Object to JSON using Jackson Library
- Java SE JDBC: Insert with PreparedStatement Example
- [Program] How to read three different values using Scanner in Java
- Java JDBC Batch Update Example with PreparedStatement
- Java Stream flatmap() Examples
- Save Java Object as JSON file using Jackson Library
- Java get day of the week as an int using DayOfWeek
- Create Nested Directories using Java Code
- Java JDBC Delete a Record in Database Table using PreparedStatement
- List of jars required for Struts2 project
- Convert Java Object to XML using Jackson Library
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Java JDBC Get Id of the Inserted Record with AutoIncrement
- How to list all tables using Java JDBC
- Java Jackson ObjectMapper Class with Examples
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Formatting Double in Java [Examples]
- How to run Java Unit Test cases with Apache Maven?
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- [Fix] java: integer number too large compilation error
- [Java] Read a File with UTF-8 Encoding
- How to detect Operating System using Java code
- How to switch between sftp and Terminal shell - FTP
- Examples: Convert String to int in JavaScript - JavaScript
- List of Java JDBC Database Driver Jars, Classes and URLs Details - Java
- Users experience call quality issue, voice distortion, disconnection with Microsoft Teams call and meeting - Teams
- [Eclipse] Locate Preferences in macOS - MacOS
- Multiline EditText in Android Example - Android
- Facebook Graph API Unavailable - Facebook
- How to install wget on macOS - MacOS