
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Milliseconds Between Two Time Instants in Java
In this article, we will demonstrate how to calculate the difference in milliseconds between two Instant objects using the ChronoUnit class. It shows how to work with time-based classes in the java.time package, specifically focusing on creating and manipulating time instants and calculating durations between them.
ChronoUnit: A standard set of date and time units lets you manipulate dates, times, and date-times. These units, such as years, months, and days, are designed to work across various calendar systems, even though the specific rules might differ slightly. You can also extend these units by implementing the TemporalUnit interface.
Problem Statement
Calculate the number of milliseconds between two specific time instants using Java's Instant, Duration, and ChronoUnit classes
Output
Milliseconds between two time instants = 18050000
Steps to get Milliseconds between two-time instants
Following are the steps to get Milliseconds between two-time instants ?
- Import the necessary classes from java.time package.
- Create initial time instant, use Instant.now() to capture the current time time1.
- Create a second instant time2 by adding 5 hours and 50 seconds to time1 using the plus() method.
- Define a duration of 13 seconds.
- Add this duration to time1 to get a new instant i.
- Use ChronoUnit.MILLIS.between() to find the difference in milliseconds between time1 and time2.
- Print result
Java program to get Milliseconds between two-time instants
Below is the Java program to get Milliseconds between two-time instants ?
import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; public class Demo { public static void main(String[] args) { Instant time1 = Instant.now(); Instant time2 = time1.plus(5, ChronoUnit.HOURS).plus(50, ChronoUnit.SECONDS); Duration duration = Duration.ofSeconds(13); Instant i = time1.plus(duration); System.out.println("Milliseconds between two time instants = " + ChronoUnit.MILLIS.between(time1, time2)); } }
Output
Milliseconds between two time instants = 18050000
Code Explanation
The program imports classes from the java.time package to work with time instants and durations. It begins by creating a time instant, time1, representing the current moment using Instant.now().
At first, create two-time instants ?
Instant time1 = Instant.now(); Instant time2 = time1.plus(5, ChronoUnit.HOURS).plus(50, ChronoUnit.SECONDS);
Then, a second instant, time2, is created by adding 5 hours and 50 seconds to time1 using the plus() method from the Instant class. A Duration object is defined with a length of 13 seconds, which is then added to time1 to create another instant, i.
Duration duration = Duration.ofSeconds(13); Instant i = time1.plus(duration); System.out.println("Milliseconds between two time instants = "+ChronoUnit.MILLIS.between(time1, time2));
To determine the time difference between time1 and time2 in milliseconds, the ChronoUnit.MILLIS.between() method is used. The result is printed, showing the total milliseconds between the two-time instants.