Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to get the seconds and minutes between two Instant timestamps in Java
The following are the two Instant timestamps:
Instant one = Instant.ofEpochSecond(1355836728); Instant two = Instant.ofEpochSecond(1355866935);
Get the Duration between both the Instant:
Duration res = Duration.between(one, two);
Now, get the seconds between the two timestamps:
long seconds = res.getSeconds();
Now, get the minutes between the two timestamps:
long minutes = res.abs().toMinutes();
Example
import java.time.Duration;
import java.time.Instant;
public class Demo {
public static void main(String[] args) {
Instant one = Instant.ofEpochSecond(1355836728);
Instant two = Instant.ofEpochSecond(1355866935);
Duration res = Duration.between(one, two);
System.out.println(res);
long seconds = res.getSeconds();
System.out.println("Seconds between Durations = "+seconds);
long minutes = res.abs().toMinutes();
System.out.println("Minutes between Durations = "+minutes);
}
}
Output
PT8H23M27S Seconds between Durations = 30207 Minutes between Durations = 503
Advertisements
