I want to display list of time interval with 15 minutes or 30 minutes like
like
1:00
1:15
1:30
1:45
or
1:00
1:30
2:00
2:30
below code is using Joda Date time library and it's showing time with interval of hours . I simply want to display time with interval of 15 or 30 minutes .
val hourDateTime = DateTime().withDate(2000, 1, 1).withTime(0, 0, 0, 0)
for (i in 1..23) {
val formattedHours = Formatter.getHoursWithMinutes(context!!, hourDateTime.withHourOfDay(i))
}
Formatter.kt
fun getHoursWithMinutes(context: Context, dateTime: DateTime) = dateTime.toString("h:mm a")
Instead of showing date(s) like 1:00 ,2:00 ,3:00 ,4:00 display with interval of 15 or 30 minutes
Currently working View which is working using Simple Calendar
I have tried another approach written below but this time time interval changes but hours remain same like
12:00 -> 12:30 -> 12:00 -> 12:30
val hourDateTime = DateTime().withDate(2000, 1, 1).withTime(0, 0, 0, 0)
var interval = 30
for (i in 1..23) {
if(i == 1 ) interval = 0
val formattedHours = Formatter.getHoursWithMinutes(context!!, hourDateTime.withMinuteOfHour(interval) )
interval += 30
if(interval == 60) interval = 0
}
java.time and ThreeTenABP
I can’t write Kotlin. Please translate from my Java.
Output:
Set the interval to
Duration.ofMinutes(30)to get 01:00, 01:30, etc.I am using java.time, the modern Java date and time API, through the backport, see below. java.time is the successor of Joda-Time. I am sure something very similar to the above is possible with Joda-Time too in case you are already using Joda-Time and don’t want to migrate to java.time just now.
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
org.threeten.bpwith subpackages.Links
java.timewas first described.java.timeto Java 6 and 7 (ThreeTen for JSR-310).