I am trying to generate an array of 72 hours timeslots with 15 minutes of interval but it starts with 00:00 after 24 hours, so basically it prints whole 24 hours timeslots 3 times, but i want to start from 00:15..23:45..24:00..24:45..25:00..25:15..72:00
I have less experience working with Calendar APIs,
Here is my code,
val start_duration = "00:00"
val end_duration = "72:00"
val _start_duration = durationFormatter.parse(start_duration)
val calenderStartDuration = Calendar.getInstance()
val calenderEndDuration = Calendar.getInstance()
calenderStartDuration.time = _start_duration
calenderEndDuration.time = calenderStartDuration.time.addHours(72)
while (calenderStartDuration.before(calenderEndDuration)){
calenderStartDuration.add(Calendar.MINUTE, 15)
val timeStr = durationFormatter.format(calenderStartDuration.time).toLowerCase()
listBookingDuration.add(timeStr)
}
CodePudding user response:
If you need to create "as many 15 minute slots" that can fit in a 72-hour time span, starting today at midnight... then I'd leverage the power of java.time apis:
This is naive pseudo-code, but you get the idea:
import java.time.*
fun main() {
val startTime = LocalTime.of(0, 0)
val today = LocalDate.now()
var current = LocalDateTime.of(today, startTime) //use datetime to account for day changes.
val endDateTime = current.plusHours(72)
val timeSlots = mutableListOf<LocalTime>()// or LocalDateTime if you need the "date" component as well.
timeSlots.add(current.toLocalTime()) // add the 1st interval
while (current.isBefore(endDateTime)) {
val newCurrent = current.plusMinutes(15)
timeSlots.add(newCurrent.toLocalTime())
current = newCurrent
}
println(timeSlots)
}
This prints:
[00:00, 00:15, 00:30, 00:45, 01:00, 01:15, 01:30, 01:45, 02:00, 02:15, 02:30, 02:45, 03:00, 03:15, 03:30, 03:45, 04:00, 04:15, 04:30, 04:45, 05:00, 05:15, 05:30, 05:45, 06:00, 06:15, 06:30, 06:45, 07:00, 07:15, 07:30, 07:45, 08:00, 08:15, 08:30, 08:45, 09:00, 09:15, 09:30, 09:45, 10:00, 10:15, 10:30, 10:45, 11:00, 11:15, 11:30, 11:45, 12:00, 12:15, 12:30, 12:45, 13:00, 13:15, 13:30, 13:45, 14:00, 14:15, 14:30, 14:45, 15:00, 15:15, 15:30, 15:45, 16:00, 16:15, 16:30, 16:45, 17:00, 17:15, 17:30, 17:45, 18:00, 18:15, 18:30, 18:45, 19:00, 19:15, 19:30, 19:45, 20:00, 20:15, 20:30, 20:45, 21:00, 21:15, 21:30, 21:45, 22:00, 22:15, 22:30, 22:45, 23:00, 23:15, 23:30, 23:45, 00:00, 00:15, 00:30, 00:45, 01:00, 01:15, 01:30, 01:45, 02:00, 02:15, 02:30, 02:45, 03:00, 03:15, 03:30, 03:45, 04:00, 04:15, 04:30, 04:45, 05:00, 05:15, 05:30, 05:45, 06:00, 06:15, 06:30, 06:45, 07:00, 07:15, 07:30, 07:45, 08:00, 08:15, 08:30, 08:45, 09:00, 09:15, 09:30, 09:45, 10:00, 10:15, 10:30, 10:45, 11:00, 11:15, 11:30, 11:45, 12:00, 12:15, 12:30, 12:45, 13:00, 13:15, 13:30, 13:45, 14:00, 14:15, 14:30, 14:45, 15:00, 15:15, 15:30, 15:45, 16:00, 16:15, 16:30, 16:45, 17:00, 17:15, 17:30, 17:45, 18:00, 18:15, 18:30, 18:45, 19:00, 19:15, 19:30, 19:45, 20:00, 20:15, 20:30, 20:45, 21:00, 21:15, 21:30, 21:45, 22:00, 22:15, 22:30, 22:45, 23:00, 23:15, 23:30, 23:45, 00:00, 00:15, 00:30, 00:45, 01:00, 01:15, 01:30, 01:45, 02:00, 02:15, 02:30, 02:45, 03:00, 03:15, 03:30, 03:45, 04:00, 04:15, 04:30, 04:45, 05:00, 05:15, 05:30, 05:45, 06:00, 06:15, 06:30, 06:45, 07:00, 07:15, 07:30, 07:45, 08:00, 08:15, 08:30, 08:45, 09:00, 09:15, 09:30, 09:45, 10:00, 10:15, 10:30, 10:45, 11:00, 11:15, 11:30, 11:45, 12:00, 12:15, 12:30, 12:45, 13:00, 13:15, 13:30, 13:45, 14:00, 14:15, 14:30, 14:45, 15:00, 15:15, 15:30, 15:45, 16:00, 16:15, 16:30, 16:45, 17:00, 17:15, 17:30, 17:45, 18:00, 18:15, 18:30, 18:45, 19:00, 19:15, 19:30, 19:45, 20:00, 20:15, 20:30, 20:45, 21:00, 21:15, 21:30, 21:45, 22:00, 22:15, 22:30, 22:45, 23:00, 23:15, 23:30, 23:45, 00:00]
You can try and play with this in the Kotlin playground.
Now if you want "day2" to print 24:00, 24:15, 24:30, 24:45, 25:00, etc., you'd likely want to keep it as a LocalDateTime
and leverage the day comparison to know where in the sequence you are; I'm sure you can figure that part out, as you're able to perform date arithmetic operations with Java.Time apis quite easily.
Note this is not the only and perhaps not even the best way, but it's a way.
An alternative is to keep an increment and get startTime.plus(increment). All in all, Java Time APIs are very straightforward.
CodePudding user response:
Use loop and iterate until 72 hours (4320 minutes)and write a logic to get values after 15 min
for (i in 0 until 4321) {
val remainder = i % 15
if(remainder==0){
val hours = i / 60; //since both are ints, you get an int
val minutes = i % 60;
listBookingDuration.add(String.format("d", hours) ":" String.format("d", minutes))
}
}