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
Java Program to get duration between two time instants
Create two time instants:
Instant time1 = Instant.now(); Instant time2 = Instant.now().plusSeconds(50);
Use between() to get the duration between two time instants:
long resMilli = Duration.between(time1, time2).toMillis();
Example
import java.time.Duration;
import java.time.Instant;
public class Demo {
public static void main(String[] args) {
Instant time1 = Instant.now();
Instant time2 = Instant.now().plusSeconds(50);
long resMilli = Duration.between(time1, time2).toMillis();
System.out.println("Duration between two time intervals = "+resMilli);
}
}
Output
Duration between two time intervals = 50000
Advertisements