android计算生日的方法

2017-02-28  本文已影响0人  见事迟

前阵子遇到一个需求:计算生日。假设生日是2016年2月1号,那么我需要得到一个“1岁28天”的字符串。我没想到java或者android API里有现成的方法可以计算,ios小哥表示他的api里有日历的方法,可以通过那个去得到。我很郁闷,就去网上找,去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){
            //当前月份等于生日月份
            day = currentDay - birthDay + 1;
        }else{
            //当前月份大于生日月份
            if(currentDay >= birthDay){
                //当前天数不小于生日天数,天数为两者之差
                day = (currentDay - birthDay) + 1;
                month = currentMonth - birthMonth;
            }else{
                //当前天数小于生日天数,天数为上月天数减去两者之差, 月数减一
                day = getMonthOfDays(currentYear, currentMonth) - (birthDay - currentDay) + 1;
                month = currentMonth - birthMonth - 1;
            }
        }
    }else{
        //生日为往年
        if(currentMonth >= birthMonth){
            //当前月份大于生日月份,年份为正常年差
            year = currentYear - birthYear;
            if(currentDay >= birthDay){
                //当前天数大于生日天数,月份为正常月差,天数为两者之差
                day = (currentDay - birthDay) + 1;
                month = currentMonth - birthMonth;
            }else{
                day = getMonthOfDays(currentYear, currentMonth) - (birthDay - currentDay) + 1;
                month = currentMonth - birthMonth - 1;
            }
        }else{
            //当前月份小于生日月份,年份差减一
            year = currentYear - birthYear - 1;
            if(currentDay >= birthDay){
                //当前天数大于生日天数,月份为正常月差,天数为两者之差
                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;
}

/**
 * 获取指定月份的天数
 *
 * @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);
}

把这个方法放上来是因为我感觉还有可以改进的地方。如果您发现有可以改进的地方或是有更方便的系统api方法,请告知,感激不尽!

2017.4.5修改

现在看太糙了,修改了一下:

/**
 * 获取生日
 *
 * @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 + "天";
    }
}

/**
 * 获取指定月份的天数
 *
 * @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);
}
上一篇下一篇

猜你喜欢

热点阅读