Table of Contents
In this post, we will see how to find difference between two Instant in Java.
Ways to find difference between two Instant in Java
There are multiple ways to find difference between two Instant in various time units such as Days, Hours, Minutes, Seconds etc.
Using Instant’s until() method
To find difference between two Instant, We can use Instant’s until()
method.
Instant’s until()
method returns amount of time with another date in terms of specified unit. You can pass specific TemporalUnit
such as DAYS, SECONDS to get result in specified units.
For example:
If you want amount of seconds between two Instant, you can use below code:
1 2 3 |
long noOfSeconds = instantBefore.until(instantAfter, ChronoUnit.SECONDS); |
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package org.arpit.java2blog; import java.time.Instant; import java.time.temporal.ChronoUnit; public class DifferenceBetweenTwoInstantUntil { public static void main(String[] args) { Instant instantBefore = Instant.parse("2022-09-14T18:42:00Z"); Instant instantAfter = Instant.parse("2022-09-16T05:25:00Z"); long noOfDays = instantBefore.until(instantAfter, ChronoUnit.DAYS); long noOfHours = instantBefore.until(instantAfter, ChronoUnit.HOURS); long noOfMinutes = instantBefore.until(instantAfter, ChronoUnit.MINUTES); long noOfSeconds = instantBefore.until(instantAfter, ChronoUnit.SECONDS); System.out.println("Days: "+noOfDays); System.out.println("Hours: "+noOfHours); System.out.println("Minutes: "+noOfMinutes); System.out.println("Seconds: "+noOfSeconds); } } |
Output
1 2 3 4 5 6 |
Days: 1 Hours: 34 Minutes: 2083 Seconds: 124980 |
Using ChronoUnit Enum
We can use ChronoUnit
method to find difference between two Instant.
It intenally calls until()
method to find the difference between two time units.
For example:
If you want amount of seconds between two Instant, we can use following syntax:
1 2 3 |
long noOfSeconds = ChronoUnit.SECONDS.between(instantBefore,instantAfter); |
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package org.arpit.java2blog; import java.time.Instant; import java.time.temporal.ChronoUnit; public class DifferenceBetweenTwoInstant { public static void main(String[] args) { Instant instantBefore = Instant.parse("2022-09-14T18:42:00Z"); Instant instantAfter = Instant.parse("2022-09-16T05:25:00Z"); long noOfDays = ChronoUnit.DAYS.between(instantBefore, instantAfter); long noOfHours = ChronoUnit.HOURS.between(instantBefore,instantAfter); long noOfMinutes = ChronoUnit.MINUTES.between(instantBefore,instantAfter); long noOfSeconds = ChronoUnit.SECONDS.between(instantBefore,instantAfter); System.out.println("Days: "+noOfDays); System.out.println("Hours: "+noOfHours); System.out.println("Minutes: "+noOfMinutes); System.out.println("Seconds: "+noOfSeconds); } } |
Output
1 2 3 4 5 6 |
Days: 1 Hours: 34 Minutes: 2083 Seconds: 124980 |
Using Duration’s between() method
We can use Duration’s between()
method to find difference between two Instant.
Duration’s between()
method returns amount of time with another date in terms of minutes, hours, seconds and nanoseconds. You can use toXXX()
method to convert into required unit.
For example:
If you want amount of seconds between two Instant, we can use following syntax:
1 2 3 |
long noOfSeconds = Duration.between(instantBefore, instantAfter).toSeconds(); |
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package org.arpit.java2blog; import java.time.Duration; import java.time.Instant; public class DifferenceBetweenTwoInstantDuration { public static void main(String[] args) { Instant instantBefore = Instant.parse("2022-09-14T18:42:00Z"); Instant instantAfter = Instant.parse("2022-09-16T05:25:00Z"); long noOfDays = Duration.between(instantBefore, instantAfter).toDays(); long noOfHours = Duration.between(instantBefore, instantAfter).toHours(); long noOfMinutes = Duration.between(instantBefore, instantAfter).toMinutes(); long noOfSeconds = Duration.between(instantBefore, instantAfter).toSeconds(); System.out.println("Days: "+noOfDays); System.out.println("Hours: "+noOfHours); System.out.println("Minutes: "+noOfMinutes); System.out.println("Seconds: "+noOfSeconds); } } |
Output
1 2 3 4 5 6 |
Days: 1 Hours: 34 Minutes: 2083 Seconds: 124980 |
Further reading:
That’s all about how to find difference two Instant in Java.