In this article, we will discuss different ways to compare two instances of ZonedDateTime in Java 1.8 version
1. Comparing two ZonedDateTime instances :
There are different methods available to compare two ZonedDateTime instances, those are –
- compareTo(ChronoZonedDateTime) –
- This method compares invoking ZonedDateTime with another ZonedDateTime
- isBefore(ChronoZonedDateTime) –
- This method checks if invoking ZonedDateTime is before the specified ZonedDateTime
- isAfter(ChronoZonedDateTime) –
- This method checks if invoking ZonedDateTime is after the specified ZonedDateTime
- isEqual(ChronoZonedDateTime) –
- This method checks if invoking ZonedDateTime is equal to the specified ZonedDateTime
- equals(Object) –
- This method checks if invoking ZonedDateTime is equal to another ZonedDateTime
2. Examples for Comparing two ZonedDateTime :
- Using compareTo(ChronoZonedDateTime) method
- Using isBefore(ChronoZonedDateTime) method
- Using isAfter(ChronoZonedDateTime) method
- Using isEqual(ChronoZonedDateTime) method
- Using equals(Object) method
2.1 Using compareTo(ChronoZonedDateTime) method :
- This method compares invoking ZonedDateTime with another ZonedDateTime and returns integer value based on the comparison
- Returns Zero, if both ZonedDateTime in comparison are equal
- Returns Positive value, if invoking ZonedDateTime is latter-than (greater-than) the specified ZonedDateTime
- Returns Negative value, if invoking ZonedDateTime is earlier-than (lesser-than) the specified ZonedDateTime
CompareTwoZonedDateTimeUsingCompareToMethod.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class CompareTwoZonedDateTimeUsingCompareToMethod {
public static void main(String[] args) {
// 1. get Current Zoned Date/time
ZoneId zoneIdLocal = ZoneId.systemDefault();
ZonedDateTime currentZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdLocal);
System.out.println("1. Current Zoned Date/time is = " + currentZonedDateTime);
// 2. form Past Date/time
ZoneId zoneIdPast = ZoneId.of("Australia/Sydney");
ZonedDateTime pastZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdPast);
System.out.println("2. Past Zoned Date/time is = " + pastZonedDateTime);
// 3. form Future Date/time
ZoneId zoneIdFuture = ZoneId.of("America/Chicago");
ZonedDateTime futureZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdFuture);
System.out.println("3. Future Zoned Date/time is = " + futureZonedDateTime);
// 4. LocalDateTime comparison using compareTo() method
System.out.println("\n\n4. Comparison with compareTo() method :- \n");
// 4.1 check whether currentZonedDateTime Older-than futureZonedDateTime
int compareTo1 = currentZonedDateTime.compareTo(futureZonedDateTime);
System.out.println("4.1 Current Zoned Date/time (" + currentZonedDateTime
+ ") \n\t is Older-than Future Zoned Date/time (" + futureZonedDateTime + ") ? "
+ (compareTo1 < 0));
// 4.2 check whether currentZonedDateTime Latter-than futureZonedDateTime
int compareTo2 = currentZonedDateTime.compareTo(futureZonedDateTime);
System.out.println("4.2 Current Zoned Date/time (" + currentZonedDateTime
+ ") \n\t is Latter-than Future Zoned Date/time (" + futureZonedDateTime + ") ? "
+ (compareTo2 > 0));
// 4.3 check whether currentZonedDateTime Older-than pastZonedDateTime
int compareTo3 = currentZonedDateTime.compareTo(pastZonedDateTime);
System.out.println("4.3 Current Zoned Date/time (" + currentZonedDateTime
+ ") \n\t is Older than Past Zoned Date/time (" + pastZonedDateTime + ") ? "
+ (compareTo3 < 0));
// 4.4 check whether currentZonedDateTime Latter-than pastZonedDateTime
int compareTo4 = currentZonedDateTime.compareTo(pastZonedDateTime);
System.out.print("4.4 Current Zoned Date/time (" + currentZonedDateTime
+ ") \n\t is Latter-than Past Zoned Date/time (" + pastZonedDateTime + ") ? "
+ (compareTo4 > 0));
}
}
Output:
1. Current Zoned Date/time is = 2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]
2. Past Zoned Date/time is = 2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]
3. Future Zoned Date/time is = 2022-08-15T12:30:30.000000500-05:00[America/Chicago]
4. Comparison with compareTo() method :-
4.1 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
is Older-than Future Zoned Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago]) ? true
4.2 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
is Latter-than Future Zoned Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago]) ? false
4.3 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
is Older than Past Zoned Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]) ? false
4.4 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
is Latter-than Past Zoned Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]) ? true
2.2 Using isBefore(ChronoZonedDateTime) method :
- This method checks if invoking ZonedDateTime is before the specified ZonedDateTime
- Returns true, if invoking ZonedDateTime is before the specified ZonedDateTime
- Returns false, if invoking ZonedDateTime is not before the specified ZonedDateTime
- Read Java 8 – How to check whether a ZonedDateTime is Before another ZonedDateTime ? for more details and examples
CheckZonedDateTimeIsBeforeAnotherZonedDateTime.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class CheckZonedDateTimeIsBeforeAnotherZonedDateTime {
public static void main(String[] args) {
// 1. get Local Zoned Date/time
ZoneId zoneIdLocal = ZoneId.systemDefault();
ZonedDateTime currentZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdLocal);
System.out.println("1. Current Zoned Date/time is = " + currentZonedDateTime);
// 2. form Past Date/time
ZoneId zoneIdPast = ZoneId.of("Australia/Sydney");
ZonedDateTime pastZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdPast);
System.out.println("2. Past Zoned Date/time is = " + pastZonedDateTime);
// 3. form Future Date/time
ZoneId zoneIdFuture = ZoneId.of("America/Chicago");
ZonedDateTime futureZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdFuture);
System.out.println("3. Future Zoned Date/time is = " + futureZonedDateTime);
// 4. isBefore() - ZonedDateTime comparison
System.out.println("\n\n4. Comparing 2 ZonedDateTime using isBefore() method :- \n");
// 4.1 check whether currentZonedDateTime isBefore futureZonedDateTime
boolean isBefore1 = currentZonedDateTime.isBefore(futureZonedDateTime);
System.out.println("4.1 Current Zoned Date/time (" + currentZonedDateTime
+ ") \n\t\t is Before Future Zoned Date/time (" + futureZonedDateTime + ") ? "
+ isBefore1);
// 4.2 check whether currentZonedDateTime isBefore pastZonedDateTime
boolean isBefore2 = currentZonedDateTime.isBefore(pastZonedDateTime);
System.out.println("4.2 Current Zoned Date/time (" + currentZonedDateTime
+ ") \n\t\t is Before Past Zoned Date/time (" + pastZonedDateTime + ") ? "
+ isBefore2);
// 4.3 check whether pastZonedDateTime isBefore currentZonedDateTime
boolean isBefore3 = pastZonedDateTime.isBefore(currentZonedDateTime);
System.out.println("4.3 Past Zoned Date/time (" + pastZonedDateTime
+ ") \n\t\t is Before Current Zoned Date/time (" + currentZonedDateTime + ") ? "
+ isBefore3);
// 4.4 check whether pastZonedDateTime isBefore futureZonedDateTime
boolean isBefore4 = pastZonedDateTime.isBefore(futureZonedDateTime);
System.out.println("4.4 Past Zoned Date/time (" + pastZonedDateTime
+ ") \n\t\t is Before Future Zoned Date/time (" + futureZonedDateTime + ") ? "
+ isBefore4);
// 4.5 check whether futureZonedDateTime isBefore currentZonedDateTime
boolean isBefore5 = futureZonedDateTime.isBefore(currentZonedDateTime);
System.out.println("4.5 Future Zoned Date/time (" + futureZonedDateTime
+ ") \n\t\t is Before Current Zoned Date/time (" + currentZonedDateTime + ") ? "
+ isBefore5);
// 4.6 check whether futureZonedDateTime isBefore pastZonedDateTime
boolean isBefore6 = futureZonedDateTime.isBefore(currentZonedDateTime);
System.out.println("4.6 Future Zoned Date/time (" + futureZonedDateTime
+ ") \n\t\t is Before Past Zoned Date/time (" + pastZonedDateTime + ") ? "
+ isBefore6);
}
}
Output:
1. Current Zoned Date/time is = 2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]
2. Past Zoned Date/time is = 2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]
3. Future Zoned Date/time is = 2022-08-15T12:30:30.000000500-05:00[America/Chicago]
4. Comparing 2 ZonedDateTime using isBefore() method :-
4.1 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
is Before Future Zoned Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago]) ? true
4.2 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
is Before Past Zoned Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]) ? false
4.3 Past Zoned Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney])
is Before Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]) ? true
4.4 Past Zoned Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney])
is Before Future Zoned Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago]) ? true
4.5 Future Zoned Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago])
is Before Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]) ? false
4.6 Future Zoned Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago])
is Before Past Zoned Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]) ? false
2.3 Using isAfter(ChronoZonedDateTime) method :
- This method checks if invoking ZonedDateTime is after the specified ZonedDateTime
- Returns true, if invoking ZonedDateTime is after the specified ZonedDateTime
- Returns false, if invoking ZonedDateTime is not after the specified ZonedDateTime
- Read Java 8 – How to check whether a ZonedDateTime is After another ZonedDateTime ? for more details and examples
CheckZonedDateTimeIsAfterAnotherZonedDateTime.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class CheckZonedDateTimeIsAfterAnotherZonedDateTime {
public static void main(String[] args) {
// 1. get Current Zoned Date/time
ZoneId zoneIdLocal = ZoneId.systemDefault();
ZonedDateTime currentZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdLocal);
System.out.println("1. Current Zoned Date/time is = " + currentZonedDateTime);
// 2. form Past Date/time
ZoneId zoneIdPast = ZoneId.of("Australia/Sydney");
ZonedDateTime pastZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdPast);
System.out.println("2. Past Zoned Date/time is = " + pastZonedDateTime);
// 3. form Future Date/time
ZoneId zoneIdFuture = ZoneId.of("America/Chicago");
ZonedDateTime futureZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdFuture);
System.out.println("3. Future Zoned Date/time is = " + futureZonedDateTime);
// 4. isAfter() - LocalDateTime comparison
System.out.println("\n\n4. Comparing 2 LocalDateTime using isAfter() method :- \n");
// 4.1 check whether currentZonedDateTime isAfter futureZonedDateTime
boolean isAfter1 = currentZonedDateTime.isAfter(futureZonedDateTime);
System.out.println("4.1 Current Date/time (" + currentZonedDateTime
+ ") \n\t\t is After Future Date/time (" + futureZonedDateTime + ") ? "
+ isAfter1);
// 4.2 check whether currentZonedDateTime isAfter pastZonedDateTime
boolean isAfter2 = currentZonedDateTime.isAfter(pastZonedDateTime);
System.out.println("4.2 Current Date/time (" + currentZonedDateTime
+ ") \n\t\t is After Past Date/time (" + pastZonedDateTime + ") ? "
+ isAfter2);
// 4.3 check whether pastZonedDateTime isAfter currentZonedDateTime
boolean isAfter3 = pastZonedDateTime.isAfter(currentZonedDateTime);
System.out.println("4.3 Past Date/time (" + pastZonedDateTime
+ ") \n\t\t is After Current Date/time (" + currentZonedDateTime + ") ? "
+ isAfter3);
// 4.4 check whether pastZonedDateTime isAfter futureZonedDateTime
boolean isAfter4 = pastZonedDateTime.isAfter(futureZonedDateTime);
System.out.println("4.4 Past Date/time (" + pastZonedDateTime
+ ") \n\t\t is After Future Date/time (" + futureZonedDateTime + ") ? "
+ isAfter4);
// 4.5 check whether futureZonedDateTime isAfter currentZonedDateTime
boolean isAfter5 = futureZonedDateTime.isAfter(currentZonedDateTime);
System.out.println("4.5 Future Date/time (" + futureZonedDateTime
+ ") \n\t\t is After Current Date/time (" + currentZonedDateTime + ") ? "
+ isAfter5);
// 4.6 check whether futureZonedDateTime isAfter pastZonedDateTime
boolean isAfter6 = futureZonedDateTime.isAfter(currentZonedDateTime);
System.out.println("4.6 Future Date/time (" + futureZonedDateTime
+ ") \n\t\t is After Past Date/time (" + pastZonedDateTime + ") ? "
+ isAfter6);
}
}
Output:
1. Current Zoned Date/time is = 2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]
2. Past Zoned Date/time is = 2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]
3. Future Zoned Date/time is = 2022-08-15T12:30:30.000000500-05:00[America/Chicago]
4. Comparing 2 LocalDateTime using isAfter() method :-
4.1 Current Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
is After Future Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago]) ? false
4.2 Current Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
is After Past Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]) ? true
4.3 Past Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney])
is After Current Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]) ? false
4.4 Past Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney])
is After Future Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago]) ? false
4.5 Future Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago])
is After Current Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]) ? true
4.6 Future Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago])
is After Past Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]) ? true
2.4 Using isEqual(ChronoZonedDateTime) method :
- This method checks if invoking ZonedDateTime is equal to the specified ZonedDateTime
- Return true, only if both invoking ZonedDateTime & specified ZonedDateTime are same
- Return false, for all other cases
CheckTwoZonedDateTimeAreEqualUsingIsEqualMethod.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class CheckTwoZonedDateTimeAreEqualUsingIsEqualMethod {
public static void main(String[] args) {
// 1. get Current Zoned Date/time 1
ZoneId zoneIdLocal = ZoneId.systemDefault();
ZonedDateTime currentZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdLocal);
System.out.println("1. Current Zoned Date/time is = " + currentZonedDateTime);
// 2. get Current Zoned Date/time 2
ZonedDateTime currentZonedDateTime2 = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdLocal);
System.out.println("2. Current Zoned Date/time is = " + currentZonedDateTime2);
// 3. form Past Date/time
ZoneId zoneIdPast = ZoneId.of("Australia/Sydney");
ZonedDateTime pastZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdPast);
System.out.println("3. Past Zoned Date/time is = " + pastZonedDateTime);
// 4. form Future Date/time
ZoneId zoneIdFuture = ZoneId.of("America/Chicago");
ZonedDateTime futureZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdFuture);
System.out.println("4. Future Zoned Date/time is = " + futureZonedDateTime);
// 5. ZonedDateTime comparison using isEqual() method
System.out.println("\n\n5. Check 2 ZonedDateTime are equal using isEqual() method :- \n");
// 5.1 check whether currentZonedDateTime & currentZonedDateTime2 are same ?
boolean isEqual1 = currentZonedDateTime.isEqual(currentZonedDateTime2);
System.out.println("5.1 Current Zoned Date/time (" + currentZonedDateTime
+ ") \n\t & Current Zoned Date/time (" + currentZonedDateTime2 + ") are equal ?\t "
+ isEqual1);
// 5.2 check whether currentZonedDateTime & pastZonedDateTime are same ?
boolean isEqual2 = currentZonedDateTime.isEqual(pastZonedDateTime);
System.out.println("5.2 Current Zoned Date/time (" + currentZonedDateTime
+ ") \n\t & Past Zoned Date/time (" + pastZonedDateTime + ") are equal ?\t "
+ isEqual2);
// 5.3 check whether currentZonedDateTime & futureZonedDateTime are same ?
boolean isEqual3 = currentZonedDateTime.isEqual(futureZonedDateTime);
System.out.print("5.3 Current Zoned Date/time (" + currentZonedDateTime
+ ") \n\t & Future Zoned Date/time (" + futureZonedDateTime + ") are equal ?\t "
+ isEqual3);
}
}
Output:
1. Current Zoned Date/time is = 2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]
2. Current Zoned Date/time is = 2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]
3. Past Zoned Date/time is = 2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]
4. Future Zoned Date/time is = 2022-08-15T12:30:30.000000500-05:00[America/Chicago]
5. Check 2 ZonedDateTime are equal using isEqual() method :-
5.1 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
& Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]) are equal ? true
5.2 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
& Past Zoned Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]) are equal ? false
5.3 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
& Future Zoned Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago]) are equal ? false
2.5 Using equals(Object) method :
- This method checks if invoking ZonedDateTime is equal to the another ZonedDateTime
- Return true, only if both invoking ZonedDateTime & specified ZonedDateTime are same
- Return false, for all other cases
- Note: this illustration very much similar to above 2.4 except that it uses equals(Object) method for comparison
CheckTwoZonedDateTimeAreEqualUsingEqualsMethod.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class CheckTwoZonedDateTimeAreEqualUsingEqualsMethod {
public static void main(String[] args) {
// 1. get Current Zoned Date/time 1
ZoneId zoneIdLocal = ZoneId.systemDefault();
ZonedDateTime currentZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdLocal);
System.out.println("1. Current Zoned Date/time is = " + currentZonedDateTime);
// 2. get Current Zoned Date/time 2
ZonedDateTime currentZonedDateTime2 = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdLocal);
System.out.println("2. Current Zoned Date/time is = " + currentZonedDateTime2);
// 3. form Past Date/time
ZoneId zoneIdPast = ZoneId.of("Australia/Sydney");
ZonedDateTime pastZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdPast);
System.out.println("3. Past Zoned Date/time is = " + pastZonedDateTime);
// 4. form Future Date/time
ZoneId zoneIdFuture = ZoneId.of("America/Chicago");
ZonedDateTime futureZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdFuture);
System.out.println("4. Future Zoned Date/time is = " + futureZonedDateTime);
// 5. ZonedDateTime comparison using isEqual() method
System.out.println("\n\n5. Check two ZonedDateTime are equal using isEqual() method :- \n");
// 5.1 check whether currentZonedDateTime & currentZonedDateTime2 are same ?
boolean equals1 = currentZonedDateTime.equals(currentZonedDateTime2);
System.out.println("5.1 Current Zoned Date/time (" + currentZonedDateTime
+ ") \n\t & Current Zoned Date/time (" + currentZonedDateTime2 + ") are equal ?\t "
+ equals1);
// 5.2 check whether currentZonedDateTime & pastZonedDateTime are same ?
boolean equals2 = currentZonedDateTime.equals(pastZonedDateTime);
System.out.println("5.2 Current Zoned Date/time (" + currentZonedDateTime
+ ") \n\t & Past Zoned Date/time (" + pastZonedDateTime + ") are equal ?\t "
+ equals2);
// 5.3 check whether currentZonedDateTime & futureZonedDateTime are same ?
boolean equals3 = currentZonedDateTime.equals(futureZonedDateTime);
System.out.print("5.3 Current Zoned Date/time (" + currentZonedDateTime
+ ") \n\t & Future Zoned Date/time (" + futureZonedDateTime + ") are equal ?\t "
+ equals3);
}
}
Output:
1. Current Zoned Date/time is = 2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]
2. Current Zoned Date/time is = 2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]
3. Past Zoned Date/time is = 2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]
4. Future Zoned Date/time is = 2022-08-15T12:30:30.000000500-05:00[America/Chicago]
5. Check two ZonedDateTime are equal using isEqual() method :-
5.1 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
& Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]) are equal ? true
5.2 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
& Past Zoned Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]) are equal ? false
5.3 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
& Future Zoned Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago]) are equal ? false
Related Articles:
- Java 8 – ZonedDateTime with method details and examples
- Java 8 – How to get Date, Time and Zone fields from ZonedDateTime ?
- Java 8 – How to form ZonedDateTime passing Date, Time and Zone fields ?
- Java 8 – How to form ZonedDateTime passing LocalDate, LocalTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing LocalDateTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing Instant and ZoneId ?
- Java 8 – How to parse ZonedDateTime in String form ?
- Java 8 – How to convert String to ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to String ?
- Java 8 – How to convert ZonedDateTime in different formats ?
- Java 8 – How to convert ZonedDateTime in different Format Style ?
- Java 8 – How to convert ZonedDateTime to LocalDateTime ?
- Java 8 – How to convert ZonedDateTime to an OffsetDateTime ?
- Java 8 – How to convert ZonedDateTime to an Instant ?
- Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert ZonedDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert ZonedDateTime to Calendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime in different ways ?
- Java 8 – How to add Date and Time fields to ZonedDateTime ?
- Java 8 – How to subtract Date and Time fields from ZonedDateTime ?
- Java 8 – How to alter Date, Time and Zone fields of ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is Before another ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is After another ZonedDateTime ?
- Java 8 – How to compare two ZonedDateTime instances ?
- Java 8 – How to find difference between two ZonedDateTime using Period & Duration ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/time/chrono/ChronoZonedDateTime.html
Happy Coding !!
Happy Learning !!