Search code examples
androidjodatimeandroid-jodatime

Joda Time .isAfter() producing incorrect result


I am getting some strange results from Joda Time and I am not sure what is causing it.

I am comparing two datetimes (alarm end and stop time) to ensure the user doesn't set an alarm to end before it starts.

I used the is .isAfter() but it seem to be giving me an incorrect result.

This is the if statement:

if (MainActivity.alarmStartDateTime.isAfter(MainActivity.alarmEndDateTime)) {
                nextReminderTextView1.setText("WARNING THE ALARM END DATE & TIME IS BEFORE THE ALARM DATE & TIME");
                alarmSetToEndBeforeItStarts = true;

            }

This was the start and end datetimes:

MainActivity.alarmStartDateTime = 2016-11-21T00:26:45.183+10:00
MainActivity.alarmEndDateTime = 2016-11-21T00:30:00.000+10:00

And this was the result:

alarmSetToEndBeforeItStarts = true

Thanks in advance for you help


Solution

  • I can't replicate your problem:

    DateTime alarmStart = DateTime.parse("2016-11-21T00:26:45.183+10:00");
    DateTime alarmEnd = DateTime.parse("2016-11-21T00:30:00.000+10:00");
    System.out.println(alarmStart.isAfter(alarmEnd));
    

    Has the following output:

    false
    

    You need to post the minimum test to replicate the issue if you think there is a bug in isAfter(). This is extremely unlikely as Joda is a pedigree library with a high degree of test coverage used in many projects around the world.

    I would suggest instead the following: perhaps alarmSetToEndBeforeItStarts is a class field and has been set by a previous call to your quoted if statement. This can happen easily if you are not careful with lifecycles in Android.