【问题标题】:Motorola devices : org.threeten.bp.DateTimeException when parsing a date in ThreeTen摩托罗拉设备:在 ThreeTen 中解析日期时出现 org.threeten.bp.DateTimeException
【发布时间】:2016-01-27 16:37:02
【问题描述】:

我在某些摩托罗拉设备上有一个非常奇怪的行为,其中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 月初。有人遇到过这个问题吗?

【问题讨论】:

  • 可能与SO-question 中描述的问题相同,另请参阅我的答案。
  • 你放弃了这个库吗?这样的问题非常烦人。
  • @MenoHochschild 感谢您提出相关问题,尽管出于某种原因没有提及摩托罗拉和 Android 6,但它看起来非常相似
  • @ar-g 我们仍在使用它,它仍然在崩溃,但我相当有信心有一天我们会修复它。我首先尝试重现该问题以便能够正确调试。
  • 为了帮助诊断和调试,如果您使用我的库 Time4A 并将 LocalDateTime.now() 替换为 SystemClock.inLocalView().now() 那么您可能能够确定哪个特定的时钟值变得疯狂,因为 Time4A 进行了正确的验证。

标签: android motorola threetenbp


【解决方案1】:

改用下面的

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

【讨论】:

    【解决方案2】:

    试试这个:

    SimpleDateFormat 类适用于 java.util.Date 实例。

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    
    String dateString = format.format( new Date()   );
    Date   date       = format.parse ( "2009-12-31" ); 
    

    以下是您可以使用的最常见的模式字母列表

    y   = year   (yy or yyyy)
    M   = month  (MM)
    d   = day in month (dd)
    h   = hour (0-12)  (hh)
    H   = hour (0-23)  (HH)
    m   = minute in hour (mm)
    s   = seconds (ss)
    S   = milliseconds (SSS)
    z   = time zone  text        (e.g. Pacific Standard Time...)
    Z   = time zone, time offset (e.g. -0800)
    

    这里有一些模式示例

    yyyy-MM-dd           (2009-12-31)
    
    dd-MM-YYYY           (31-12-2009)
    
    yyyy-MM-dd HH:mm:ss  (2009-12-31 23:59:59)
    
    HH:mm:ss.SSS         (23:59.59.999)
    
    yyyy-MM-dd HH:mm:ss.SSS   (2009-12-31 23:59:59.999)
    
    yyyy-MM-dd HH:mm:ss.SSS Z   (2009-12-31 23:59:59.999 +0100)
    

    这可能对你有帮助吗:)

    【讨论】:

      【解决方案3】:

      这是旧 TBP 上的一个已知错误:Threetenbp - Date formatting fails on some Android devices 但它已在您使用的 ThreeTenABP 上解决。

      这三行几乎可以告诉你一切:

      1.

      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
      

      2.

      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
      

      3.

      Caused by org.threeten.bp.DateTimeException: Invalid value for MonthOfYear (valid values 1 - 12): 0
      

      您已将 '0000-00-00T00:00:00.8' 作为参数传递。

      我已经通过使用 ZonedDateTime(也建议使用它是最安全的,甚至 Google 推荐)而不是 LocalDateTime 解决了这个问题。

      在你对 ThreeTenABP 做任何事情之前,还有一个愚蠢的问题。 您是否在 Application 类中初始化了 ThreeTenABP?

      public class App extends Application {
      
          @Override
          public void onCreate() {
              super.onCreate();
      
              AndroidThreeTen.init(this); // Without this ThreeTenABP cannot work properly
          }
      }
      

      此外,请勿使用任何其他日期/时间库,因为它们都已弃用,目前仅支持 ThreeTenABP(如果您需要在 API 26 之前的设备上运行应用程序)和 java.time( API 26+),仅此而已。不谈其他老库的性能问题。

      【讨论】:

        【解决方案4】:

        试试这个:-

        String dateFormat = "HH:mm:ss MM/dd/uuuu";
                String dateString = "11:30:59 02/31/2015";
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter
                    .ofPattern(dateFormat, Locale.US)
                    .withResolverStyle(ResolverStyle.STRICT);
                try {
                    LocalDateTime date = LocalDateTime.parse(dateString, dateTimeFormatter);
                    System.out.println(date);
                } catch (DateTimeParseException e) {
                    // Throw invalid date message
                    System.out.println("Exception was thrown");
                }
        

        【讨论】:

          【解决方案5】:

          您会看到错误提示“MonthOfYear 的值无效(有效值 1 - 12)” MonthOfYear 以零 (0) 开头。所以无论你在哪里使用 MonthOfYear 加 1。所以它的格式是正确的。

          【讨论】:

            【解决方案6】:

            您可以使用这种格式来解析日期 txtldate.setText(Utility.convertMilliSecondsToFormatedDate(leedsModel.getCreatedDateTimeLong(), GLOBAL_DATE_FORMATE));

            txtldate 是一个编辑文本,也可能是文本视图,而 GLOBAL_DATE_FORMATE 是一个值为“dd MMM yyyy”的常量

            【讨论】:

              猜你喜欢
              • 2016-04-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-01-07
              • 2012-07-16
              相关资源
              最近更新 更多