The answer by Basil Bourque is correct and the recommended solution. You can use this answer as a supplement to his answer.
java.time
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Solution using java.time
, the modern Date-Time API:
The modern Date-Time API is based on ISO 8601 and does not require using a DateTimeFormatter
object explicitly as long as the Date-Time string conforms to the ISO 8601 standards. Both of your Date-Time strings conform to the ISO 8601 format for Date-Time with timezone offset and therefore it makes perfect sense to parse them directly using OffsetDateTime
. However, Java SE 12 onwards, you can parse them directly to even Instant
.
Demo:
import java.time.Instant;
public class Main {
public static void main(String[] args) {
String s1 = "2021-08-07T03:00:01-07:00";
String s2 = "2021-08-07T15:30:00+05:30";
Instant instant1 = Instant.parse(s1);
Instant instant2 = Instant.parse(s2);
System.out.println(s1 + " represents " + instant1);
System.out.println(s1 + " represents " + instant2);
if (instant1.isBefore(instant2)) {
System.out.println(s1 + " is before " + s2);
} else if (instant1.isAfter(instant2)) {
System.out.println(s1 + " is after " + s2);
} else {
System.out.println("The strings represent the same instants.");
}
}
}
Output:
2021-08-07T03:00:01-07:00 represents 2021-08-07T10:00:01Z
2021-08-07T03:00:01-07:00 represents 2021-08-07T10:00:00Z
2021-08-07T03:00:01-07:00 is after 2021-08-07T15:30:00+05:30
ONLINE DEMO
An Instant
represents an instantaneous point on the timeline, normally represented in UTC time. The Z
in the output is the timezone designator for a zero-timezone offset. It stands for Zulu and specifies the Etc/UTC
timezone (which has the timezone offset of +00:00
hours).
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.