In this article, we will learn how to check whether one OffsetDateTime is After another OffsetDateTime using OffsetDateTime.isAfter() method provided in Java 1.8 version
Compare two OffsetDateTime using isAfter() method :
- If there are two OffsetDateTime and the requirement is to check whether one OffsetDateTime is after another OffsetDateTime, then isAfter() method can be used
- isAfter((OffsetDateTime) – checks if the invoking OffsetDateTime is after the specified OffsetDateTime
- Comparison results –
- Returns true, if invoking OffsetDateTime is after the specified OffsetDateTime
- Returns false, if invoking OffsetDateTime is not after the specified OffsetDateTime
- Always returns false for self comparing OffsetDateTime
- Lets see an example for comparing two OffsetDateTime using isAfter() method
CheckOffsetDateTimeIsAfterAnotherOffsetDateTime.java
package in.bench.resources.java8.offsetdatetime.examples;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class CheckOffsetDateTimeIsAfterAnotherOffsetDateTime {
public static void main(String[] args) {
// 1. get Local Offset Date/time
ZoneOffset zoneOffsetLocal = ZoneOffset.of("+05:30");
OffsetDateTime currentOffsetDateTime = OffsetDateTime
.of(2022, 8, 15, 12, 30, 30, 500, zoneOffsetLocal);
System.out.println("1. Current Offset Date/time is = " + currentOffsetDateTime);
// 2. form Past Offset Date/time
ZoneOffset zoneOffsetPast = ZoneOffset.of("+10:30");
OffsetDateTime pastOffsetDateTime = OffsetDateTime
.of(2022, 8, 15, 12, 30, 30, 500, zoneOffsetPast);
System.out.println("2. Past Offset Date/time is = " + pastOffsetDateTime);
// 3. form Future Offset Date/time
ZoneOffset zoneOffsetFuture = ZoneOffset.of("-09:30");
OffsetDateTime futureOffsetDateTime = OffsetDateTime
.of(2022, 8, 15, 12, 30, 30, 500, zoneOffsetFuture);
System.out.println("3. Future Offset Date/time is = " + futureOffsetDateTime);
// 4. isAfter() - LocalDateTime comparison
System.out.println("\n\n4. Comparing 2 LocalDateTime using isAfter() method :- \n");
// 4.1 check whether currentOffsetDateTime isAfter futureOffsetDateTime
boolean isAfter1 = currentOffsetDateTime.isAfter(futureOffsetDateTime);
System.out.println("4.1 Current Offset Date/time (" + currentOffsetDateTime
+ ") \n\t\t is After Future Offset Date/time (" + futureOffsetDateTime + ") ? "
+ isAfter1);
// 4.2 check whether currentOffsetDateTime isAfter pastOffsetDateTime
boolean isAfter2 = currentOffsetDateTime.isAfter(pastOffsetDateTime);
System.out.println("4.2 Current Offset Date/time (" + currentOffsetDateTime
+ ") \n\t\t is After Past Offset Date/time (" + pastOffsetDateTime + ") ? "
+ isAfter2);
// 4.3 check whether pastOffsetDateTime isAfter currentOffsetDateTime
boolean isAfter3 = pastOffsetDateTime.isAfter(currentOffsetDateTime);
System.out.println("4.3 Past Offset Date/time (" + pastOffsetDateTime
+ ") \n\t\t is After Offset Current Date/time (" + currentOffsetDateTime + ") ? "
+ isAfter3);
// 4.4 check whether pastOffsetDateTime isAfter futureOffsetDateTime
boolean isAfter4 = pastOffsetDateTime.isAfter(futureOffsetDateTime);
System.out.println("4.4 Past Offset Date/time (" + pastOffsetDateTime
+ ") \n\t\t is After Future Offset Date/time (" + futureOffsetDateTime + ") ? "
+ isAfter4);
// 4.5 check whether futureOffsetDateTime isAfter currentOffsetDateTime
boolean isAfter5 = futureOffsetDateTime.isAfter(currentOffsetDateTime);
System.out.println("4.5 Future Offset Date/time (" + futureOffsetDateTime
+ ") \n\t\t is After Current Offset Date/time (" + currentOffsetDateTime + ") ? "
+ isAfter5);
// 4.6 check whether futureOffsetDateTime isAfter pastOffsetDateTime
boolean isAfter6 = futureOffsetDateTime.isAfter(currentOffsetDateTime);
System.out.println("4.6 Future Offset Date/time (" + futureOffsetDateTime
+ ") \n\t\t is After Past Offset Date/time (" + pastOffsetDateTime + ") ? "
+ isAfter6);
// 5. isAfter() - self LocalDateTime comparison
System.out.println("\n\n5. Self comparing LocalDateTime using isAfter() method :- \n");
// 5.1 check whether currentOffsetDateTime isAfter currentOffsetDateTime
boolean isAfter7 = currentOffsetDateTime.isAfter(currentOffsetDateTime);
System.out.println("5.1 Current Offset Date/time (" + currentOffsetDateTime
+ ") \n\t\t is After Current Offset Date/time (" + currentOffsetDateTime + ") ? "
+ isAfter7);
// 5.2 check whether pastOffsetDateTime isAfter pastOffsetDateTime
boolean isAfter8 = pastOffsetDateTime.isAfter(pastOffsetDateTime);
System.out.println("5.2 Past Offset Date/time (" + pastOffsetDateTime
+ ") \n\t\t is After Past Offset Date/time (" + pastOffsetDateTime + ") ? "
+ isAfter8);
// 5.3 check whether futureOffsetDateTime isAfter futureOffsetDateTime
boolean isAfter9 = futureOffsetDateTime.isAfter(futureOffsetDateTime);
System.out.print("5.3 Future Offset Date/time (" + futureOffsetDateTime
+ ") \n\t\t is After Future Offset Date/time (" + futureOffsetDateTime + ") ? "
+ isAfter9);
}
}
Output:
1. Current Offset Date/time is = 2022-08-15T12:30:30.000000500+05:30
2. Past Offset Date/time is = 2022-08-15T12:30:30.000000500+10:30
3. Future Offset Date/time is = 2022-08-15T12:30:30.000000500-09:30
4. Comparing 2 LocalDateTime using isAfter() method :-
4.1 Current Offset Date/time (2022-08-15T12:30:30.000000500+05:30)
is After Future Offset Date/time (2022-08-15T12:30:30.000000500-09:30) ? false
4.2 Current Offset Date/time (2022-08-15T12:30:30.000000500+05:30)
is After Past Offset Date/time (2022-08-15T12:30:30.000000500+10:30) ? true
4.3 Past Offset Date/time (2022-08-15T12:30:30.000000500+10:30)
is After Offset Current Date/time (2022-08-15T12:30:30.000000500+05:30) ? false
4.4 Past Offset Date/time (2022-08-15T12:30:30.000000500+10:30)
is After Future Offset Date/time (2022-08-15T12:30:30.000000500-09:30) ? false
4.5 Future Offset Date/time (2022-08-15T12:30:30.000000500-09:30)
is After Current Offset Date/time (2022-08-15T12:30:30.000000500+05:30) ? true
4.6 Future Offset Date/time (2022-08-15T12:30:30.000000500-09:30)
is After Past Offset Date/time (2022-08-15T12:30:30.000000500+10:30) ? true
5. Self comparing LocalDateTime using isAfter() method :-
5.1 Current Offset Date/time (2022-08-15T12:30:30.000000500+05:30)
is After Current Offset Date/time (2022-08-15T12:30:30.000000500+05:30) ? false
5.2 Past Offset Date/time (2022-08-15T12:30:30.000000500+10:30)
is After Past Offset Date/time (2022-08-15T12:30:30.000000500+10:30) ? false
5.3 Future Offset Date/time (2022-08-15T12:30:30.000000500-09:30)
is After Future Offset Date/time (2022-08-15T12:30:30.000000500-09:30) ? false
Related Articles:
- Java 8 – OffsetDateTime with method details and examples
- Java 8 – How to get Date, Time and Offset fields from OffsetDateTime ?
- Java 8 – How to form OffsetDateTime passing Date, Time and Offset fields ?
- Java 8 – How to form OffsetDateTime passing LocalDate, LocalTime and ZoneOffset ?
- Java 8 – How to form OffsetDateTime passing LocalDateTime and ZoneOffset ?
- Java 8 – How to form OffsetDateTime passing Instant and ZoneId ?
- Java 8 – How to parse OffsetDateTime in String form ?
- Java 8 – How to convert String to OffsetDateTime ?
- Java 8 – How to convert ZonedDateTime to String ?
- Java 8 – How to convert OffsetDateTime in different formats ?
- Java 8 – How to convert OffsetDateTime in different Format Style ?
- Java 8 – How to convert OffsetDateTime to LocalDateTime ?
- Java 8 – How to convert OffsetDateTime to ZonedDateTime ?
- Java 8 – How to convert OffsetDateTime to an Instant ?
- Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from OffsetDateTime ?
- Java 8 – How to extract OffsetTime from OffsetDateTime ?
- Java 8 – How to convert OffsetDateTime to number of Seconds ?
- Java 8 – How to convert OffsetDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert OffsetDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert OffsetDateTime to Calendar and vice-versa ?
- Java 8 – How to convert OffsetDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert OffsetDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to an OffsetDateTime in different ways ?
- Java 8 – How to add Date and Time fields to OffsetDateTime ?
- Java 8 – How to subtract Date and Time fields from OffsetDateTime ?
- Java 8 – How to alter Date, Time and Offset fields of OffsetDateTime ?
- Java 8 – How to check whether an OffsetDateTime is Before another OffsetDateTime ?
- Java 8 – How to check whether an OffsetDateTime is After another OffsetDateTime ?
- Java 8 – How to compare two OffsetDateTime instances ?
- Java 8 – How to find difference between two OffsetDateTime using Period & Duration ?
- Java 9 – Find difference between two OffsetDateTime instances upto nanosecond precision ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
Happy Coding !!
Happy Learning !!