Java Calculate Number of Days Between two Instant
Tags: Java Instant ChronoUnit Java 8
In this Java core tutorial, we learn how to calculate the number of days between two Instance objects using the java.time.temporal.ChronoUnit class in Java programming language.
How to get number of days between two Instant in Java
In Java, with given start and end Instant values we can use the ChronoUnit.DAYS.between() method to calculate the number of days between them as the following example Java code.
DaysBetweenInstantExample.java
import java.time.Instant;
import java.time.temporal.ChronoUnit;
public class DaysBetweenInstantExample {
public static void main(String... args) {
Instant startInstant = Instant.parse("2022-08-11T07:00:00.0Z");
Instant endInstant = Instant.parse("2022-08-13T07:00:00.0Z");
// Calculate Number of Days Between two Instant
long numberOfDays = ChronoUnit.DAYS.between(startInstant, endInstant);
System.out.println(String.format("Number of days between %s and %s is %d", startInstant, endInstant, numberOfDays));
}
}
Number of days between 2022-08-11T07:00:00Z and 2022-08-13T07:00:00Z is 2
Happy Coding 😊
Related Articles
Java Calculate Number of Hours Between two Instant
Java Calculate Number of Minutes Between two Instant
Java Calculate Number of Seconds Between two Instant