I am experiencing a problem with the code below. It’s converting milliseconds to months, days, hours, and minutes.
long diffms = date2l - date1l; //The result here is in milliseconds; The value of date2l - date1l are different
long diff_minute = diffms / 60000;
long diff_hour = diff_minute / 60; float diff_minute_now = (diff_minute % 1) * 60; int dmn = (int) diff_minute_now;
long diff_day = diff_hour / 24; float diff_hour_now = (diff_hour % 1) * 24; int dhn = (int) diff_hour_now;
long diff_month = diff_day / 30; float diff_day_now = (diff_day % 1) * 30; int ddn = (int) diff_day_now;
diffe = new LabelField
("Remaining Time : " + Long.toString(diff_month) + " month(s) "
+ Integer.toString(ddn) + " day(s) "
+ Integer.toString(dhn) + " hour(s) "
+ Integer.toString(dmn) + " minute(s)");
add(diffe);
Why are the result values all zeroes?
EDIT:
@BicycleDude I modify your code into:
long diffms = date2l - date1l;
long ts = diffms / 1000;
long mo = ts / 60 / 60 / 24 / 30;
long d = (ts - mo * 30 * 24 * 60 * 60) / (60 * 60 * 24);
long h = (ts - d * 24 * 60 * 60) / (60 * 60);
long m = (ts - h * 60 * 60) / 60;
But the hours doesn’t work
I’m going to base my answer on the edited part of the code, since BicycleDude has already pointed out what was wrong with the modulo operations.
Alright, I thought there would be multiple problems, but I think (think, I’m probably missing something…) that the main error is coming from your calculation of months, which I denoted in the code by the comment
(1). You can’t calculate the difference in months like that.Why? What about if there was more than one month in differences? Wouldn’t you need to divide by 31, instead? Or if it was a leap year? You’d need to divide by 29 if it was February. Since integer division doesn’t round or account for decimals, you can get inaccuracies in your difference-in-months calculation. It’s probably best if you instead use the differences in hours to calculate the difference in days, and from there you can figure out the difference in months. (Edit: I think you’d also need to take into account the factors I mentioned above when calculating the difference in months from the difference in days, by checking what your “origin” and “target” dates are, though I wouldn’t be too sure of writing it myself at thie stage…)
Since you have a problem with the way you calculate the difference in months, and you use the erroneous value to calculate the difference in days, I think this results in the error propagating down to some of your other values (eg. the difference in hours).
EDIT
Okay, I mucked around with the code a bit more. As I noted earlier, your months calculation was dangerous and affected your days calculation, which potentially introduced errors later on.
With the example you provided in the comments, your code had a discrepancy of 3 days in the number of days. (The example was 19 January 2012 to 3 May 2012). I ran this against BicycleDude’s code and it was fine.
I’ll repost the code, and I’ve just added one line to find the number of days, based on how many hours have passed.
If you like to make it so that you can read that “there are
wdays,xhours,yminutes andzseconds between date2l and date1l”, you could do something like this:And it appears to work.
I haven’t figured out the months part because that’s a lot more non-trivial, but yeah. This kinda works?