LocalDateTime 无法解析特定设备。

huangapple go评论61阅读模式
英文:

LocalDateTime not parsing for a specfic device

问题

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
...

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

英文:

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

答案1

得分: 2

This solved my issue.

英文:

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

This solved my issue.

huangapple
  • 本文由 发表于 2023年4月16日 23:23:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定