Java Compare two ZonedDateTime Values
Tags: Java ZonedDateTime Java 8
In this Java core tutorial we learn how to compare two java.time.ZonedDateTime objects in Java programming language using compareTo(), isEqual(), equals(), isBefore() and isAfter() methods.
Table of contents
- Compare two ZonedDateTime objects for Ordering using compareTo() method
- How to compare two ZonedDateTime objects for Equality
- Compare if a ZonedDateTime object is before another ZonedDateTime object
- Compare if a ZonedDateTime object is after another ZonedDateTime object
Compare two ZonedDateTime objects for Ordering using compareTo() method
In Java, to compare two ZonedDateTime objects for ordering we can use the ZonedDateTime .compareTo() method which return int value of -1, 0 or 1 if the ZonedDateTime object is before, equal to or after the ZonedDateTime object it compare to.
The following Java example code to show the ZonedDateTime .compareTo() method return -1 when the ZonedDateTime object is before another ZonedDateTime object it compare to.
ZonedDateTimeCompareToExample1.java
import java.time.ZonedDateTime;
public class ZonedDateTimeCompareToExample1 {
public static void main(String... args) {
ZonedDateTime zonedDateTime1 = ZonedDateTime.parse("2022-06-10T06:00:00.777777+07:00[Asia/Bangkok]");
ZonedDateTime zonedDateTime2 = ZonedDateTime.parse("2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]");
int compareToResult = zonedDateTime1.compareTo(zonedDateTime2);
System.out.println("zonedDateTime1: " + zonedDateTime1);
System.out.println("zonedDateTime2: " + zonedDateTime2);
System.out.println("zonedDateTime1 compare to zonedDateTime2: " + compareToResult);
}
}
zonedDateTime1: 2022-06-10T06:00:00.777777+07:00[Asia/Bangkok]
zonedDateTime2: 2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]
zonedDateTime1 compare to zonedDateTime2: -1
The following Java example code to show the ZonedDateTime .compareTo() method return 0 when the ZonedDateTime object is equal to another ZonedDateTime object it compare to.
ZonedDateTimeCompareToExample2.java
import java.time.ZonedDateTime;
public class ZonedDateTimeCompareToExample2 {
public static void main(String... args) {
ZonedDateTime zonedDateTime1 = ZonedDateTime.parse("2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]");
ZonedDateTime zonedDateTime2 = ZonedDateTime.parse("2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]");
int compareToResult = zonedDateTime1.compareTo(zonedDateTime2);
System.out.println("zonedDateTime1: " + zonedDateTime1);
System.out.println("zonedDateTime2: " + zonedDateTime2);
System.out.println("zonedDateTime1 compare to zonedDateTime2: " + compareToResult);
}
}
zonedDateTime1: 2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]
zonedDateTime2: 2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]
zonedDateTime1 compare to zonedDateTime2: 0
The following Java example code to show the ZonedDateTime .compareTo() method return 1 when the ZonedDateTime object is after another ZonedDateTime object it compare to.
ZonedDateTimeCompareToExample3.java
import java.time.ZonedDateTime;
public class ZonedDateTimeCompareToExample3 {
public static void main(String... args) {
ZonedDateTime zonedDateTime1 = ZonedDateTime.parse("2022-08-12T08:00:00.777777+07:00[Asia/Bangkok]");
ZonedDateTime zonedDateTime2 = ZonedDateTime.parse("2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]");
int compareToResult = zonedDateTime1.compareTo(zonedDateTime2);
System.out.println("zonedDateTime1: " + zonedDateTime1);
System.out.println("zonedDateTime2: " + zonedDateTime2);
System.out.println("zonedDateTime1 compare to zonedDateTime2: " + compareToResult);
}
}
zonedDateTime1: 2022-08-12T08:00:00.777777+07:00[Asia/Bangkok]
zonedDateTime2: 2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]
zonedDateTime1 compare to zonedDateTime2: 1
How to compare two ZonedDateTime objects for Equality
In Java to compare two ZonedDateTime values are equal or not we can use the ZonedDateTime.isEqual() method as the following example Java code.
ZonedDateTimeIsEqualExample1.java
import java.time.ZonedDateTime;
public class ZonedDateTimeIsEqualExample1 {
public static void main(String... args) {
ZonedDateTime zonedDateTime1 = ZonedDateTime.parse("2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]");
ZonedDateTime zonedDateTime2 = ZonedDateTime.parse("2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]");
ZonedDateTime zonedDateTime3 = ZonedDateTime.parse("2023-08-22T08:01:01.777777+07:00[Asia/Bangkok]");
boolean result1 = zonedDateTime1.isEqual(zonedDateTime2);
boolean result2 = zonedDateTime1.isEqual(zonedDateTime3);
System.out.println("zonedDateTime1: " + zonedDateTime1);
System.out.println("zonedDateTime2: " + zonedDateTime2);
System.out.println("zonedDateTime3: " + zonedDateTime3);
System.out.println("zonedDateTime1 is equal zonedDateTime2: " + result1);
System.out.println("zonedDateTime1 is equal zonedDateTime3: " + result2);
}
}
zonedDateTime1: 2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]
zonedDateTime2: 2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]
zonedDateTime3: 2023-08-22T08:01:01.777777+07:00[Asia/Bangkok]
zonedDateTime1 is equal zonedDateTime2: true
zonedDateTime1 is equal zonedDateTime3: false
We also can use the ZonedDateTime.equals() method which return the same result as ZonedDateTime.isEqual() method.
ZonedDateTimeEqualsExample1.java
import java.time.ZonedDateTime;
public class ZonedDateTimeEqualsExample1 {
public static void main(String... args) {
ZonedDateTime zonedDateTime1 = ZonedDateTime.parse("2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]");
ZonedDateTime zonedDateTime2 = ZonedDateTime.parse("2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]");
ZonedDateTime zonedDateTime3 = ZonedDateTime.parse("2023-08-22T08:01:01.777777+07:00[Asia/Bangkok]");
boolean result1 = zonedDateTime1.equals(zonedDateTime2);
boolean result2 = zonedDateTime1.equals(zonedDateTime3);
System.out.println("zonedDateTime1: " + zonedDateTime1);
System.out.println("zonedDateTime2: " + zonedDateTime2);
System.out.println("zonedDateTime3: " + zonedDateTime3);
System.out.println("zonedDateTime1 is equal zonedDateTime2: " + result1);
System.out.println("zonedDateTime1 is equal zonedDateTime3: " + result2);
}
}
zonedDateTime1: 2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]
zonedDateTime2: 2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]
zonedDateTime3: 2023-08-22T08:01:01.777777+07:00[Asia/Bangkok]
zonedDateTime1 is equal zonedDateTime2: true
zonedDateTime1 is equal zonedDateTime3: false
Compare if a ZonedDateTime object is before another ZonedDateTime object
In Java to check if a ZonedDateTime object is before another specified ZonedDateTime object we can use the ZonedDateTime.isBefore() method as following Java code.
ZonedDateTimeIsBeforeExample1.java
import java.time.ZonedDateTime;
public class ZonedDateTimeIsBeforeExample1 {
public static void main(String... args) {
ZonedDateTime zonedDateTime1 = ZonedDateTime.parse("2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]");
ZonedDateTime zonedDateTime2 = ZonedDateTime.parse("2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]");
ZonedDateTime zonedDateTime3 = ZonedDateTime.parse("2023-08-22T08:01:01.777777+07:00[Asia/Bangkok]");
boolean result1 = zonedDateTime1.isBefore(zonedDateTime2);
boolean result2 = zonedDateTime1.isBefore(zonedDateTime3);
System.out.println("zonedDateTime1: " + zonedDateTime1);
System.out.println("zonedDateTime2: " + zonedDateTime2);
System.out.println("zonedDateTime3: " + zonedDateTime3);
System.out.println("zonedDateTime1 is before zonedDateTime2: " + result1);
System.out.println("zonedDateTime1 is before zonedDateTime3: " + result2);
}
}
zonedDateTime1: 2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]
zonedDateTime2: 2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]
zonedDateTime3: 2023-08-22T08:01:01.777777+07:00[Asia/Bangkok]
zonedDateTime1 is before zonedDateTime2: false
zonedDateTime1 is before zonedDateTime3: true
Compare if a ZonedDateTime object is after another ZonedDateTime object
In Java to check if a ZonedDateTime object is after another specified ZonedDateTime object we can use the ZonedDateTime.isAfter() method as following Java code.
ZonedDateTimeIsAfterExample1.java
import java.time.ZonedDateTime;
public class ZonedDateTimeIsAfterExample1 {
public static void main(String... args) {
ZonedDateTime zonedDateTime1 = ZonedDateTime.parse("2022-08-12T07:00:00.777777+07:00[Asia/Bangkok]");
ZonedDateTime zonedDateTime2 = ZonedDateTime.parse("2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]");
ZonedDateTime zonedDateTime3 = ZonedDateTime.parse("2023-06-22T08:01:01.777777+07:00[Asia/Bangkok]");
boolean result1 = zonedDateTime1.isAfter(zonedDateTime2);
boolean result2 = zonedDateTime1.isAfter(zonedDateTime3);
System.out.println("zonedDateTime1: " + zonedDateTime1);
System.out.println("zonedDateTime2: " + zonedDateTime2);
System.out.println("zonedDateTime3: " + zonedDateTime3);
System.out.println("zonedDateTime1 is after zonedDateTime2: " + result1);
System.out.println("zonedDateTime1 is after zonedDateTime3: " + result2);
}
}
zonedDateTime1: 2022-08-12T07:00:00.777777+07:00[Asia/Bangkok]
zonedDateTime2: 2022-07-11T07:00:00.777777+07:00[Asia/Bangkok]
zonedDateTime3: 2023-06-22T08:01:01.777777+07:00[Asia/Bangkok]
zonedDateTime1 is after zonedDateTime2: true
zonedDateTime1 is after zonedDateTime3: false
Happy Coding 😊
Related Articles
Java ZonedDateTime.now() method with Examples
Java ZonedDateTime.of() Method with Examples
Java ZonedDateTime.ofInstant() Method with Examples
Java ZonedDateTime.ofStrict() Method with Examples
Java ZonedDateTime.parse() Method with Examples
Java ZonedDateTime.getOffset() Method with Examples
Java ZonedDateTime.withEarlierOffsetAtOverlap() Method with Examples
Java ZonedDateTime.withLaterOffsetAtOverlap() Method with Examples
Java ZonedDateTime.getZone() Method with Examples
Java ZonedDateTime.withZoneSameLocal() Method with Examples
Java ZonedDateTime.withZoneSameInstant() Method with Examples
Java ZonedDateTime.withFixedOffsetZone() Method with Examples
Java ZonedDateTime.toLocalDateTime() Method with Examples
Java ZonedDateTime.toLocalDate() Method with Examples
Java ZonedDateTime.toLocalTime() Method with Examples
Java ZonedDateTime.getYear() Method with Examples
Java ZonedDateTime.getMonthValue() Method with Examples
Java ZonedDateTime.getMonth() Method with Examples
Java ZonedDateTime.getDayOfMonth() Method with Examples
Java ZonedDateTime.getDayOfYear() Method with Examples
Java ZonedDateTime.getDayOfWeek() Method with Examples
Java ZonedDateTime.getHour() Method with Examples
Java ZonedDateTime.getMinute() Method with Examples
Java ZonedDateTime.getSecond() Method with Examples
Java ZonedDateTime.getNano() Method with Examples
Java ZonedDateTime.withYear() Method with Examples
Java ZonedDateTime.withMonth() Method with Examples
Java ZonedDateTime.withDayOfMonth() Method with Examples
Java ZonedDateTime.withDayOfYear() Method with Examples
Java ZonedDateTime.withHour() Method with Examples
Java ZonedDateTime.withMinute() Method with Examples
Java ZonedDateTime.withSecond() Method with Examples
Java ZonedDateTime.withNano() Method with Examples
Java ZonedDateTime.plusYears() Method with Examples
Java ZonedDateTime.plusMonths() Method with Examples
Java ZonedDateTime.plusWeeks() Method with Examples
Java ZonedDateTime.plusDays() Method with Examples
Java ZonedDateTime.plusHours() Method with Examples
Java ZonedDateTime.plusMinutes() Method with Examples
Java ZonedDateTime.plusSeconds() Method with Examples
Java ZonedDateTime.plusNanos() Method with Examples
Java ZonedDateTime.minusYears() Method with Examples
Java ZonedDateTime.minusMonths() Method with Examples
Java ZonedDateTime.minusWeeks() Method with Examples
Java ZonedDateTime.minusDays() Method with Examples
Java ZonedDateTime.minusHours() Method with Examples
Java ZonedDateTime.minusMinutes() Method with Examples
Java ZonedDateTime.minusSeconds() Method with Examples
Java ZonedDateTime.minusNanos() Method with Examples
Java ZonedDateTime.format() Method with Examples
Java ZonedDateTime.toOffsetDateTime() Method with Examples
Java ZonedDateTime.equals() Method with Examples
Java ZonedDateTime.toString() Method with Examples