获取两次之间的周期

Get period between two times

提问人:Jekis Osipov 提问时间:5/1/2023 最后编辑:AnonymousJekis Osipov 更新时间:5/23/2023 访问量:276

问:

我有两个字符串时间。我怎样才能获得两个时间之间的持续时间? 我尝试使用 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) 

我认为它需要将两个日期都转换为 millis,然后进行减法。但我不明白该怎么做!请问你的建议?!

错误信息:

进程: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) 
Kotlin 日期 java 时间 datetime-parsesing timeofday

评论

2赞 Abra 5/1/2023
您正在分析一个字符串,该字符串将一天中的某个小时表示为日期。你为什么认为这应该有效?也许你应该使用类而不是类?LocalDateTimeLocalDate
2赞 Jorn 5/1/2023
你的问题是“我如何解析这个时间字符串?
4赞 Abra 5/1/2023
Duration.between(LocalTime.parse("19:30"), LocalTime.parse("20:30"))
2赞 Halil Ozel 5/1/2023
应添加错误信息。
2赞 Louis Wasserman 5/2/2023
@Abra:这没有道理。 用于物理时间; 是民用时间。如果你想要一个 ,你需要通过一个 转换为实数,它可以知道这两个时间点之间是否存在 DST 跳转之类的事情。DurationLocalTimeDurationInstantZoneId

答:

2赞 Jekis Osipov 5/6/2023 #1

一段时间后,我接下来解决了这个问题:

@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мин.”

与问题中的原始代码相比,更改包括:

  • 我用 代替 .因为 a 是没有时间的日期,而是一天中没有日期的时间。LocalTimeLocalDateLocalDateLocalTime
  • 我使用 Kotlin 而不是 Java 。 表示年、月和日,而表示小时、分钟、秒和秒的分数,而 Kotlin 的功能比 java.time 中的功能多一点,并且命名更好。DurationPeriodPeriodDurationDurationinWholeMinutes