Shu*_*pta 1 java format calendar date difference
Date currentDate=new Date();
DateFormat formatter=new SimpleDateFormat("dd-MM-yyyy");;
Date date =(Date)formatter.parse(birthDate); //birthDate is a String, in format dd-MM-yyyy
long diff = currentDate.getTime() - date.getTime();
long d=(1000*60*60*24*365);
long years = Math.round(diff / d);
age=(int) years;
Run Code Online (Sandbox Code Playgroud)
年龄的价值没有正确回归.我究竟做错了什么?
Enter your birthdate: (in format dd-MM-yyyy)
25-07-1992
Current Date: Tue Apr 21 14:05:19 IST 2015
Birthday: Sat Jul 25 00:00:00 IST 1992
Output: Age is: 487
Run Code Online (Sandbox Code Playgroud)
如果你写long d=(1000*60*60*24*365);,结果1000*60*60*24*365将被计算为int,而这对于int类型来说太大了.你应该1000l*60*60*24*365用来计算这个.
| 归档时间: |
|
| 查看次数: |
5649 次 |
| 最近记录: |