In this article, we will learn how to check whether one ZonedDateTime is Before another ZonedDateTime using ZonedDateTime.isBefore() method in Java 1.8 version
Compare two ZonedDateTime using isBefore() method :
- If there are two ZonedDateTime and the requirement is to check whether one ZonedDateTime is before another ZonedDateTime, then isBefore() method can be used
- isBefore(ChronoZonedDateTime) – checks if the invoking ZonedDateTime is before the specified ZonedDateTime
- Comparison results –
- Returns true, if invoking ZonedDateTime is before the specified ZonedDateTime
- Returns false, if invoking ZonedDateTime is not before the specified ZonedDateTime
- Always returns false for self comparing ZonedDateTime
- Lets see an example for comparing two ZonedDateTime using isBefore() method
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);
// 5. isBefore() - self ZonedDateTime comparison
System.out.println("\n\n5. Self comparing ZonedDateTime using isBefore() method :- \n");
// 5.1 check whether todaysLocalDate isBefore todaysLocalDate
boolean isBefore7 = currentZonedDateTime.isBefore(currentZonedDateTime);
System.out.println("5.1 Current Zoned Date/time (" + currentZonedDateTime
+ ") \n\t\t is Before Current Zoned Date/time (" + currentZonedDateTime + ") ? "
+ isBefore7);
// 5.2 check whether pastLocalDate isBefore pastLocalDate
boolean isBefore8 = pastZonedDateTime.isBefore(pastZonedDateTime);
System.out.println("5.2 Past Zoned Date/time (" + pastZonedDateTime
+ ") \n\t\t is Before Past Zoned Date/time (" + pastZonedDateTime + ") ? "
+ isBefore8);
// 5.3 check whether futureLocalDate isBefore futureLocalDate
boolean isBefore9 = futureZonedDateTime.isBefore(futureZonedDateTime);
System.out.print("5.3 Future Zoned Date/time (" + futureZonedDateTime
+ ") \n\t\t is Before Zoned Future Date/time (" + futureZonedDateTime + ") ? "
+ isBefore9);
}
}
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
5. Self comparing ZonedDateTime using isBefore() method :-
5.1 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta])
is Before Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]) ? false
5.2 Past Zoned Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney])
is Before Past Zoned Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]) ? false
5.3 Future Zoned Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago])
is Before Zoned Future Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago]) ? 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/ChronoLocalDateTime.html
Happy Coding !!
Happy Learning !!