javaandroiddatetimejodatimeandroid-jodatime

Cannot get the correct difference between two time stamps using Joda time


I am working with Joda time android and when I try to get the difference between two time stamps, the value increments from 0 to 59 and it never goes beyond that.

I am using https://github.com/dlew/joda-time-android

Any pointers would be much appreciated.

here is what I have

DateTime currentTime = new DateTime();
                Period difference = new Period(calculator.getLastTotalizerChangeTime(meterMeasurement.getFlowMeterId()), currentTime);
                Log.d("totaliserSenseCheck", "fcalculator.getLastTotalizerChangeTime() - " + calculator.getLastTotalizerChangeTime(meterMeasurement.getFlowMeterId())
                        + " currentTime - " + currentTime
                        + " difference - " + difference.getSeconds());

Logs fcalculator.getLastTotalizerChangeTime() is not changed and only the current time changes but the difference is never above 59 and resets to 0

D/totaliserSenseCheck: fcalculator.getLastTotalizerChangeTime() - 2019-09-12T16:03:40.002+01:00 currentTime - 2019-09-12T16:03:42.730+01:00 difference - 2
D/totaliserSenseCheck: fcalculator.getLastTotalizerChangeTime() - 2019-09-12T16:03:40.002+01:00 currentTime - 2019-09-12T16:03:47.335+01:00 difference - 7
D/totaliserSenseCheck: fcalculator.getLastTotalizerChangeTime() - 2019-09-12T16:03:40.002+01:00 currentTime - 2019-09-12T16:03:51.113+01:00 difference - 11
D/totaliserSenseCheck: fcalculator.getLastTotalizerChangeTime() - 2019-09-12T16:03:40.002+01:00 currentTime - 2019-09-12T16:04:00.793+01:00 difference - 20
D/totaliserSenseCheck: fcalculator.getLastTotalizerChangeTime() - 2019-09-12T16:03:40.002+01:00 currentTime - 2019-09-12T16:04:40.369+01:00 difference - 0
D/totaliserSenseCheck: fcalculator.getLastTotalizerChangeTime() - 2019-09-12T16:03:40.002+01:00 currentTime - 2019-09-12T16:04:51.574+01:00 difference - 11
D/totaliserSenseCheck: fcalculator.getLastTotalizerChangeTime() - 2019-09-12T16:03:40.002+01:00 currentTime - 2019-09-12T16:05:08.914+01:00 difference - 28
D/totaliserSenseCheck: fcalculator.getLastTotalizerChangeTime() - 2019-09-12T16:03:40.002+01:00 currentTime - 2019-09-12T16:05:36.840+01:00 difference - 56
D/totaliserSenseCheck: fcalculator.getLastTotalizerChangeTime() - 2019-09-12T16:03:40.002+01:00 currentTime - 2019-09-12T16:05:39.070+01:00 difference - 59
D/totaliserSenseCheck: fcalculator.getLastTotalizerChangeTime() - 2019-09-12T16:03:40.002+01:00 currentTime - 2019-09-12T16:05:44.526+01:00 difference - 4
D/totaliserSenseCheck: fcalculator.getLastTotalizerChangeTime() - 2019-09-12T16:03:40.002+01:00 currentTime - 2019-09-12T16:09:09.579+01:00 difference - 29
D/totaliserSenseCheck: fcalculator.getLastTotalizerChangeTime() - 2019-09-12T16:03:40.002+01:00 currentTime - 2019-09-12T16:09:43.226+01:00 difference - 3

Solution

  • The method getSeconds() doesn't return total seconds, it returns seconds as a separate field where a time period has been broken down into multiple fields for hours, minutes, seconds, etc. So if a time difference were 10 hours, 11 minutes, and 40 seconds, getSeconds() would only give you the 40 part of that.

    I haven't used Joda time before, but I think the method you want is toStandardSeconds().getSeconds().