This question already has answers here:
Java Milliseconds in Year

(9个答案)


6年前关闭。





这是场景:

如果我将int传递给Date,如下所示,则输出将为Thu Jan 22 20:56:36 CST 1970

System.out.println(new Date(1393430400*1000));


当我通过long时,输出正是我想要的:Thu Feb 27 00:00:00 CST 2014

System.out.println(new Date((long)1393430400*1000));


我已经检查了new Date(long l)调用的方法。这是相同的:

public Date(long date) {
    fastTime = date;
}


谁能给我一些关于这个问题的解释?非常感谢!

最佳答案

   System.out.println(new Date(1393430400*1000));


在此1393430400中,作为intoverflow。一些不同的值将乘以1000,因此您将获得日期。

    System.out.println(new Date((long)1393430400*1000));


1393430400,这将强制转换为long,乘以1000将成为longno overflow

关于java - 为什么java.util.Date的构造函数对int和long之间的参数的作用不同? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22060637/

10-09 16:56
查看更多