kotlindatejava-timedatetime-parsingtimeofday

Get period between two times


I have two string time. How can I get a duration of period between theese two time? I try to use DateTimeFormatter, but it doesn't work.

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) 

I think it needs to convert both date to millis and then to do a substraction. But I can't understand how to do it! Your advice, please?!

ErrorInfo:

Process: 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) 

Solution

  • After some time, I solved this issue next:

    @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
        }
    

    Examples of received data: "1ч.45мин., "2ч.", "15мин."

    Changes compared to the original code in the question include: