0

I have two dates:

Date dt1 = new Date("04/11/2013 11:00:00");//date
Date dt2 = new Date("04/12/2013 11:00:00");

Then I process with this code to know how much week between dt1 and dt2

double tanggal = (dt2.getTime() - dt1.getTime()) / (24 * 60 * 60 * 1000);
double week= (double) Math.ceil(tanggal/7);

I tried to run this code and the result week is 1.0, but when the dt2 is 04/11/2013 11:00:01 , the result week is 0.0.

How to change the result so the result week is 1.0 if dt2 is 04/11/2013 11:00:01? And I want, when the dt2 until the seventh day or 04/18/2013 11:00:01 the result change to 2.0. How to do that?

4

1 回答 1

2

如果我理解正确,您想将日期差异四舍五入到最近的一周。如果日期时间差小于一天,你得到 0 的原因是你太早了。您需要做的就是double在除以当天的毫秒数之前将时间戳转换为。这样你就不会失去一天以下的时差。将您的代码更改为

double tanggal = ((double)(dt2.getTime() - dt1.getTime())) / (24 * 60 * 60 * 1000);
double week= (double) Math.ceil(tanggal/7);

这样,您的结果将是1.0.

于 2013-04-11T07:34:24.607 回答