我有两个字符串时间。我怎样才能得到这两个时间之间的持续时间? 我尝试使用 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)
一段时间后,我接下来解决了这个问题:
@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мин.” 感谢你们的鼓励,伙计们!