Java 8 – How to compare two LocalTime instances ?

In this article, we will discuss different ways to compare two instances of LocalTime using different methods of LocalTime provided in Java 1.8 version

1. Comparing two LocalTime instances :

There are different methods available to compare 2 LocalTime instances, those are –

  1. compareTo(LocalTime) –
    • This method compares invoking LocalTime with another LocalTime
  2. isBefore(LocalTime) –
    • This method checks if invoking LocalTime is before the specified LocalTime
  3. isAfter(LocalTime) –
    • This method checks if invoking LocalTime is after the specified LocalTime
  4. equals(Object) –
    • This method checks if invoking LocalTime is equal to another LocalTime

2. Examples for Comparing 2 LocalTime instances :

  1. Using compareTo(LocalTime) method
  2. Using isBefore(LocalTime) method
  3. Using isAfter(LocalTime) method
  4. Using equals(Object) method

2.1 Using compareTo(LocalTime) method :

  • This method compares invoking LocalTime with another LocalTime and returns integer value based on the comparison
    1. Returns Zero, if both LocalTime in comparison are equal
    2. Returns Positive value, if invoking LocalTime is latter-than (greater-than) the specified LocalTime
    3. Returns Negative value, if invoking LocalTime is earlier-than (lesser-than) the specified LocalTime

Compare2LocalTimeUsingCompareToMethod.java

package in.bench.resources.java8.localtime.examples;

import java.time.LocalTime;

public class Compare2LocalTimeUsingCompareToMethod {

	public static void main(String[] args) {

		// 1. get current system time
		LocalTime currentTime = LocalTime.of(17, 29, 59, 999);
		System.out.println("1. Current System Time = " + currentTime);


		// 2. form past LocalTime
		LocalTime pastTime = LocalTime.MIDNIGHT;
		System.out.println("2. Past LocalTime is = " + pastTime);


		// 3. form future LocalTime
		LocalTime lastTime = LocalTime.MAX;
		System.out.println("3. Future LocalTime is = " + lastTime);


		// 4. LocalTime comparison using compareTo() method
		System.out.println("\n\n4. Comparison with compareTo() method :- \n");


		// 4.1 check whether currentTime Older-than lastTime
		int compareTo1 = currentTime.compareTo(lastTime);
		System.out.println("4.1 Current Time (" + currentTime 
				+ ") is Earlier-than Future Time (" + lastTime + ") ?\t " 
				+ (compareTo1 < 0));


		// 4.2 check whether currentTime Latter-than lastTime
		int compareTo2 = currentTime.compareTo(lastTime);
		System.out.println("4.2 Current Time (" + currentTime 
				+ ") is Latter-than Future Time (" + lastTime + ") ?\t " 
				+ (compareTo2 > 0));


		// 4.3 check whether currentTime Older-than pastTime
		int compareTo3 = currentTime.compareTo(pastTime);
		System.out.println("4.3 Current Time (" + currentTime 
				+ ") is Earlier-than Past Time (" + pastTime + ") ?\t " 
				+ (compareTo3 < 0));


		// 4.4 check whether currentTime Latter-than pastTime
		int compareTo4 = currentTime.compareTo(pastTime);
		System.out.print("4.4 Current Time (" + currentTime 
				+ ") is Latter-than Past Time (" + pastTime + ") ?\t " 
				+ (compareTo4 > 0));
	}
}

Output:

1. Current System Time = 17:29:59.000000999
2. Past LocalTime is = 00:00
3. Future LocalTime is = 23:59:59.999999999


4. Comparison with compareTo() method :- 

4.1 Current Time (17:29:59.000000999) is Earlier-than Future Time (23:59:59.999999999) ?	 true
4.2 Current Time (17:29:59.000000999) is Latter-than Future Time (23:59:59.999999999) ?	 false
4.3 Current Time (17:29:59.000000999) is Earlier-than Past Time (00:00) ?	 false
4.4 Current Time (17:29:59.000000999) is Latter-than Past Time (00:00) ?	 true

2.2 Using isBefore(LocalTime) method :

CheckLocalTimeIsBeforeAnotherLocalTime.java

package in.bench.resources.java8.localtime.examples;

import java.time.LocalTime;

public class CheckLocalTimeIsBeforeAnotherLocalTime {

	public static void main(String[] args) {

		// 1. get current system time
		LocalTime currentTime = LocalTime.of(17, 29, 59, 999);
		System.out.println("1. Current System Time = " + currentTime);


		// 2. form past LocalTime
		LocalTime pastTime = LocalTime.MIDNIGHT;
		System.out.println("2. Past LocalTime is = " + pastTime);


		// 3. form future LocalTime
		LocalTime lastTime = LocalTime.MAX;
		System.out.println("3. Future LocalTime is = " + lastTime);


		// 4. isBefore() - LocalTime comparison
		System.out.println("\n\n4. Comparing 2 LocalTime using isBefore() method :- \n");


		// 4.1 check whether currentTime isBefore lastTime
		boolean isBefore1 = currentTime.isBefore(lastTime);
		System.out.println("4.1 Current LocalTime (" + currentTime 
				+ ") is Before Future LocalTime (" + lastTime + ") ? "
				+ isBefore1);


		// 4.2 check whether currentTime isBefore pastTime
		boolean isBefore2 = currentTime.isBefore(pastTime);
		System.out.println("4.2 Current LocalTime (" + currentTime 
				+ ") is Before Past LocalTime (" + pastTime + ") ? "
				+ isBefore2);


		// 4.3 check whether pastTime isBefore currentTime 
		boolean isBefore3 = pastTime.isBefore(currentTime);
		System.out.println("4.3 Past LocalTime (" + pastTime 
				+ ") is Before Current LocalTime (" + currentTime + ") ? "
				+ isBefore3);


		// 4.4 check whether pastTime isBefore lastTime 
		boolean isBefore4 = pastTime.isBefore(lastTime);
		System.out.println("4.4 Past LocalTime (" + pastTime 
				+ ") is Before Future LocalTime (" + lastTime + ") ? "
				+ isBefore4);


		// 4.5 check whether lastTime isBefore currentTime 
		boolean isBefore5 = lastTime.isBefore(currentTime);
		System.out.println("4.5 Future LocalTime (" + lastTime 
				+ ") is Before Current LocalTime (" + currentTime + ") ? "
				+ isBefore5);


		// 4.6 check whether lastTime isBefore pastTime 
		boolean isBefore6 = lastTime.isBefore(pastTime);
		System.out.print("4.6 Future LocalTime (" + lastTime 
				+ ") is Before Past LocalTime (" + pastTime + ") ? "
				+ isBefore6);
	}
}

Output:

1. Current System Time = 17:29:59.000000999
2. Past LocalTime is = 00:00
3. Future LocalTime is = 23:59:59.999999999


4. Comparing 2 LocalTime using isBefore() method :- 

4.1 Current LocalTime (17:29:59.000000999) is Before Future LocalTime (23:59:59.999999999) ? true
4.2 Current LocalTime (17:29:59.000000999) is Before Past LocalTime (00:00) ? false
4.3 Past LocalTime (00:00) is Before Current LocalTime (17:29:59.000000999) ? true
4.4 Past LocalTime (00:00) is Before Future LocalTime (23:59:59.999999999) ? true
4.5 Future LocalTime (23:59:59.999999999) is Before Current LocalTime (17:29:59.000000999) ? false
4.6 Future LocalTime (23:59:59.999999999) is Before Past LocalTime (00:00) ? false

2.3 Using isAfter(LocalTime) method :

CheckLocalTimeIsAfterAnotherLocalTime.java

package in.bench.resources.java8.localtime.examples;

import java.time.LocalTime;

public class CheckLocalTimeIsAfterAnotherLocalTime {

	public static void main(String[] args) {

		// 1. get current system time
		LocalTime currentTime = LocalTime.of(17, 29, 59, 999);
		System.out.println("1. Current System Time = " + currentTime);


		// 2. form past LocalTime
		LocalTime pastTime = LocalTime.MIDNIGHT;
		System.out.println("2. Past LocalTime is = " + pastTime);


		// 3. form future LocalTime
		LocalTime lastTime = LocalTime.MAX;
		System.out.println("3. Future LocalTime is = " + lastTime);


		// 4. isAfter() - LocalTime comparison
		System.out.println("\n\n4. Comparing 2 LocalTime using isAfter() method :- \n");


		// 4.1 check whether currentTime isAfter lastTime
		boolean isAfter1 = currentTime.isAfter(lastTime);
		System.out.println("4.1 Current LocalTime (" + currentTime 
				+ ") is After Future LocalTime (" + lastTime + ") ? "
				+ isAfter1);


		// 4.2 check whether currentTime isAfter pastTime
		boolean isAfter2 = currentTime.isAfter(pastTime);
		System.out.println("4.2 Current LocalTime (" + currentTime 
				+ ") is After Past LocalTime (" + pastTime + ") ? "
				+ isAfter2);


		// 4.3 check whether pastTime isAfter currentTime 
		boolean isAfter3 = pastTime.isAfter(currentTime);
		System.out.println("4.3 Past LocalTime (" + pastTime 
				+ ") is After Current LocalTime (" + currentTime + ") ? "
				+ isAfter3);


		// 4.4 check whether pastTime isAfter lastTime 
		boolean isAfter4 = pastTime.isAfter(lastTime);
		System.out.println("4.4 Past LocalTime (" + pastTime 
				+ ") is After Future LocalTime (" + lastTime + ") ? "
				+ isAfter4);


		// 4.5 check whether lastTime isAfter currentTime 
		boolean isAfter5 = lastTime.isAfter(currentTime);
		System.out.println("4.5 Future LocalTime (" + lastTime 
				+ ") is After Current LocalTime (" + currentTime + ") ? "
				+ isAfter5);


		// 4.6 check whether lastTime isAfter pastTime 
		boolean isAfter6 = lastTime.isAfter(currentTime);
		System.out.print("4.6 Future LocalTime (" + lastTime 
				+ ") is After Past LocalTime (" + pastTime + ") ? "
				+ isAfter6);
	}
}

Output:

1. Current System Time = 17:29:59.000000999
2. Past LocalTime is = 00:00
3. Future LocalTime is = 23:59:59.999999999


4. Comparing 2 LocalTime using isAfter() method :- 

4.1 Current LocalTime (17:29:59.000000999) is After Future LocalTime (23:59:59.999999999) ? false
4.2 Current LocalTime (17:29:59.000000999) is After Past LocalTime (00:00) ? true
4.3 Past LocalTime (00:00) is After Current LocalTime (17:29:59.000000999) ? false
4.4 Past LocalTime (00:00) is After Future LocalTime (23:59:59.999999999) ? false
4.5 Future LocalTime (23:59:59.999999999) is After Current LocalTime (17:29:59.000000999) ? true
4.6 Future LocalTime (23:59:59.999999999) is After Past LocalTime (00:00) ? true

2.4 Using equals(Object) method :

  • This method checks if invoking LocalTime is equal to another LocalTime
    1. Return true, only if both invoking LocalTime & specified LocalTime are same
    2. Return false, for all other cases

CheckTwoLocalTimeAreEqualUsingEqualsMethod.java

package in.bench.resources.java8.localtime.examples;

import java.time.LocalTime;

public class CheckTwoLocalTimeAreEqualUsingEqualsMethod {

	public static void main(String[] args) {

		// 1. get current system time
		LocalTime currentTime = LocalTime.of(17, 29, 59, 999);
		System.out.println("1. Current Time is = " + currentTime);


		// 2. get system time
		LocalTime systemTime = LocalTime.of(17, 29, 59, 999);
		System.out.println("2. System Time is = " + systemTime);


		// 3. form past LocalTime
		LocalTime pastTime = LocalTime.MIDNIGHT;
		System.out.println("3. Past Time is = " + pastTime);


		// 4. form future LocalTime
		LocalTime lastTime = LocalTime.MAX;
		System.out.println("4. Future Time is = " + lastTime);


		// 5. LocalTime comparison using isEqual() method
		System.out.println("\n\n5. Check 2 LocalTime are equal using equals() method :- \n");


		// 5.1 check whether currentTime & systemTime are same ?
		boolean equals1 = currentTime.equals(systemTime);
		System.out.println("5.1 Current Time (" + currentTime 
				+ ") & System Time (" + systemTime + ") are equal ?\t "
				+ equals1);


		// 5.2 check whether currentTime & pastTime are same ?
		boolean equals2 = currentTime.equals(pastTime);
		System.out.println("5.2 Current Time (" + currentTime 
				+ ") & Past Time (" + pastTime + ") are equal ?\t "
				+ equals2);


		// 5.3 check whether currentTime & lastTime are same ?
		boolean equals3 = currentTime.equals(lastTime);
		System.out.print("5.3 Current Time (" + currentTime 
				+ ") & Future Time (" + lastTime + ") are equal ?\t "
				+ equals3);
	}
}

Output:

1. Current Time is = 17:29:59.000000999
2. System Time is = 17:29:59.000000999
3. Past Time is = 00:00
4. Future Time is = 23:59:59.999999999


5. Check 2 LocalTime are equal using equals() method :- 

5.1 Current Time (17:29:59.000000999) & System Time (17:29:59.000000999) are equal ?	 true
5.2 Current Time (17:29:59.000000999) & Past Time (00:00) are equal ?	 false
5.3 Current Time (17:29:59.000000999) & Future Time (23:59:59.999999999) are equal ?	 false

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to find time duration between two LocalTime instances ?
Java 8 – How to check whether a LocalTime is After another LocalTime ?