Nag*_*ddy 5 android calendar date-format
我正在做一个与根据给定的生日日期输入获得一个人的年龄相关的应用程序.因为我从下面的代码获得从该日期到当前日期的总天数.
String strThatDay = "1991/05/10";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
Date d = null;
try {
try {
d = formatter.parse(strThatDay);
Log.i(TAG, "" +d);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
} catch (ParseException e) {
e.printStackTrace();
}
Calendar thatDay = Calendar.getInstance();
thatDay.setTime(d); //rest is the same....
Calendar today = Calendar.getInstance();
long diff = today.getTimeInMillis() - thatDay.getTimeInMillis();
long days = diff / (24 * 60 * 60 * 1000);
Run Code Online (Sandbox Code Playgroud)
从这段代码我得到总天数.所以我的要求是将总天数转换为年,月和日......请帮助....
你应该使用这个Duration类:
Duration duration = new Duration();
duration.add( today );
duration.substract( birthDate);
int years = duration.getYears();
int months = duration.getMonths();
int days = duration.getDays();
Run Code Online (Sandbox Code Playgroud)
其他一些选择包括使用专用于时间管理的库:Joda时间.请参阅计算年,月,日,小时,分钟和秒的年龄
| 归档时间: |
|
| 查看次数: |
2539 次 |
| 最近记录: |