public static String getAge(String date) {
String[] data = date.split("-");
if (data.length < 3) return "";
Integer.valueOf(Log.d(TAG, data[0]));
Integer.valueOf(Log.d(TAG, data[1]));
Integer.valueOf(Log.d(TAG, data[2]));
Calendar birthday = new GregorianCalendar(Integer.valueOf(data[0]), Integer.valueOf(data[1]), Integer.valueOf(data[2]));
Calendar now = Calendar.getInstance();
int day = now.get(Calendar.DAY_OF_MONTH) - birthday.get(Calendar.DAY_OF_MONTH);
int month = now.get(Calendar.MONTH) + 1 - birthday.get(Calendar.MONTH);
int year = now.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
//按照减法原理,先day相减,不够向month借;然后month相减,不够向year借;最后year相减。
if (day < 0) {
month -= 1;
now.add(Calendar.MONTH, -1);//得到上一个月,用来得到上个月的天数。
day = day + now.getActualMaximum(Calendar.DAY_OF_MONTH);
}
if (month < 0) {
month = (month + 12) % 12;
year--;
}
System.out.println("年龄:" + year + "岁" + month + "月" + day + "天");
StringBuffer tag = new StringBuffer();
if (year > 0) {
tag.append(year + "岁");
}
if (month > 0) {
tag.append(month + "个月");
}
if (day > 0) {
tag.append(day + "天");
}
if (year == 0 && month == 0 && day == 0) {
tag.append("今日出生");
}
return String.valueOf(tag);
}
最后吐槽一下csdn 编辑器排版真不好弄。
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- oldu.cn 版权所有 浙ICP备2024123271号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务