比较不同时区的两个日期对象并获得以秒为单位的确切时差

Comparing two date objects of different TimeZones and get the exact time difference in seconds

我正在尝试将电子邮件的接收日期(日期对象)与 JSONObject 中的日期(字符串)进行比较。这两个日期将属于不同的时区,相差 1 小时。

我的情况是我必须在几秒钟内得到它们之间的确切差异,如果差异是 +/- 10 秒,我就会找到一个匹配项。为此,我决定将两者都转换成 'UTC' 然后做一个差值。这是我的代码:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

String MsgDtStr = sdf.format(messagesToBeProcessed.getReceivedDate()); 

Date MsgDt = sdf.parse(MsgDtStr);

Date ObjDt = sdf.parse((JsonObj.get("date").toString()));

int DiffInSecs = (int) (ObjDt.getTime() - MsgDt.getTime()) / 1000 % 60;

if (DiffInSecs >= -10 && DiffInSecs <= 10) {
                System.out.println("Found a match!");
                MatchedList.add(i);
}

这似乎给了我正确的结果。应该与 json 对象匹配的邮件。即差异为 +/- 10 秒。但是,当我尝试按照上述逻辑打印日期和 DiffInSecs 时,我得到如下值:

date in object: 2017-06-22 19:47:25
date in message: 2017-06-22 23:47:30
DiffInSecs: -5

        ........

date in object: 2017-06-22 17:50:38
date in message: 2017-06-22 21:50:39
DiffInSecs: -1

我看到时间相差了4个小时。现在我很困惑我的逻辑是如何工作的。对此有什么解释吗?我解决问题的方法是否正确?

这些是我应该使用的邮件和 json 对象的样本日期值,它们应该匹配:(我怎样才能以任何方式实现这一点,而不仅仅是我发布的方法)

messagesToBeProcessed[i].getReceivedDate(): Thu Jun 22 01:03:13 CDT 2017 messagesToBeProcessed[i].getReceivedDate().getTime(): 1498111393000

Obj.get('date'): 2017-06-22 02:03:03 ObjDt.getTime(): 1498096983000

编辑:

正如 JBNizet 在他的评论中指出的那样,我需要删除 %60 才能获得精确的秒数差异。

好吧,现在我觉得坚持旧的 API 没有意义。现在我在消息对象中的日期不再像我在上面的结果中发布的那样转换为 UTC。我不确定为什么它更早工作并且不再工作(欢迎任何 explanations/comments)。正如雨果在他的回答中指出的那样,使用 new Java.Time API 更好。

希望这个话题能帮助别人找到未来的路。

使用此代码

int DiffInSecs = (int) (ObjDt.getTime() - MsgDt.getTime()) / 1000 % 60;

您正在使两个日期之间相差秒数的 模块 60

这始终是一个介于 0 和 59 之间的值,因此它不考虑分钟、天、小时、月和年的差异

修改如下:

int diffInSecs = (int) (ObjDt.getTime() - MsgDt.getTime()) / 1000;

这才是真正的秒差。

注意:变量名不要大写。仅对 类 和接口使用大写名称。

我不确定您使用的是哪个 java 版本,但无论如何:旧的 类(DateCalendarSimpleDateFormat ) 有 lots of problems and design issues,它们正在被新的 Date/Time API 取代。

如果您正在使用 Java 8,请考虑使用 new java.time API. It's easier, less bugged and less error-prone than the old APIs.

如果您使用 Java <= 7,您可以使用 ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it ).

下面的代码适用于两者。 唯一的区别是包名称(在 Java 8 中是 java.time,在 ThreeTen Backport(或 Android 的 ThreeTenABP)中是 org.threeten.bp),但是 类和方法名称是一样的。


根据 getReceivedDate() 的输出,您的默认时区是 CDT。 但是像 ESTCDT 这样的短时区名称是 ambiguous and not standard.

新的 API 使用 Continent/City 格式的长名称(被 IANA database 使用。所以, CDT 可以在许多不同的位置,例如 America/ChicagoAmerica/Havana。您必须首先检查哪个最适合您的上下文 - 您可以使用 ZoneId.getAvailableZoneIds().

获得所有可用名称的列表

对于下面的示例,我随机选择了其中一个 (America/Chicago) 只是为了展示它是如何工作的。我使用 DateTimeFormatter 来解析输入 StringZoneId 将其转换为时区。

// parse jsonInput
String jsonInput = "2017-06-22 02:03:03";
// formatter
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// parse the input to a LocalDateTime
LocalDateTime dt = LocalDateTime.parse(jsonInput, fmt); // 2017-06-22T02:03:03
// convert it to the EDT timezone (using America/Chicago as it's currently in CDT)
ZonedDateTime z = dt.atZone(ZoneId.of("America/Chicago")); // 2017-06-22T02:03:03-05:00[America/Chicago]

// convert the java.util.Date object to the new API
Date date = new Date(1498111393000L); // using your timestamp = 2017-06-22T06:03:13 UTC
// for Java >= 8, use toInstant method
Instant instant = date.toInstant();
// for Java <= 7, use org.threeten.bp.DateTimeUtils
Instant instant = DateTimeUtils.toInstant(date);

// get the difference between them
long diffSecs = ChronoUnit.SECONDS.between(instant, z);

差异为 3590 秒 - 这是因为 CDT 中的 2017-06-22 02:03:03(在我的示例中为芝加哥)等于 UTC 2017-06-22T07:03:03Z。你的日期(1498111393000 毫秒)等于 UTC 2017-06-22T06:03:13Z.

在您的代码中,您使用 sdf 来解析输入 2017-06-22 02:03:03,但 sdf 设置为 UTC(而不是 CDT),导致结果不正确(假设 JSON 输入也在 CDT 中)。


如果要将 JSON 输入转换为 UTC,可以使用 ZoneOffset.UTC 而不是 ZoneId

实际上,您必须检查输入是否真的是 UTC 并相应地更改代码(上面的代码假设 JSON 输入是 CDT)。


当然你可以用SimpleDateFormat做同样的事情:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("America/Chicago"));

Date jsonDate = sdf.parse("2017-06-22 02:03:03"); // 2017-06-22 02:03:03 in Chicago, or 2017-06-22T07:03:03 in UTC

然后按照与您相同的方式计算差值 ((jsonDate.getTime() - date.getTime()) / 1000)。结果将与上面相同:3590 秒。