【问题标题】:Conversion of Long to Specific Conditional Format将 Long 转换为特定条件格式
【发布时间】:2021-09-25 02:33:26
【问题描述】:

我需要根据 long 值范围将 Long 转换为小时、分钟和秒。
但是,我需要以下条件转换:

  1. 如果 Long 值小于 1 小时,则输出应以分钟为单位。
    例如如果小于3_600_000L,则输出为 59 分钟或类似。

  2. 如果 Long 值等于精确/完整/整小时(例如 1 小时/2 小时左右),则输出应以小时为单位。
    例如如果是3_600_000L OR 7_200_000L,则输出为 1 Hour OR 2 Hours 或类似的。

  3. 如果长值大于小时并且还有一些分钟,那么输出也应该包含小时和分钟。
    例如如果是7_400_000L,则输出为 2 小时 3 分钟

  4. 如果长值大于 1 天(即 24 小时)并且等于精确/完整/整个小时 24 小时、48 小时,则输出应相应为 1 天、2 天。

  5. 如果 long 值大于 1 天并且有一些额外的小时数,那么输出应该是 1 天 5 小时。

注意:

  1. 我研究了已回答的与此主题相关的问题,但找不到任何解决方案,因此发布了此查询。
  2. 我尝试了Simple Date FormatTimeUnit.MILLISECONDS.toHour/toSeconds/toMinutes,但没有成功。
  3. 我在 Android 上需要这个 使用 Kotlin
  4. 我需要创建一个接受 long 并根据上述条件返回值的函数。

请大家多多指导。

【问题讨论】:

标签: android datetime kotlin


【解决方案1】:

您可以使用java.time.Duration 将毫秒数转换为所需的单位部分:

Java 9 及更高版本:

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""")
}

两种解决方案的输出

        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。但请注意,上述解决方案适用于版本 O (26) 及更高版本。我的 MIN API LEVEL 为 21,您能否提出一些将涵盖 API 21 的内容
  • @SVK 您可以使用ThreeTen ABPAndroid API Desugaring 以使其在较低的API 版本中可用。确保您使用 Java 9+ 以使 to...Part() 方法可用。
  • 嗨@deHaar:感谢您的支持。请注意,为了使用 Java 9,我更新了 sourceCompatibility = JavaVersion.VERSION_1_9targetCompatibility = JavaVersion.VERSION_1_9kotlinOptions { jvmTarget = "9" } 但现在构建抛出错误,我必须降级到 Java 1.8 才能成功构建。请指导和抱歉来回
  • @SVK 然后要么修复那些构建错误(不知何故,我不能告诉你原因)或者使用 Java 1.8 和没有Part 的方法。例如,您必须使用模运算符计算完整单位。
  • 您似乎错过了输入上面的示例..即评论在for example -之后结束,请您重新编写示例
猜你喜欢
  • 2011-12-19
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 2020-12-09
  • 2021-02-04
  • 1970-01-01
相关资源
最近更新 更多