0
life  = 91
today = System.currentTimeMillis()
expireDate = new Date(today + life * 24 * 3600 * 1000);

new Date(today)Wed Nov 28 15:21:01 GMT+05:30 2012按预期返回今天的日期

为什么new Date(expireDate)返回Tue Nov 20 05:17:16 GMT+05:30 2012早于今天的日期,而我实际上期望提前一个日期?

4

2 回答 2

6

这是因为您今天添加的值是一个 int 并且它实际上超出了Integer.MAX_VALUE,当这种情况发生时,它从Integer.MIN_VALUE.

为了解决这个问题,将其中一个值声明为long. 例如,3600可能是3600l.

于 2012-11-28T09:58:40.517 回答
1

尝试

int life = 91;
long today = System.currentTimeMillis();
Date expireDate = new Date(today + life * 24 * 3600 * 1000L);
System.out.println(expireDate);

印刷

Wed Feb 27 10:03:32 GMT 2013

注意:我1000L用来防止溢出,所以life制作long.

于 2012-11-28T10:04:15.890 回答