Java Compare two OffsetDateTime Values

Tags: Java OffsetDateTime Java 8

In this Java core tutorial we learn how to compare two java.time.OffsetDateTime objects in Java programming language using compareTo(), isEqual(), equals(), isBefore() and isAfter() methods.

Table of contents

  1. Compare two OffsetDateTime objects using compareTo() method
  2. Compare two OffsetDateTime objects for Equality
  3. Compare if a OffsetDateTime object is before another OffsetDateTime object
  4. Compare if a OffsetDateTime object is after another OffsetDateTime object

Compare two OffsetDateTime objects using compareTo() method

In Java, to compare two OffsetDateTime objects for ordering we can use the OffsetDateTime.compareTo() method which return int value of negative, zero or positive integer value if the OffsetDateTime object is before, equal to or after the OffsetDateTime object it compare to.

The following Java example code to show the OffsetDateTime.compareTo() method return negative integer value when the OffsetDateTime object is before another OffsetDateTime object it compare to.

OffsetDateTimeCompareToExample1.java

import java.time.OffsetDateTime;

public class OffsetDateTimeCompareToExample1 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("2022-05-10T07:30:40.111222333+10:00");
        OffsetDateTime offsetDateTime2 = OffsetDateTime.parse("2022-05-15T07:30:40.111222333+10:00");

        int compareToResult = offsetDateTime1.compareTo(offsetDateTime2);

        System.out.println("offsetDateTime1: " + offsetDateTime1);
        System.out.println("offsetDateTime2: " + offsetDateTime2);
        System.out.println("offsetDateTime1 compare to offsetDateTime2: " + compareToResult);
    }
}
The output as below.
offsetDateTime1: 2022-05-10T07:30:40.111222333+10:00
offsetDateTime2: 2022-05-15T07:30:40.111222333+10:00
offsetDateTime1 compare to offsetDateTime2: -5

The following Java example code to show the OffsetDateTime.compareTo() method return zero integer value when the OffsetDateTime object is equal to another OffsetDateTime object it compare to.

OffsetDateTimeCompareToExample2.java

import java.time.OffsetDateTime;

public class OffsetDateTimeCompareToExample2 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("2022-05-15T07:30:40.111222333+10:00");
        OffsetDateTime offsetDateTime2 = OffsetDateTime.parse("2022-05-15T07:30:40.111222333+10:00");

        int compareToResult = offsetDateTime1.compareTo(offsetDateTime2);

        System.out.println("offsetDateTime1: " + offsetDateTime1);
        System.out.println("offsetDateTime2: " + offsetDateTime2);
        System.out.println("offsetDateTime1 compare to offsetDateTime2: " + compareToResult);
    }
}
The output as below.
offsetDateTime1: 2022-05-15T07:30:40.111222333+10:00
offsetDateTime2: 2022-05-15T07:30:40.111222333+10:00
offsetDateTime1 compare to offsetDateTime2: 0

The following Java example code to show the OffsetDateTime.compareTo() method return positive integer value when the OffsetDateTime object is after another OffsetDateTime object it compare to.

OffsetDateTimeCompareToExample3.java

import java.time.OffsetDateTime;

public class OffsetDateTimeCompareToExample3 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("2022-05-25T07:30:40.111222333+10:00");
        OffsetDateTime offsetDateTime2 = OffsetDateTime.parse("2022-05-15T07:30:40.111222333+10:00");

        int compareToResult = offsetDateTime1.compareTo(offsetDateTime2);

        System.out.println("offsetDateTime1: " + offsetDateTime1);
        System.out.println("offsetDateTime2: " + offsetDateTime2);
        System.out.println("offsetDateTime1 compare to offsetDateTime2: " + compareToResult);
    }
}
The output as below.
offsetDateTime1: 2022-05-25T07:30:40.111222333+10:00
offsetDateTime2: 2022-05-15T07:30:40.111222333+10:00
offsetDateTime1 compare to offsetDateTime2: 10

Compare two OffsetDateTime objects for Equality

In Java to compare two OffsetDateTime values are equal or not we can use the OffsetDateTime.isEqual() method as the following example Java code.

OffsetDateTimeIsEqualExample1.java

import java.time.OffsetDateTime;

public class OffsetDateTimeIsEqualExample1 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("2022-05-10T07:30:40.111222333+10:00");
        OffsetDateTime offsetDateTime2 = OffsetDateTime.parse("2022-05-10T07:30:40.111222333+10:00");
        OffsetDateTime offsetDateTime3 = OffsetDateTime.parse("2022-05-15T07:30:40.111222333+10:00");

        boolean result1 = offsetDateTime1.isEqual(offsetDateTime2);
        boolean result2 = offsetDateTime1.isEqual(offsetDateTime3);

        System.out.println("offsetDateTime1: " + offsetDateTime1);
        System.out.println("offsetDateTime2: " + offsetDateTime2);
        System.out.println("offsetDateTime3: " + offsetDateTime3);
        System.out.println("offsetDateTime1 equal to offsetDateTime2: " + result1);
        System.out.println("offsetDateTime1 equal to offsetDateTime3: " + result2);
    }
}
The output as below.
offsetDateTime1: 2022-05-10T07:30:40.111222333+10:00
offsetDateTime2: 2022-05-10T07:30:40.111222333+10:00
offsetDateTime3: 2022-05-15T07:30:40.111222333+10:00
offsetDateTime1 equal to offsetDateTime2: true
offsetDateTime1 equal to offsetDateTime3: false

The OffsetDateTime class also provide equals() method which return the same result with isEqual() method, we learn how to use the OffsetDateTime.equals() method in the following Java program.

OffsetDateTimeEqualsExample1.java

import java.time.OffsetDateTime;

public class OffsetDateTimeEqualsExample1 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("2022-05-10T07:30:40.111222333+10:00");
        OffsetDateTime offsetDateTime2 = OffsetDateTime.parse("2022-05-10T07:30:40.111222333+10:00");
        OffsetDateTime offsetDateTime3 = OffsetDateTime.parse("2022-05-15T07:30:40.111222333+10:00");

        boolean result1 = offsetDateTime1.equals(offsetDateTime2);
        boolean result2 = offsetDateTime1.equals(offsetDateTime3);

        System.out.println("offsetDateTime1: " + offsetDateTime1);
        System.out.println("offsetDateTime2: " + offsetDateTime2);
        System.out.println("offsetDateTime3: " + offsetDateTime3);
        System.out.println("offsetDateTime1 equal to offsetDateTime2: " + result1);
        System.out.println("offsetDateTime1 equal to offsetDateTime3: " + result2);
    }
}
The output as below.
offsetDateTime1: 2022-05-10T07:30:40.111222333+10:00
offsetDateTime2: 2022-05-10T07:30:40.111222333+10:00
offsetDateTime3: 2022-05-15T07:30:40.111222333+10:00
offsetDateTime1 equal to offsetDateTime2: true
offsetDateTime1 equal to offsetDateTime3: false

Compare if a OffsetDateTime object is before another OffsetDateTime object

In Java to check if a OffsetDateTime object is before another specified OffsetDateTime object we can use the OffsetDateTime.isBefore() method as following Java code.

OffsetDateTimeIsBeforeExample1.java

import java.time.OffsetDateTime;

public class OffsetDateTimeIsBeforeExample1 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("2022-05-10T07:30:40.111222333+10:00");
        OffsetDateTime offsetDateTime2 = OffsetDateTime.parse("2022-05-10T07:30:40.111222333+10:00");
        OffsetDateTime offsetDateTime3 = OffsetDateTime.parse("2022-05-15T07:30:40.111222333+10:00");

        boolean result1 = offsetDateTime1.isBefore(offsetDateTime2);
        boolean result2 = offsetDateTime1.isBefore(offsetDateTime3);

        System.out.println("offsetDateTime1: " + offsetDateTime1);
        System.out.println("offsetDateTime2: " + offsetDateTime2);
        System.out.println("offsetDateTime3: " + offsetDateTime3);
        System.out.println("offsetDateTime1 is before to offsetDateTime2: " + result1);
        System.out.println("offsetDateTime1 is before to offsetDateTime3: " + result2);
    }
}
The output as below.
offsetDateTime1: 2022-05-10T07:30:40.111222333+10:00
offsetDateTime2: 2022-05-10T07:30:40.111222333+10:00
offsetDateTime3: 2022-05-15T07:30:40.111222333+10:00
offsetDateTime1 is before to offsetDateTime2: false
offsetDateTime1 is before to offsetDateTime3: true

Compare if a OffsetDateTime object is after another OffsetDateTime object

In Java to check if a OffsetDateTime object is after another specified OffsetDateTime object we can use the OffsetDateTime.isAfter() method as following Java code.

OffsetDateTimeIsAfterExample1.java

import java.time.OffsetDateTime;

public class OffsetDateTimeIsAfterExample1 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("2022-05-12T07:30:40.111222333+10:00");
        OffsetDateTime offsetDateTime2 = OffsetDateTime.parse("2022-05-10T07:30:40.111222333+10:00");
        OffsetDateTime offsetDateTime3 = OffsetDateTime.parse("2022-05-15T07:30:40.111222333+10:00");

        boolean result1 = offsetDateTime1.isAfter(offsetDateTime2);
        boolean result2 = offsetDateTime1.isAfter(offsetDateTime3);

        System.out.println("offsetDateTime1: " + offsetDateTime1);
        System.out.println("offsetDateTime2: " + offsetDateTime2);
        System.out.println("offsetDateTime3: " + offsetDateTime3);
        System.out.println("offsetDateTime1 is after to offsetDateTime2: " + result1);
        System.out.println("offsetDateTime1 is after to offsetDateTime3: " + result2);
    }
}
The output as below.
offsetDateTime1: 2022-05-12T07:30:40.111222333+10:00
offsetDateTime2: 2022-05-10T07:30:40.111222333+10:00
offsetDateTime3: 2022-05-15T07:30:40.111222333+10:00
offsetDateTime1 is after to offsetDateTime2: true
offsetDateTime1 is after to offsetDateTime3: false

Happy Coding 😊

Java OffsetDateTime.format() Method with Examples

Java OffsetDateTime.atZoneSameInstant() Method with Examples

Java OffsetDateTime.atZoneSimilarLocal() Method with Examples

Java OffsetDateTime.minusYears() Method with Examples

Java OffsetDateTime.minusMonths() Method with Examples

Java OffsetDateTime.minusWeeks() Method with Examples

Java OffsetDateTime.minusDays() Method with Examples

Java OffsetDateTime.minusHours() Method with Examples

Java OffsetDateTime.minusMinutes() Method with Examples

Java OffsetDateTime.minusSeconds() Method with Examples

Java OffsetDateTime.minusNanos() Method with Examples

Java OffsetDateTime.getOffset() Method with Examples

Java OffsetDateTime.getYear() Method with Examples

Java OffsetDateTime.getMonthValue() Method with Examples

Java OffsetDateTime.getMonth() Method with Examples

Java OffsetDateTime.getDayOfMonth() Method with Examples

Java OffsetDateTime.getDayOfYear() Method with Examples

Java OffsetDateTime.getDayOfWeek() Method with Examples

Java OffsetDateTime.getHour() Method with Examples

Java OffsetDateTime.getMinute() Method with Examples

Java OffsetDateTime.getSecond() Method with Examples

Java OffsetDateTime.getNano() Method with Examples

Java OffsetDateTime.withYear() Method with Examples

Java OffsetDateTime.withMonth() Method with Examples

Java OffsetDateTime.withDayOfMonth() Method with Examples

Java OffsetDateTime.withDayOfYear() Method with Examples

Java OffsetDateTime.withHour() Method with Examples

Java OffsetDateTime.withMinute() Method with Examples

Java OffsetDateTime.withSecond() Method with Examples

Java OffsetDateTime.withNano() Method with Examples

Java OffsetDateTime.plusYears() Method with Examples

Java OffsetDateTime.plusMonths() Method with Examples

Java OffsetDateTime.plusWeeks() Method with Examples

Java OffsetDateTime.plusDays() Method with Examples

Java OffsetDateTime.plusHours() Method with Examples

Java OffsetDateTime.plusMinutes() Method with Examples

Java OffsetDateTime.plusSeconds() Method with Examples

Java OffsetDateTime.plusNanos() Method with Examples

Java OffsetDateTime.now() Method with Examples

Java OffsetDateTime.of() Method with Examples

Java OffsetDateTime.ofInstant() Method with Examples

Java OffsetDateTime.parse() Method with Examples

Java OffsetDateTime.withOffsetSameLocal() Method with Examples

Java OffsetDateTime.withOffsetSameInstant() Method with Examples

Java OffsetDateTime.toLocalDateTime() Method with Examples

Java OffsetDateTime.toLocalDate() Method with Examples

Java OffsetDateTime.toLocalTime() Method with Examples

Java OffsetDateTime.toOffsetTime() Method with Examples

Java OffsetDateTime.toZonedDateTime() Method with Examples

Java OffsetDateTime.toInstant() Method with Examples

Java OffsetDateTime.toEpochSecond() Method with Examples

Java OffsetDateTime.toString() Method with Examples

Java OffsetDateTime.format() Method with Examples

Java OffsetDateTime.atZoneSameInstant() Method with Examples

Java OffsetDateTime.atZoneSimilarLocal() Method with Examples

Java Compare two OffsetDateTime Values