- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the duration between two Instant timestamps in Java
Let us first set two Instants with ofEpochSeconds() method using seconds from the epoch of 1970-01- 01T00:00:00Z.
Now get the duration between the above two Instant:
Duration res = Duration.between(one, two);
Example
import java.time.Duration; import java.time.Instant; public class Demo { public static void main(String[] args) { Instant one = Instant.ofEpochSecond(1845836728); Instant two = Instant.ofEpochSecond(1845866935); Duration res = Duration.between(one, two); System.out.println(res); } }
Output
PT8H23M27S
Advertisements