0

I calculating time difference between two dates. One coming from database and another current time.

Date currentDate = new Date();
Date dbDate = loader.getsuccessDate();
DateTime d1 = new DateTime (currentDate.getTime());
DateTime d2 = new DateTime (dbDate.getTime());

System.out.println(Hours.hoursBetween(d1 - d2).getHours);

// gives results as -285 

How to convert the negative results to positive value.

Sean Mickey
  • 7,618
  • 2
  • 32
  • 58
Maana
  • 640
  • 3
  • 9
  • 22
  • 1
    The mathematical conversion would be a multiplication with `-1`, I think... Would that be correct here? I mean is `285` the correct result? I cannot tell you that without example data... – deHaar Nov 12 '19 at 15:36
  • Please post compilable code when not asking about a compile error. I get *The operator - is undefined for the argument type(s) org.joda.time.DateTime, org.joda.time.DateTime*. It means that I am unsure which code exactly is giving you the negative result, a very big disadvantage for anyone trying to answer your question. – Ole V.V. Nov 12 '19 at 18:18

1 Answers1

1

I assume that the result is w.r.t. minute. I would do as following.

int totalTimeInMinutes = Hours.hoursBetween(d1 - d2).getHours;
int hour = 0;
int minute = 0;
if (totalTimeInMinutes < 0)
    totalTimeInMinutes *= -1;

hour = totalTimeInMinutes / 60;
minute = totalTimeInMinutes % 60;