In this article, we will learn how to check whether one LocalTime is After another LocalTime using LocalTime.isBefore() method provided in Java 1.8 version
Compare 2 LocalTime using isAfter() method :
- If there are 2 LocalTime and the requirement is to check whether one LocalTime is after another LocalTime, then isAfter() method can be used
- isAfter(LocalTime) – checks if the invoking LocalTime is after the specified LocalTime
- Lets see an example for comparing 2 LocalTime using isAfter() method
- Note: Self comparing LocalTime using isAfter() method always returns false, as shown in the below illustration
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.println("4.6 Future LocalTime (" + lastTime
+ ") is After Past LocalTime (" + pastTime + ") ? "
+ isAfter6);
// 5. isAfter() - self LocalTime comparison
System.out.println("\n\n5. Self comparing LocalTime using isAfter() method :- \n");
// 5.1 check whether currentTime isAfter currentTime
boolean isAfter7 = currentTime.isAfter(currentTime);
System.out.println("5.1 Current LocalTime (" + currentTime
+ ") is After Current LocalTime (" + currentTime + ") ? "
+ isAfter7);
// 5.2 check whether pastTime isAfter pastTime
boolean isAfter8 = pastTime.isAfter(pastTime);
System.out.println("5.2 Past LocalTime (" + pastTime
+ ") is After Past LocalTime (" + pastTime + ") ? "
+ isAfter8);
// 5.3 check whether pastTime isAfter pastTime
boolean isAfter9 = pastTime.isAfter(pastTime);
System.out.print("5.3 Future LocalTime (" + pastTime
+ ") is After Future LocalTime (" + pastTime + ") ? "
+ isAfter9);
}
}
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
5. Self comparing LocalTime using isAfter() method :-
5.1 Current LocalTime (17:29:59.000000999) is After Current LocalTime (17:29:59.000000999) ? false
5.2 Past LocalTime (00:00) is After Past LocalTime (00:00) ? false
5.3 Future LocalTime (00:00) is After Future LocalTime (00:00) ? false
Related Articles:
- Java 8 – LocalTime with method details and examples
- Java 8 – How to get Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?
- Java 8 – How to parse LocalTime in String form ?
- Java 8 – How to convert String to LocalTime ?
- Java 8 – How to convert LocalTime to String ?
- Java 8 – How to convert LocalTime in different formats ?
- Java 8 – How to convert LocalTime in different Format Style ?
- Java 8 – How to convert LocalTime to LocalDateTime ?
- Java 8 – How to convert LocalTime to ZonedDateTime ?
- Java 8 – How to convert LocalTime to an OffsetDateTime ?
- Java 8 – How to convert LocalTime to an Instant ?
- Java 8 – How to convert LocalTime to an OffsetTime ?
- Java 8 – How to convert LocalTime to Seconds and vice-versa ?
- Java 8 – How to convert LocalTime to Nanoseconds and vice-versa ?
- Java 8 – How to convert LocalTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime in different ways ?
- Java 8 – How to add Hour, Minute and Second fields to LocalTime ?
- Java 8 – How to subtract Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?
- Java 8 – How to check whether a LocalTime is Before another LocalTime ?
- Java 8 – How to check whether a LocalTime is After another LocalTime ?
- Java 8 – How to compare two LocalTime instances ?
- Java 8 – How to find time duration between two LocalTime instances ?
- Java 8 – What are all the Temporal Fields supported by LocalTime ?
- Java 8 – What are all the Temporal Units supported by LocalTime ?
- Java 9 – Find difference between two LocalTime instances upto nanosecond precision ?
- More Java 8 Date/Time API examples
References:
Happy Coding !!
Happy Learning !!