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

后端 未结 6 1802
猫巷女王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:36

    You can see the error saying "Invalid value for MonthOfYear (valid values 1 - 12)" MonthOfYear starts with zero(0). So wherever you are using MonthOfYear add 1. so it will be in correct format.

    0 讨论(0)
  • 2021-02-11 12:38

    Use the following instead

    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MINUTE,10);
    Date date =   calendar.getTime();
    
    0 讨论(0)
  • 2021-02-11 12:42

    Try this:

    The SimpleDateFormat class works on java.util.Date instances.

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

    Below is a list of the most common pattern letters you can use

    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)
    

    Here are a few pattern examples

    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)
    

    Might this will help you:)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-11 12:46

    try this:-

    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");
            }
    
    0 讨论(0)
  • 2021-02-11 12:49

    You can use this format to parse the date txtldate.setText(Utility.convertMilliSecondsToFormatedDate(leedsModel.getCreatedDateTimeLong(), GLOBAL_DATE_FORMATE));

    txtldate is a edittext or it may be textview and GLOBAL_DATE_FORMATE is a constant with value "dd MMM yyyy"

    0 讨论(0)
提交回复
热议问题