获取当前时间和下周六之间的剩余 minutes/hours?
Getting remaining minutes/hours between current time and the next saturday?
我今天大部分时间都在浏览文档,但一直无法弄清楚具体应该如何完成。
我希望每周 运行 一个活动,每个星期六 00:00 直到星期一 00:00 (48h)。
public static void scheduleEvent() {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Long saturdayMidnight = LocalDateTime.now().until(LocalDateTime.now().plusMinutes(1/* ??? */), ChronoUnit.MINUTES);
scheduler.scheduleAtFixedRate(new EventTimer(), saturdayMidnight, TimeUnit.DAYS.toMinutes(1), TimeUnit.MINUTES);
}
作为此处的测试,我将其设置为等待一分钟,直到调用 EventTimer class。这按预期工作,但我如何计算当前时间和星期六午夜之间的剩余分钟数或小时数,然后我可以在调度程序中使用它在每个周末的正确时间启动事件?
至于副本,我不是要获取下周六的日期,而是要获取当前时间和下周六之间的 minutes/hours。虽然如果它可以通过约会来完成,我不介意。
这是获取 "next Saturday, midnight" LocalDateTime
实例的片段,然后是到那时为止的小时和分钟。
// getting next saturday midnight
LocalDateTime nextSaturdayMidnight = LocalDateTime.now()
// truncating to midnight
.truncatedTo(ChronoUnit.DAYS)
// adding adjustment to next saturday
.with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
// getting hours until next saturday midnight
long hoursUntilNextSaturdayMidnight = LocalDateTime.now()
// getting offset in hours
.until(nextSaturdayMidnight, ChronoUnit.HOURS);
// getting minutes until...
long minutesUntilNextSaturdayMidnight = LocalDateTime.now()
// getting offset in minutes
.until(nextSaturdayMidnight, ChronoUnit.MINUTES);
在撰写本文时印刷(8 月 14 日 14:02),3 个变量应如下所示:
2018-08-18T00:00
81
4917
我今天大部分时间都在浏览文档,但一直无法弄清楚具体应该如何完成。
我希望每周 运行 一个活动,每个星期六 00:00 直到星期一 00:00 (48h)。
public static void scheduleEvent() {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Long saturdayMidnight = LocalDateTime.now().until(LocalDateTime.now().plusMinutes(1/* ??? */), ChronoUnit.MINUTES);
scheduler.scheduleAtFixedRate(new EventTimer(), saturdayMidnight, TimeUnit.DAYS.toMinutes(1), TimeUnit.MINUTES);
}
作为此处的测试,我将其设置为等待一分钟,直到调用 EventTimer class。这按预期工作,但我如何计算当前时间和星期六午夜之间的剩余分钟数或小时数,然后我可以在调度程序中使用它在每个周末的正确时间启动事件?
至于副本,我不是要获取下周六的日期,而是要获取当前时间和下周六之间的 minutes/hours。虽然如果它可以通过约会来完成,我不介意。
这是获取 "next Saturday, midnight" LocalDateTime
实例的片段,然后是到那时为止的小时和分钟。
// getting next saturday midnight
LocalDateTime nextSaturdayMidnight = LocalDateTime.now()
// truncating to midnight
.truncatedTo(ChronoUnit.DAYS)
// adding adjustment to next saturday
.with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
// getting hours until next saturday midnight
long hoursUntilNextSaturdayMidnight = LocalDateTime.now()
// getting offset in hours
.until(nextSaturdayMidnight, ChronoUnit.HOURS);
// getting minutes until...
long minutesUntilNextSaturdayMidnight = LocalDateTime.now()
// getting offset in minutes
.until(nextSaturdayMidnight, ChronoUnit.MINUTES);
在撰写本文时印刷(8 月 14 日 14:02),3 个变量应如下所示:
2018-08-18T00:00
81
4917