Find Difference Between Two Instant in Java

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:

long noOfSeconds = instantBefore.until(instantAfter, ChronoUnit.SECONDS);

Here is an example:

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

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:

long noOfSeconds = ChronoUnit.SECONDS.between(instantBefore,instantAfter);

Here is an example:

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

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:

long noOfSeconds = Duration.between(instantBefore, instantAfter).toSeconds();

Here is an example:

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

Days: 1
Hours: 34
Minutes: 2083
Seconds: 124980

That’s all about how to find difference two Instant in Java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.