简体   繁体   中英

How to show progress in Date/time range in progress/seek bar android

I need to show in progress bar time lapse. I have start date/time and end date/time, so between start/date/time and end/date/time progress should be showed. Please help who had experience with this.

start========(current time)=======end 

this is how it should looks like:

You can use this simple small function:

fun progress(start: LocalDateTime,
             end: LocalDateTime,
             now: LocalDateTime = LocalDateTime.now()): Double = when {
    now <= start -> 0.0
    now >= end -> 1.0
    else -> start.until(now, ChronoUnit.SECONDS) /
            start.until(end, ChronoUnit.SECONDS).toDouble()
}

It has 3 different cases:

  1. now is before or equal to start then the progress is 0
  2. now is after or equal to end then the progress is 1
  3. calculate (now - start) / (end - start) which gives a fraction in the range 0-1

You can then take that fraction and multiply it by 100 to get the percentage:

val progress = progress(start, end)
val percentage = (progress * 100).roundToInt()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM