为什么这次单位转换返回零?
我在使用下面的代码时遇到问题。它将毫秒转换为月、日、小时和分钟。
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);
为什么结果值全为零?
编辑: @BicycleDude我将你的代码修改为:
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;
但是时间不起作用
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
'anything % 1' 将返回 0。这可能不是您想要的。
我假设一个月有 31 天,重新设计了公式:
'anything % 1' will return 0. it's probably not what you intended.
I've reworked the formulas with the assumption there are 31 days in a month:
TimeUnit 类提供了工厂方法,简化您的大部分工作:
The TimeUnit class provides factory methods that simplifies most of your work:
我将根据代码的编辑部分来回答,因为 BicycleDude 已经指出了模运算的问题。
好吧,我认为会有多个问题,但我认为(认为,我可能遗漏了一些东西......)主要错误来自您对月份的计算,我在代码通过注释
(1)
。你无法像这样计算几个月的差异。为什么?如果差异超过一个月怎么办?难道你不需要除以 31 吗?或者如果是闰年?如果是二月,则需要除以 29。由于整数除法不会四舍五入或考虑小数,因此月差计算可能会不准确。最好使用小时数差异来计算天数差异,然后您可以计算出月数差异。 (编辑:我认为在计算月数与天数的差异时,您还需要考虑我上面提到的因素,通过检查您的“起始”日期和“目标”日期是,虽然我不太确定在这个阶段自己写它......)
由于您计算月份差异的方式有问题,并且您使用错误的值来计算天数差异,我认为这会导致错误传播到您的某些其他值(例如小时数差异)。
编辑
好吧,我又对代码进行了一些修改。正如我之前指出的,您的月份计算很危险,并影响了您的天数计算,这可能会在以后引入错误。
根据您在评论中提供的示例,您的代码的天数存在 3 天的差异。 (示例为2012年1月19日至2012年5月3日)。我针对 BicycleDude 的代码运行了这个,结果很好。
我将重新发布代码,并且我刚刚添加了一行来根据过去的小时数查找天数。
如果您想将其设置为“有
w
天、x
小时、y
分钟和z< /code> date2l 和 date1l 之间的秒数”,你可以这样做:
它似乎有效。
我还没有弄清楚月份部分,因为这更重要,但是是的。这有点管用吗?
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
w
days,x
hours,y
minutes andz
seconds 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?