-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
  • 500
  • 1
  • 4
  • 13
  • 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
  • 14,698
  • 10
  • 35
  • 45
  • Thanks @deHaar. But please note that the above solution works with Version O (26) and higher. I have MIN API LEVEL as 21, Can you please suggest something that will cover from API 21 – SVK Jul 17 '21 at 08:36
  • @SVK You can use the [ThreeTen ABP](https://github.com/JakeWharton/ThreeTenABP) or [Android API Desugaring](https://developer.android.com/studio/write/java8-support) in order to make it available in lower API versions. Make sure you use Java 9+ to have the `to...Part()` methods available. – deHaar Jul 17 '21 at 09:52
  • Hi @deHaar: Thanks for your support. Please note in order to use Java 9 I updated following `sourceCompatibility = JavaVersion.VERSION_1_9` and `targetCompatibility = JavaVersion.VERSION_1_9` and `kotlinOptions { jvmTarget = "9" }` but now the build throws error and I have to downgrade to Java 1.8 for build to be successfull. Please guide and sorry for the to and fro – SVK Jul 18 '21 at 07:12
  • @SVK Then either fix those build errors (somehow, I can't tell you the reason) or use Java 1.8 and the methods without `Part`. You will have to calculate full units with the modulo operator, for example. – deHaar Jul 19 '21 at 07:03
  • It seems like you missed to type the example above.. i.e. the comment ended after `for example -` Would you please re-write the example – SVK Jul 19 '21 at 07:11
  • @SVK have a look at my edit, I added a Java 1.8 solution. – deHaar Jul 19 '21 at 07:15
  • @SVK The example is in the answer ;-) – deHaar Jul 19 '21 at 07:21