java 30天前,在java中添加30天到目前为止

why when i add 30 days to today's day i got today's day - 30, and when i add 20 it adds??

here is a sample

import java.text.DateFormat;

import java.util.Date;

public class DatePlus

{

public static void main(String[] args)

{

Date now = new Date();

Date now1 = new Date();

Date now2 = new Date();

DateFormat currentDate = DateFormat.getDateInstance();

Date addedDate1 = addDays(now2, 20);

Date addedDate2 = addDays(now1, 30);

System.out.println(currentDate.format(now));

System.out.println(currentDate.format(addedDate1));

System.out.println(currentDate.format(addedDate2));

}

public static Date addDays(Date d, int days)

{

d.setTime(d.getTime() + days * 1000 * 60 * 60 * 24);

return d;

}

}

"this is the console"

Jul 30, 2012

Aug 19, 2012

Jul 10, 2012

解决方案

This is because 30 * 1000 * 60 * 60 * 24 overflows Integer.MAX_VALUE, while 20 * 1000 * 60 * 60 * 24 does not.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值