gpt4 book ai didi

android - 摩托罗拉设备 : org. threeten.bp.DateTimeException 解析 ThreeTen 中的日期时

转载 作者:IT老高 更新时间:2023-10-28 13:25:24 34 4
gpt4 key购买 nike

我在某些摩托罗拉设备上有一个非常奇怪的行为,其中 LocalDateTime.now() 返回 0000-00-00T00:00:00.0ThreeTenABP .

代码如下:

@Override
protected void onResume() {
super.onResume();
if (!TextUtils.isEmpty(timeout)) {
LocalDateTime savedTime = LocalDateTime.parse(timeout, DateTimeFormatter.ISO_DATE_TIME);
if (LocalDateTime.now().isAfter(savedTime)) {
refresh()
}
}
}

@Override
protected void onPause() {
super.onPause();
LocalDateTime currentTime = LocalDateTime.now().plus(Duration.ofMinutes(10));
timeout = currentTime.format(DateTimeFormatter.ISO_DATE_TIME);
}

仅在这些设备上(仅 3 台运行 6.0 的摩托罗拉设备):

我遇到了这个崩溃:

Fatal Exception: java.lang.RuntimeException: Unable to resume activity {com.myapp/com.myapp.MainActivity}: org.threeten.bp.format.DateTimeParseException: Text '0000-00-00T00:00:00.8' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 0
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3121)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by org.threeten.bp.format.DateTimeParseException: Text '0000-00-00T00:00:00.8' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 0
at org.threeten.bp.format.DateTimeFormatter.createError(DateTimeFormatter.java:1559)
at org.threeten.bp.format.DateTimeFormatter.parse(DateTimeFormatter.java:1496)
at org.threeten.bp.LocalDateTime.parse(LocalDateTime.java:444)
at com.myapp.MainActivity.onResume(MainActivity.java:273)
at android.app.Activity.performResume(Activity.java:6344)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3110)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by org.threeten.bp.DateTimeException: Invalid value for MonthOfYear (valid values 1 - 12): 0
at org.threeten.bp.temporal.ValueRange.checkValidValue(ValueRange.java:278)
at org.threeten.bp.temporal.ChronoField.checkValidValue(ChronoField.java:557)
at org.threeten.bp.LocalDate.of(LocalDate.java:237)
at org.threeten.bp.chrono.IsoChronology.resolveDate(IsoChronology.java:452)
at org.threeten.bp.format.DateTimeBuilder.mergeDate(DateTimeBuilder.java:297)
at org.threeten.bp.format.DateTimeBuilder.resolve(DateTimeBuilder.java:206)
at org.threeten.bp.format.DateTimeFormatter.parse(DateTimeFormatter.java:1491)
at org.threeten.bp.LocalDateTime.parse(LocalDateTime.java:444)
at com.myapp.MainActivity.onPostResume(MainActivity.java:273)
at android.app.Activity.performResume(Activity.java:6344)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3110)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

第 273 行是:

LocalDateTime savedTime = LocalDateTime.parse(timeout, DateTimeFormatter.ISO_DATE_TIME);

所以基本上 LocaleDateTime.now() 返回一个无效的日期时间并解析它失败。

另一件有趣的事情是它只发生在 1 月初。有人遇到过这个问题吗?

最佳答案

改用下面的

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE,10);
Date date = calendar.getTime();

关于android - 摩托罗拉设备 : org. threeten.bp.DateTimeException 解析 ThreeTen 中的日期时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35043000/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com