我有一个 spring-boot 调度程序,每 15 分钟唤醒一次。它是一个 cron,因此它总是会在适当的边界周围醒来,并伴有一些抖动,但通常在
00
、15
、30
、45
、60
附近。
我需要执行以下操作:
A
。因此,仅当时间在 11:00am
左右时,才在 A
左右执行操作 10:57-11:03
。与 30
分钟边界类似,如果增量在 10:57-11:03
或 11:27-11:33
当调度程序唤醒时,我正在读取如下时间
ZonedDateTime currentDate = (ZonedDateTime.now(ZoneOffset.UTC).truncatedTo(ChronoUnit.MINUTES));
ZonedDateTime prior = currentDate.minus(3,ChronoUnit.MINUTES);
ZonedDateTime next = currentDate.plus(3,ChronoUnit.MINUTES);
我很熟悉我可以通过加减来得到上面的窗口。但是我迷失的是如何比较前一个和下一个边界内的时间的琐碎部分? 因为如果调度程序在 11:35 醒来,我不希望在 11:30 采取操作并跳过处理。
对于仅偏移量 (UTC) 而不是时区,请使用 OffsetDateTime。
OffsetDateTime now = OffsetDateTime.now( ZoneOffset.UTC ) ;
提取分钟,测试。
int minute = now.getMinute() ;