前陣子遇到一個需求:計算生日漫蛔。假設(shè)生日是2016年2月1號幕袱,那么我需要得到一個“1歲28天”的字符串访得。我沒想到j(luò)ava或者android API里有現(xiàn)成的方法可以計算州既,ios小哥表示他的api里有日歷的方法氮凝,可以通過那個去得到羔巢。我很郁悶,就去網(wǎng)上找,去api里找竿秆,找了半天也沒找到启摄,感覺找的功夫自己寫可能就寫完了,于是我就開始自己寫:
/**
* 獲取生日
* @param currentTime
* @param birthTime
* @return
*/
public static String getHomeBirthday(long currentTime, long birthTime){
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.setTimeInMillis(currentTime);
int currentYear = currentCalendar.get(Calendar.YEAR);
int currentMonth = currentCalendar.get(Calendar.MONTH);
int currentDay = currentCalendar.get(Calendar.DAY_OF_MONTH);
Calendar birthCalendar = Calendar.getInstance();
birthCalendar.setTimeInMillis(birthTime);
int birthYear = birthCalendar.get(Calendar.YEAR);
int birthMonth = birthCalendar.get(Calendar.MONTH);
int birthDay = birthCalendar.get(Calendar.DAY_OF_MONTH);
int year = 0;
int month = 0;
int day = 0;
if(currentYear == birthYear){
//生日為本年
if(currentMonth == birthMonth){
//當(dāng)前月份等于生日月份
day = currentDay - birthDay + 1;
}else{
//當(dāng)前月份大于生日月份
if(currentDay >= birthDay){
//當(dāng)前天數(shù)不小于生日天數(shù)幽钢,天數(shù)為兩者之差
day = (currentDay - birthDay) + 1;
month = currentMonth - birthMonth;
}else{
//當(dāng)前天數(shù)小于生日天數(shù)歉备,天數(shù)為上月天數(shù)減去兩者之差, 月數(shù)減一
day = getMonthOfDays(currentYear, currentMonth) - (birthDay - currentDay) + 1;
month = currentMonth - birthMonth - 1;
}
}
}else{
//生日為往年
if(currentMonth >= birthMonth){
//當(dāng)前月份大于生日月份,年份為正常年差
year = currentYear - birthYear;
if(currentDay >= birthDay){
//當(dāng)前天數(shù)大于生日天數(shù)匪燕,月份為正常月差蕾羊,天數(shù)為兩者之差
day = (currentDay - birthDay) + 1;
month = currentMonth - birthMonth;
}else{
day = getMonthOfDays(currentYear, currentMonth) - (birthDay - currentDay) + 1;
month = currentMonth - birthMonth - 1;
}
}else{
//當(dāng)前月份小于生日月份,年份差減一
year = currentYear - birthYear - 1;
if(currentDay >= birthDay){
//當(dāng)前天數(shù)大于生日天數(shù),月份為正常月差帽驯,天數(shù)為兩者之差
day = (currentDay - birthDay) + 1;
month = 12 - (birthMonth - currentMonth);
}else{
day = getMonthOfDays(currentYear, currentMonth) - (birthDay - currentDay) + 1;
month = 12 - (birthMonth - currentMonth) - 1;
}
}
}
String yearStr = "";
String monthStr = "";
String dayStr = "";
if(year > 0){
yearStr = year + "歲";
}
if(month > 0){
monthStr = month + "個月";
}
if(day > 0){
dayStr = day + "天";
}
return yearStr + monthStr + dayStr;
}
/**
* 獲取指定月份的天數(shù)
*
* @param year
* @param month
* @return
*/
public static int getMonthOfDays(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DATE, 1);
calendar.roll(Calendar.DATE, -1);
return calendar.get(Calendar.DATE);
}
把這個方法放上來是因?yàn)槲腋杏X還有可以改進(jìn)的地方龟再。如果您發(fā)現(xiàn)有可以改進(jìn)的地方或是有更方便的系統(tǒng)api方法,請告知尼变,感激不盡利凑!
2017.4.5修改
現(xiàn)在看太糙了,修改了一下:
/**
* 獲取生日
*
* @param currentTime
* @param birthTime
* @return
*/
public static String getHomeBirthday(long currentTime, long birthTime) {
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.setTimeInMillis(currentTime);
int currentYear = currentCalendar.get(Calendar.YEAR);
int currentMonth = currentCalendar.get(Calendar.MONTH);
int currentDay = currentCalendar.get(Calendar.DAY_OF_MONTH);
Calendar birthCalendar = Calendar.getInstance();
birthCalendar.setTimeInMillis(birthTime);
int birthYear = birthCalendar.get(Calendar.YEAR);
int birthMonth = birthCalendar.get(Calendar.MONTH);
int birthDay = birthCalendar.get(Calendar.DAY_OF_MONTH);
int year = 0;
int month = 0;
int day = 0;
day = currentDay - birthDay;
if(day <= 0){
day = getMonthOfDays(currentYear, currentMonth) - birthDay + currentDay;
month = -1;
}
month += currentMonth - birthMonth;
if(month < 0){
month = 12 + month;
year = -1;
}
year += currentYear - birthYear;
if (year > 0) {
return year + "歲" + month + "個月" + day + "天";
}
if (month > 0) {
return month + "個月" + day + "天";
} else {
return day + "天";
}
}
/**
* 獲取指定月份的天數(shù)
*
* @param year
* @param month
* @return
*/
public static int getMonthOfDays(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DATE, 1);
calendar.roll(Calendar.DATE, -1);
return calendar.get(Calendar.DATE);
}