Переход от Joda-Time в Java.Time: Как заменить время. [закрыто]JAVA

Программисты JAVA общаются здесь
Anonymous
Переход от Joda-Time в Java.Time: Как заменить время. [закрыто]

Сообщение Anonymous »

В настоящее время я перемещаю свою кодовую базу с Joda-Time в API Java.Time (Java 8+). < /p>
В одной части моего приложения я планирую задачи для выполнения в определенное время либо сегодня, либо завтра, в зависимости от бизнеса.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.

✅ What I’ve tried for scheduleToday():
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

Вернуться в «JAVA»