-1

I need to convert Long to Hours, Minutes and Seconds based on the long value range.
However, I need following conditional conversion:

  1. If the Long value is less than 1 Hour, then the output should be in minutes.
    e.g. if it is less than 3_600_000L the output is 59 minutes or likewise.

  2. If the Long value is equal to exact/complete/whole hour like 1 Hour/2 Hour or so, then the output should be in hours.
    e.g. if it is 3_600_000L OR 7_200_000L the output is 1 Hour OR 2 Hours or likewise.

  3. If the Long Value is greater than hours and also has some minutes then the output should consist hours and minutes as well.
    e.g. if it is 7_400_000L the output is 2 Hour, 3 Minute

  4. If the long value is greater than 1 Day (i.e. 24 Hours) and is equal to exact/complete/whole hour 24 Hours, 48 Hour then output should be 1 Day, 2 Days accordingly.

  5. If the long value is greater than 1 Day and has some additional hours then the output should be 1 day, 5 hours.

Note:

  1. I studied already answered questions related to this topic but I could not find any solution, hence posting this query.
  2. I tried Simple Date Format and TimeUnit.MILLISECONDS.toHour/toSeconds/toMinutes but it did not work.
  3. I need this for Android with Kotlin
  4. I need to make a single function which accepts long and returns value based on above conditions.

Request you all to please guide.

SVK
  • 676
  • 1
  • 5
  • 17
  • Does this answer your question? [how to show milliseconds in days:hours:min:seconds](https://stackoverflow.com/questions/19667473/how-to-show-milliseconds-in-dayshoursminseconds) – grrigore Jul 16 '21 at 09:17
  • Don't try `SimpleDateFormat` anymore if you don't have to. You can use `java.time` from Java 1.8, it's a lot better. – deHaar Jul 16 '21 at 10:18

1 Answers1

0

You can use a java.time.Duration in order to convert the amount of milliseconds to the desired unit parts:

Java 9 and higher versions:

import java.time.Duration

fun main() {
    val first = 3_600_000L
    val second = 7_200_000L
    val third = 180_182_234L
    // create Durations of the milliseconds
    val firstDuration = Duration.ofMillis(first)
    val secondDuration = Duration.ofMillis(second)
    val thirdDuration = Duration.ofMillis(third)
    // then print their value parts (largest should not be partially displayed)
    println("""
        First duration:  ${firstDuration.toDays()} days
                         ${firstDuration.toHoursPart()} hours,
                         ${firstDuration.toMinutesPart()} minutes
                     and ${firstDuration.toSecondsPart()} seconds""")
    println("""
        Second duration: ${secondDuration.toDays()} days
                         ${secondDuration.toHoursPart()} hours,
                         ${secondDuration.toMinutesPart()} minutes
                     and ${secondDuration.toSecondsPart()} seconds""")
    println("""
        Third duration:  ${thirdDuration.toDays()} days
                         ${thirdDuration.toHoursPart()} hours,
                         ${thirdDuration.toMinutesPart()} minutes
                     and ${thirdDuration.toSecondsPart()} seconds""")
}

Java 1.8:

import java.time.Duration

fun main() {
    val first = 3_600_000L
    val second = 7_200_000L
    val third = 180_182_234L
    // create Durations of the milliseconds
    val firstDuration = Duration.ofMillis(first)
    val secondDuration = Duration.ofMillis(second)
    val thirdDuration = Duration.ofMillis(third)
    // then print their value parts (largest should not be partially displayed)
    println("""
        First duration:  ${firstDuration.toDays()} days
                         ${firstDuration.toHours() % 24} hours,
                         ${firstDuration.toMinutes() % 60} minutes
                     and ${firstDuration.toSeconds() % 60} seconds""")
    println("""
        Second duration: ${secondDuration.toDays()} days
                         ${secondDuration.toHours() % 24} hours,
                         ${secondDuration.toMinutes() % 60} minutes
                     and ${secondDuration.toSeconds() % 60} seconds""")
    println("""
        Third duration:  ${thirdDuration.toDays()} days
                         ${thirdDuration.toHours() % 24} hours,
                         ${thirdDuration.toMinutes() % 60} minutes
                     and ${thirdDuration.toSeconds() % 60} seconds""")
}

Both solutions output

        First duration:  0 days
                         1 hours,
                         0 minutes
                     and 0 seconds

        Second duration: 0 days
                         2 hours,
                         0 minutes
                     and 0 seconds

        Third duration:  2 days
                         2 hours,
                         3 minutes
                     and 2 seconds
deHaar
  • 17,687
  • 10
  • 38
  • 51