How to calculate the number of seconds between time and next Saturday using Java8

I need to implement a function

int secondsTillNextSaturday(LocalDateTime start);

Which does the same thing as said, calculates the number of seconds until the next Saturday relative to the start time (if the start is already on Saturday, then it should return the number of seconds until the next Saturday after it).

For example, for 04/27/2017 00:00:00 (Thursday), he should return 2 * 24 * 60 * 60.

+4
source share
1 answer

This can be done easily using the java 8 time api:

public long secondsTillNextSaturday(LocalDateTime start) {
    LocalDate nextSaturday = start.toLocalDate().with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
    return ChronoUnit.SECONDS.between(start, nextSaturday.atStartOfDay());
}
+9
source

Source: https://habr.com/ru/post/1675840/


All Articles