我不明白 Quartz 中的 getNextIncludedTime()

发布于 2024-08-02 03:18:59 字数 796 浏览 5 评论 0原文

我可能只是个白痴 - 这真是漫长的一天! 在我第一次涉足 Quartz 时,我误解了一些东西......

给定以下代码:

DateTime dt = new DateTime();
dt = dt.withDayOfMonth(20);

Calendar cal = new CronCalendar("0 0/10 * * * ?" );
long start = dt.getMillis();
System.out.println("Starting at " + start);
long end = start + 10;
long current = start;
int i = 0;
while (current < end) {
  if (i > 0) {
    System.out.println(i + ":" + current);
  }
  long next = cal.getNextIncludedTime(current);
  current = next;
  i++;
}

我预计输出中最多会包含一个时间,因为时间窗口为 10 毫秒,并且日历中包含的时间相隔 10 分钟。

但当我运行它时:

Starting at 1250796103004
1:1250796103005
2:1250796103006
3:1250796103007
4:1250796103008
5:1250796103009
6:1250796103010
7:1250796103011
8:1250796103012
9:1250796103013

请帮忙!

I'm probably just being an idiot - it's been a long day! I've misunderstood something in my first foray into Quartz...

Given this code:

DateTime dt = new DateTime();
dt = dt.withDayOfMonth(20);

Calendar cal = new CronCalendar("0 0/10 * * * ?" );
long start = dt.getMillis();
System.out.println("Starting at " + start);
long end = start + 10;
long current = start;
int i = 0;
while (current < end) {
  if (i > 0) {
    System.out.println(i + ":" + current);
  }
  long next = cal.getNextIncludedTime(current);
  current = next;
  i++;
}

I expect that there will be at most one included time in the output, as the time window is 10ms and the times included in the Calendar are 10 minutes apart.

But when I run it:

Starting at 1250796103004
1:1250796103005
2:1250796103006
3:1250796103007
4:1250796103008
5:1250796103009
6:1250796103010
7:1250796103011
8:1250796103012
9:1250796103013

Please help!

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

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

发布评论

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

评论(1

丿*梦醉红颜 2024-08-09 03:18:59

是的,只有我是个笨蛋。

日历指定排除时间。

我应该使用 CronTrigger 来指定我想要包含的时间。 代码应该看起来更像这样......

CronTrigger cal = new CronTrigger("Test", "Test", "0 0/10 * * * ?" );
...
end = start + 1000000;
...
while (current < end) {
  if (i > 0) {
  System.out.println(i + ":" + current);
  }
  Date next = cal.getFireTimeAfter(new Date(current));
  current = next.getTime();
  i++;
}

这给出了我期望的输出。

Starting at 1250798091337
1:1250798400000
2:1250799000000

Yep, just me being a dumbass.

Calendars specify EXCLUDED times.

I should have been using a CronTrigger to specify the times I wanted to include. The code should look more like this...

CronTrigger cal = new CronTrigger("Test", "Test", "0 0/10 * * * ?" );
...
end = start + 1000000;
...
while (current < end) {
  if (i > 0) {
  System.out.println(i + ":" + current);
  }
  Date next = cal.getFireTimeAfter(new Date(current));
  current = next.getTime();
  i++;
}

Which gives the output I was expecting.

Starting at 1250798091337
1:1250798400000
2:1250799000000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文