Android 。一个月内写x天 如: 0天,1天,...,28天 。1个月以上6个月以内,写x个月x天 如: 1个月22天 。 6个月以上1岁以内,写x个月 如: 6个月,11个月 。 1岁以上12岁以内,写x岁x人月 如: 1岁,1岁1个月,11岁11个月 。 12岁及以上写周岁 如: 12岁,13岁
时间: 2024-03-12 16:50:16 浏览: 9
在Android中,可以使用Java的日期和时间类(如Calendar类)来计算年龄并根据年龄输出相应的字符串。以下是一个示例代码:
```java
public static String getAgeString(Calendar birthDate) {
Calendar now = Calendar.getInstance();
int years = now.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);
int months = now.get(Calendar.MONTH) - birthDate.get(Calendar.MONTH);
int days = now.get(Calendar.DAY_OF_MONTH) - birthDate.get(Calendar.DAY_OF_MONTH);
if (years < 1) {
if (months < 1) {
return days + "天";
} else if (months < 6) {
return months + "个月" + days + "天";
} else {
return months + "个月";
}
} else if (years < 12) {
if (months == 0) {
return years + "岁";
} else {
return years + "岁" + months + "个月";
}
} else {
return years + "周岁";
}
}
```
其中,`birthDate`是一个表示出生日期的Calendar对象。该方法会根据出生日期计算年龄并返回相应的字符串。例如:
```java
Calendar birthDate = Calendar.getInstance();
birthDate.set(2000, Calendar.JANUARY, 1); // 假设出生日期为2000年1月1日
String ageString = getAgeString(birthDate); // ageString为21岁
```