简体   繁体   English

为什么Java的Date.getTime()会返回1月份的错误响应?

[英]Why does Java's Date.getTime() returns an incorrect response for January?

The code for calculating the date in milliseconds is: 以毫秒为单位计算日期的代码是:

//installment date converted to milliseconds
long localDateInstall = installDate.getTime();

//installment date converted to milliseconds
long localDatePay = payDate.getTime();

and here I calculate the number of days between these dates: 在这里我计算这些日期之间的天数:

days=Math.abs((localDatePay - localDateInstall))/86400000;

When I want to test it, I enter 1/Jan/2012 for localDateInstall and 1/Feb/2012 for localDatePay . 当我想测试它时,我输入1/Jan/2012 for localDateInstall1/Feb/2012 for localDatePay The result is: 29 Days . 结果是: 29天 What is the problem? 问题是什么? Thanks 谢谢

Counter-example: 反例:

public static void main(String[] args) throws Exception {
    SimpleDateFormat format = new SimpleDateFormat("d MMM yyyy");
    Date installDate = format.parse("1 Jan 2012");
    Date payDate = format.parse("1 Feb 2012");
    long localDatePay = payDate.getTime();
    long localDateInstall = installDate.getTime();
    long days = Math.abs(localDatePay - localDateInstall) / 86400000;
    System.out.println(days);
}

This works fine, and the output is "31", as expected. 这工作正常,输出为“31”,如预期的那样。 The problem is somewhere in what you haven't told us. 问题出在你没告诉我们的地方。 Shot in the dark: you're probably constructing your test input like this: 在黑暗中拍摄:您可能正在构建您的测试输入,如下所示:

Date installDate = new Date(2012, 1, 1);
Date payDate = new Date(2012, 2, 1);

Sadly, that's wrong in two ways. 可悲的是,这在两个方面是错误的。 First, the "year" argument is supposed to be " the year minus 1900 ". 首先,“年”论证应该是“ 减去1900年的年份 ”。 Second, January is month 0, not month 1 . 其次, 1月是0月,而不是1月 With those two dates as input, you would, indeed, get an answer of 29 because you're actually asking it for the number of days in February of 3912, which is a leap year. 有了这两个日期作为输入,你的确会得到29的答案,因为你实际上是在询问它在2912年2月的天数,这是闰年。

You can try: 你可以试试:

//installment date converted to milliseconds
long localDateInstall=installDate.getTimeInMillis();

//installment date converted to milliseconds
long localDatePay=payDate.getTimeInMillis();

long diff = localDatePay-localDateInstall;

long diffInDays = diff / (24 * 60 * 60 * 1000);    

I don't think there's enough information to go on, but here are some ideas to pursue. 我认为没有足够的信息可以继续,但这里有一些想法。

  1. You're performing integer arithmetic, which doesn't round, but rather truncates anything short of a whole day. 你正在执行整数运算,它不会舍入,而是截断一整天。 If your end time of day is even a little earlier in the day than your start time, you'll lose a whole day. 如果你的一天结束时间比你的开始时间早一点,那么你将失去一整天。

  2. Make sure there isn't something funny going on with the time zones. 确保时区没有搞笑的事情。 Are the two dates input in the same way, or are they coming from different sources which might have different time zones attached? 这两个日期是以相同的方式输入的,还是来自可能有不同时区的不同来源?

  3. If you post the actual millisecond values for each, and your expected time zone, it should be pretty easy to determine which one of the two is off. 如果你发布每个的实际毫秒值和你预期的时区,那么很容易确定两者中哪一个是关闭的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM