将秒添加到System.currentTimeMills()
我正在尝试使用以下方法计算从现在起 100 天的时间:
import java.util.Date;
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
System.out.println(new Date(System.currentTimeMillis() + 8640000 * 1000));
}
}
它给了我Jan 04 08:57:25 UTC 2021不正确的回报。我该如何补救?
回答
使用以下内容:
System.out.println(new Date(System.currentTimeMillis() + (long)8640000 * 1000));
或者
System.out.println(new Date(System.currentTimeMillis() + 8640000L * 1000));
8640000 * 1000 是 8,640,000,000,超过 Integer.MAX_VALUE 的 2,147,483,647。您可以通过将其转换为long.
这给出了结果:
Tue Apr 13 15:26:10 EDT 2021