如何比较Java中的两个UTC时间戳?

Nit*_*tal 3 java timestamp utc

我正在使用以下方法生成 UTC 时间戳,并希望将其与另一个进行比较。我为此使用 Apache Commons Lang 库。

如何比较两个时间戳并确定哪个更大?

String msgPushedTimestamp = 2016-05-11T19:50:17.141Z
String logFileTimestamp = 2016-05-11T14:52:02.970Z
Run Code Online (Sandbox Code Playgroud)

这就是我使用 Apache Commons Lang 库生成时间戳的方式。

String msgPushedTimestamp = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", TimeZone.getTimeZone("UTC")).format(System.currentTimeMillis());
Run Code Online (Sandbox Code Playgroud)

shm*_*sel 5

您可以将它们作为字符串进行比较,因为您的日期格式 ( ISO 8601 ) 在字典上具有可比性。

int compare = msgPushedTimestamp.compareTo(logFileTimestamp);
if (compare < 0) {
    // msgPushedTimestamp is earlier
} else if (compare > 0) {
    // logFileTimestamp is earlier
} else {
    // they are equal
}
Run Code Online (Sandbox Code Playgroud)