给定一个 Calendar 实例,确定它是否已经过一小时的最简单方法是什么?

发布于 2024-12-09 05:15:14 字数 426 浏览 1 评论 0原文

追踪代码块中一些微妙的、烦人的错误。

此代码给出了一个 Calendar 实例,该实例是在某个事件开始时创建的。我们需要弄清楚该事件是否已经发生了一小时。

我知道有几种方法可以做到这一点:

  • 使用 JodaTime 并计算持续时间
  • 将给定的日历与一小时前创建的新日历进行
  • 比较 将给定的 Calendar.getTime 与一小时前创建的日期进行比较

每个方法都有自己的怪癖并且似乎有代码味道。 Java中有没有一种简单、简洁的方式来表达“从现在到一小时前发生的事情”?

当我对此进行单元测试时,我会检查:

  • 45 分钟前(真)
  • 61 分钟前(假)
  • 0 分钟前(真)
  • -1 分钟前(错误还是真?)

Chasing some subtle, annoying bugs in a block of code.

This code is given an instance of Calendar which was created at the start of some event. We need to figure out if this event is an hour old.

I know of several ways to do this:

  • using JodaTime and calculating a Duration
  • comparing the given Calendar to a new Calendar created for one hour ago
  • comparing the given Calendar.getTime to a Date created for one hour ago

Each of these methods has its own quirks and seems to have code smells. Is there a simple, concise way to express "happened between now and one hour ago" in Java?

When I unit test this I would check:

  • 45 minutes ago (true)
  • 61 minutes ago (false)
  • 0 minutes ago (true)
  • -1 minutes ago (error or true?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过潦 2024-12-16 05:15:14

您可以创建一个新的日历实例(包含当前日期/时间)并将其与给定的实例进行比较:

public boolean isWithinHour(Calendar given)
{
    long oldTime = given.getTimeInMillis();
    long newTime = Calendar.getInstance().getTimeInMillis();

    long diff = newTime - oldTime;
    return (diff <= 3600 * 1000);    // 3600 seconds in millis
}

You can create a new Calendar instance (with current date/time) and compare it with the given one:

public boolean isWithinHour(Calendar given)
{
    long oldTime = given.getTimeInMillis();
    long newTime = Calendar.getInstance().getTimeInMillis();

    long diff = newTime - oldTime;
    return (diff <= 3600 * 1000);    // 3600 seconds in millis
}
十年不长 2024-12-16 05:15:14

作为个人喜好,我会选择 Joda-Time,但我认为实现并不重要。重要的是明确目的并创建一个可以使用的方法。

首先,您最好定义什么意思一小时前发生

  • 是指正好一个小时前吗?
  • 是指一个小时前吗?
  • 是指45分钟前到1小时15分钟前吗?

之后,您选择您提到的方法之一。然后您编写自己的方法并使用描述性名称,该名称应该在您的代码中自然地读取。

As a personal preference I'd choose Joda-Time, but I think the implementation is not important. What is important is being clear on the purpose and create a method that you can use.

First you'd better define what means happened one hour ago.

  • Does it mean exactly a hour ago?
  • Does it mean within a hour ago?
  • Does it mean between 45 minutes ago and 1 hour and 15 minutes ago?

After that you choose one of the methods that you mentioned. and you write your own method with a descriptive name that should read naturally in your code.

北方的韩爷 2024-12-16 05:15:14

可能与您列出的方法没有太大不同,但我不明白它的气味如何或为何:

if (calendar.getTimeInMillis() + (60L * 60L * 1000L)) <= System.currentTimeMillis()) {
    // created more than one hour ago
}

Probably not very different from the methods you listed, but I don't see how or why it smells:

if (calendar.getTimeInMillis() + (60L * 60L * 1000L)) <= System.currentTimeMillis()) {
    // created more than one hour ago
}
西瑶 2024-12-16 05:15:14

我使用的方法是与过去 1 小时的时间戳进行比较:

return cal.getTimeInMillis() < (System.currentTimeMillis() - 1000L * 60L * 60L);

The method I would use is to compare against a timestamp 1 hour in the past:

return cal.getTimeInMillis() < (System.currentTimeMillis() - 1000L * 60L * 60L);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文