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

为君一笑 提交于 2019-12-03 09:17:31
Daniel Raouf

Use the following instead

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

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:)

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.

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");
        }

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.

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"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!