1

I'm trying to calculate the difference between 2 times. It works well, unless the two dates are on each side of midnight. In that case I get a negative answer. Any ideas how I can improve the code to fix this issue?

String sleepStart = editFallAsleepTime.getText().toString();
                String sleepStop = editWakeUpTime.getText().toString();
                String awakeTimeString = ((Globals) getActivity().getApplication()).getAwakeTime();

//HH converts hour in 24 hours format (0-23), day calculation
                SimpleDateFormat format = new SimpleDateFormat("HH:mm");

                Date d1 = null;
                Date d2 = null;

                try
            {
                d1 = format.parse(sleepStart);
                d2 = format.parse(sleepStop);

                float t1 = d1.getTime();
                float t2 = d2.getTime();

                if( t2 <= t1 ){
                    t2 +=24;
                }

                float diff = t2 - t1;

                //in milliseconds
                    float diff = d2.getTime() - d1.getTime();
                    float diffHours = diff / (60 * 60 * 1000);
                    float awakeTime = Float.valueOf(awakeTimeString);
                    float awakeHours = awakeTime / 60;
                    float calcEffectiveSleep = diffHours - awakeHours;
                    String sleepTime = Float.toString(diffHours);
                    String effectiveSleep = Float.toString(calcEffectiveSleep);
                    ((Globals) getActivity().getApplication()).setEffectiveSleep(effectiveSleep);
                }
                catch (Exception e)
                {
                    Log.e("timediff","didntwork");
                }
  • Look at the answers here: http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances – alzee Jul 09 '16 at 23:01
  • You are only working with a time. So u are assuming both times are at the same day ? And your problem only happens if end – Andre Classen Jul 09 '16 at 23:08
  • No, I am not assuming both times are at the same day. I should be assuming they are not if the first time is before 23.59.. Should I put the date in there somehow? – Rune Langaard Jul 09 '16 at 23:28

1 Answers1

1

If your difference under 1 day interval then you can consider handling d2 < d1 case as @Andre Classen said. How to do is:

t1 = d1.getTime();
t2 = d2.getTime();

if( t2 <= t1 ){
   t2 +=24*60*60*1000;
}

diff = t2 - t1; 
ugur
  • 3,604
  • 3
  • 26
  • 57
  • Tried that now, but it didn't work. Maybe I put it in wrong? I agree that this must be the solution, I maybe just did it wrong.. Edited my code above to show how I implemented it. – Rune Langaard Jul 10 '16 at 00:43
  • you edited and mixed everything up. what is format 2, bedtime, getUptime, d3, d4 and where do you print diff2? it is not clear in edited code. it is better if you put what error you get, or what values you tried and what output you get. – ugur Jul 10 '16 at 01:17
  • Sorry, I copied the wrong part and pasted it without looking. Its right now. I do this for several different numbers, thats why I got it wrong. Should be right now. I think I am getting a bit tired.. – Rune Langaard Jul 10 '16 at 01:29
  • still wrong you should remove "float diff = d2.getTime() - d1.getTime();" line. I guess it is your bed time if you didnt calculate wrong :) lets discuss tomorrow. – ugur Jul 10 '16 at 01:37
  • Hi. Got some sleep, hope my head is clearer now. I removed that line. But still it does nothing. The value still comes out wrong. – Rune Langaard Jul 10 '16 at 12:19
  • I got some sleep too and noticed that we are adding 24 hours milliseconds so it doesnt effect the result. I edited my answer as 24*60*60*1000. – ugur Jul 10 '16 at 13:28