Java Calculate Number of Decades Between two OffsetDateTime
Tags: Java OffsetDateTime ChronoUnit Java 8
In this Java core tutorial we learn how to calculate the number of decades between two OffsetDateTime objects using the java.time.temporal.ChronoUnit class in Java programming language.
How to get number of decades between two OffsetDateTime in Java
In Java, to calculate the number of decades value between two OffsetDateTime values we can use the ChronoUnit.DECADES.between() method as the following example Java program.
DecadesBetweenOffsetDateTimeExample.java
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
public class DecadesBetweenOffsetDateTimeExample {
public static void main(String... args) {
OffsetDateTime startOffsetDateTime = OffsetDateTime.parse("1900-01-01T07:00:00.00+07:00");
OffsetDateTime endOffsetDateTime = OffsetDateTime.parse("2022-08-13T07:00:00.00+07:00");
// Calculate Number of Decades Between two OffsetDateTime
long numberOfDecades = ChronoUnit.DECADES.between(startOffsetDateTime, endOffsetDateTime);
System.out.println(String.format("Number of decades between %s and %s is %d",
startOffsetDateTime,
endOffsetDateTime,
numberOfDecades));
}
}
Number of decades between 1900-01-01T07:00+07:00 and 2022-08-13T07:00+07:00 is 12
Happy Coding 😊
Related Articles
Java Calculate Number of Years Between two OffsetDateTime
Java Calculate Number of Months Between two OffsetDateTime
Java Calculate Number of Weeks Between two OffsetDateTime
Java Calculate Number of Days Between two OffsetDateTime
Java Calculate Number of Hours Between two OffsetDateTime
Java Calculate Number of Minutes Between two OffsetDateTime
Java Calculate Number of Seconds Between two OffsetDateTime
Java Calculate Number of Milliseconds Between two OffsetDateTime
Java Calculate Number of Nanoseconds Between two OffsetDateTime