echo ""; echo ""; echo ""; echo ""; if(!is_admin){echo "";} » 一个容易被忽视的Java整数溢出错误:无法正确获得年份之差的原因 仲子说

2009-05-27

一个容易被忽视的Java整数溢出错误:无法正确获得年份之差的原因

Posted in Java at 13:17 Author:仲远

标签:

??????????????????????????????????????C????????????int???????????????????????C??????????????????C????int???????-32768~32767???java??int???????????-2147483648 ?2147483647??????????????????????????????java?????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????long???????????????????????????????

SimpleDateFormat sdf=new SimpleDateFormat(”yyyy-MM-dd HH:mm:ss”);  
Date date = sdf.parse(”2008-05-24 00:00:00″);
Date date2 = sdf.parse(”2009-05-24 00:00:00″);  
System.out.println((date2.getTime()-date.getTime())/((long)(1000*3600*24*365)));

????????????????????2(date2)?????????1(date)????????1000?????????????3600*24?????????365??????????????????????????????365???366??????????????????????????????????????21?????????????????????????1???????????????????

System.out.println((date2.getTime()-date.getTime())/((long)(1000*3600*24)));

?????????????366??????????????

System.out.println((date2.getTime()-date.getTime())/((long)(1000*3600*24))/365);

??????????????1?????????????????????????????????????????????????????????????1???????

1000*3600*24*365 = 31536000000 ms/year

?????????int????????2147483647????????????????????????

System.out.println((date2.getTime()-date.getTime())/(((long)(3600*24*1000))*((long)(365))));

????????????????????????????????????????????????????????????????????????????????????????

本文可以自由转载,转载时请保留全文并注明出处:
转载自仲子说 [ http://www.wangzhongyuan.com/ ]
原文链接:

Leave a Comment

*
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word