kotlin 获取两次之间的周期

velaa5lx  于 2023-05-07  发布在  Kotlin
关注(0)|答案(1)|浏览(116)

bounty还有5天到期。回答此问题可获得+50声望奖励。Ole V.V.想要引起更多关注这个问题:这个问题已经有了很大的改进,值得重新关注。这是关于许多人可能会犯的几个真正的错误,所以一个好的答案对未来的读者也是有用和有帮助的。

我有两个弦乐时间。我怎样才能得到这两个时间之间的持续时间?我尝试使用DateTimeFormatter,但它不起作用。

val startTime = LocalDate.parse("19:30", DateTimeFormatter.ofPattern("HH:mm"))
val endTime = LocalDate.parse("20:30", DateTimeFormatter.ofPattern("HH:mm"))
val newDate = Period.between(startTime, endTime)

我认为它需要将两个日期转换为米利斯,然后做减法。但我不知道该怎么做!你的建议,好吗?!
错误信息:
进程:com.example.leetcodedraft,PID:23723

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.leetcodedraft/com.example.leetcodedraft.MainActivity}: java.time.format.DateTimeParseException: Text '19:30' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {},ISO resolved to 19:30 of type java.time.format.Parsed
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    Caused by: java.time.format.DateTimeParseException: Text '19:30' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {},ISO resolved to 19:30 of type java.time.format.Parsed
        at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
        at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
        at java.time.LocalDate.parse(LocalDate.java:394)
        at com.example.leetcodedraft.Test.startTest(Test.kt:16)
        at com.example.leetcodedraft.MainActivity.onCreate(MainActivity.kt:36)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
    Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {},ISO resolved to 19:30 of type java.time.format.Parsed
        at java.time.LocalDate.from(LocalDate.java:362)
        at java.time.-$$Lambda$Bq8PKq1YWr8nyVk9SSfRYKrOu4A.queryFrom(Unknown Source:0)
        at java.time.format.Parsed.query(Parsed.java:226)
        at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
        at java.time.LocalDate.parse(LocalDate.java:394) 
        at com.example.leetcodedraft.Test.startTest(Test.kt:16) 
        at com.example.leetcodedraft.MainActivity.onCreate(MainActivity.kt:36) 
        at android.app.Activity.performCreate(Activity.java:7136) 
        at android.app.Activity.performCreate(Activity.java:7127) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
wi3ka0sx

wi3ka0sx1#

过了一段时间,我解决了这个问题:

@RequiresApi(Build.VERSION_CODES.O)
    private fun setLessonDuration(startTime: String, endTime: String): String {

        val start = LocalTime.parse(startTime)
        val finish = LocalTime.parse(endTime)
        val newDate = Duration.between(start, finish).toKotlinDuration()

        var time = if (newDate.inWholeMinutes.toInt() > 60) {
            "${newDate.inWholeMinutes.toInt() / 60}ч.${newDate.inWholeMinutes.toInt() - 60}мин."
        } else if (newDate.inWholeMinutes.toInt()%60 == 0) {
            "${newDate.inWholeHours}ч."
        } else "${newDate.inWholeMinutes}мин."

        return time
    }

接收数据的示例:“1ч.45ми н.,“2ч.",“15ми н.”谢谢你的动力,伙计们!

相关问题