2

I am parsing a String to LocalDateTime (by ThreeTen, not the Java one) with this method.

for input: 28-Aug-20 10:37:41 AM BST

  private fun toLocalDate(date: String?): LocalDateTime? {
        return try {
            val inputFormat = DateTimeFormatter.ofPattern("dd-MMM-yy hh:mm:ss a")
            val formattedDate = date?.replace(" BST", "")
            LocalDateTime.parse(formattedDate,inputFormat)
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }

    }

Now the issue is, This method is works fine on Android Emulator (13) and some other devices (11/12) but getting a crash in particular device Samsung Galaxy S21 FE (13). I have no Idea why this code is not working on this device.

Here's a crash log

org.threeten.bp.format.DateTimeParseException: Text '28-Aug-20 10:37:41 AM' could not be parsed at index 19
21:15:37.341 System.err       W     at org.threeten.bp.format.DateTimeFormatter.parseToBuilder(DateTimeFormatter.java:1588)
21:15:37.341 System.err       W     at org.threeten.bp.format.DateTimeFormatter.parse(DateTimeFormatter.java:1491)
21:15:37.341 System.err       W     at org.threeten.bp.LocalDateTime.parse(LocalDateTime.java:444)
21:15:37.341 System.err       W     at dev.souravdas.taaka.MainVM.toLocalDate(MainVM.kt:81)
21:15:37.341 System.err       W     at dev.souravdas.taaka.MainVM.getDebitTransaction(MainVM.kt:72)
21:15:37.341 System.err       W     at dev.souravdas.taaka.MainVM$getAllTxns$1.invokeSuspend(MainVM.kt:40)
21:15:37.341 System.err       W     at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
21:15:37.341 System.err       W     at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:367)
21:15:37.341 System.err       W     at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
21:15:37.341 System.err       W     at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:25)
21:15:37.341 System.err       W     at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:110)
21:15:37.341 System.err       W     at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
21:15:37.341 System.err       W     at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
21:15:37.341 System.err       W     at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
21:15:37.341 System.err       W     at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
21:15:37.341 System.err       W     at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source:1)
21:15:37.341 System.err       W     at dev.souravdas.taaka.MainVM.getAllTxns(MainVM.kt:33)
21:15:37.341 System.err       W     at dev.souravdas.taaka.MainVM.<init>(MainVM.kt:28)
21:15:37.341 System.err       W     at java.lang.reflect.Constructor.newInstance0(Native Method)
21:15:37.341 System.err       W     at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
21:15:37.341 System.err       W     at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:202)
21:15:37.341 System.err       W     at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:324)
21:15:37.341 System.err       W     at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:306)
21:15:37.341 System.err       W     at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:280)
21:15:37.341 System.err       W     at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.kt:128)
21:15:37.341 System.err       W     at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187)
21:15:37.341 System.err       W     at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153)
21:15:37.341 System.err       W     at androidx.lifecycle.viewmodel.compose.ViewModelKt.get(ViewModel.kt:215)
21:15:37.341 System.err       W     at androidx.lifecycle.viewmodel.compose.ViewModelKt.viewModel(ViewModel.kt:156)
21:15:37.341 System.err       W     at dev.souravdas.taaka.HomeKt.Home(Home.kt:277)
21:15:37.341 System.err       W     at dev.souravdas.taaka.ComposableSingletons$MainActivityKt$lambda-1$1.invoke(MainActivity.kt:42)
21:15:37.341 System.err       W     at dev.souravdas.taaka.ComposableSingletons$MainActivityKt$lambda-1$1.invoke(MainActivity.kt:41)
21:15:37.341 System.err       W     at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
21:15:37.341 System.err       W     at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
21:15:37.341 System.err       W     at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
21:15:37.341 System.err       W     at androidx.compose.material3.TextKt.ProvideTextStyle(Text.kt:261)
21:15:37.341 System.err       W     at androidx.compose.material3.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:81)
21:15:37.341 System.err       W     at androidx.compose.material3.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:80)
21:15:37.341 System.err       W     at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
21:15:37.341 System.err       W     at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
21:15:37.341 System.err       W     at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
21:15:37.341 System.err       W     at androidx.compose.material3.MaterialThemeKt.MaterialTheme(MaterialTheme.kt:73)
21:15:37.341 System.err       W     at dev.souravdas.taaka.ui.theme.ThemeKt.TaakaTheme(Theme.kt:109)
21:15:37.341 System.err       W     at dev.souravdas.taaka.ComposableSingletons$MainActivityKt$lambda-2$1.invoke(MainActivity.kt:41)
21:15:37.341 System.err       W     at dev.souravdas.taaka.ComposableSingletons$MainActivityKt$lambda-2$1.invoke(MainActivity.kt:40)
21:15:37.341 System.err       W     at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
21:15:37.341 System.err       W     at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:404)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:250)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:249)
21:15:37.341 System.err       W     at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
21:15:37.341 System.err       W     at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
21:15:37.341 System.err       W     at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:177)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:123)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:122)
21:15:37.341 System.err       W     at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
21:15:37.341 System.err       W     at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
21:15:37.341 System.err       W     at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.ProvideAndroidCompositionLocals(AndroidCompositionLocals.android.kt:114)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:157)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:156)
21:15:37.341 System.err       W     at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
21:15:37.341 System.err       W     at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
21:15:37.341 System.err       W     at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:156)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:140)
21:15:37.341 System.err       W     at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
21:15:37.341 System.err       W     at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
21:15:37.341 System.err       W     at androidx.compose.runtime.ActualJvm_jvmKt.invokeComposable(ActualJvm.jvm.kt:78)
21:15:37.341 System.err       W     at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:3248)
21:15:37.341 System.err       W     at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:3238)
21:15:37.341 System.err       W     at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:341)
21:15:37.341 System.err       W     at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source:1)
21:15:37.341 System.err       W     at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3238)
21:15:37.341 System.err       W     at androidx.compose.runtime.ComposerImpl.composeContent$runtime_release(Composer.kt:3173)
21:15:37.341 System.err       W     at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:587)
21:15:37.341 System.err       W     at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:950)
21:15:37.341 System.err       W     at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:519)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:140)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:131)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.android.kt:1060)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.android.kt:131)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.android.kt:182)
21:15:37.341 System.err       W     at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.kt:314)
21:15:37.341 System.err       W     at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.kt:192)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:138)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:131)
21:15:37.341 System.err       W     at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.android.kt:1147)
21:15:37.341 System.err       W     at android.view.View.dispatchAttachedToWindow(View.java:22479)
21:15:37.341 System.err       W     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3686)
21:15:37.341 System.err       W     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3695)
21:15:37.341 System.err       W     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3695)
21:15:37.341 System.err       W     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3695)
21:15:37.341 System.err       W     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3695)
21:15:37.341 System.err       W     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3669)
21:15:37.341 System.err       W     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:3116)
21:15:37.341 System.err       W     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:10885)
21:15:37.342 System.err       W     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1301)
21:15:37.342 System.err       W     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1309)
21:15:37.342 System.err       W     at android.view.Choreographer.doCallbacks(Choreographer.java:923)
21:15:37.342 System.err       W     at android.view.Choreographer.doFrame(Choreographer.java:852)
21:15:37.342 System.err       W     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1283)
21:15:37.342 System.err       W     at android.os.Handler.handleCallback(Handler.java:942)
21:15:37.342 System.err       W     at android.os.Handler.dispatchMessage(Handler.java:99)
21:15:37.342 System.err       W     at android.os.Looper.loopOnce(Looper.java:226)
21:15:37.342 System.err       W     at android.os.Looper.loop(Looper.java:313)
21:15:37.342 System.err       W     at android.app.ActivityThread.main(ActivityThread.java:8757)
21:15:37.342 System.err       W     at java.lang.reflect.Method.invoke(Native Method)
21:15:37.342 System.err       W     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
21:15:37.342 System.err       W     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

I've tried changing to LocalDate, OffsetDateTime, Adding Locale on DateTimeFormatter but did not work

Sourav
  • 312
  • 1
  • 8
  • Remove the a and the AM. – blackapps Apr 16 '23 at 15:28
  • Try with a `DateTimeFormatter` that is able to `parseCaseInsensitive()`… use a `new DateTimeFormatterBuilder()` to create one. – deHaar Apr 16 '23 at 15:31
  • Parse this to a `ZonedDateTime` **and** double check if it parses your zone name, which is ambiguous. Add a `Locale.ENGLISH` to prevent the use of the default `Locale`, which might have a problem with upper case am/pm of day. So add a `z` to the pattern (last character, can be used to parse `BST` to the zone id `Pacific/Bougainville`). – deHaar Apr 16 '23 at 15:49
  • 1
    @blackapps That will not work. – Ole V.V. Apr 16 '23 at 16:14
  • 1
    *Adding Locale on DateTimeFormatter but did not work* Which locale did you add? I assure you that adding the correct locale is the way forward. – Ole V.V. Apr 16 '23 at 16:18
  • 1
    As an aside, the `BST` that you are removing, is that for Brazil Summer Time, Bangladesh Standard Time, Bougainville Standard Time, British Summer Time or something else? If you know which, you can parse it as a time zone abbreviation, which you should prefer, also when it is basically ambiguous. – Ole V.V. Apr 16 '23 at 16:21
  • hello @OleV.V. I've tried using Locale like this `DateTimeFormatter.ofPattern("dd-MMM-yy hh:mm:ss a", Locale.getDefault())` . Did I do it wrong? – Sourav Apr 16 '23 at 16:24
  • @OleV.V. I do know what BST stands for in this scenario. But don't know how to use this or what to do with it. Can you point me to a solution for this? I would appreciate it – Sourav Apr 16 '23 at 16:26
  • 1
    *Did I do it wrong?* Yes. You need to give the locale that the string is using, not the locale that the user is using. See [this question](https://stackoverflow.com/questions/63288135/convert-string-to-localdatetime-in-java). For the time zone abbreviation see [IST mapped to wrong ZoneId in java.time library](https://stackoverflow.com/questions/57733849/ist-mapped-to-wrong-zoneid-in-java-time-library). – Ole V.V. Apr 16 '23 at 16:32

1 Answers1

2

val inputFormat = DateTimeFormatter.ofPattern("dd-MMM-yy hh:mm:ss a", Locale.ENGLISH)

This solved my issue.

Sourav
  • 312
  • 1
  • 8