Java 8 – How to check whether a ZonedDateTime is Before another ZonedDateTime ?

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 –
    1. Returns true, if invoking ZonedDateTime is before the specified ZonedDateTime
    2. Returns false, if invoking ZonedDateTime is not before the specified ZonedDateTime
    3. 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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to check whether a ZonedDateTime is After another ZonedDateTime ?
Java 8 – How to alter Date, Time and Zone fields of ZonedDateTime ?