-1

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) 
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Jekis Osipov
  • 133
  • 10
  • 2
    You are parsing a string that represents an hour of the day as a date. Why do you think that should work? Maybe you should use class `LocalDateTime` rather than class `LocalDate`? – Abra May 01 '23 at 13:04
  • 2
    Is your question "how do I parse this time string?" – Jorn May 01 '23 at 13:09
  • 4
    `Duration.between(LocalTime.parse("19:30"), LocalTime.parse("20:30"))` – Abra May 01 '23 at 13:18
  • 2
    You should add error info. – Halil Ozel May 01 '23 at 13:59
  • 2
    @Abra: That does not make sense. `Duration` is for physical time; `LocalTime` is for civil time. If you want a `Duration`, you need to convert to a real `Instant` via a `ZoneId`, which can know things like whether or not there's a DST jump between those two points in time. – Louis Wasserman May 01 '23 at 16:44
  • 2
    Use `LocalTime` instead of `LocalDate` and `Duration` instead of `Period`. The rest should be the same and should work. Because a `LocalDate` is a date without time while `LocalTime` is a time of day without date. And `Period ` is for years, months and days while `Duration` is for hours, minutes, seconds and fraction of second. – Ole V.V. May 01 '23 at 19:45

1 Answers1

2

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:

  • I use LocalTime instead of LocalDate. Because a LocalDate is a date without time while LocalTime is a time of day without date.
  • I use a Kotlin Duration instead of Java Period. Period is for years, months and days while Duration is for hours, minutes, seconds and fraction of second, and Kotlin’s Duration has a little more functionality than the one from java.time and better naming of inWholeMinutes.
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Jekis Osipov
  • 133
  • 10