58

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 is as follow:

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

Only on these devices (only 3 Motorola devices running 6.0):

I have this crash:

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)

Line 273 is:

LocalDateTime savedTime = LocalDateTime.parse(timeout, DateTimeFormatter.ISO_DATE_TIME);

So basically LocaleDateTime.now() is returning an invalid date time and parsing it fails.

The other interesting thing is that it only happened since beginning of January. Anyone has ever faced that problem?

Romain Piel
  • 11,017
  • 15
  • 71
  • 106
  • Maybe the same problem as described in this [SO-question](http://stackoverflow.com/questions/34691990/weird-org-threeten-bp-datetimeexception-thrown), see also my answer there. – Meno Hochschild Feb 17 '16 at 18:18
  • Did you abandon the lib? Such issue is extremely annoying. – ar-g Feb 19 '16 at 12:07
  • @MenoHochschild Thanks for the related question, it looks very similar although there's no mention of Motorola & Android 6 for some reason – Romain Piel Feb 26 '16 at 10:56
  • @ar-g We're still using it, it's still crashing but I'm fairly confident we'll fix it someday. I'm first trying to reproduce the issue to be able to debug it properly. – Romain Piel Feb 26 '16 at 10:56
  • For helping in diagnosis and debugging, if you use my library Time4A and replace `LocalDateTime.now()` by `SystemClock.inLocalView().now()` then you will probably be able to determine which specific clock value goes mad because Time4A does proper validation. – Meno Hochschild Mar 04 '16 at 04:07
  • FWIW, something seems dreadfully wrong on these devices. In our case, it is formatting a ZonedDateTime that fails, but only for certain values. For example, `Instant.ofEpochMillis(1457636400000L).atZone(ZoneId.getSystemDefault())` should be March 3, 2016, but formatting it to "yyyy-MM-dd" crashes... only on Moto G devices. No answers yet, just another data point. – Ben Mar 05 '16 at 00:21
  • Have you made any progress with your analysis which clock value causes such problems? – Meno Hochschild Apr 14 '16 at 07:27
  • The issue was reported to ThreeTenBp: https://github.com/ThreeTen/threetenbp/issues/50 – Romain Piel Sep 05 '16 at 10:50
  • The reason of the issue is stated in the error log quite clearly. – Chandler Feb 21 '19 at 00:30

6 Answers6

1

Use the following instead

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE,10);
Date date =   calendar.getTime();
AStopher
  • 4,207
  • 11
  • 50
  • 75
Daniel Raouf
  • 2,307
  • 1
  • 22
  • 28
1

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

AndroWaqar
  • 231
  • 2
  • 9
1

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.

Slobodan Antonijević
  • 2,533
  • 2
  • 17
  • 27
0

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");
        }
Sejpal Pavan
  • 130
  • 10
0

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.

Amruta
  • 1
  • 1
0

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"