В одной части моего приложения я планирую задачи для выполнения в определенное время либо сегодня, либо завтра, в зависимости от бизнеса.private synchronized void scheduleToday(LocalTime time) {
long now = System.currentTimeMillis();
task = normalScheduler.schedule(this, time.toDateTimeToday().getMillis() - now, TimeUnit.MILLISECONDS);
}
private synchronized void scheduleTomorrow(LocalTime time) {
LocalDate tomorrow = new LocalDate().plusDays(1);
long now = System.currentTimeMillis();
task = normalScheduler.schedule(this, tomorrow.toDateTime(time).getMillis() - now, TimeUnit.MILLISECONDS);
}
I’m trying to migrate these methods to use java.time API instead of Joda-Time, without changing the functionality.
I tried replacing it with the following:
private synchronized void scheduleToday(LocalTime time) {
long now = System.currentTimeMillis();
LocalDateTime dateTimeToday = LocalDateTime.of(LocalDate.now(), time);
long millis = dateTimeToday.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
task = normalScheduler.schedule(this, millis - now, TimeUnit.MILLISECONDS);
}
My questions:
Is the above scheduleToday() code a correct and reliable replacement for the Joda-Time version? Will it handle edge cases (e.g., past time or daylight saving time shifts)?
How should I rewrite the scheduleTomorrow() method using java.time to match this equivalent functionality?
Подробнее здесь: https://stackoverflow.com/questions/796 ... etoday-get