Skip to content
Advertisement

Get time period ( hour / half-hour / quarter hour ) of day

Is there any way to download every hour and another period of the day? I mean exactly the result in the form of a list.

JavaScript

and

JavaScript

and

JavaScript

14.03 means March 14 for the sake of the example.

Of course it is possible to add this manually, but it would not be a particularly elegant solution. Is it possible to do it without explicitly declaring constants as values of each hour ? The best solution would be a function without using loops and if else constructions.

I have not yet been able to find an implementation of such a solution. I myself also spend another hour on this without success. I need it to implement a functionality that creates a Map or a list of pairs from a list of such e.q hours:

JavaScript

Has anyone had occasion to implement such a problem and would be able to help?

Advertisement

Answer

You could write a function to generate a Sequence<LocalTime> like this:

JavaScript

Then you have all the LocalTimes you can use with toString() or with .format() and some DateTimeFormatter to format them how you like. You can use zipWithNext() to create the time ranges.

JavaScript

Note, I’m using fully qualified java.time.Duration to avoid conflicts with the Kotlin standard library Duration class.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement