Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to implement function

int secondsTillNextSaturday(LocalDateTime start);

Which does pretty same as it says, calculates number of seconds till next Saturday relatively to start time(if start is already Saturday, then it should return number of seconds till next Saturday after it).

For example for 27.04.2017 00:00:00 (Thursday) it should return 2 * 24 * 60 * 60.

like image 888
Viacheslav Shalamov Avatar asked Jan 05 '23 02:01

Viacheslav Shalamov


1 Answers

It could be done easily using 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());
}
like image 71
Ruslan Akhundov Avatar answered Jan 06 '23 16:01

Ruslan Akhundov