Java Code To Calculate Time Difference In Milliseconds
Chapter:
Date and Time
Last Updated:
20-09-2023 14:53:07 UTC
Program:
/* ............... START ............... */
public class TimeDifferenceCalculator {
public static void main(String[] args) {
// Get the current time in milliseconds before the operation
long startTime = System.currentTimeMillis();
// Perform some task or operation here
// Get the current time in milliseconds after the operation
long endTime = System.currentTimeMillis();
// Calculate the time difference in milliseconds
long timeDifference = endTime - startTime;
System.out.println("Time difference in milliseconds: " + timeDifference);
}
}
/* ............... END ............... */
Output
Time difference in milliseconds: 1234
In this example, the time difference is calculated and printed, and the value 1234
represents the time difference in milliseconds between the startTime and endTime.
The actual value will vary depending on the duration of the task or operation you
perform between the two time measurements.
Notes:
-
Step by step explanation of this program.
- System.currentTimeMillis() is used to obtain the current time in milliseconds before and after the operation.
- The time difference is calculated by subtracting startTime from endTime.
- The result is then printed to the console.
Tags
Java code to calculate time difference in milliseconds #How do you calculate time difference in milliseconds in Java? #Java program to calculate time difference in Milliseconds