Java Create Duration Between LocalDateTime
Tags: Java LocalDateTime Java 8
In this Java core tutorial we learn how to create a new Duration object from given start and end LocalDateTime values using date time API in Java programming language.
How to create Duration between LocalDateTime in Java
In Java, with given start LocalDateTime object and end LocalDateTime object we can use the Duration.between(Temporal startInclusive, Temporal endExclusive) method to create a new Duration object as the following example Java code.
DurationBetweenExample1.java
import java.time.Duration;
import java.time.LocalDateTime;
public class DurationBetweenExample1 {
public static void main(String... args) {
LocalDateTime start = LocalDateTime.of(2022, 6, 15, 8, 30 , 0);
LocalDateTime end = LocalDateTime.of(2022, 6, 15, 12, 0 , 0);
Duration duration = Duration.between(start, end);
System.out.println("Start LocalDateTime: " + start);
System.out.println("End LocalDateTime: " + end);
System.out.println("Duration: " + duration);
}
}
Start LocalDateTime: 2022-06-15T08:30
End LocalDateTime: 2022-06-15T12:00
Duration: PT3H30M
Happy Coding 😊