为什么这段代码在 intellij 中运行良好,但在 Android Studio 中运行时抛出异常?
fun main() {
val date = "Wed Mar 31 17:27:07 ICT 2021"
println(date.toEpochSecond())
}
fun String.toEpochSecond(): Long {
val dbResponsePattern = "EEE MMM dd HH:mm:ss z yyyy"
val dateTimeFormatter = DateTimeFormatter.ofPattern(dbResponsePattern)
return ZonedDateTime.parse(this, dateTimeFormatter).toInstant().epochSecond
}
还有这个
fun main() {
val date = "Wed Mar 31 17:27:07 ICT 2021"
println(date.toEpochSecond())
}
fun String.toEpochSecond(): Long {
val dbResponsePattern = "EEE MMM dd HH:mm:ss z yyyy"
val dateTimeFormatter = DateTimeFormatter.ofPattern(dbResponsePattern)
return Date.from(
LocalDateTime.parse(this, dateTimeFormatter)
.atZone(ZoneId.systemDefault())
.toInstant()
).toInstant().epochSecond
}
发生异常时,文本与我在这里使用的文本完全相同。
例外:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wo1f.the_earth, PID: 16409
java.time.format.DateTimeParseException: Text 'Wed Mar 31 17:27:07 ICT 2021' could not be parsed at index 20
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:486)
at com.wo1f.base_android.DateMapperKt.toEpochSecond(DateMapper.kt:17)
at com.wo1f.domain.usecases.SyncProfile.syncPost(SyncProfile.kt:46)
at com.wo1f.domain.usecases.SyncProfile.access$syncPost(SyncProfile.kt:15)
at com.wo1f.domain.usecases.SyncProfile$invoke$2$1$invokeSuspend$$inlined$collect$1.emit(Collect.kt:144)
at com.wo1f.domain.usecases.SyncProfile$invoke$2$1$invokeSuspend$$inlined$collect$1$1.invokeSuspend(Unknown Source:15)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)