Motorola devices : org.threeten.bp.DateTimeException when parsing a date in ThreeTen

后端 未结 6 1799
猫巷女王i
猫巷女王i 2021-02-11 12:23

I have a very weird behaviour on some Motorola devices where LocalDateTime.now() is returning 0000-00-00T00:00:00.0 with ThreeTenABP.

The code

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-11 12:44

    This is a known bug on old TBP: Threetenbp - Date formatting fails on some Android devices But it is resolved on ThreeTenABP that you're using.

    These three lines pretty much tell you all:

    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
    

    You've passed '0000-00-00T00:00:00.8' as the argument.

    I've solved this by using ZonedDateTime (also advised to be the safest one to use, and recommended by even Google) instead of LocalDateTime.

    And one stupid question before you do anything on ThreeTenABP. Did you init ThreeTenABP in your Application class?

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

    Also, DO NOT use any other Date/Time lib, as they are all deprecated, the only two supported right now are ThreeTenABP (if you need app running on devices prior to API 26) and java.time (API 26+), none other. Not to talk about performance issues with other old libraries.

提交回复
热议问题