0

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

Nicholas Muir
  • 2,897
  • 8
  • 39
  • 89
  • I also just experienced this issue as well - and one thing that may be hidden is where the TimeZone is set for these instances of DateTime upon their creation. Even when parsing with a DateTimeFormatter, you aren't guaranteed to have the same time zone, it may be worth checking that out if you get a similar issue. – NateH06 Oct 16 '18 at 19:41

1 Answers1

1

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.

David Rawson
  • 20,912
  • 7
  • 88
  • 124