code

Motorola 장치 : ThreeTen에서 날짜 구문 분석시 org.threeten.bp.DateTimeException

codestyles 2020. 12. 26. 10:04
반응형

Motorola 장치 : ThreeTen에서 날짜 구문 분석시 org.threeten.bp.DateTimeException


좀 모토로라 장치에 매우 이상한 행동이 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);
}

다음 장치에서만 (6.0을 실행하는 3 대의 Motorola 장치 만) :

이 충돌이 있습니다.

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 월 초부터 일어난 일입니다. 누구든지 그 문제에 직면 한 적이 있습니까?


대신 다음을 사용하십시오.

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

이 시도:

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)

이것이 당신을 도울 것입니다 :)


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"

ReferenceURL : https://stackoverflow.com/questions/35043000/motorola-devices-org-threeten-bp-datetimeexception-when-parsing-a-date-in-thre

반응형