Starting at midnight, I want to iterate over LocalTime
s in steps of a certain period length to the end of the day.
Eg. if the period is 8hr10Minutes I would like times: 0:00, 8:10, 16:20 (not 24:30 as this is the next day)
I have the following working code for periods smaller than one day. Is there a better (esp. more readable) logic?
Duration period = ...;
for(LocalTime t = LocalTime.MIDNIGHT, p = LocalTime.MIDNIGHT;
t.isAfter(p) || t.equals(p) ; p = t, t = t.plus(period)){
// do something with t
}
Also, the above fails for periods equal to multiples of one day (infinite loop) or, if greater than one day, returns the number as if the period was cast to be shorter than one day, (eg. 24hr10min behaves the same as 10Minutes), so if that can be fixed at the same time (without explicit if
s) I'll take it as well.