Я смотрю на документацию Даты и пытаюсь выяснить, как я могу выразить ТЕПЕРЬ + 5 секунд. Вот некоторый псевдокод:
import java.util.Date
public class Main {
public static void main(String args[]) {
Date now = new Date();
now.setSeconds(now.getSeconds() + 5);
}
}
Instant // Use modern `java.time.Instant` class to represent a moment in UTC.
.now() // Capture the current moment in UTC.
.plusSeconds( 5 ) // Add five seconds into the future. Returns another `Instant` object per the Immutable Objects pattern.
современное java.time классы, которые несколько лет назад вытеснили ужасное Date
& Calendar
классы.
Для работы в UTC используйте Instant
.
Instant later = Instant.now().plusSeconds( 5 ) ;
Для работы в определенном часовом поясе используйте ZonedDateTime
.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime later = ZonedDateTime.now( z ).pluSeconds( 5 ) ;
Duration
Вы можете мягкий код сумма и гранулярность времени для добавления. Используйте Duration
класс.
Duration d = Duration.ofSeconds( 5 ) ;
Instant later = Instant.now().plus( d ) ; // Soft-code the amount of time to add or subtract.
Попробуйте Это..
Date now = new Date();
System.out.println(now);
Calendar c = Calendar.getInstance();
c.setTime(now);
c.add(Calendar.SECOND, 5);
now = c.getTime();
System.out.println(now);
// Output
Tue Jun 11 16:46:43 BDT 2019
Tue Jun 11 16:46:48 BDT 2019