LocalDateTime 未解析特定设备 [duplicate]

LocalDateTime not parsing for a specfic device [duplicate]

提问人:Sourav 提问时间:4/16/2023 更新时间:4/17/2023 访问量:77

问:

我正在使用此方法将 String 解析为 LocalDateTime(由 ThreeTen,而不是 Java 的字符串)。

输入: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
        }

    }

现在的问题是,此方法在 Android 模拟器 (13) 和其他一些设备 (11/12) 上运行良好,但在特定设备三星 Galaxy S21 FE (13) 中崩溃。我不知道为什么此代码在此设备上不起作用。

下面是崩溃日志

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)

我尝试更改为 LocalDate、OffsetDateTime、在 DateTimeFormatter 上添加区域设置,但没有奏效

Android Kotlin java-time localdate

评论

0赞 blackapps 4/16/2023
删除 a 和 AM。
1赞 Anonymous 4/17/2023
@blackapps 那行不通。
1赞 Anonymous 4/17/2023
在 DateTimeFormatter 上添加区域设置,但不起作用您添加了哪个区域设置?我向你保证,添加正确的语言环境是前进的方向。
1赞 Anonymous 4/17/2023
顺便说一句,您要删除的是巴西夏令时、孟加拉国标准时间、布干维尔标准时间、英国夏令时还是其他时间?如果你知道哪个,你可以把它解析为一个时区缩写,你应该更喜欢哪个,当它基本上是模棱两可的。BST
1赞 Anonymous 4/17/2023
我做错了吗?是的。您需要提供字符串正在使用的区域设置,而不是用户正在使用的区域设置。请参阅此问题。有关时区缩写,请参阅 java.time 库中映射到错误的 ZoneId 的 IST

答:

2赞 Sourav 4/17/2023 #1

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

这解决了我的问题。