0

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
    } 
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300

1 Answers1

0

java.time and ThreeTenABP

I can’t write Kotlin. Please translate from my Java.

    Duration interval = Duration.ofMinutes(15);

    LocalTime time = LocalTime.of(1, 0);
    for (int i = 0; i < 10; i++) {
        System.out.println(time);
        time = time.plus(interval);
    }

Output:

01:00
01:15
01:30
01:45
02:00
02:15
02:30
02:45
03:00
03:15

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.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161