2016-01-27 69 views
36

我在一些摩托羅拉設備上有一個非常奇怪的行爲,其中LocalDateTime.now()返回0000-00-00T00:00:00.0ThreeTenABP摩托羅拉設備:org.threeten.bp.DateTimeException解析ThreeTen中的日期

的代碼如下:

@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月初。任何人都曾經遇到過這個問題?

+0

也許如本[SO-問題]中所述的相同問題(http://stackoverflow.com/questions/34691990/weird-org-threeten-bp-datetimeexception-拋出),另請參閱我的答案。 –

+0

你放棄了lib嗎?這個問題非常煩人。 –

+0

@MenoHochschild感謝相關問題,雖然沒有提及摩托羅拉和Android 6出於某種原因,但它看起來非常相似 –

回答

0

使用以下代替

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